使用Java代码进行AES加密,使用OpenSSL进行解密(终端)
我能够使用下面提到的Java代码加密文件中的数据。但是,当我试图从命令行使用OpenSSL解密加密文件时,我无法做到这一点 我试过这个命令使用Java代码进行AES加密,使用OpenSSL进行解密(终端),java,android,linux,encryption,command-line,Java,Android,Linux,Encryption,Command Line,我能够使用下面提到的Java代码加密文件中的数据。但是,当我试图从命令行使用OpenSSL解密加密文件时,我无法做到这一点 我试过这个命令 openssl enc -aes-256-cbc -d -in file_05.encrypt.txt -out file_05.decrypt.txt 它要求输入密码- 输入aes-256-cbc解密密码: 我输入的密码是helloworld 然后在终端中显示一条错误的幻数错误消息 String pwd = "helloworld"; String S
openssl enc -aes-256-cbc -d -in file_05.encrypt.txt -out file_05.decrypt.txt
它要求输入密码-
输入aes-256-cbc解密密码:
我输入的密码是helloworld
然后在终端中显示一条错误的幻数错误消息
String pwd = "helloworld";
String SALT_VALUE = "12345@salt";
private String algorithm = "PBEWITHSHA256AND256BITAES-CBC-BC";
String finalTxt = "Hi, Goof After noon All.";
char[] pwd = Crypt.password.toCharArray();
SecretKey originalKey = Crypt.generateSK(pwd);
byte[] cipherText = Crypt.encrypt(finalTxt.getBytes(),SALT_VALUE.getBytes(), originalKey);
public static SecretKey generateSK(char[] passPhrase) throws NoSuchAlgorithmException,
InvalidKeySpecException,
NoSuchPaddingException,
InvalidAlgorithmParameterException,
InvalidKeyException {
PBEKeySpec pbeKeySpec = new PBEKeySpec(passPhrase);
SecretKeyFactory secretKeyFactory;
secretKeyFactory = SecretKeyFactory.getInstance(algorithm);
return secretKeyFactory.generateSecret(pbeKeySpec);
}
public static byte[] encrypt(byte[] image, byte[] salt, SecretKey sKey) throws InvalidKeyException,
IllegalBlockSizeException,
BadPaddingException,
InvalidKeySpecException,
UnsupportedEncodingException,
InvalidAlgorithmParameterException {
Cipher cipher;
try {
cipher = getCipher(Cipher.ENCRYPT_MODE, salt, sKey);
return cipher.doFinal(image);
} catch (Exception e) {
e.printStackTrace();
}
return null;
}
private static Cipher getCipher(int mode, @NonNull byte[] salt, @NonNull SecretKey secretKey) throws Exception {
PBEParameterSpec pbeParamSpecKey = new PBEParameterSpec(salt, 1000);
Cipher cipher = Cipher.getInstance(algorithm);
cipher.init(mode, secretKey, pbeParamSpecKey);
return cipher;
}
看起来您缺少openssl所期望的头-字符串Salted_uuu,后跟8字节salt,后跟密文 它要求输入密码-输入aes-256-cbc解密密码:我输入的密码是helloworld, 然后在终端中显示一条错误的幻数错误消息 Openssl默认使用其内部函数从提供的密码和salt生成密钥和IV。如果需要,只需在internet上搜索Java实现 默认情况下,如果您不直接提供密钥和IV,则Openssl期望使用Salted_uuuu格式 我试图从命令行使用OpenSSL解密加密文件,但我无法做到这一点
我不确定您的加密类实现了什么,您可以尝试打印十六进制编码的密钥和iv。使用带有参数-iv-K的openssl,您可以直接提供iv和密钥值来解密密文您的问题是什么?从命令行打开SSL…-我不相信OpenSSL支持PBewithsha1和256biates。也可以查看和一些澄清,但仍不清楚是否有具体问题。请提供完整的错误信息。他丢失了更多信息,密钥和iv的计算方式不同