2010-08-07 76 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加密。