如何进行不同规格的AES加解密

如何进行不同规格的AES加解密


HarmonyOS
2024-06-13 10:36:22
浏览
收藏 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:44:47
相关问题
HarmonyOS 如何进行DES加解密
327浏览 • 1回复 待解决
求大佬告知如何进行des加解密
1804浏览 • 1回复 待解决
基于加解密算法框架规格问题
576浏览 • 1回复 待解决
HarmonyOS AES加解密与java无法通用
329浏览 • 1回复 待解决
AES加解密长字符串是否需要分段
444浏览 • 1回复 待解决
加解密HmacSha1 、HmacSha256、aes参考Demo
1917浏览 • 1回复 待解决
如何使用国密SM2算法进行加解密
3030浏览 • 1回复 待解决
SM4采用OFB模式进行加解密
737浏览 • 1回复 待解决
HarmonyOS 加解密 demo
338浏览 • 1回复 待解决
加解密问题定位指导
230浏览 • 1回复 待解决
HarmonyOS AES解密报错
431浏览 • 1回复 待解决
如何使用SM4CBC模式加解密
717浏览 • 1回复 待解决
HarmonyOS 有RSA加解密相关吗
292浏览 • 0回复 待解决
多种加密方式实现加解密
891浏览 • 1回复 待解决
加解密算法库框架使用
762浏览 • 1回复 待解决