des解密同步方法解密失败

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

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


HarmonyOS
2024-11-08 11:28:48
浏览
收藏 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
微信
回复
2024-11-08 16:29:31
相关问题
HarmonyOS 使用DES解密,转换 smkey失败
506浏览 • 1回复 待解决
HarmonyOS 3des解密失败
21浏览 • 1回复 待解决
HarmonyOS CryptoJS Des解密问题
24浏览 • 1回复 待解决
HarmonyOS 如何进行DES解密
598浏览 • 1回复 待解决
HarmonyOS 关于DES解密的疑问
34浏览 • 1回复 待解决
求大佬告知如何进行des解密
1958浏览 • 1回复 待解决
HarmonyOS 文档中没有查找到DES解密
406浏览 • 1回复 待解决
HarmonyOS Sm2和DES解密问题
512浏览 • 1回复 待解决
关于3des解密问题,有人知道吗?
229浏览 • 1回复 待解决