如何在 CBC 模式和 PKCS7 填充中解码使用 AES-256 加密的数据?
How to decode data encrypted using AES-256 in CBC mode and PKCS7 padding?
我们使用下面的 java 代码来解密使用 CBC 模式和 PKCS7 填充的 AES-256 加密的数据。
Java代码:
import javax.crypto.spec.SecretKeySpec;
import javax.crypto.Cipher;
import javax.crypto.spec.IvParameterSpec;
import java.security.*;
import org.apache.commons.codec.binary.Base64;
import org.apache.commons.lang.StringUtils;
import org.apache.log4j.Logger;
public class AES256 {
private static byte[] initVector = { 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 };
public String decrypt (String encryptedDataBase64, String keyBase64)
{
try {
Security.setProperty("crypto.policy", "unlimited");
IvParameterSpec ivSpec = new IvParameterSpec(initVector); // Get the init vector
// Get the Base64-encoded key
byte[] key = Base64.decodeBase64(keyBase64.getBytes("UTF-8"));
Cipher cipher = Cipher.getInstance("AES/CBC/PKCS5PADDING"); // AES / CBC / PKCS5 padding
SecretKeySpec skeySpec = new SecretKeySpec(key, "AES");
cipher.init(Cipher.DECRYPT_MODE, skeySpec, ivSpec);
byte[] encryptedData = Base64.decodeBase64(encryptedDataBase64.getBytes("UTF-8"));
byte[] decryptedData = cipher.doFinal(encryptedData);
return new String(decryptedData);
}
catch (Exception e) {
logger.error("AES256 Decrypt: Decryption exception: "+ e.getMessage());
return null;
}
}
}
现在我们需要将此解密逻辑转换为 Java 脚本,因为我们的应用程序在 headers 中发送加密数据,同时从服务器请求 index.html。我尝试使用 Crypto.js 进行解密,但我得到的解码字符串为空。下面是我尝试使用的Java脚本代码。
var key = CryptoJS.enc.Base64.parse(keyBase64);
var data = CryptoJS.enc.Base64.parse(encryptedDataBase64);
var dec_data = CryptoJS.AES.decrypt(data, key);
dec_data 空空如也。
我在阅读了一些堆栈溢出问题后尝试了这个。任何人都可以让我知道代码中是否有任何错误。
很遗憾,您没有提供任何示例数据来测试您的环境,因此我设置了自己的值。使用此值:
ciphertext = rjygE0TjIqiQ4ETnpszoieRWzaSD+9oINf1c748VcL/3zD5AazSFomx4paeanihz
keyBase64 = MTIzNDU2Nzg5MDEyMzQ1Njc4OTAxMjM0NTY3ODkwMTI=
ivBase = AAAAAAAAAAAAAAAAAAAAAA==
我在 Java 收到这个 decrypt-result:
The quick brown fox jumps over the lazy dog
在 Javascript-side 我收到与我的代码相同的结果:
decrypted (str): The quick brown fox jumps over the lazy dog
可以在此处进行实时测试 - Java: https://paiza.io/projects/e/dHG73CRgJojOOfLxtvJjtg and Javascript: https://playcode.io/672463.
请注意,我的代码没有异常处理,仅用于教育目的。此外 - 您的代码是不安全的,因为它使用静态(固定)密钥和初始化向量。
Java 代码(由于我使用的是 Java 11 内置的 Base64 编码器而略有更改):
import javax.crypto.Cipher;
import javax.crypto.spec.IvParameterSpec;
import javax.crypto.spec.SecretKeySpec;
import java.io.UnsupportedEncodingException;
import java.security.Security;
import java.util.Base64;
public class Main {
private static byte[] initVector = { 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 };
public static void main(String[] args) throws UnsupportedEncodingException {
System.out.println("How to decode data encrypted using AES-256 in CBC mode and PKCS7 padding");
String plaintext = "The quick brown fox jumps over the lazy dog";
byte[] key = "12345678901234567890123456789012".getBytes("UTF-8");
String keyBase64 = Base64.getEncoder().encodeToString(key);
System.out.println("keyBase64: " + keyBase64);
String ivBase64 = Base64.getEncoder().encodeToString(initVector);
System.out.println("initVector: " + ivBase64);
String ciphertext = "rjygE0TjIqiQ4ETnpszoieRWzaSD+9oINf1c748VcL/3zD5AazSFomx4paeanihz";
System.out.println("ciphertext: " + ciphertext);
String decryptedtext = decrypt(ciphertext, Base64.getEncoder().encodeToString(key));
System.out.println("decryptedtext: " + decryptedtext);
}
public static String decrypt (String encryptedDataBase64, String keyBase64)
{
try {
Security.setProperty("crypto.policy", "unlimited");
IvParameterSpec ivSpec = new IvParameterSpec(initVector); // Get the init vector
// Get the Base64-encoded key
byte[] key = Base64.getDecoder().decode(keyBase64.getBytes("UTF-8"));
//byte[] key = Base64.decodeBase64(keyBase64.getBytes("UTF-8"));
Cipher cipher = Cipher.getInstance("AES/CBC/PKCS5PADDING"); // AES / CBC / PKCS5 padding
SecretKeySpec skeySpec = new SecretKeySpec(key, "AES");
cipher.init(Cipher.DECRYPT_MODE, skeySpec, ivSpec);
byte[] encryptedData = Base64.getDecoder().decode(encryptedDataBase64.getBytes("UTF-8"));
//byte[] encryptedData = Base64.decodeBase64(encryptedDataBase64.getBytes("UTF-8"));
byte[] decryptedData = cipher.doFinal(encryptedData);
return new String(decryptedData);
}
catch (Exception e) {
System.out.println("AES256 Decrypt: Decryption exception: "+ e.getMessage());
//logger.error("AES256 Decrypt: Decryption exception: "+ e.getMessage());
return null;
}
}
}
Java脚本代码:
let keyBase64 = 'MTIzNDU2Nzg5MDEyMzQ1Njc4OTAxMjM0NTY3ODkwMTI=';
let ivBase64 = 'AAAAAAAAAAAAAAAAAAAAAA==';
let iv = CryptoJS.enc.Base64.parse(ivBase64);
let secret_key = CryptoJS.enc.Base64.parse(keyBase64);
ciphertext = 'rjygE0TjIqiQ4ETnpszoieRWzaSD+9oINf1c748VcL/3zD5AazSFomx4paeanihz';
console.log('ciphertext: ', ciphertext);
var decrypted = CryptoJS.AES.decrypt(ciphertext, secret_key,{
iv: iv,
padding: CryptoJS.pad.Pkcs7,
mode: CryptoJS.mode.CBC,
});
console.log('decrypted (str): ', CryptoJS.enc.Utf8.stringify(decrypted).toString());
我们使用下面的 java 代码来解密使用 CBC 模式和 PKCS7 填充的 AES-256 加密的数据。
Java代码:
import javax.crypto.spec.SecretKeySpec;
import javax.crypto.Cipher;
import javax.crypto.spec.IvParameterSpec;
import java.security.*;
import org.apache.commons.codec.binary.Base64;
import org.apache.commons.lang.StringUtils;
import org.apache.log4j.Logger;
public class AES256 {
private static byte[] initVector = { 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 };
public String decrypt (String encryptedDataBase64, String keyBase64)
{
try {
Security.setProperty("crypto.policy", "unlimited");
IvParameterSpec ivSpec = new IvParameterSpec(initVector); // Get the init vector
// Get the Base64-encoded key
byte[] key = Base64.decodeBase64(keyBase64.getBytes("UTF-8"));
Cipher cipher = Cipher.getInstance("AES/CBC/PKCS5PADDING"); // AES / CBC / PKCS5 padding
SecretKeySpec skeySpec = new SecretKeySpec(key, "AES");
cipher.init(Cipher.DECRYPT_MODE, skeySpec, ivSpec);
byte[] encryptedData = Base64.decodeBase64(encryptedDataBase64.getBytes("UTF-8"));
byte[] decryptedData = cipher.doFinal(encryptedData);
return new String(decryptedData);
}
catch (Exception e) {
logger.error("AES256 Decrypt: Decryption exception: "+ e.getMessage());
return null;
}
}
}
现在我们需要将此解密逻辑转换为 Java 脚本,因为我们的应用程序在 headers 中发送加密数据,同时从服务器请求 index.html。我尝试使用 Crypto.js 进行解密,但我得到的解码字符串为空。下面是我尝试使用的Java脚本代码。
var key = CryptoJS.enc.Base64.parse(keyBase64);
var data = CryptoJS.enc.Base64.parse(encryptedDataBase64);
var dec_data = CryptoJS.AES.decrypt(data, key);
dec_data 空空如也。
我在阅读了一些堆栈溢出问题后尝试了这个。任何人都可以让我知道代码中是否有任何错误。
很遗憾,您没有提供任何示例数据来测试您的环境,因此我设置了自己的值。使用此值:
ciphertext = rjygE0TjIqiQ4ETnpszoieRWzaSD+9oINf1c748VcL/3zD5AazSFomx4paeanihz
keyBase64 = MTIzNDU2Nzg5MDEyMzQ1Njc4OTAxMjM0NTY3ODkwMTI=
ivBase = AAAAAAAAAAAAAAAAAAAAAA==
我在 Java 收到这个 decrypt-result:
The quick brown fox jumps over the lazy dog
在 Javascript-side 我收到与我的代码相同的结果:
decrypted (str): The quick brown fox jumps over the lazy dog
可以在此处进行实时测试 - Java: https://paiza.io/projects/e/dHG73CRgJojOOfLxtvJjtg and Javascript: https://playcode.io/672463.
请注意,我的代码没有异常处理,仅用于教育目的。此外 - 您的代码是不安全的,因为它使用静态(固定)密钥和初始化向量。
Java 代码(由于我使用的是 Java 11 内置的 Base64 编码器而略有更改):
import javax.crypto.Cipher;
import javax.crypto.spec.IvParameterSpec;
import javax.crypto.spec.SecretKeySpec;
import java.io.UnsupportedEncodingException;
import java.security.Security;
import java.util.Base64;
public class Main {
private static byte[] initVector = { 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 };
public static void main(String[] args) throws UnsupportedEncodingException {
System.out.println("How to decode data encrypted using AES-256 in CBC mode and PKCS7 padding");
String plaintext = "The quick brown fox jumps over the lazy dog";
byte[] key = "12345678901234567890123456789012".getBytes("UTF-8");
String keyBase64 = Base64.getEncoder().encodeToString(key);
System.out.println("keyBase64: " + keyBase64);
String ivBase64 = Base64.getEncoder().encodeToString(initVector);
System.out.println("initVector: " + ivBase64);
String ciphertext = "rjygE0TjIqiQ4ETnpszoieRWzaSD+9oINf1c748VcL/3zD5AazSFomx4paeanihz";
System.out.println("ciphertext: " + ciphertext);
String decryptedtext = decrypt(ciphertext, Base64.getEncoder().encodeToString(key));
System.out.println("decryptedtext: " + decryptedtext);
}
public static String decrypt (String encryptedDataBase64, String keyBase64)
{
try {
Security.setProperty("crypto.policy", "unlimited");
IvParameterSpec ivSpec = new IvParameterSpec(initVector); // Get the init vector
// Get the Base64-encoded key
byte[] key = Base64.getDecoder().decode(keyBase64.getBytes("UTF-8"));
//byte[] key = Base64.decodeBase64(keyBase64.getBytes("UTF-8"));
Cipher cipher = Cipher.getInstance("AES/CBC/PKCS5PADDING"); // AES / CBC / PKCS5 padding
SecretKeySpec skeySpec = new SecretKeySpec(key, "AES");
cipher.init(Cipher.DECRYPT_MODE, skeySpec, ivSpec);
byte[] encryptedData = Base64.getDecoder().decode(encryptedDataBase64.getBytes("UTF-8"));
//byte[] encryptedData = Base64.decodeBase64(encryptedDataBase64.getBytes("UTF-8"));
byte[] decryptedData = cipher.doFinal(encryptedData);
return new String(decryptedData);
}
catch (Exception e) {
System.out.println("AES256 Decrypt: Decryption exception: "+ e.getMessage());
//logger.error("AES256 Decrypt: Decryption exception: "+ e.getMessage());
return null;
}
}
}
Java脚本代码:
let keyBase64 = 'MTIzNDU2Nzg5MDEyMzQ1Njc4OTAxMjM0NTY3ODkwMTI=';
let ivBase64 = 'AAAAAAAAAAAAAAAAAAAAAA==';
let iv = CryptoJS.enc.Base64.parse(ivBase64);
let secret_key = CryptoJS.enc.Base64.parse(keyBase64);
ciphertext = 'rjygE0TjIqiQ4ETnpszoieRWzaSD+9oINf1c748VcL/3zD5AazSFomx4paeanihz';
console.log('ciphertext: ', ciphertext);
var decrypted = CryptoJS.AES.decrypt(ciphertext, secret_key,{
iv: iv,
padding: CryptoJS.pad.Pkcs7,
mode: CryptoJS.mode.CBC,
});
console.log('decrypted (str): ', CryptoJS.enc.Utf8.stringify(decrypted).toString());