我需要在PHP中使用AES-128加密文件,并在Android中解密。
我使用下面的代码。我已经成功地加密和解密它使用PHP代码,但我需要解密它使用Android从我的应用程序。
PHP代码:$key= "asdfghjklzxccvbn";
$in_filename = $_FILES["fileToUpload"]["tmp_name"];
$aes_filename =$target_dir."encry_".$_FILES["fileToUpload"]["name"];
$decry_filename =$target_dir."decry_".$_FILES["fileToUpload"]["name"];
//encrypt file
$iv_size = mcrypt_get_iv_size(MCRYPT_RIJNDAEL_128, MCRYPT_MODE_CBC);
$iv = '1234567890123456';
$fin = fopen($in_filename, "rb");
$fcrypt = fopen($aes_filename, 'wb');
fwrite($fcrypt, $iv);
$opts = array('iv'=>$iv, 'key'=>$key, 'mode'=>'cbc');
stream_filter_append($fcrypt, 'mcrypt.rijndael-128', STREAM_FILTER_WRITE, $opts);
while (!feof($fin))
{
fwrite($fcrypt, fread($fin, 8192));
}
fclose($fcrypt);
fclose($fin);
我的Android代码解密加密文件:
// encripted file stored in android device for decrypt
String uri= Environment.getExternalStorageDirectory().toString();
uri=uri+"/encry_file.mp4";
File file = new File(uri.toString());
FileInputStream fis = new FileInputStream(file);
spec =getIV();
FileOutputStream fos = new FileOutputStream(Environment.getExternalStorageDirectory().toString() + "/decrypted.mp4");
SecretKeySpec sks = new SecretKeySpec("asdfghjklzxccvbn".getBytes(),
"AES");
Cipher cipher = Cipher.getInstance("AES");
cipher.init(Cipher.DECRYPT_MODE, sks, spec);
CipherInputStream cis = new CipherInputStream(fis, cipher);
int b;
byte[] d = new byte[8192];
while ((b = cis.read(d)) != -1) {
fos.write(d, 0, b);
}
fos.flush();
fos.close();
cis.close();
get iv function
public AlgorithmParameterSpec getIV() {
byte[] iv = { 1,2,3,4,5,6,7,8,9,0,1,2,3,4,5,6 };
IvParameterSpec ivParameterSpec;
ivParameterSpec = new IvParameterSpec(iv);
return ivParameterSpec;
}
android代码将生成一个文件,但它是不可读的。请检查我的代码是做正确的代码或如果它包含任何问题。请帮我解决一下
模式和填充不匹配。在PHP中使用AES/CBC/ZeroPadding (Java符号),但在Java中使用Cipher.getInstance("AES")
(可能)默认为Cipher.getInstance("AES/ECB/PKCS5Padding")
。始终使用完全限定的密码描述:
Cipher cipher = Cipher.getInstance("AES/CBC/ZeroPadding", "BC");
(That doesn't solution the problem)
你没有使用相同的IV.字符'1'和字节1不是一回事,因为'1'实际上是字节49。
byte[] iv = { 49, 50, 51, 52, 53, 54, 55, 56, 57, 48, 49, 50, 51, 52, 53, 54 };
由于BouncyCastles/sponycastles ZeroPadding
与mcrypt的零填充不完全相同,您应该使用Cipher cipher = Cipher.getInstance("AES/CBC/NoPadding");
来解析解密的最后16个字节并删除尾随的0x00字节。
在你的情况下,这是一种方法:
int b;
byte[] d = new byte[8192];
byte[] p = null;
int holdOff;
while ((b = cis.read(d)) != -1) {
holdOff = Math.max(b - cipher.getBlockSize(), 0);
if (p != null) {
fos.write(p, 0, p.length);
Arrays.fill(p, 0);
}
if (p == null) {
p = new byte[cipher.getBlockSize()];
}
System.arraycopy(d, holdOff, p, 0, p.length);
fos.write(d, 0, holdOff);
}
// here p contains the end of the plaintext followed by padding bytes
// remove padding:
int i = cipher.getBlockSize() - 1;
while(i >= 0 && p[i] == 0) {
i--;
}
// write remaining bytes
fos.write(Arrays.copyOf(p, i+1), 0, i+1);
fos.flush();
fos.close();
cis.close();
这个想法是,您推迟将最后16个字节写入文件并单独处理它们,因为解密文件的最后16个字节可能包含需要删除的0x00个字节。
其他注意事项:
每次加密总是随机生成IV。它不必是秘密的,但它必须是不可预测的。您可以将它与密文一起发送,例如将它放在密文的前面。
通过运行HMAC来验证您的密文(encrypt-then-MAC)。在尝试解密之前,您需要检查接收方的MAC,看看它是否在途中被操纵。