基于加解密算法框架的规格问题

​AES加解密,缺少基础的加解密示例,在原有的文档示例基础上不知道如何修改。

对于不同的分组模式下表中给出了相应的参数适用说明,代码以AES128为例,密钥传入的为base64格式,偏移量IV为字符串。对于GCM的参数设置,这里给了IV的,其余参数参考IV的写法即可。模板中使用的加解密算法以及密钥规格可以参考以下链接:对称密钥生成和转换规格对称密钥加解密算法规格

HarmonyOS
2024-06-13 10:32:58
浏览
已于2024-6-13 10:33:36修改
收藏 0
回答 1
待解决
回答 1
按赞同
/
按时间
橱柜里的蛙

ECB加解密模板。

//加密 
async function aesEncrypt(text:string,puKey:string): Promise<string>{ 
  let globalResult = "" 
  try { 
    //这里已AES加解密为例支持AES、SM4、3DES 
    let cipherAlgName = 'AES128|ECB|PKCS7'; 
    // 创建加解密对象 
    let globalCipher = cryptoFramework.createCipher(cipherAlgName); 
    //这里已AES加解密为例支持AES、SM4、3DES 
    let symAlgName = 'AES128'; 
    //创建密钥对象 
    let symKeyGenerator = cryptoFramework.createSymKeyGenerator(symAlgName); 
    //将传入的base格式的密钥转为Uint8Array数组 
    let dataUint8Array = base.decodeSync(puKey) 
    let keyBlob: cryptoFramework.DataBlob = { data: dataUint8Array } 
    //导入外部密钥 
    let promiseSymKey = await symKeyGenerator.convertKey(keyBlob) 
    //初始化 
    await globalCipher.init(cryptoFramework.CryptoMode.ENCRYPT_MODE, promiseSymKey, null); 
    //加密 
    let result = await globalCipher.doFinal({data:stringToUint8Array(text)}) 
    //将加密结果转换为base64格式,用于保存或者传递 
    globalResult = base.encodeToStringSync(result.data); 
  } catch (err) { 
    console.log(err.message) 
  } 
  return globalResult; 
}
// 解密 
async function aesDecrypt(text: string, key: string) { 
  let globalResult = "" 
  try { 
    //这里已AES加解密为例支持AES、SM4、3DES 
    let cipherAlgName = 'AES128|ECB|PKCS7'; 
    // 创建加解密对象 
    let globalCipher = cryptoFramework.createCipher(cipherAlgName); 
    //这里已AES加解密为例支持AES、SM4、3DES 
    let symAlgName = 'AES128'; 
    //创建密钥对象 
    let symKeyGenerator = cryptoFramework.createSymKeyGenerator(symAlgName); 
    //将传入的base格式的密钥转为Uint8Array数组 
    let dataUint8Array = base.decodeSync(key) 
    let keyBlob: cryptoFramework.DataBlob = { data: dataUint8Array } 
    //导入外部密钥 
    let promiseSymKey = await symKeyGenerator.convertKey(keyBlob) 
    await globalCipher.init(cryptoFramework.CryptoMode.DECRYPT_MODE, promiseSymKey, null); 
    let plainText: cryptoFramework.DataBlob = { data: base.decodeSync(text) } 
    let result = await globalCipher.doFinal(plainText) 
    //将解密后的结果result解码之后得到明文 
    globalResult = uint8ArrayToString(result.data); 
    console.log("解密后的明文==》" + globalResult) 
  } catch (err) { 
    console.log(err.message) 
  } 
}

CBC加解密模板。

