HarmonyOS AES加解密咨询

发现有个问题,就是在遇到比如\n这种换行符等符号时候,就和之前java的加密结果不一致。如果删掉\n就能得到同样的加密结果。用同样的密码,对同一个内容加密。

密码:718fc3a5e0e1d05a内容:

{"anwser":51.0,"content":"x - 15 = 36\nx = ?","image":"","index":3,"subject":"","tips":""},{"anwser":9.0,"content":"1, 3, 5, 7, ?","image":"","index":4,"subject":"","tips":"后一个数比前一个数+2"} 

java测试:

String preContent="{\"anwser\":51.0,\"content\":\"x - 15 = 36\\nx = ?\",\"image\":\"\",\"index\":3,\"subject\":\"\",\"tips\":\"\"},{\"anwser\":9.0,\"content\":\"1, 3, 5, 7, ?\",\"image\":\"\",\"index\":4,\"subject\":\"\",\"tips\":\"后一个数比前一个数+2\"}"; String test1 = BaseAesUtil.aesEncrypt(preContent, "718fc3a5e0e1d05a"); 

得到的加密结果:test1为:

2423E707876B19976C72ED9EA78EDCF1D5DA8417699A28F9FFCC2448C72FED797B5E77E499C0B9E6F961BDC05A22AF16A5EA2863E151272B66FB8D4A0B00278F4DF0643E55A66A0D81FF244C5D9BCCB6E594BB39234B7701224D773CE73E6D87EA03CF39468C4C92B3A4B2B89D3D946EA44F8A39FD38A96D1C20FAF7B5FC686FFB651D73959B79D35D919AF395D00A688A1E5EC44D8D95CD5BB7A20B83E26DBACF7D68F9E303FC7845B407B6B23EA73BAB4090230166CB07D00EE5D3F64B46252C332ADBDDC858D910287F442E56D22A 
 let preContent: string = '{"anwser":51.0,"content":"x - 15 = 36\nx = ?","image":"","index":3,"subject":"","tips":""},{"anwser":9.0,"content":"1, 3, 5, 7, ?","image":"","index":4,"subject":"","tips":"后一个数比前一个数+2"}'     
let test2 : string = await BAesUtil.aesEncrypt(preContent, '718fc3a5e0e1d05a') as string; 

得到的加密结果:test2为:

2423E707876B19976C72ED9EA78EDCF1D5DA8417699A28F9FFCC2448C72FED79DC5B54269865B4692B80A81D80520323849218F04FAC33EFE7B59A97AC55AD81E1E1C36B8A63EE0B3B6C2B3C80A30E528E1A33BF5FDACAD5C2DF7D1579B35D93F0C9134656EF4A838078D1348C8BB189CA17F8753A0A05F525AAE3F5D8F16339F016BC91C9A892470205BCA9F4FE2A088F3A7600933D9D913A12EDCF106B50B64587765AEEB2A951B40F1BF0CEEF1BC458593ED6B29A9A5AD1358C65EC789B7394478B131ABD20B7D0F5EF28EF09E8E7 

可以看出加密结果前面相同,大概在有换行符地方就开始不一样了。

HarmonyOS
3天前
浏览
收藏 0
回答 1
待解决
回答 1
按赞同
/
按时间
Excelsior_abit

AES同步方法代码demo实例如下:

import { cryptoFramework } from '@kit.CryptoArchitectureKit';
import { buffer, util } from '@kit.ArkTS';

@Entry
@Component
struct AESCBCCrypto {
  @State message: string = '点击开始';

  build() {
    Row() {
      Column() {
        Text(this.message)
          .fontSize(50)
          .fontWeight(FontWeight.Bold)
          .onClick(() => {
            aesCBC()
          })
      }
      .width('100%')
    }
    .height('100%')
  }
}

function genIvParamsSpec() {
  // let iv = new Uint8Array(buffer.from('0fa849f99a7b4738', 'utf-8').buffer);
  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;
}

