【问题标题】:Encrypt file with AES-128 in PHP and decrypt it in Android在 PHP 中使用 AES-128 加密文件并在 Android 中解密
【发布时间】:2015-10-16 22:18:16
【问题描述】:

我需要在 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();

获取iv函数

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 android encryption aes


【解决方案1】:

模式和填充不匹配。您在 PHP 中使用 AES/CBC/ZeroPadding(Java 表示法),但在 Java 中您使用的是Cipher.getInstance("AES"),它(可能)默认为Cipher.getInstance("AES/ECB/PKCS5Padding")。始终使用完全限定的密码描述:

Cipher cipher = Cipher.getInstance("AES/CBC/ZeroPadding", "BC");

(这并不能解决问题。)

您没有使用相同的 IV。字符 '1' 和字节 1 不是一回事,因为 '1' 实际上是字节 49。

byte[] iv = { 49, 50, 51, 52, 53, 54, 55, 56, 57, 48, 49, 50, 51, 52, 53, 54 };

由于 BouncyCastles/SpongyCastles 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(加密然后 MAC)来验证密文。在尝试解密之前,您需要检查接收方的 MAC,看看它是否在途中被操纵。

【讨论】:

  • 我已根据您的评论更改了我的 android 代码,但仍然无法正常工作。
  • 有错误吗?如果有文件,生成的文件与预期的文件有何不同?长度一样吗?第一个、最后一个或所有字节是否不同?
  • 我不能使用“ZeroPadding”它会抛出异常。
  • 我的加密文件是“encry_file.mp4”,它存储在设备内存中用于测试,我需要从中创建一个解密文件“decrypted.mp4”
  • 您可能需要指定“NoPadding”而不是“ZeroPadding”并自己删除尾随的 0x00 字节,但只包含 SpongyCastle 应该更容易。
猜你喜欢
  • 2011-02-18
  • 1970-01-01
  • 2013-10-12
  • 2020-12-03
  • 1970-01-01
  • 1970-01-01
  • 2016-09-22
  • 1970-01-01
  • 2021-12-20
相关资源
最近更新 更多