我必须使用openssl命令行或C api来加密xml文件。输出应为Base64。
将使用java程序员进行解密。此程序由客户提供,不能更改(他们将此代码用于遗留应用程序)。正如您在下面的代码中所看到的,客户提供了一个密码短语,因此将使用SecretKeySpec方法生成密钥。
Java代码:
// Passphrase
private static final byte[] pass = new byte[] { '0', '1', '2', '3', '4', '5', '6', '7', '8', '9', '0','1', '2', '3', '4', '5' };
public static String encrypt(String Data) throws Exception {
Key key = generateKey();
Cipher c = Cipher.getInstance("AES/ECB/PKCS5Padding");
c.init(Cipher.ENCRYPT_MODE, key);
byte[] encVal = c.doFinal(Data.getBytes());
String encryptedValue = new BASE64Encoder().encode(encVal);
return encryptedValue;
}
public static String decrypt(String encryptedData) throws Exception {
Key key = generateKey();
Cipher c = Cipher.getInstance("AES/ECB/PKCS5Padding");
c.init(Cipher.DECRYPT_MODE, key);
byte[] decordedValue = new BASE64Decoder().decodeBuffer(encryptedData);
byte[] decValue = c.doFinal(decordedValue);
String decryptedValue = new String(decValue);
return decryptedValue;
}
private static Key generateKey() throws Exception {
Key key = new SecretKeySpec(pass, "AES");
return key;
}
我测试了几个命令,例如:
openssl enc -aes-128-ecb -a -salt -in file.xml -out file_enc.xml -pass pass:123456789012345
openssl enc -aes-128-ecb -a -nosalt -in file.xml -out file_enc.xml -pass pass:123456789012345
但是没有使用java成功地解密给定的输出。出于测试目的,我使用给定的java代码进行加密,结果当然与openssl的结果不同。
有没有一种方法可以使用openssl C api或命令行来加密数据,以便使用给定的java代码成功解密数据?
发布于 2013-01-24 02:54:12
Java的SecretKeySpec
直接使用密码ASCII字节作为密钥字节,而OpenSSL的-pass pass:...
方法使用key derivation function从密码派生密钥,从而以安全的方式将密码转换为密钥。您可以尝试在Java语言中进行相同的密钥派生(如果我正确解释了您的问题,则可能无法实现),或者使用OpenSSL的-K
选项传递密钥(以十六进制字节形式!)而不是密码。
你可以了解到there是如何。
发布于 2021-01-29 21:12:16
再加一点。我也在纠结于同样的问题。我能够使用以下设置从Java解密AES-128加密消息。
我使用openssl
对数据进行加密:
openssl enc -nosalt -aes-128-ecb -in data.txt -out crypted-aes.data -K 50645367566B59703373367639792442
正如@Daniel建议的那样,改变游戏规则的方法是使用-K
属性。让我们解密生成的文件的Java配置如下:
final byte[] aesKey = "PdSgVkYp3s6v9y$B".getBytes(StandardCharsets.UTF_8);
final SecretKeySpec aesKeySpec = new SecretKeySpec(aesKey, "AES");
Path path = Paths.get("src/test/resources/crypted-aes.data");
final byte[] cryptedData = Files.readAllBytes(path);
final Cipher cipher = Cipher.getInstance("AES/ECB/PKCS5Padding");
cipher.init(Cipher.DECRYPT_MODE, aesKeySpec);
final byte[] decryptedMsg = cipher.doFinal(cryptedData);
当十六进制键50645367566B59703373367639792442
、它的String
表示"PdSgVkYp3s6v9y$B"
和AES/ECB/PKCS5Padding
对齐时,魔术就会发生。
https://stackoverflow.com/questions/14486814
复制相似问题