已更新
我已对C#代码进行了更改,因此它使用的块大小为256.但是现在hello世界看起来像http://pastebin.com/5sXhMV11,我无法弄清楚我应该使用rtrim()来获取最后的混乱。
另外当你说IV应该是随机的时,你的意思是不要再使用相同的IV一次或者我编码错误的方式吗?
再次感谢!
您好,
我正在尝试使用在C#中加密的PHP解密字符串。我似乎无法让PHP使用mcrypt解密它,并且可以提供一些帮助。我用php得到以下错误,所以我猜我没有正确设置IV。
错误:IV参数必须与blocksize
一样长两个函数使用相同的密码,密钥,IV并设置为CBC模式:
来自c#的加密文本= UmzUCnAzThH0nMkIuMisqg ==
key 32 long = qwertyuiopasdfghjklzxcvbnmqwerty
iv 16 long = 1234567890123456
C#
public static string EncryptString(string message, string KeyString, string IVString)
{
byte[] Key = ASCIIEncoding.UTF8.GetBytes(KeyString);
byte[] IV = ASCIIEncoding.UTF8.GetBytes(IVString);
string encrypted = null;
RijndaelManaged rj = new RijndaelManaged();
rj.Key = Key;
rj.IV = IV;
rj.Mode = CipherMode.CBC;
try
{
MemoryStream ms = new MemoryStream();
using (CryptoStream cs = new CryptoStream(ms, rj.CreateEncryptor(Key, IV), CryptoStreamMode.Write))
{
using (StreamWriter sw = new StreamWriter(cs))
{
sw.Write(message);
sw.Close();
}
cs.Close();
}
byte[] encoded = ms.ToArray();
encrypted = Convert.ToBase64String(encoded);
ms.Close();
}
catch (CryptographicException e)
{
Console.WriteLine("A Cryptographic error occurred: {0}", e.Message);
return null;
}
catch (UnauthorizedAccessException e)
{
Console.WriteLine("A file error occurred: {0}", e.Message);
return null;
}
catch (Exception e)
{
Console.WriteLine("An error occurred: {0}", e.Message);
}
finally
{
rj.Clear();
}
return encrypted;
}
PHP
var $mcrypt_cipher = MCRYPT_RIJNDAEL_256;
var $mcrypt_mode = MCRYPT_MODE_CBC;
function decrypt($key, $iv, $encrypted)
{
$encrypted = base64_decode($encrypted);
$decrypted = rtrim(mcrypt_decrypt($this->mcrypt_cipher, $key, $encrypted, $this->mcrypt_mode, $iv), "\0");;
return $decrypted;
}
由于
答案 0 :(得分:10)
如果要在C#应用程序中使用Rijndael256,则必须将BlockSize设置为256.
RijndaelManaged rj = new RijndaelManaged();
rj.BlockSize = 256;
然后你的iv也必须是256位长 见SymmetricAlgorithm.BlockSize Property
反之亦然:目前您的C#应用程序使用Rijndael128,因此必须使用您的PHP脚本。
<?php
class Foo {
protected $mcrypt_cipher = MCRYPT_RIJNDAEL_128;
protected $mcrypt_mode = MCRYPT_MODE_CBC;
public function decrypt($key, $iv, $encrypted)
{
$iv_utf = mb_convert_encoding($iv, 'UTF-8');
return mcrypt_decrypt($this->mcrypt_cipher, $key, base64_decode($encrypted), $this->mcrypt_mode, $iv_utf);
}
}
$encrypted = "UmzUCnAzThH0nMkIuMisqg==";
$key = "qwertyuiopasdfghjklzxcvbnmqwerty";
$iv = "1234567890123456";
$foo = new Foo;
echo $foo->decrypt($key, $iv, $encrypted);
打印hello world
答案 1 :(得分:-1)
使用PHP加密;
/Generate public key for encrytion
$path = "keys/";
$crt = openssl_x509_read(file_get_contents($path."cert.crt"));
$publickey = openssl_get_publickey($crt);
//Encrypt using public key
openssl_public_encrypt($source, $crypted, $publickey);
//openssl_private_encrypt($source, $crypted, $privkey);
echo base64_encode($crypted);
使用C#解密
X509Certificate2 x509cert = new X509Certificate2(pKeyFilename);
RSACryptoServiceProvider.UseMachineKeyStore = false;
RSACryptoServiceProvider crypt = (RSACryptoServiceProvider)x509cert.PrivateKey;
byte[] decrypted = crypt.Decrypt(Convert.FromBase64String(data), false);
return ASCIIEncoding.UTF8.GetString(decrypted);
其中pKeyFilename是使用证书文件cert.crt创建的个人信息交换文件。此示例使用AES-256加密。