// 加密消息
function encryptMessage(symKey: cryptoFramework.SymKey, plainText: cryptoFramework.DataBlob) {
  let cipher = cryptoFramework.createCipher('AES128|CBC|PKCS7');
  let iv = genIvParamsSpec();
  cipher.init(cryptoFramework.CryptoMode.ENCRYPT_MODE, symKey, iv);
  let cipherData = cipher.doFinalSync(plainText);
  return cipherData;
}

// 解密消息
function decryptMessage(symKey: cryptoFramework.SymKey, cipherText: cryptoFramework.DataBlob) {
  let decoder = cryptoFramework.createCipher('AES128|CBC|PKCS7');
  let iv = genIvParamsSpec();
  decoder.init(cryptoFramework.CryptoMode.DECRYPT_MODE, symKey, iv);
  let decryptData = decoder.doFinalSync(cipherText);
  return decryptData;
}

function genSymKeyByData(symKeyData: Uint8Array) {
  let symKeyBlob: cryptoFramework.DataBlob = { data: symKeyData };
  let aesGenerator = cryptoFramework.createSymKeyGenerator('AES128');
  let symKey = aesGenerator.convertKeySync(symKeyBlob);
  console.info('convertKey success');
  return symKey;
}

function aesCBC() {
  try {
    let base64 = new util.Base64Helper();
    let pk = 'V2hoODJHdFcvRVZqQmtEOA==';
    let key = base64.decodeSync(pk);
    let symKey = genSymKeyByData(key);
    let message = `{"password":"abc12345"}`;
    let newBuf = buffer.from(buffer.from(message, 'utf-8').buffer);
    let plainText: cryptoFramework.DataBlob = { data: new Uint8Array(newBuf.buffer) };
    let encryptText = encryptMessage(symKey, plainText);
    console.error('encrypt 16进制 plainText: ' + buffer.from(encryptText.data).toString('hex'));
    console.error('encrypt base64 plainText: ' + base64.encodeToStringSync(encryptText.data));
    buffer.from(encryptText.data.toString(), 'hex').buffer
    let decryptText = decryptMessage(symKey, encryptText);
    if (plainText.data.toString() === decryptText.data.toString()) {
      console.error('decrypt ok' + plainText.data);
      console.error('decrypt plainText: ' + buffer.from(decryptText.data).toString('utf-8'));
    } else {
      console.error('decrypt failed');
    }
  }catch (error){
    console.error(JSON.stringify(error));
  }
}
分享
微博
QQ
微信
回复
3天前
相关问题
HarmonyOS AES加解密问题
43浏览 • 1回复 待解决
HarmonyOS 加解密咨询
37浏览 • 1回复 待解决
HarmonyOS AES加解密与java无法通用
518浏览 • 1回复 待解决
HarmonyOS aes gcm加解密c/c++库
68浏览 • 1回复 待解决
HarmonyOS AES/ECB/PKCS7Padding 加解密
26浏览 • 1回复 待解决
如何进行不同规格的AES加解密
863浏览 • 1回复 待解决
HarmonyOS AES加解密过程实在是调不通
22浏览 • 1回复 待解决
HarmonyOS Native RSA 加解密实现咨询
43浏览 • 1回复 待解决
HarmonyOS 加解密方式 AES/ECB/PKCS5Padding
43浏览 • 1回复 待解决
AES加解密长字符串是否需要分段
624浏览 • 1回复 待解决
加解密HmacSha1 、HmacSha256、aes参考Demo
2220浏览 • 1回复 待解决
HarmonyOS 加解密问题
52浏览 • 1回复 待解决
HarmonyOS 加解密 demo
534浏览 • 1回复 待解决
HarmonyOS AES解密报错
720浏览 • 1回复 待解决
HarmonyOS 加解密算法匹配
77浏览 • 1回复 待解决
HarmonyOS 加解密算法如何使用
66浏览 • 1回复 待解决