Gus*_*des 9 java encryption base64 openssl aes
我必须使用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;
}
Run Code Online (Sandbox Code Playgroud)
我测试了几个命令,如:
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
Run Code Online (Sandbox Code Playgroud)
但是使用java成功解密了给定输出的非.出于测试目的,我使用给定的java代码进行加密,结果当然不同于openssl中的结果.
有没有办法使用openssl C api或命令行来加密数据,以便使用给定的Java代码成功解密?
| 归档时间: |
|
| 查看次数: |
7272 次 |
| 最近记录: |