OpenHarmony开发者论坛

标题: [Bug]: 使用cryptoFramework加解密的时候,经常崩溃 [打印本页]

作者: shrimp    时间: 2024-5-8 12:18
标题: [Bug]: 使用cryptoFramework加解密的时候,经常崩溃
【问题描述】
问题现象和发生的背景
HarmonyOS NEXT Developer Preview2模拟器:使用cryptoFramework加解密的时候,经常崩溃,同样的加解密代码,同样的数据,运行效果不同:大概率会失败、小概率会成功、经常会崩溃
同样的代码同样的数据,为什么会执行出不同的结果呢?

相关的代码,截图,日志信息
奔溃信息如下:
Device info:emulator
Build info:emulator 4.1.0.67(DEVC00E67R4P11log)
Fingerprint:40e9623ff2f651ca929a11ebf2b304f66d9ac967411f0e43c3f8563ce3ce89ae
Module name:com.example.hbcanner\_hm
Version:1.0.0
VersionCode:1000000
PreInstalled:No
Foreground:Yes
Timestamp:2024-04-17 09:58:30.093
Pid:2986
Uid:20020033
Process name:com.example.hbcanner\_hm
Reason:Signal:SIGSEGV(SEGV\_MAPERR)@0x0000000000000010 probably caused by NULL pointer dereference
Fault thread Info:
Tid:3008, Name:ple.hbcanner\_hm
#00 pc 000000000030f820 /system/lib64/chipset-pub-sdk/libcrypto\_openssl.z.so(EVP\_CipherUpdate+0)(7291138c2fde3e6d1c79c8b516f1e13b)
#01 pc 000000000003649d /system/lib64/platformsdk/libcrypto\_openssl\_plugin\_lib.z.so(7fe31d85dd1d9486203b84ec12ca038f)
#02 pc 0000000000019392 /system/lib64/module/security/libcryptoframework\_napi.z.so(df78b46e2eac852554011d94aa429577)
#03 pc 0000000000050305 /system/lib64/platformsdk/libace\_napi.z.so(NativeAsyncWork::AsyncWorkCallback(uv\_work\_s\*)+197)(19e93c5b5dd3c64c2d49e1787c064b5f)
#04 pc 00000000000159fc /system/lib64/platformsdk/libuv.so(97aa20cec7cddf74b13f681315cff7eb)
#05 pc 0000000000114ab7 /system/lib/ld-musl-x86\_64.so.1(70f5e1a4df8aa3510e8ec8132b8f901f)
#06 pc 000000000008728e /system/lib/ld-musl-x86\_64.so.1(70f5e1a4df8aa3510e8ec8132b8f901f)
Registers:
rax:00007f2796399560 rdx:00007f26f89aa8dc rcx:00007f2796399380 rbx:00007f2796517b58
rsi:00007f2796399560 rdi:0000000000000000 rbp:00000000000001e0 rsp:00007f26f89aa8c8
r8:00000000000001d0 r9:000056232d2a5a68 r10:00007f2792202878 r11:0000000000000206
r12:00007f27986a1758 r13:0000000000000001 r14:00007f2796517b68 r15:00007f27962fb4f0 rip:00007f279370f820



以下是我编写的AES加解密代码

import {
base64SafeStringToString,
stringToBase64SafeString,
stringToUint8Array,
uint8ArrayToString
} from '../StringUtil';
import util from '@ohos.util';
import logger from '../LoggerUtil';
import cryptoFramework from '@ohos.security.cryptoFramework';
import { Event } from '@kit.ArkTS';

const AES\_CBC\_PKCS5 = 'AES128|CBC|PKCS5'
const AES = 'AES128'