//加密 
async function aesEncrypt(text: string, key: string, iv:string): Promise<string> { 
  let globalResult = "" 
  try { 
    //这里已AES加解密为例支持AES、SM4、3DES 
    let cipherAlgName = 'AES128|CBC|PKCS7'; 
    let globalCipher = cryptoFramework.createCipher(cipherAlgName); 
    //这里已AES加解密为例支持AES、SM4、3DES 
    let symAlgName = 'AES128'; 
    let symKeyGenerator = cryptoFramework.createSymKeyGenerator(symAlgName); 
    let dataUint8Array = base.decodeSync(key) 
    let keyBlob: cryptoFramework.DataBlob = { data: dataUint8Array } 
    let promiseSymKey = await symKeyGenerator.convertKey(keyBlob) 
 
    let ivData = stringToUint8Array(iv); 
    let ivdata: cryptoFramework.DataBlob = { data: ivData }; //偏移 
    let iv: cryptoFramework.IvParamsSpec = { iv: ivdata, algName: 'IvParamsSpec' } //cbc 模式的参数 
 
    await globalCipher.init(cryptoFramework.CryptoMode.ENCRYPT_MODE, promiseSymKey, iv); 
    let plainText: cryptoFramework.DataBlob = { data: this.stringToUint8Array(text) } 
    let result = await globalCipher.doFinal(plainText) 
    globalResult = base.encodeToStringSync(result.data); 
  } catch (err) { 
    console.log(err.message) 
  } 
  return globalResult; 
}
// 解密 
async function aesDecrypt(text: string, key: string,iv:string) { 
  let globalResult = "" 
  try { 
    let cipherAlgName = 'AES128|CBC|PKCS7'; 
    let globalCipher = cryptoFramework.createCipher(cipherAlgName); 
 
    let symAlgName = 'AES128'; 
    let symKeyGenerator = cryptoFramework.createSymKeyGenerator(symAlgName); 
 
    let dataUint8Array = base.decodeSync(key) 
    let keyBlob: cryptoFramework.DataBlob = { data: dataUint8Array } 
    let promiseSymKey = await symKeyGenerator.convertKey(keyBlob) 
 
    // /*设置偏移量 */ 
    let ivData = stringToUint8Array(iv); 
    let ivdata: cryptoFramework.DataBlob = { data: ivData }; //偏移 
    let iv: cryptoFramework.IvParamsSpec = { iv: ivdata, algName: 'IvParamsSpec' } //cbc 模式的参数 
 
    await globalCipher.init(cryptoFramework.CryptoMode.DECRYPT_MODE, promiseSymKey, globalCbcParams); 
    let plainText: cryptoFramework.DataBlob = { data: base.decodeSync(text) } 
    let result = await globalCipher.doFinal(plainText) 
    globalResult = uint8ArrayToString(result.data); 
    console.log("解密后的明文==》" + globalResult) 
  } catch (err) { 
    console.log(err.message) 
  } 
  return globalResult; 
}

GCM加解密模板。

