HarmonyOS AES加解密问题

平台下载的加解密demo Cipher项目,本地运行起来后

选AES 测试:

英文加密解密功能正常;

中文加密后,把密文进行解密,结果乱码。

期望中英文都能加解密正常。

HarmonyOS
1天前
浏览
收藏 0
回答 1
待解决
回答 1
按赞同
/
按时间
aquaa
import { cryptoFramework } from "@kit.CryptoArchitectureKit";
import { AsyncCallback, BusinessError } from '@kit.BasicServicesKit';
import { buffer, util } from '@kit.ArkTS';
import Logger from '../utils/Logger';

const AES128: string = 'AES128';
const AES128_PKCS7: string = 'AES128|PKCS7';
const AES_ENCRYPT_KEY: string = 'abcdefghijklmn!%';

const TAG: string = '[CryptoToos]'

class CryptoToos {

  stringToUint8Array(str: string) {
    return new Uint8Array(buffer.from(str, 'utf-8').buffer);
  }

  // 字节流转成可理解的字符串
  uint8ArrayToString(array: Uint8Array) {
    let out: string = '';
    let index: number = 0;
    let len: number = array.length;
    while (index < len) {
      let character = array[index++];
      switch (character >> 4) {
        case 0:
        case 1:
        case 2:
        case 3:
        case 4:
        case 5:
        case 6:
        case 7:
          out += String.fromCharCode(character);
          break;
        case 12:
        case 13:
          out += String.fromCharCode(((character & 0x1F) << 6) | (array[index++] & 0x3F));
          break;
        case 14:
          out += String.fromCharCode(((character & 0x0F) << 12) | ((array[index++] & 0x3F) << 6) |
            ((array[index++] & 0x3F) << 0));
          break;
        default:
          break;
      }
    }
    return out;
  }

  //摘要结果是Uint8Array类型,转换得到十六进制字符串数据
  uint8ArrayToHexStr(data: Uint8Array): string {
    let hexString = "";
    let i: number;
    for (i = 0; i < data.length; i++) {
      let char = ('00' + data[i].toString(16)).slice(-2);
      hexString += char;
    }
    return hexString;
  }

  /**
   * AES Encrypt
   * @param message
   * @param callback
   */
  aesEncrypt(message: string, callback: Callback<string>) {
    let aesGenerator = cryptoFramework.createSymKeyGenerator(AES128);
    let cipher = cryptoFramework.createCipher(AES128_PKCS7);
    let pubKey = this.stringToUint8Array(AES_ENCRYPT_KEY)
    let pubKeyBlob: cryptoFramework.DataBlob = { data: pubKey };
    aesGenerator.convertKey(pubKeyBlob, (err, symKey) => {
      if (err) {
        console.error("convertKey: error." + (err as BusinessError).code);
        return;
      }
      cipher.init(cryptoFramework.CryptoMode.ENCRYPT_MODE, symKey, null, (err, data) => {
        let input: cryptoFramework.DataBlob = { data: this.stringToUint8Array(message) };
        cipher.doFinal(input, (err, data) => {
          Logger.info(TAG, "EncryptOutPut is " + data.data);
          let that = new util.Base64Helper();
          let result = that.encodeToStringSync(data.data)
          Logger.info(TAG, "result is " + result);
          callback(result)
        })
      })
    })
  }

  /**
   * AES Decrypt
   * @param message
   * @param callback
   */
  aesDecrypt(message: string, callback: Callback<string>) {
    let aesGenerator = cryptoFramework.createSymKeyGenerator(AES128);
    let cipher = cryptoFramework.createCipher(AES128_PKCS7);
    let pubKey = this.stringToUint8Array(AES_ENCRYPT_KEY);
    let pubKeyBlob: cryptoFramework.DataBlob = { data: pubKey };
    aesGenerator.convertKey(pubKeyBlob, (err, symKey) => {
      if (err) {
        console.error("convertKey: error." + (err as BusinessError).code);
        return;
      }
      cipher.init(cryptoFramework.CryptoMode.DECRYPT_MODE, symKey, null, (err, data) => {
        try {
          let that = new util.Base64Helper();
          let newMessage = that.decodeSync(message);
          let input: cryptoFramework.DataBlob = { data: newMessage };
          cipher.doFinal(input, (err, data) => {
            if (err) {
              Logger.error(TAG, "cipher doFinal." + (err as BusinessError).code);
              return;
            }
            Logger.info(TAG, "DecryptOutPut is " + data?.data);
            let result = this.uint8ArrayToString(data?.data)
            Logger.info(TAG, "result is " + result);
            callback(result)
          })
        } catch (err) {
          Logger.info(TAG, "cipher init error: " + (err as BusinessError).code);
          return err;
        }
      })
    })
  }

密钥:

const AES_ENCRYPT_KEY: string = 'abcdef..ijklmn!%';

得到的加密结果集:

result = "n2r3tj...DvRaBNYYzVLiQ=="
分享
微博
QQ
微信
回复
1天前
相关问题
HarmonyOS AES加解密咨询
89浏览 • 1回复 待解决
HarmonyOS AES加解密与java无法通用
518浏览 • 1回复 待解决
HarmonyOS aes gcm加解密c/c++库
65浏览 • 1回复 待解决
HarmonyOS AES/ECB/PKCS7Padding 加解密
26浏览 • 1回复 待解决
HarmonyOS 加解密问题
48浏览 • 1回复 待解决
如何进行不同规格的AES加解密
858浏览 • 1回复 待解决
HarmonyOS AES加解密过程实在是调不通
22浏览 • 1回复 待解决
HarmonyOS 加解密方式 AES/ECB/PKCS5Padding
38浏览 • 1回复 待解决
AES加解密长字符串是否需要分段
619浏览 • 1回复 待解决
加解密HmacSha1 、HmacSha256、aes参考Demo
2220浏览 • 1回复 待解决
加解密问题的定位指导
391浏览 • 1回复 待解决
HarmonyOS 关于RSA公钥加解密问题
38浏览 • 1回复 待解决
HarmonyOS 加解密 demo
532浏览 • 1回复 待解决
HarmonyOS 加解密咨询
33浏览 • 1回复 待解决
基于加解密算法框架的规格问题
743浏览 • 1回复 待解决
HarmonyOS AES解密报错
720浏览 • 1回复 待解决
HarmonyOS Sm2和DES加解密问题
512浏览 • 1回复 待解决