小程序如何进行AES加密?要求:AES加密Key、IV(加密偏移量)都是tyte[]数组。
下面是一段安卓代码,我需要将此代码翻译成js引用到我的小程序里面去使用。我试过网上的一些小程序AES加密的方法,key和iv都是字符串,不是tyte[]数组,怎么处理?private static final byte[] IV = new byte[]{65, 20, -91, 123, -102, 126, 105, -28, -15, 13, 51, 32, 53, 45, -97, -40};
private static final byte[] KEY = new byte[]{-49, 59, -97, -82, 5, -125, -92, -15, -7, -4, 95, -87, 85, -47, -34, -10};
private void test(String originStr){
try {
byte[] encrypt = encrypt(IV, KEY, originStr.getBytes("utf-8"));
String base64 = Base64.encodeToString(encrypt, Base64.NO_WRAP);
StringBuilder sb = new StringBuilder();
for (int i = 0; i < base64.length(); i++) {
char c = base64.charAt(i);
switch (c) {
case '=':
sb.append('二');
break;
case '+':
sb.append('十');
break;
case '/':
sb.append('丿');
break;
default:
sb.append(c);
break;
}
}
String dstStr = sb.toString();
Log.d(TAG, String.format("originStr: %s, dstStr: %s", originStr, dstStr));
} catch (Exception e) {
Log.e(TAG, e.getMessage(), e);
}
}
public static byte[] encrypt(byte[] iv, byte[] key, byte[] input) throws NoSuchPaddingException, NoSuchAlgorithmException, BadPaddingException, IllegalBlockSizeException, InvalidAlgorithmParameterException, InvalidKeyException {
SecretKeySpec secretKeySpec = new SecretKeySpec(key, "AES");
Cipher cipher = Cipher.getInstance("AES/CBC/PKCS5Padding");
IvParameterSpec ivParameterSpec = new IvParameterSpec(iv);
cipher.init(Cipher.ENCRYPT_MODE, secretKeySpec, ivParameterSpec);
return cipher.doFinal(input);
}
我记得有java转js的工具网站,你找找看