namespace AesUtil {
export function decryptAES(strCipherText: string, key: string, ivPara?: string): Promise {
return new Promise(async (resolve: Function, \_: Function) => {
try {
strCipherText = stringToBase64SafeString(strCipherText)
let base64Helper = new util.Base64Helper()
while (strCipherText.length % 4 != 0) {
strCipherText += '='
}
let dataArrayBase64: Uint8Array = base64Helper.decodeSync(strCipherText)
    let keyArray = stringToUint8Array(key)
    let pkBlob: cryptoFramework.DataBlob = { data: keyArray }

    let symKeyGenerator = cryptoFramework.createSymKeyGenerator(AES);
    symKeyGenerator.convertKey(pkBlob).then(keyInfo => {
      try {
        let cipher: cryptoFramework.Cipher
        if (ivPara === undefined) {
          cipher = cryptoFramework.createCipher(AES)
          cipher.init(cryptoFramework.CryptoMode.DECRYPT_MODE, keyInfo, null)
        } else {
          cipher = cryptoFramework.createCipher(AES_CBC_PKCS5)
          let ivParamsSpec: cryptoFramework.IvParamsSpec = {
            iv: { data: stringToUint8Array(ivPara) },
            algName: 'IvParamsSpec'
          }
          cipher.init(cryptoFramework.CryptoMode.DECRYPT_MODE, keyInfo, ivParamsSpec)
        }
        cipher.doFinal({ data: dataArrayBase64 }).then(result => {
          resolve(uint8ArrayToString(result.data))
        }).catch((error: Error | ErrorConstructor | Event | RegExp | RegExpConstructor) => {
          logger.debug(`CodeUtil - AES decryptAES doFinal: ${error} text: ${strCipherText} key: ${key}`)
          resolve('')
        })
      } catch (error) {
        logger.debug(`CodeUtil - AES decryptAES convertKey-catch: ${error} text: ${strCipherText} key: ${key}`)
        resolve('')
      }
    }).catch((error: string) => {
      logger.debug(`CodeUtil - AES decryptAES convertKey: ${error} text: ${strCipherText} key: ${key}`)
      resolve('')
    })
  } catch (error) {
    logger.debug(`CodeUtil - AES decryptAES catch: ${error} text: ${strCipherText} key: ${key}`)
    resolve('')
  }
});
}

export function encryptAES(strCipherText: string, key: string): Promise {
return new Promise(async (resolve: Function, \_: Function) => {
try {
    let keyArray = stringToUint8Array(key)
    let dataArray = stringToUint8Array(strCipherText)
    let pkBlob: cryptoFramework.DataBlob = { data: keyArray }
    let symKeyGenerator = cryptoFramework.createSymKeyGenerator(AES);
    symKeyGenerator.convertKey(pkBlob).then(keyInfo => {
      let cipher = cryptoFramework.createCipher(AES)
      cipher.init(cryptoFramework.CryptoMode.ENCRYPT_MODE, keyInfo, null)
      cipher.doFinal({ data: dataArray }).then(result => {
        let base64Helper = new util.Base64Helper
        resolve(base64SafeStringToString(base64Helper.encodeToStringSync(result.data)))
      }).catch((error: string) => {
        logger.debug(`CodeUtil - AES encryptAES doFinal: ${error} text: ${strCipherText} key: ${key}`)
        resolve('')
      })
    }).catch((error: string) => {
      logger.debug(`CodeUtil - AES encryptAES convertKey: ${error} text: ${strCipherText} key: ${key}`)
      resolve('')
    })
  } catch (error) {
    logger.debug(`CodeUtil - AES encryptAES catch: ${error} text: ${strCipherText} key: ${key}`)
    resolve('')
  }
});
}
}
export default AesUtil


【运行环境】
DevEvoStudio版本:
DevEco Studio NEXT Developer Preview2
Build Version: 4.1.3.700, built on March 19, 2024
Build #DS-223.8617.56.36.413700
Runtime version: 17.0.6+10-b829.5 amd64
VM: OpenJDK 64-Bit Server VM by JetBrains s.r.o.
Windows 10 10.0
GC: G1 Young Generation, G1 Old Generation
Memory: 1536M
Cores: 8
Registry:
    external.system.auto.import.disabled=true

SDK版本:4.1.0(11)

作者: 旺旺崔冰冰    时间: 2024-5-11 10:31
您好,我这边加解密传输测试暂时比较稳定,您可以试一下eftool中的加解密,或者整体传输加解密,如果不稳定的话烦请提交一个issue,eftool加解密详见eftool第二部分,efAxios整体传输加解密详见第三部分




欢迎光临 OpenHarmony开发者论坛 (https://forums.openharmony.cn/) Powered by Discuz! X3.5