如何在JavaScript中执行AES加密并在Java中解密结果?

huangapple go评论81阅读模式
英文:

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(&quot;base64&quot;)
  // secret_key = Buffer.from(secret_key).toString(&quot;base64&quot;)

  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(&quot;iv&quot;, iv)
  console.log(&quot;Secret&quot;, secret_key)
  const ciphertext = CryptoJS.AES.encrypt(jsonDataString, secret_key, {
    iv: iv,
    padding: CryptoJS.pad.Pkcs7,
    mode: CryptoJS.mode.CBC,
  })
  console.log(&quot;cipherText&quot;, 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), &quot;AES&quot;);
        Cipher cipher = Cipher.getInstance(ALGORITHM, &quot;BC&quot;);
        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(&quot;cipherText&quot;, ciphertext);

  // Print the ciphertext
  console.log(ciphertext.toString());
}

huangapple
  • 本文由 发表于 2023年4月17日 01:04:12
  • 转载请务必保留本文链接:https://go.coder-hub.com/76029197.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定