【发布时间】:2019-08-16 19:39:59
【问题描述】:
我可以使用下面提到的 Java 代码加密文件中的数据。但是当我尝试从命令行使用 OpenSSL 解密加密文件时,我无法做到这一点。
我试过命令
openssl enc -aes-256-cbc -d -in file_05.encrypt.txt -out file_05.decrypt.txt
它要求输入密码 - 输入aes-256-cbc解密密码: 我输入的密码是“helloworld”,
然后在终端中显示“bad magic number”错误消息。
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 from command line ..." - 我不相信 OpenSSL 支持
PBEWithSHA1And256BitAES。另见Bouncy Castle's Password Based Encryption With AES in CBC mode 和What Java encryption algorithms should I use? -
一些澄清,但仍不清楚是否有具体问题。请提供完整的错误信息。
标签: java android linux encryption command-line