static async decryptData(originData:Uint8Array,keyData:Uint8Array,ivData:Uint8Array):Promise<Uint8Array|null>{
let callData:Uint8Array|null = null;
if (originData.length>0 && keyData.length>0) {
try {
let cipher = cryptoFramework.createCipher('AES256|CBC|NoPadding');
let syKey =await genSymKeyByData(keyData);
let ivParams:cryptoFramework.IvParamsSpec =
{iv:{ data : ivData},algName:'IvParamsSpec' };
await cipher.init(cryptoFramework.CryptoMode.DECRYPT_MODE,syKey,ivParams)
console.log('decrypt cipher.init succ');
let updateCipher = await cipher.update({data:originData})
if (updateCipher != null && updateCipher.data != null) {
callData = updateCipher.data;
}
// let data =await cipher.doFinal(null);
// let data = await cipher.doFinal({data:originData});
// if (data != null && data.data != null) {
// console.log('decrypt doFinal succ:' + data.data.length);
// callData = data.data;
// }else {
// console.log('decrypt doFinal err: ');
// }
} catch (err) {
console.log('decrypt :error ' +(err as BusinessError ).code+(err as BusinessError ).message);
}
}
return callData;
}
static async genSymKeyByData(symKeyData: Uint8Array):Promise<cryptoFramework.SymKey> {
let symKeyBlob: cryptoFramework.DataBlob = { 'AES256' };
let aesGenerator = cryptoFramework.createSymKeyGenerator(ReaderUtil.generatorAlgName);
let symKey = await aesGenerator.convertKey(symKeyBlob);
console.info('convertKey success symKey '+symKey.getEncoded().data.toString());
console.info('convertKey success symKeyData '+symKeyData.toString());
return symKey;
}
使用cipher.doFinal一直错误,最后专用cipher.update;
AES256对应的是32位的key;
AES128对应的是16位的key。