AES gcm模式的加密有对应的库吗?

HarmonyOS如何aes gcm加密一个json数据,并且做数据签名,hmacSHA1。

HarmonyOS
2024-11-08 11:43:53
浏览
收藏 0
回答 1
待解决
回答 1
按赞同
/
按时间
superinsect

demo如下:

import cryptoFramework from '@ohos.security.cryptoFramework'; 
import buffer from '@ohos.buffer'; 
 
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; 
} 
 
let gcmParams = genGcmParamsSpec(); 
 
// 加密消息 
async function encryptMessagePromise(symKey: cryptoFramework.SymKey, plainText: cryptoFramework.DataBlob) { 
  let cipher = cryptoFramework.createCipher('AES128|GCM|PKCS7'); 
  await cipher.init(cryptoFramework.CryptoMode.ENCRYPT_MODE, symKey, gcmParams); 
  let encryptUpdate = await cipher.update(plainText); 
  // gcm模式加密doFinal时传入空,获得tag数据,并更新至gcmParams对象中。 
  gcmParams.authTag = await cipher.doFinal(null); 
  return encryptUpdate; 
} 
// 解密消息 
async function decryptMessagePromise(symKey: cryptoFramework.SymKey, cipherText: cryptoFramework.DataBlob) { 
  let decoder = cryptoFramework.createCipher('AES128|GCM|PKCS7'); 
  await decoder.init(cryptoFramework.CryptoMode.DECRYPT_MODE, symKey, gcmParams); 
  let decryptUpdate = await decoder.update(cipherText); 
  // gcm模式解密doFinal时传入空,验证init时传入的tag数据,如果验证失败会抛出异常。 
  let decryptData = await decoder.doFinal(null); 
  if (decryptData == null) { 
    console.info('GCM decrypt success, decryptData is null'); 
  } 
  return decryptUpdate; 
} 
async function genSymKeyByData(symKeyData: Uint8Array) { 
  let symKeyBlob: cryptoFramework.DataBlob = { data: symKeyData }; 
  let aesGenerator = cryptoFramework.createSymKeyGenerator('AES128'); 
  let symKey = await aesGenerator.convertKey(symKeyBlob); 
  console.info('convertKey success'); 
  return symKey; 
} 
async function main() { 
  let keyData = new Uint8Array([83, 217, 231, 76, 28, 113, 23, 219, 250, 71, 209, 210, 205, 97, 32, 159]); 
  let symKey = await genSymKeyByData(keyData); 
  let message = "This is a test"; 
  let plainText: cryptoFramework.DataBlob = { data: new Uint8Array(buffer.from(message, 'utf-8').buffer) }; 
  let encryptText = await encryptMessagePromise(symKey, plainText); 
  let decryptText = await decryptMessagePromise(symKey, encryptText); 
  if (plainText.data.toString() === decryptText.data.toString()) { 
    console.info('decrypt ok'); 
    console.info('decrypt plainText: ' + buffer.from(decryptText.data).toString('utf-8')); 
  } else { 
    console.error('decrypt failed'); 
  } 
}

​相关使用指南参考链接: 【​https://developer.huawei.com/consumer/cn/doc/harmonyos-guides-V5/crypto-aes-sym-encrypt-decrypt-gcm-V5 ​】

相关API参考链接 : 【 ​https://developer.huawei.com/consumer/cn/doc/harmonyos-references-V5/js-apis-cryptoframework-V5#generatesymkey​】

相关使用规格参考链接:【​https://developer.huawei.com/consumer/cn/doc/harmonyos-guides-V5/crypto-generate-sym-key-randomly-V5​】​

分享
微博
QQ
微信
回复
2024-11-08 16:31:41
相关问题
是否AESECB模式加密算法
517浏览 • 1回复 待解决
HarmonyOS HMAC加密对应方法
389浏览 • 1回复 待解决
AES 加密问题,有知道
109浏览 • 1回复 待解决
如何使用AES ECB 加密方式?
143浏览 • 1回复 待解决
HarmonyOS AES CBC加密问题
469浏览 • 1回复 待解决
是否支持非对称加密三方
964浏览 • 1回复 待解决
aes-128加密问题如何实现?
118浏览 • 1回复 待解决
RSA NoPadding模式加密失败
1169浏览 • 1回复 待解决
HarmonyOS可用RPC组件
268浏览 • 1回复 待解决
数据迁移问题了解
1925浏览 • 1回复 待解决