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 コードを使用して正常に復号化する方法はありますか?