//GCM的参数设置 
function genGcmParamsSpec() { 
  let arr = [0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0]; // 12 bytes 
  let dataIv = new Uint8Array(arr); 
  let ivBlob: cryptoFramework.DataBlob = { data: dataIv }; 
  arr = [0, 0, 0, 0, 0, 0, 0, 0]; // 8 bytes 
  let dataAad = new Uint8Array(arr); 
  let aadBlob: cryptoFramework.DataBlob = { data: dataAad }; 
  arr = [0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0]; // 16 bytes 
  let dataTag = new Uint8Array(arr); 
  let tagBlob: cryptoFramework.DataBlob = { 
    data: dataTag 
  }; 
  // GCM的authTag在加密时从doFinal结果中获取,在解密时填入init函数的params参数中 
  let gcmParamsSpec: cryptoFramework.GcmParamsSpec = { 
    iv: ivBlob, 
    aad: aadBlob, 
    authTag: tagBlob, 
    algName: "GcmParamsSpec" 
  }; 
  return gcmParamsSpec; 
}
//加密 
export async function aesEncryptGCM(text: string, key: string,iv:string): Promise<string> { 
  let globalResult = "" 
  try { 
    let cipherAlgName = 'AES128|GCM|PKCS5'; 
    let globalCipher = cryptoFramework.createCipher(cipherAlgName); 
 
    let symAlgName = 'AES128'; 
    let symKeyGenerator = cryptoFramework.createSymKeyGenerator(symAlgName); 
    let dataUint8Array = stringToUint8Array(key) 
    let keyBlob: cryptoFramework.DataBlob = { data: dataUint8Array } 
    let promiseSymKey = await symKeyGenerator.convertKey(keyBlob) 
    let getParamsSpec: cryptoFramework.GcmParamsSpec = genGcmParamsSpec(); 
    getParamsSpec.iv = { data: stringToUint8Array(iv) } 
    await globalCipher.init(cryptoFramework.CryptoMode.ENCRYPT_MODE, promiseSymKey, getParamsSpec); 
 
    let plainText: cryptoFramework.DataBlob = { data: stringToUint8Array(text) } 
    let res = await globalCipher.doFinal(plainText) 
    authTag = res.data.subarray(res.data.length - 16, res.data.length)//authTag 
    let a = res.data.subarray(0, res.data.length - authTag.length);//密文 
    globalResult = base.encodeToStringSync(a); 
  } catch (err) { 
    console.log(err.message) 
  } 
  return globalResult; 
}
// 解密 
export async function aesDecryptGCM(text: string, key: string) { 
  let globalResult = "" 
  try { 
    let cipherAlgName = 'AES128|GCM|PKCS5'; 
    let globalCipher = cryptoFramework.createCipher(cipherAlgName); 
    let symAlgName = 'AES128'; 
    let symKeyGenerator = cryptoFramework.createSymKeyGenerator(symAlgName); 
    let dataUint8Array = stringToUint8Array(key) 
    let keyBlob: cryptoFramework.DataBlob = { data: dataUint8Array } 
    let promiseSymKey = await symKeyGenerator.convertKey(keyBlob) 
    let getParamsSpec: cryptoFramework.GcmParamsSpec = genGcmParamsSpec(); 
    getParamsSpec.authTag = {data:authTag} 
    getParamsSpec.iv = { data: stringToUint8Array(iv) } 
    await globalCipher.init(cryptoFramework.CryptoMode.DECRYPT_MODE, promiseSymKey, getParamsSpec); 
    let plainText: cryptoFramework.DataBlob = { data: base.decodeSync(text) } 
    let result = await globalCipher.doFinal(plainText) 
    globalResult = uint8ArrayToString(result.data); 
    console.log("解密后的明文==》" + globalResult) 
  } catch (err) { 
    console.log(err.message) 
  } 
  return globalResult; 
}
分享
微博
QQ
微信
回复
2024-06-13 20:42:43
相关问题
加解密算法框架使用
762浏览 • 1回复 待解决
如何进行不同规格AES加解密
647浏览 • 1回复 待解决
加解密问题定位指导
233浏览 • 1回复 待解决
如何使用国密SM2算法进行加解密
3041浏览 • 1回复 待解决
HarmonyOS 加解密 demo
338浏览 • 1回复 待解决
HarmonyOS Sm2和DES加解密问题
264浏览 • 1回复 待解决
多种加密方式实现加解密
891浏览 • 1回复 待解决
HarmonyOS 请提供RAS加解密文档
239浏览 • 1回复 待解决
RSA导入外部密钥实现加解密
665浏览 • 1回复 待解决
HarmonyOS 有RSA加解密相关吗
292浏览 • 0回复 待解决
HarmonyOS 如何进行DES加解密
327浏览 • 1回复 待解决
如何使用SM4CBC模式加解密
722浏览 • 1回复 待解决
HarmonyOS AES加解密与java无法通用
335浏览 • 1回复 待解决
HarmonyOS 文档中没有查找到DES加解密
251浏览 • 1回复 待解决
求大佬告知如何进行des加解密
1804浏览 • 1回复 待解决
SM4采用OFB模式进行加解密
741浏览 • 1回复 待解决