des解密同步方法解密失败

des解密同步方法解密会失败(报错:Error: do final fail!),异步的则没问题。

能否提供一个以下api连起来使用的demo:convertKeySync、initSync、updateSync12、doFinalSync。


HarmonyOS
9h前
浏览
收藏 0
回答 1
待解决
回答 1
按赞同
/
按时间
superinsect

参考:

//加密 
function aesEncrypt(text: string, key: string, iv: string): string { 
  let globalResult = "" 
  try { 
    let globalCipher = cryptoFramework.createCipher("3DES192|CBC|PKCS5"); 
    let symKeyGenerator = cryptoFramework.createSymKeyGenerator("3DES192"); 
    let keyBlob: cryptoFramework.DataBlob = { data: base.decodeSync(key) } 
    let promiseSymKey = symKeyGenerator.convertKeySync(keyBlob) 
 
    let cbcParamsSpec: cryptoFramework.IvParamsSpec = { 
      iv: { data: new Uint8Array() }, 
      algName: 'IvParamsSpec' 
    }; 
    //传入业务的偏移量 
    cbcParamsSpec.iv = { data: stringToUint8Array(iv) } 
    //初始化的时候传入偏移量 
    globalCipher.initSync(cryptoFramework.CryptoMode.ENCRYPT_MODE, promiseSymKey, cbcParamsSpec); 
    let plainText: cryptoFramework.DataBlob = { data: stringToUint8Array(text) } 
    let result = globalCipher.doFinalSync(plainText) 
    globalResult = base.encodeToStringSync(result.data); 
  } catch (err) { 
    console.log("error log" + err.message) 
  } 
  return globalResult; 
} 
 
 
 
// 解密 
function aesDecrypt(text: string, key: string, iv: string) { 
  let globalResult = "" 
  try { 
    let globalCipher = cryptoFramework.createCipher("3DES192|CBC|PKCS5"); 
    let symKeyGenerator = cryptoFramework.createSymKeyGenerator("3DES192"); 
    let base64Helper = new util.Base64Helper; 
    let dataUint8Array = base64Helper.decodeSync(key) 
    let keyBlob: cryptoFramework.DataBlob = { data: dataUint8Array } 
    let promiseSymKey = symKeyGenerator.convertKeySync(keyBlob) 
 
    let cbcParamsSpec: cryptoFramework.IvParamsSpec = { 
      iv: { data: new Uint8Array() }, 
      algName: 'IvParamsSpec' 
    }; 
    //传入业务的偏移量 
    cbcParamsSpec.iv = { data: stringToUint8Array(iv) } 
 
    globalCipher.initSync(cryptoFramework.CryptoMode.DECRYPT_MODE, promiseSymKey, cbcParamsSpec); 
    let plainText: cryptoFramework.DataBlob = { data: base64Helper.decodeSync(text) } 
    let result = globalCipher.doFinalSync(plainText) 
    globalResult = uint8ArrayToString(result.data); 
    console.log("解密后的明文==》" + globalResult) 
  } catch (err) { 
    console.log(err.message) 
  } 
} 
 
 
 
function main() { 
  //加密 
  let data = aesEncrypt("加密信息", "uj3CcSEeMFatR/xaRjnufLo7wnGroDBy", "1234567812345678") 
  //解密 
  aesDecrypt(data, "uj3CcSEeMFatR/xaRjnufLo7wnGroDBy", "1234567812345678") 
} 
 
 
 
export const base = new util.Base64Helper(); 
 
// 字符串转成字节流 
export function stringToUint8Array(str: string) { 
  return new Uint8Array(buffer.from(str,'utf-8').buffer); 
} 
// 字节流转成可理解的字符串 
export function uint8ArrayToString(array:Uint8Array) { 
  // 将UTF-8编码转换成Unicode编码 
  let out: string = ""; 
  let index: number = 0; 
  let len: number = array.length; 
  while (index < len) { 
    let character = array[index++]; 
    switch(character >> 4) { 
      case 0: 
      case 1: 
      case 2: 
      case 3: 
      case 4: 
      case 5: 
      case 6: 
      case 7: 
        out += String.fromCharCode(character); 
        break; 
      case 12: 
      case 13: 
        out += String.fromCharCode(((character & 0x1F) << 6) | (array[index++] & 0x3F)); 
        break; 
      case 14: 
        out += String.fromCharCode(((character & 0x0F) << 12) | ((array[index++] & 0x3F) << 6) | ((array[index++] & 0x3F) << 0)); 
        break; 
      default: 
        break; 
    } 
  } 
  return out; 
}
分享
微博
QQ
微信
回复
4h前
相关问题
HarmonyOS 使用DES解密,转换 smkey失败
307浏览 • 1回复 待解决
HarmonyOS 如何进行DES解密
327浏览 • 1回复 待解决
HarmonyOS Sm2和DES解密问题
264浏览 • 1回复 待解决
HarmonyOS 文档中没有查找到DES解密
251浏览 • 1回复 待解决
求大佬告知如何进行des解密
1804浏览 • 1回复 待解决
HarmonyOS RSA解密问题
309浏览 • 1回复 待解决
HarmonyOS 加解密 demo
338浏览 • 1回复 待解决
HarmonyOS AES解密报错
433浏览 • 1回复 待解决
解密算法库框架使用
762浏览 • 1回复 待解决
多种加密方式实现加解密
892浏览 • 1回复 待解决
解密问题的定位指导
233浏览 • 1回复 待解决
HarmonyOS RSA如何用公钥解密
405浏览 • 0回复 待解决
RSA导入外部密钥实现加解密
669浏览 • 1回复 待解决
HarmonyOS 有RSA加解密相关吗
292浏览 • 0回复 待解决
HarmonyOS rsa解密速度过于缓慢
366浏览 • 1回复 待解决
HarmonyOS 请提供RAS加解密的文档
239浏览 • 1回复 待解决