【发布时间】:2019-12-27 05:55:26
【问题描述】:
我们在网络上使用coldfusion encrypt 方法。
Encrypt(plainText, key, "AES", "Hex")
在Android 中,我们使用加密方法如下:
public static String aesEncryption(String plainText, String key) {
try {
SecretKey secKey = new SecretKeySpec(key.getBytes(), "AES");
Cipher aesCipher = Cipher.getInstance("AES");
aesCipher.init(Cipher.ENCRYPT_MODE, secKey);
aesCipher.update(plainText.getBytes());
byte[] cipherText = aesCipher.doFinal();
return bytesToHex(cipherText);
} catch (NoSuchAlgorithmException | InvalidKeyException | NoSuchPaddingException | BadPaddingException | IllegalBlockSizeException e) {
e.printStackTrace();
}
return null;
}
private static final char[] HEX_ARRAY = "0123456789ABCDEF".toCharArray();
public static String bytesToHex(byte[] bytes) {
char[] hexChars = new char[bytes.length * 2];
for (int j = 0; j < bytes.length; j++) {
int v = bytes[j] & 0xFF;
hexChars[j * 2] = HEX_ARRAY[v >>> 4];
hexChars[j * 2 + 1] = HEX_ARRAY[v & 0x0F];
}
return new String(hexChars);
}
但是在Android 中加密的输出不匹配,如何使用Android AES 加密与coldfusionencrypt 相同?
【问题讨论】:
-
你能发布一个 java 和 cf 输出的例子吗?使用 generateSecretKey() 生成一次性密钥。
-
为什么使用ECB模式?它不安全。在今天的标准中,我们使用 AES-GCM 等经过身份验证的加密模式。
标签: java android encryption coldfusion aes