用于解密java加密base64文件aes的Openssl等效命令
通过参考,我能够用java实现文件加密。 加密后,我用base64对文件进行编码(注意:我不想使用其他库,例如:Base64InputStream) 原始文件的内容为“hello”.getBytes(UTF_8) 使用下面的命令,我可以解密(没有base64数据) 但我无法向openssl提供base64编码文件,尝试了以下命令:用于解密java加密base64文件aes的Openssl等效命令,java,encryption,openssl,command,aes,Java,Encryption,Openssl,Command,Aes,通过参考,我能够用java实现文件加密。 加密后,我用base64对文件进行编码(注意:我不想使用其他库,例如:Base64InputStream) 原始文件的内容为“hello”.getBytes(UTF_8) 使用下面的命令,我可以解密(没有base64数据) 但我无法向openssl提供base64编码文件,尝试了以下命令: openssl enc -nosalt -aes-256-cbc -d -base64 -in file.base64 -out file.txt -k abcdef
openssl enc -nosalt -aes-256-cbc -d -base64 -in file.base64 -out file.txt -k abcdefghijklmop -md sha1
解密错误
坏幻数
加密java代码:
static String password = "abcdefghijklmop";
public static void encryptNew(String path) {
try {
Log.e("test", "encrypt start " + path);
FileInputStream fis = new FileInputStream(path);
FileOutputStream fos = new FileOutputStream(path.concat(".crypt"));
byte[] hash = new byte[0];
byte[] keyAndIv = new byte[0];
for (int i = 0; i < 3 && keyAndIv.length < 48; i++) {
final byte[] hashData = array_concat(hash, password.getBytes(UTF_8));
final MessageDigest md = MessageDigest.getInstance("SHA-1");
hash = md.digest(hashData);
keyAndIv = array_concat(keyAndIv, hash);
}
final byte[] keyValue = Arrays.copyOfRange(keyAndIv, 0, 32);
final byte[] iv = Arrays.copyOfRange(keyAndIv, 32, 48);
final SecretKeySpec secretKeySpec = new SecretKeySpec(keyValue, "AES");
IvParameterSpec ivParameterSpec = new IvParameterSpec(iv);
Cipher cipher = Cipher.getInstance("AES/CBC/PKCS5Padding");
cipher.init(Cipher.ENCRYPT_MODE, secretKeySpec, ivParameterSpec);
CipherOutputStream cos = new CipherOutputStream(fos, cipher);
int b;
byte[] d = new byte[8];
while ((b = fis.read(d)) != -1) {
cos.write(d, 0, b);
}
cos.flush();
cos.close();
fis.close();
} catch (Exception e) {
e.printStackTrace();
}
encodeFile(path.concat(".crypt"));
}
请建议使用正确的openssl命令来解密java(aes加密->base64编码)文件。在您的链接问题中@Topaco告知您他的评论“encryptfile()和openssl(使用-base64选项)提供的结果不兼容。”此加密方法不适用于OpenSSL-由“坏幻数”明确指示。
encodeFile
方法无法可靠工作,因此可能无法使用OpenSSL(或任何其他工具)对结果进行解密。正确的方法是修复encodeFile
方法。还请注意,从不含盐的密码中派生密钥是一个严重的安全风险。
base64 file.base64 | openssl enc -d -a -aes-256-cbc > decrypted -k abcdefghijklmop -md sha1
static String password = "abcdefghijklmop";
public static void encryptNew(String path) {
try {
Log.e("test", "encrypt start " + path);
FileInputStream fis = new FileInputStream(path);
FileOutputStream fos = new FileOutputStream(path.concat(".crypt"));
byte[] hash = new byte[0];
byte[] keyAndIv = new byte[0];
for (int i = 0; i < 3 && keyAndIv.length < 48; i++) {
final byte[] hashData = array_concat(hash, password.getBytes(UTF_8));
final MessageDigest md = MessageDigest.getInstance("SHA-1");
hash = md.digest(hashData);
keyAndIv = array_concat(keyAndIv, hash);
}
final byte[] keyValue = Arrays.copyOfRange(keyAndIv, 0, 32);
final byte[] iv = Arrays.copyOfRange(keyAndIv, 32, 48);
final SecretKeySpec secretKeySpec = new SecretKeySpec(keyValue, "AES");
IvParameterSpec ivParameterSpec = new IvParameterSpec(iv);
Cipher cipher = Cipher.getInstance("AES/CBC/PKCS5Padding");
cipher.init(Cipher.ENCRYPT_MODE, secretKeySpec, ivParameterSpec);
CipherOutputStream cos = new CipherOutputStream(fos, cipher);
int b;
byte[] d = new byte[8];
while ((b = fis.read(d)) != -1) {
cos.write(d, 0, b);
}
cos.flush();
cos.close();
fis.close();
} catch (Exception e) {
e.printStackTrace();
}
encodeFile(path.concat(".crypt"));
}
public void encodeFile(String path) {
FileOutputStream stream = null;
FileReader fr = null;
try {
stream = new FileOutputStream(path.concat(".base64"),true);
fr = new FileReader(path);
} catch (FileNotFoundException e) {
e.printStackTrace();
}
try {
BufferedReader br=new BufferedReader(fr);
String line;
while((line=br.readLine())!=null)
{
String encoded = android.util.Base64.encodeToString(line.getBytes(), android.util.Base64.DEFAULT);
stream.write(encoded.getBytes());
}
} catch (IOException e) { e.printStackTrace(); } finally { try { fr.close(); stream.close(); } catch (IOException e) {e.printStackTrace();}}}