3DES如何根据指定KEY、IV生成SymKey对称秘钥?

3DES如何根据指定KEY、IV生成SymKey对称秘钥?

HarmonyOS
2024-06-05 23:29:23
浏览
收藏 0
回答 1
待解决
回答 1
按赞同
/
按时间
e_leaner

以加密为例(解密参考使用即可):

//加密 
public static async aesEncrypt(text: string,key:string,iv:string):Promise<string> { 
  let base = new util.Base64Helper(); 
  let globalResult = '' 
  try { 
  let globalCipher = cryptoFramework.createCipher(cipherAlgName); 
  let symKeyGenerator = cryptoFramework.createSymKeyGenerator(symAlgName); 
  let keyBlob: cryptoFramework.DataBlob = { data: base.decodeSync(key) } 
  let promiseSymKey = await symKeyGenerator.convertKey(keyBlob) 
  // 偏移量 
  let globalCbcParams = genCBCParamsSpec(); 
  globalCbcParams.iv = { data: base.decodeSync(iv, util.Type.BASIC) } 
  //初始化的时候传入偏移量 
  await globalCipher.init(cryptoFramework.CryptoMode.ENCRYPT_MODE, promiseSymKey, globalCbcParams); 
  let plainText: cryptoFramework.DataBlob = { data: DESWork.stringToUint8Array(text) } 
  let result = await globalCipher.doFinal(plainText) 
  globalResult = base.encodeToStringSync(result.data) 
}catch (err){ 
  console.log(err.message) 
} 
return globalResult; 
} 
/* 
偏移量参数 
*/ 
function genCBCParamsSpec() { 
  let arr = [0x36, 0x61, 0x38, 0x77, 0x30, 0x63, 0x39, 0x31, 0x6b, 0x63, 0x37, 0x45, 0x55, 0x44, 0x57, 0x46]; 
  let dataIv = new Uint8Array(arr); 
  let ivBlob: cryptoFramework.DataBlob = { 
    data: dataIv 
  }; 
  let cbcParamsSpec: cryptoFramework.IvParamsSpec = { 
    iv: ivBlob, 
    algName: 'IvParamsSpec' 
  }; 
  return cbcParamsSpec; 
}

导入字符串密钥:

async function testGenerateAesKey(str:string) { 
  try { 
    let symKeyGenerator = cryptoFramework.createSymKeyGenerator('3DES192'); 
    let keyBlob: cryptoFramework.DataBlob = { data: stringToUint8Array("Ac6jzpE2Ac6jzpE2Ac6jzpE2") } 
    let promiseSymKey = await symKeyGenerator.convertKey(keyBlob); 
  }catch (err){ 
    console.error(err); 
  } 
} 
// 字符串转成字节流 
function stringToUint8Array(str: string) { 
  return new Uint8Array(buffer.from(str,'utf-8').buffer); 
}
分享
微博
QQ
微信
回复
2024-06-06 23:09:42
相关问题
HarmonyOS 3des加解密失败
9浏览 • 1回复 待解决
关于3des加解密问题,有人知道吗?
228浏览 • 1回复 待解决
HarmonyOS 使用crypto进行交换
57浏览 • 1回复 待解决
HarmonyOS 是否有带ivDES加密算法
515浏览 • 1回复 待解决
HarmonyOS 加密导入RSA1024对Demo
17浏览 • 1回复 待解决
HarmonyOS RSA加密的base64转换
12浏览 • 1回复 待解决
使用32字节加解密后报错
1695浏览 • 1回复 待解决
如何通过uuid生成、md5、RSA、AES、 DES
1923浏览 • 1回复 待解决