openssl decrypt java,AES encrypt with openssl decrypt using java

I have to encrypt an xml file using openssl command line or a C api. The output shall be Base64.

A java programm will be used for decrypting. This programm is provided by the customer and cannot be changed (they are using this code for legacy applications). As you can see in the code below the customer provides a passphrase so the key will be generated using the SecretKeySpec method.

Java code:

// 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;

}

I have tested several commands like:

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

But non of the given outputs is successfuly decrypted using java. For testing purposes I used the given java code for encrypting and the result is of course different than the one from openssl.

Is there a way to use openssl C api or command line to encrypt data so it could be successful decrypted using the given java code?

# Answer 1

4d350fd91e33782268f371d7edaa8a76.png

Java's SecretKeySpec uses the password ASCII bytes directly as key bytes, while OpenSSL's -pass pass:... method derives a key from the password using a key derivation function to transform the password into a key in a secure fashion. You can either try to do the same key derivation in Java (which you probably cannot if I interpret your question correctly), or use OpenSSL's -K option to pass in a key (as hex bytes!) instead of a password.

You can find out how there.

# Answer 2

To add a bit. I was struggling with the same problem. I was able to decrypt from Java an AES-128 encrypted message using the following settings.

I used openssl to encrypt the data:

openssl enc -nosalt -aes-128-ecb -in data.txt -out crypted-aes.data -K 50645367566B59703373367639792442

As @Daniel suggested, the game changer is to use the -K property. The Java configuration that let us to decrypt the generated file is the following:

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);

The magic happens when the hex key 50645367566B59703373367639792442, its String representation "PdSgVkYp3s6v9y$B", and the AES/ECB/PKCS5Padding align together.

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值