英文:
How do I carry out AES Encryption in javascript and decrypt the result in Java?
问题
The issue might be related to the padding scheme used during encryption and decryption. Make sure you are using the same padding scheme in both your JavaScript and Java code. In your JavaScript code, you are using CryptoJS.pad.Pkcs7
for padding. However, in your Java code, you are using PKCS7Padding
.
Ensure that you are using consistent padding schemes in both codes. If you want to use PKCS7 padding, make sure to specify it correctly in both your JavaScript and Java implementations.
英文:
I have a JavaScript frontend that's meant to encrypt data using AES/CBC/PKCS7Padding and send to a Java backend for decryption.
This is an extract of my JavaScript code:
<!-- language: lang-js -->
export const encryptPaymentData = (data) => {
var encrypt = new JSEncrypt()
encrypt.setPublicKey(publicKey)
// convert the JSON object to a string
const jsonDataString = JSON.stringify(data)
// Create iv and secret_key
let iv = crypto.randomBytes(16)
let secret_key = crypto.randomBytes(32)
// iv = Buffer.from(iv).toString("base64")
// secret_key = Buffer.from(secret_key).toString("base64")
iv = CryptoJS.enc.Base64.stringify(CryptoJS.lib.WordArray.create(iv))
secret_key = CryptoJS.enc.Base64.stringify(
CryptoJS.lib.WordArray.create(secret_key)
)
// Encrypt the plaintext using AES/CBC/PKCS5Padding
console.log("iv", iv)
console.log("Secret", secret_key)
const ciphertext = CryptoJS.AES.encrypt(jsonDataString, secret_key, {
iv: iv,
padding: CryptoJS.pad.Pkcs7,
mode: CryptoJS.mode.CBC,
})
console.log("cipherText", ciphertext)
// Print the ciphertext
console.log(ciphertext.toString())
}
And this is an extract of my Java code:
<!-- language: lang-java -->
private static final String ALGORITHM = "AES/CBC/PKCS7Padding";
public String decryptAES(String ciphertext, String secretKey, String iv) throws Exception {
IvParameterSpec ivSpec = new IvParameterSpec(Base64.getDecoder().decode(iv));
SecretKeySpec keySpec = new SecretKeySpec(Base64.getDecoder().decode(secretKey), "AES");
Cipher cipher = Cipher.getInstance(ALGORITHM, "BC");
cipher.init(Cipher.DECRYPT_MODE, keySpec, ivSpec);
byte[] decrypted = cipher.doFinal(Base64.getDecoder().decode(ciphertext));
return new String(decrypted);
}
When I copy the secret key and iv and encrypted value and try to decrypt in my Java program, I keep getting an error: javax.crypto.BadPaddingException: pad block corrupted
Please, what could the problem be?
答案1
得分: 0
Following Topaco's explanation, I passed the secret_key and iv as WordArray not Base64, and that resolved the issue.
function encrypt(data) {
// convert the JSON object to a string
const jsonDataString = JSON.stringify(data);
// Create iv and secret_key
let iv = crypto.randomBytes(16);
let secret_key = crypto.randomBytes(32);
iv = CryptoJS.lib.WordArray.create(iv);
secret_key = CryptoJS.lib.WordArray.create(secret_key);
// Encrypt the plaintext using AES/CBC/PKCS5Padding
const ciphertext = CryptoJS.AES.encrypt(jsonDataString, secret_key, {
iv: iv,
padding: CryptoJS.pad.Pkcs7,
mode: CryptoJS.mode.CBC,
});
console.log("cipherText", ciphertext);
// Print the ciphertext
console.log(ciphertext.toString());
}
英文:
Following Topaco's explanation, I passed the secret_key and iv as WordArray not Base64, and that resolved the issue.
function encrypt(data) {
// convert the JSON object to a string
const jsonDataString = JSON.stringify(data);
// Create iv and secret_key
let iv = crypto.randomBytes(16);
let secret_key = crypto.randomBytes(32);
iv = CryptoJS.lib.WordArray.create(iv);
secret_key = CryptoJS.lib.WordArray.create(secret_key);
// Encrypt the plaintext using AES/CBC/PKCS5Padding
const ciphertext = CryptoJS.AES.encrypt(jsonDataString, secret_key, {
iv: iv,
padding: CryptoJS.pad.Pkcs7,
mode: CryptoJS.mode.CBC,
});
console.log("cipherText", ciphertext);
// Print the ciphertext
console.log(ciphertext.toString());
}
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论