In a specific scenario, I encounter data encryption from the API followed by decryption in TypeScript. I have utilized CryptoJS for decryption in TypeScript. Below is the decryption code snippet:
decrypt(source: string, iv: string): string {
var key = environment.config.KEY_PAYMENT.substring(0, 32);
const keyValue = CryptoJS.enc.Utf8.parse(key);
const ivValue = CryptoJS.enc.Utf8.parse(iv);
const plainText = CryptoJS.AES.decrypt(source, keyValue, {
keySize: 16,
iv: ivValue,
mode: CryptoJS.mode.CBC,
padding: CryptoJS.pad.Pkcs7
});
return CryptoJS.enc.Latin1.stringify(plainText);
} The provided IV and key values are crucial. Additionally, I have a Java code sample used for decryption on a mobile application that is functioning as intended. The code sample is as follows:
fun decrypt(
source: ByteArray,
key: String,
iv: ByteArray
): ByteArray {
val cipher = Cipher.getInstance("AES/CBC/PKCS5Padding")
cipher.init(Cipher.DECRYPT_MODE, makeKey(key), makeIv(iv))
return cipher.doFinal(source)
}
private fun makeIv(iv: ByteArray): AlgorithmParameterSpec {
return IvParameterSpec(iv)
}
private fun makeKey(baseKey: String): Key? {
return try {
val key = baseKey.substring(0, 32)
.toByteArray(charset("UTF-8"))
SecretKeySpec(key, "AES")
} catch (e: UnsupportedEncodingException) {
null
}
}
Sample Output:
ªîto7“ßH«3©@V¨sr","paymentType":"credit_card",...
Upon decryption, the first 16 characters appear as garbage while the remaining string is successfully decrypted. Assistance is needed at this point.