有 Java 编程相关的问题?

你可以在下面搜索框中键入要查询的问题!

加密AES使用java使用openssl加密解密

我必须使用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) 个答案

  1. # 1 楼答案

    Java的SecretKeySpec将密码ASCII字节直接用作密钥字节,而OpenSSL的-pass pass:...方法使用key derivation function从密码派生出一个密钥,以安全的方式将密码转换为密钥。您可以尝试在Java中执行相同的密钥派生(如果我正确解释了您的问题,您可能无法执行),或者使用OpenSSL的-K选项传入密钥(作为十六进制字节!)而不是密码

    您可以了解如何there

  2. # 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对齐在一起时,魔术就发生了