2010-08-07 170 views
9

修订Rijndael 256在c#和php之间加密/解密?

所以它使用的256块大小,但现在的hello world看起来像这样http://pastebin.com/5sXhMV11,我不能找出我应该RTRIM使用()来我所进行的更改到C#代码在最后得到一团糟。

另外,当你说IV应该是随机的,这是否意味着不要使用相同的IV多于一次,或者是我编写错误的方式?

再次感谢!

嗨,

我试图解密与在C#中被加密PHP的字符串。我似乎无法让PHP使用mcrypt解密,并可以做一些帮助。我得到以下错误与PHP,所以我猜我没有正确设置IV。

错误:IV参数必须是只要块大小

两个函数都使用相同的密码,密钥,IV和设置为CBC模式:

加密文本从C#= UmzUCnAzThH0nMkIuMisqg ==
键32长= qwertyuiopasdfghjklzxcvbnmqwerty
IV 16长= 123456789

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; 
} 

感谢

+1

IV应该真的是随机的。如果不是这样的话,它就会失败。 – quantumSoup 2010-08-07 21:10:09

+0

带有256位块的Rijndael是非标准的。 – kroiz 2014-12-16 08:22:16

回答

10

如果你想在你的C#应用​​程序中使用Rijndael256你必须块大小设置为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 = "123456789"; 

$foo = new Foo; 
echo $foo->decrypt($key, $iv, $encrypted); 

打印使用PHP hello world

+3

我知道这不是你的错,但IV应该是随机的。如果不是这样的话,它就会失败。 – quantumSoup 2010-08-07 21:09:01

+1

表示同意。请参阅http://msdn.microsoft.com/en-us/library/system.security.cryptography.rijndaelmanaged.generateiv.aspx和http://docs.php.net/function.mcrypt-create-iv – VolkerK 2010-08-07 21:13:31

+0

你可以看到这个好吗? http://stackoverflow.com/questions/18908613/mcrypt-and-base64-with-php-and-c-sharp – hsuk 2013-09-20 04:29:27

-1

加密;

/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加密。