aes-128加密问题如何实现?

需要将字符串 zw12345678 用aes-128加密算法加密,加密后用base64输出。 665319212RMX2202为密钥, 加密结果为kLOBGtdO1Q7NKB4vqxssRw==

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

以CBC加密为例来实现:

import cryptoFramework from '@ohos.security.cryptoFramework'; 
import buffer from '@ohos.buffer'; 
 
function genIvParamsSpec() { 
  let arr = [0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0]; // 16 bytes 
  let dataIv = new Uint8Array(arr); 
  let ivBlob: cryptoFramework.DataBlob = { data: dataIv }; 
  let ivParamsSpec: cryptoFramework.IvParamsSpec = { 
    algName: "IvParamsSpec", 
    iv: ivBlob 
  }; 
  return ivParamsSpec; 
} 
// 加密消息 
async function encryptMessagePromise(symKey: cryptoFramework.SymKey, plainText: cryptoFramework.DataBlob) { 
  let cipher = cryptoFramework.createCipher('AES128|CBC|PKCS7'); 
  let iv = genIvParamsSpec(); 
  await cipher.init(cryptoFramework.CryptoMode.ENCRYPT_MODE, symKey, iv); 
  let cipherData = await cipher.doFinal(plainText); 
  return cipherData; 
} 
// 解密消息 
async function decryptMessagePromise(symKey: cryptoFramework.SymKey, cipherText: cryptoFramework.DataBlob) { 
  let decoder = cryptoFramework.createCipher('AES128|CBC|PKCS7'); 
  let iv = genIvParamsSpec(); 
  await decoder.init(cryptoFramework.CryptoMode.DECRYPT_MODE, symKey, iv); 
  let decryptData = await decoder.doFinal(cipherText); 
  return decryptData; 
} 
// 生成AES CBC密钥 
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; 
} 
// aesCBC main代码 
async function aesCBC() { 
  //服务器传入的密钥 
  let keyData = "665319212RMX2202"; 
  // 先对密钥进行base64解码,获取密钥Uint8Array对象 
  let base64 = new util.Base64Helper(); 
  let keyDatabase64= base64.decodeSync(keyData ); 
  // 密钥Uint8Array 生成密钥 
  let symKey = await genSymKeyByData(keyDatabase64); 
  // 此处为明文 
  let message = " zw12345678"; 
  // 把明文字符串按utf-8解码为Uint8Array 
  let plainText: cryptoFramework.DataBlob = { data: new Uint8Array(buffer.from(message, 'utf-8').buffer) }; 
  // 加密消息 
  let encryptText = await encryptMessagePromise(symKey, plainText); 
 
  //对encryptTex:cryptoFramework.DataBlob 进行base64解码 转换成string字符串 
  let stringText: string = { data: base64Helper.encodeToStringSync(encryptText) } 
  //stringText ==kLOBGtdO1Q7NKB4vqxssRw== 
  // 解密消息 
  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'); 
  } 
}

​GCM模式请参考指南链接: 【 ​https://developer.huawei.com/consumer/cn/doc/harmonyos-guides-V5/crypto-aes-sym-encrypt-decrypt-gcm-V5​】

CBC模式请参考指南链接: 【 ​https://developer.huawei.com/consumer/cn/doc/harmonyos-guides-V5/crypto-aes-sym-encrypt-decrypt-cbc-V5​】

GCM模式请参考指南链接: 【 ​https://developer.huawei.com/consumer/cn/doc/harmonyos-guides-V5/crypto-aes-sym-encrypt-decrypt-gcm-by-segment-V5​​加密规格请参考指南链接: 【​https://developer.huawei.com/consumer/cn/doc/harmonyos-guides-V5/crypto-sym-encrypt-decrypt-spec-V5​】​

分享
微博
QQ
微信
回复
2024-11-08 16:35:00
相关问题
HarmonyOS AES128|CBC|NoPadding 加密报错
39浏览 • 1回复 待解决
HarmonyOS AES加密编码问题
49浏览 • 1回复 待解决
HarmonyOS AES CBC加密问题
586浏览 • 1回复 待解决
AES 加密问题,有知道的吗?
177浏览 • 1回复 待解决
如何使用AES ECB 加密方式?
214浏览 • 1回复 待解决
HarmonyOS RAS加密AES加密和解密
44浏览 • 1回复 待解决
是否有AES的ECB模式的加密算法
563浏览 • 1回复 待解决
AES gcm模式的加密有对应的库吗?
193浏览 • 1回复 待解决
HarmonyOS hmac加密问题
248浏览 • 1回复 待解决
HarmonyOS 请问ArkTS如何实现RSA加密
510浏览 • 1回复 待解决
HarmonyOS 实现RSA加密功能
602浏览 • 1回复 待解决