如何解决Openssl等效命令来解密Java加密的base64文件aes
通过引用this无法在Java中实现文件加密。 并加密后使用base64对文件进行编码(注意:我不想使用其他库,例如:Base64InputStream)
原始文件的内容为“ hello” .getBytes(UTF_8);
使用以下命令无法解密(不包含base64数据)
openssl enc -nosalt -aes-256-cbc -d -in file.crypt -out file.txt -k abcdefghijklmop -md sha1
但是我无法向openssl提供base64编码的文件,请尝试以下命令:
openssl enc -nosalt -aes-256-cbc -d -base64 -in file.base64 -out file.txt -k abcdefghijklmop -md sha1
不良解密
base64 file.base64 | openssl enc -d -a -aes-256-cbc > decrypted -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,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,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();}}}
请在正确的openssl命令上建议解密java(aes加密-> base64编码)文件。
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。