CryptoJS.AES.encrypt 在 android (kotlin) 中给出不同的结果
CryptoJS.AES.encrypt giving different result in android (kotlin)
Android 客户端需要使用我们其他 Javascript 应用程序中使用的相同加密。即
const key = CryptoJS.enc.Utf8.parse(getSecretKey());
const piv = new Date().toISOString().substring(0, 16);
const iv = CryptoJS.enc.Utf8.parse(piv);
const miv = piv + userCredentials;
const msg = CryptoJS.enc.Utf8.parse(miv);
val msg = "64 character long string"
val key = "32 character long string"
val iv = "32 character long string"
const encrypted = CryptoJS.AES.encrypt(msg, key, {
keySize: 16,
iv,
mode: CryptoJS.mode.CBC,
padding: CryptoJS.pad.Pkcs7
});
我尝试在 Kotlin 中使用以下代码
val key = toHexString(getSecretKey())
val iv = toHexString(getCurrentUTCTime())
val msg = toHexString(getMessage())
//1. Create a cipher object
val cipher = Cipher.getInstance("AES/CBC/PKCS7Padding")
//2. Initialize cipher
val keySpec = SecretKeySpec(key.toByteArray(),0,16,"AES")
val ivParameterSpec = IvParameterSpec(iv.toByteArray(),0,16)
cipher.init(Cipher.ENCRYPT_MODE, keySpec, ivParameterSpec)
//3. Encryption and decryption
val encrypt = cipher.doFinal(msg.toByteArray())
return Base64.getEncoder().encodeToString(encrypt)
private fun toHexString(bytes: ByteArray): String {
val formatter = Formatter()
for (b in bytes) {
formatter.format("%02x", b)
}
return formatter.toString()
}
两者都返回不同的值。有人可以告诉我,如果两个算法相同,它们的配置相同,它们的密钥相同,为什么它们返回不同的值?
而不是这样做
val key = toHexString(getSecretKey())
val iv = toHexString(getCurrentUTCTime())
val msg = toHexString(getMessage())
我不得不删除函数调用。
val key = getSecretKey()
val iv = getCurrentUTCTime()
val msg = getMessage()
然后 javascript 应用程序和 android 客户端的两个结果相同。
Android 客户端需要使用我们其他 Javascript 应用程序中使用的相同加密。即
const key = CryptoJS.enc.Utf8.parse(getSecretKey());
const piv = new Date().toISOString().substring(0, 16);
const iv = CryptoJS.enc.Utf8.parse(piv);
const miv = piv + userCredentials;
const msg = CryptoJS.enc.Utf8.parse(miv);
val msg = "64 character long string"
val key = "32 character long string"
val iv = "32 character long string"
const encrypted = CryptoJS.AES.encrypt(msg, key, {
keySize: 16,
iv,
mode: CryptoJS.mode.CBC,
padding: CryptoJS.pad.Pkcs7
});
我尝试在 Kotlin 中使用以下代码
val key = toHexString(getSecretKey())
val iv = toHexString(getCurrentUTCTime())
val msg = toHexString(getMessage())
//1. Create a cipher object
val cipher = Cipher.getInstance("AES/CBC/PKCS7Padding")
//2. Initialize cipher
val keySpec = SecretKeySpec(key.toByteArray(),0,16,"AES")
val ivParameterSpec = IvParameterSpec(iv.toByteArray(),0,16)
cipher.init(Cipher.ENCRYPT_MODE, keySpec, ivParameterSpec)
//3. Encryption and decryption
val encrypt = cipher.doFinal(msg.toByteArray())
return Base64.getEncoder().encodeToString(encrypt)
private fun toHexString(bytes: ByteArray): String {
val formatter = Formatter()
for (b in bytes) {
formatter.format("%02x", b)
}
return formatter.toString()
}
两者都返回不同的值。有人可以告诉我,如果两个算法相同,它们的配置相同,它们的密钥相同,为什么它们返回不同的值?
而不是这样做
val key = toHexString(getSecretKey())
val iv = toHexString(getCurrentUTCTime())
val msg = toHexString(getMessage())
我不得不删除函数调用。
val key = getSecretKey()
val iv = getCurrentUTCTime()
val msg = getMessage()
然后 javascript 应用程序和 android 客户端的两个结果相同。