HarmonyOS AES加密算法demo

其他系统实现方法:

public static String encryptAES(String data, String key, String iv)  {
  try {
    Cipher cipher = Cipher.getInstance("AES/CBC/NoPadding");
    int blockSize = cipher.getBlockSize();
    byte[] dataBytes = data.getBytes();
    int plaintextLength = dataBytes.length;

    if (plaintextLength % blockSize != 0) {
      plaintextLength = plaintextLength + (blockSize - (plaintextLength % blockSize));
    }

    byte[] plaintext = new byte[plaintextLength];
    System.arraycopy(dataBytes, 0, plaintext, 0, dataBytes.length);

    SecretKeySpec keyspec = new SecretKeySpec(key.getBytes(), "AES");
    IvParameterSpec ivspec = new IvParameterSpec(iv.getBytes());  // CBC模式,需要一个向量iv,可增加加密算法的强度

    cipher.init(Cipher.ENCRYPT_MODE, keyspec, ivspec);

    return Base64Util.encodeBytes(cipher.doFinal(plaintext));

  } catch (Throwable e) {
    ShareAlcLogUtil.handOffDebugLog("AMapCarHandoffAESUtil","encryptAES " + Log.getStackTraceString(e));
    return "";
  }
}
HarmonyOS
1天前
浏览
收藏 0
回答 1
待解决
回答 1
按赞同
/
按时间
fox280

参考demo:

const base64 = new util.Base64Helper();
const textEncoder = new util.TextEncoder();
let key = "LRGg4aDNkG4tDkD=";
let vi = "BpLsJEYGIlvi9DLJ";

function encryptByAES(data: string): string {
  try {
    let aesGenerator = cryptoFramework.createSymKeyGenerator('AES128');
    let symKey = aesGenerator.convertKeySync({ data: textEncoder.encodeInto(key) });
    let ivspec: cryptoFramework.IvParamsSpec = {
      algName: "IvParamsSpec", // CBC模式
      iv: { data: textEncoder.encodeInto(vi) }
    };
    let cipher = cryptoFramework.createCipher('AES|CBC|NoPadding');
    cipher.initSync(cryptoFramework.CryptoMode.ENCRYPT_MODE, symKey, ivspec);
    let plainText: cryptoFramework.DataBlob = { data: textEncoder.encodeInto(data) };
    return base64.encodeToStringSync(cipher.doFinalSync(plainText).data);
  } catch (e) {
    console.log('encrypt with error code ' + e.code)
    console.log('encrypt with error message ' + e.message)
    return "";
  }
}

function encryptByAESWithNoPadding(data: string): string {
  try {
    let aesGenerator = cryptoFramework.createSymKeyGenerator('AES128');
    let symKey = aesGenerator.convertKeySync({ data: textEncoder.encodeInto(key) });
    let ivspec: cryptoFramework.IvParamsSpec = {
      algName: "IvParamsSpec", // CBC模式
      iv: { data: textEncoder.encodeInto(vi) }
    };
    let cipher = cryptoFramework.createCipher('AES|CBC|NoPadding');
    // let cipher = cryptoFramework.createCipher('AES|CBC|PKCS5');
    cipher.initSync(cryptoFramework.CryptoMode.ENCRYPT_MODE, symKey, ivspec);

    let blockSize: number = 16; // AES为分组加密算法,分组长度为128位,最后一组明文可能不足128位(16字节),此时可以通过不同的填充模式进行数据填充,如果是NoPadding需要手动补全
    let dataBytes = textEncoder.encodeInto(data);
    let plaintextLength = dataBytes.length;
    if (plaintextLength % blockSize != 0) { // 如果使用PKCS5补全模式,不需要if语句判断补全
      plaintextLength = plaintextLength + (blockSize - (plaintextLength % blockSize));
    }
    let plaintext = new Uint8Array(plaintextLength);
    plaintext.set(dataBytes);

    let plainTextBlob: cryptoFramework.DataBlob = { data: plaintext };
    let result = cipher.doFinalSync(plainTextBlob).data;
    return base64.encodeToStringSync(result);
  } catch (e) {
    return "";
  }
}
分享
微博
QQ
微信
回复
1天前
相关问题
HarmonyOS MD5加密算法demo
37浏览 • 1回复 待解决
是否有AES的ECB模式的加密算法
579浏览 • 1回复 待解决
HarmonyOS RSA加密算法实现
36浏览 • 1回复 待解决
安全加密算法代码参考
1807浏览 • 1回复 待解决
HarmonyOS SM2加密算法
23浏览 • 1回复 待解决
HarmonyOS 需要MD5加密算法
49浏览 • 1回复 待解决
HarmonyOS 是否有带iv的DES加密算法
526浏览 • 1回复 待解决
HarmonyOS HmacSHA1 加密算法 参数问题
38浏览 • 1回复 待解决
加密算法(crypto-js)Arkts转化代码
1144浏览 • 1回复 待解决
HarmonyOS AES加密
34浏览 • 1回复 待解决
HarmonyOS RAS加密AES加密和解密
67浏览 • 1回复 待解决
HarmonyOS AES加密相关
36浏览 • 1回复 待解决
HarmonyOS AES CBC加密问题
607浏览 • 1回复 待解决
HarmonyOS AES加密编码问题
83浏览 • 1回复 待解决
如何使用AES ECB 加密方式?
222浏览 • 1回复 待解决
aes-128加密问题如何实现?
236浏览 • 1回复 待解决
AES 加密问题,有知道的吗?
190浏览 • 1回复 待解决
HarmonyOS AES128|CBC|NoPadding 加密报错
58浏览 • 1回复 待解决