银光+ PHP-AES 加密
Silverlight 和 PHP 之间的加密协议存在问题。
我尝试在 Silverlight 中加密文本值,并借助 AES 算法在 PHP 中解密。 这个例子是我如何在 Silverlight 中加密它:
<!-- language: lang-cs -->
SaveFileDialog dlg = new SaveFileDialog();
dlg.Filter = "Crypt file (CRP)|*.crp";
bool res = dlg.ShowDialog().Value;
if (res == true)
{
Stream outStream = dlg.OpenFile();
AesManaged cryptAlg = new AesManaged();
ICryptoTransform encryptor = cryptAlg.CreateEncryptor(_encryptKey, _encryptIv);
CryptoStream cryptStream = new CryptoStream(outStream, encryptor, CryptoStreamMode.Write);
byte[] buffer = Encoding.UTF8.GetBytes(textBox1.Text);
int size = buffer.Length;
cryptStream.Write(buffer, 0, size);
cryptStream.Close();
outStream.Close();
}
这个例子是我如何在 PHP 中解密它:
<!-- language: lang-php -->
function fb_get_contents($filename) {
$fp = fopen($filename, "rb");
$cc = fgets($fp);
fclose($fp);
return $cc;
}
$value = fb_get_contents($_FILES['file']['tmp_name']);
$module = mcrypt_module_open(MCRYPT_RIJNDAEL_128, '', MCRYPT_MODE_CBC, '');
$blockSize = mcrypt_get_block_size(MCRYPT_RIJNDAEL_128, MCRYPT_MODE_CBC);
$key = substr($key, 0, 16);
$iv = $key;
$rc = mcrypt_generic_init($module, $key, $iv);
$value = base64_decode($value);
$value = mdecrypt_generic($module, $value);
//apply pkcs7 padding removal
$packing = ord($value[strlen($value) - 1]);
if($packing && $packing < $blockSize){
for($P = strlen($value) - 1; $P >= strlen($value) - $packing; $P--){
if(ord($value{$P}) != $packing){
$packing = 0;
}//end if
}//end for
}//end if
$value = substr($value, 0, strlen($value) - $packing);
它不起作用。我认为这可能是 AES 算法的填充参数的问题。谢谢。
Problem with cryptography protocol between Silverlight and PHP.
I tried to encrypt text value in Silverlight and decrypt it in PHP with help of AES algorithm.
This example how I encrypt it in Silverlight:
<!-- language: lang-cs -->
SaveFileDialog dlg = new SaveFileDialog();
dlg.Filter = "Crypt file (CRP)|*.crp";
bool res = dlg.ShowDialog().Value;
if (res == true)
{
Stream outStream = dlg.OpenFile();
AesManaged cryptAlg = new AesManaged();
ICryptoTransform encryptor = cryptAlg.CreateEncryptor(_encryptKey, _encryptIv);
CryptoStream cryptStream = new CryptoStream(outStream, encryptor, CryptoStreamMode.Write);
byte[] buffer = Encoding.UTF8.GetBytes(textBox1.Text);
int size = buffer.Length;
cryptStream.Write(buffer, 0, size);
cryptStream.Close();
outStream.Close();
}
This example how I decrypt it in PHP:
<!-- language: lang-php -->
function fb_get_contents($filename) {
$fp = fopen($filename, "rb");
$cc = fgets($fp);
fclose($fp);
return $cc;
}
$value = fb_get_contents($_FILES['file']['tmp_name']);
$module = mcrypt_module_open(MCRYPT_RIJNDAEL_128, '', MCRYPT_MODE_CBC, '');
$blockSize = mcrypt_get_block_size(MCRYPT_RIJNDAEL_128, MCRYPT_MODE_CBC);
$key = substr($key, 0, 16);
$iv = $key;
$rc = mcrypt_generic_init($module, $key, $iv);
$value = base64_decode($value);
$value = mdecrypt_generic($module, $value);
//apply pkcs7 padding removal
$packing = ord($value[strlen($value) - 1]);
if($packing && $packing < $blockSize){
for($P = strlen($value) - 1; $P >= strlen($value) - $packing; $P--){
if(ord($value{$P}) != $packing){
$packing = 0;
}//end if
}//end for
}//end if
$value = substr($value, 0, strlen($value) - $packing);
And it doesn't work. I think this is could be a problem with padding parameter of AES algorithm. Thanks.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
如果没有其余的 c# 代码以及您所说的“它不起作用”的意思,我无法确定,但我想说这是您的问题。
$iv = $key;
您想从 cryptAlg.IV 中提取 iv 形式
此外,我认为 AesManaged 的默认编码不是 PKCS7,2)您有时需要在缓冲区上调用刷新以写入最后一个块。
Absent the rest of the c# code and what you mean by "its not working", I can't be sure, but I'd say this is your problem.
$iv = $key;
You want to pull the iv form cryptAlg.IV
Moreover, I don't think the default encoding is PKCS7 for AesManaged and 2) you sometimes need to call flush on the buffer to get the last block written.