使用OpenSSL进行AES加密,使用Java进行解密

13

我需要使用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代码成功解密?

2个回答

10
Java中的SecretKeySpec直接使用密码ASCII字节作为密钥字节,而OpenSSL的-pass pass:...方法使用密钥派生函数从密码中派生出一个安全的密钥。您可以尝试在Java中执行相同的密钥派生(如果我正确理解了您的问题,则可能无法实现),或者使用OpenSSL的-K选项传递密钥(以十六进制字节形式!)而不是密码。
您可以在这里找到更多信息。

2

补充一下。我曾经也遇到过同样的问题。我成功地使用以下设置从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对齐时,魔法就发生了。


网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接