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算法的填充参数的问题。感谢。
答案 0 :(得分:0)
缺少其余的c#代码以及“无法正常工作”的含义,我无法确定,但我会说这是你的问题。
$ iv = $ key;
您想要提取iv表格cryptAlg.IV
此外,我不认为默认编码是AesManaged的PKCS7和2)你有时需要在缓冲区上调用flush来获取最后一个块。