HarmonyOS AES加解密与java无法通用

客户端参数AES加密后post给服务端解密,但是HarmonyOS AES加密后无法在java中解密,麻烦看看怎样解决。

HarmonyOS
2024-09-02 10:47:07
浏览
收藏 0
回答 1
待解决
回答 1
按赞同
/
按时间
superinsect

ArkTs使用AES加密过程如下:

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('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, 7x6, 2x8, 113, x23, 219, 250, 7x1, xxx, 210, 205, xxx, 3x2, 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'); 
  } 
}

Java使用AES实现数据加密解密过程如下:

package com.example.demo;

import org.junit.jupiter.api.Test; 
 
import javax.crypto.Cipher; 
import javax.crypto.spec.SecretKeySpec; 
import java.util.Base64; 
 
/** 
 * Java使用AES加密算法进行加密解密 
 * */ 
public class AES { 
  /** 
   * AES算法加密 
   * @Param:text原文 
   * @Param:key密钥 
   * */ 
  public String AESEncryptDemo(String text,String key) throws Exception { 
  // 创建AES加密算法实例(根据传入指定的秘钥进行加密) 
  Cipher cipher = Cipher.getInstance("AES/ECB/PKCS5Padding"); 
  SecretKeySpec keySpec = new SecretKeySpec(key.getBytes(), "AES"); 
 
  // 初始化为加密模式,并将密钥注入到算法中 
  cipher.init(Cipher.ENCRYPT_MODE, keySpec); 
 
  // 将传入的文本加密 
  byte[] encrypted = cipher.doFinal(text.getBytes()); 
 
  //生成密文 
  // 将密文进行Base64编码,方便传输 
  return Base64.getEncoder().encodeToString(encrypted); 
} 
 
/** 
 * AES算法解密 
 * @Param:base64Encrypted密文 
 * @Param:key密钥 
 * */ 
public String AESDecryptDemo(String base64Encrypted,String key)throws Exception{ 
  // 创建AES解密算法实例 
  Cipher cipher = Cipher.getInstance("AES/ECB/PKCS5Padding"); 
  SecretKeySpec keySpec = new SecretKeySpec(key.getBytes(), "AES"); 
 
  // 初始化为解密模式,并将密钥注入到算法中 
  cipher.init(Cipher.DECRYPT_MODE, keySpec); 
 
  // 将Base64编码的密文解码 
  byte[] encrypted = Base64.getDecoder().decode(base64Encrypted); 
 
  // 解密 
  byte[] decrypted = cipher.doFinal(encrypted); 
  return new String(decrypted); 
} 
@Test 
public void AESTest() throws Exception { 
  //明文 
  String text="从大漠孤烟塞北,到杏花春雨江南,从山水田园牧歌,到金戈铁马阳关,我们在吟诵着千古名句,我们也在体味着人间百态"; 
  //秘钥(需要使用长度为16、24或32的字节数组作为AES算法的密钥,否则就会遇到java.security.InvalidKeyException异常) 
  String key="20230610HelloDog"; 
  //加密,生成密文 
  String base64Encrypted = AESEncryptDemo(text,key); 
 
  System.out.println(base64Encrypted); 
  //解密,获取明文 
  String text2 = AESDecryptDemo(base64Encrypted,key); 
 
  System.out.println(text2); 
} 
}
分享
微博
QQ
微信
回复
2024-09-02 17:40:08
相关问题
AES加解密长字符串是否需要分段
530浏览 • 1回复 待解决
如何进行不同规格的AES加解密
786浏览 • 1回复 待解决
加解密HmacSha1 、HmacSha256、aes参考Demo
2079浏览 • 1回复 待解决
HarmonyOS 加解密 demo
432浏览 • 1回复 待解决
HarmonyOS AES解密报错
610浏览 • 1回复 待解决
HarmonyOS 如何进行DES加解密
459浏览 • 1回复 待解决
HarmonyOS 有RSA加解密相关吗
385浏览 • 0回复 待解决
加解密问题的定位指导
339浏览 • 1回复 待解决
HarmonyOS 请提供RAS加解密的文档
347浏览 • 1回复 待解决
使用AES解密时,一直在报解密异常
204浏览 • 1回复 待解决
多种加密方式实现加解密
986浏览 • 1回复 待解决
加解密算法库框架使用
852浏览 • 1回复 待解决
HarmonyOS Sm2和DES加解密问题
381浏览 • 1回复 待解决