簡體   English   中英

AES加密使用openssl解密使用java

[英]AES encrypt with openssl decrypt using java

我必須使用 openssl 命令行或 C api 加密一個 xml 文件。 輸出應為 Base64。

java程序將用於解密。 此程序由客戶提供且無法更改(他們將此代碼用於遺留應用程序)。 正如您在下面的代碼中看到的那樣,客戶提供了一個密碼短語,因此將使用 SecretKeySpec 方法生成密鑰。

爪哇代碼:

// 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代碼成功解密?

Java的SecretKeySpec使用密碼ASCII直接字節密鑰字節,而OpenSSL的-pass pass:...方法得出使用從密碼鍵密鑰導出函數,以密碼轉變成以安全的方式的關鍵。 您可以嘗試在 Java 中進行相同的密鑰派生(如果我正確解釋您的問題,您可能無法這樣做),或者使用 OpenSSL 的-K選項傳遞密鑰(作為十六進制字節!)而不是密碼。

你可以在那里找到方法。

要補充一點。 我正在努力解決同樣的問題。 我能夠使用以下設置從 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對齊時,魔法就會發生。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM