PHP AES加密PKCS5Padding [英] PHP AES encryption PKCS5Padding
问题描述
我正在编写代码,我正在加密PHP中的图像文件,然后用Java(Android)解密它们。当加密/解密PNG文件时,一切都可以正常工作,但是当我尝试使用JPG时,Java解密引发异常:
WARN / System.err(345):java.io.IOException:data not block size aligned
基于在线搜索,似乎这是因为我没有正确填充。
如何正确执行?
以下是加密的PHP代码:
<?php
$ secret_key =01234567890abcde;
$ iv =fedcba9876543210;
$ infile =5.png;
$ outfile =5_encrypted.png;
$ crypttext = file_get_contents($ infile);
$ plaintext = mcrypt_encrypt(MCRYPT_RIJNDAEL_128,$ secret_key,$ crypttext,MCRYPT_MODE_CBC,$ iv);
header('Content-Type:application / octet-stream');
header('Content-Length:'。strlen($ plaintext));
header('Content-Disposition:attachment; filename ='。($ outfile));
echo $ plaintext;
// file_put_contents($ outfile,$ plaintext);
//将文件保存在服务器
的文件夹中
PKCS5Padding的以下示例来自 mcrypt docs 。
<?php
函数encrypt_something($ input)
{
$ size = mcrypt_get_block_size('des','ecb');
$ input = pkcs5_pad($ input,$ size);
$ key ='你的秘密钥匙在这里';
$ td = mcrypt_module_open('des','','ecb','');
$ iv = mcrypt_create_iv(mcrypt_enc_get_iv_size($ td),MCRYPT_RAND);
mcrypt_generic_init($ td,$ key,$ iv);
$ data = mcrypt_generic($ td,$ input);
mcrypt_generic_deinit($ td);
mcrypt_module_close($ td);
$ data = base64_encode($ data);
返回$数据;
}
函数pkcs5_pad($ text,$ blocksize)
{
$ pad = $ blocksize - (strlen($ text)%$ blocksize);
返回$文本。 str_repeat(chr($ pad),$ pad);
}
函数pkcs5_unpad($ text)
{
$ pad = ord($ text {strlen($ text)-1});
if($ pad> strlen($ text))return false;
if(strspn($ text,chr($ pad),strlen($ text) - $ pad)!= $ pad)return false;
return substr($ text,0,-1 * $ pad);
}
I'm not a PHP coder so I need a little help with PHP AES encryption.
I'm writing code where I am encrypting image files in PHP and then decrypting them in Java (Android). Everything works fine when I'm encrypting/decrypting PNG files, but when I try to do the same with JPG, Java decrypting throws an exception:
WARN/System.err(345): java.io.IOException: data not block size aligned
Based on searching online, it seems that this is because I am not doing padding properly.
How can I do this properly?
Here is the PHP code for encryption:
<?php
$secret_key = "01234567890abcde";
$iv = "fedcba9876543210";
$infile = "5.png";
$outfile = "5_encrypted.png";
$crypttext = file_get_contents($infile);
$plaintext = mcrypt_encrypt(MCRYPT_RIJNDAEL_128, $secret_key, $crypttext, MCRYPT_MODE_CBC, $iv);
header('Content-Type: application/octet-stream');
header('Content-Length: ' . strlen($plaintext));
header('Content-Disposition: attachment; filename=' . ($outfile));
echo $plaintext;
//file_put_contents($outfile,$plaintext);
//save the file in the folder of server
The following example for PKCS5Padding comes from the comments to the mcrypt docs.
<?php
function encrypt_something($input)
{
$size = mcrypt_get_block_size('des', 'ecb');
$input = pkcs5_pad($input, $size);
$key = 'YOUR SECRET KEY HERE';
$td = mcrypt_module_open('des', '', 'ecb', '');
$iv = mcrypt_create_iv (mcrypt_enc_get_iv_size($td), MCRYPT_RAND);
mcrypt_generic_init($td, $key, $iv);
$data = mcrypt_generic($td, $input);
mcrypt_generic_deinit($td);
mcrypt_module_close($td);
$data = base64_encode($data);
return $data;
}
function pkcs5_pad ($text, $blocksize)
{
$pad = $blocksize - (strlen($text) % $blocksize);
return $text . str_repeat(chr($pad), $pad);
}
function pkcs5_unpad($text)
{
$pad = ord($text{strlen($text)-1});
if ($pad > strlen($text)) return false;
if (strspn($text, chr($pad), strlen($text) - $pad) != $pad) return false;
return substr($text, 0, -1 * $pad);
}
这篇关于PHP AES加密PKCS5Padding的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!