2010-08-07 91 views
9

CẬP NHẬTRijndael 256 Mã hóa/giải mã giữa C# và php?

tôi đã thực hiện các thay đổi đối với mã C# để nó sử dụng một kích thước khối 256. nhưng bây giờ thế giới chào trông giống như http://pastebin.com/5sXhMV11 này và tôi không thể tìm ra những gì tôi nên sử dụng với rtrim() để có được sự lộn xộn ở cuối.

Ngoài ra khi bạn nói IV nên ngẫu nhiên, bởi điều này làm bạn có nghĩa là không sử dụng nhiều giống IV sau đó một lần hoặc là cách tôi đã mã hoá nó sai?

Cảm ơn bạn lần nữa!

Xin chào,

Tôi đang cố gắng giải mã một chuỗi bằng PHP được mã hóa trong C#. Tôi dường như không thể có được PHP để giải mã nó bằng cách sử Mcrypt và có thể làm với một số sự giúp đỡ xin vui lòng. Tôi nhận được lỗi sau với php vì vậy tôi đoán tôi không thiết lập IV một cách chính xác.

Lỗi: Tham số IV phải được miễn là kích cỡ khối

Cả hai chức năng sử dụng cùng một mật mã, chìa khóa, IV và thiết lập chế độ CBC:

văn bản được mã hóa từ C# = UmzUCnAzThH0nMkIuMisqg ==
chìa khóa 32 dài = qwertyuiopasdfghjklzxcvbnmqwerty dài
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; 
} 

Cảm ơn

+1

IV nên thực sự được chọn ngẫu nhiên. Nó đánh bại mục đích có một nếu nó không. – quantumSoup

+0

Rijndael với khối 256 bit không chuẩn. – kroiz

Trả lời

10

Nếu bạn muốn sử dụng Rijndael256 trong ứng dụng C# của bạn, bạn phải thiết lập kích cỡ khối để 256.

RijndaelManaged rj = new RijndaelManaged(); 
rj.BlockSize = 256; 

Và sau đó iv của bạn có dài 256 bit.
thấy SymmetricAlgorithm.BlockSize Property


Hoặc theo chiều ngược lại: Hiện nay # ứng dụng C của bạn sử dụng Rijndael128 và do đó phải php script của bạn.

<?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); 

in hello world

+3

Tôi biết đó không phải lỗi của bạn, nhưng IV thực sự nên được ngẫu nhiên. Nó đánh bại mục đích có một nếu nó không. – quantumSoup

+1

đã đồng ý. xem http://msdn.microsoft.com/en-us/library/system.security.cryptography.rijndaelmanaged.generateiv.aspx và http://docs.php.net/function.mcrypt-create-iv – VolkerK

+0

Bạn có thể xem điều này xin vui lòng? http://stackoverflow.com/questions/18908613/mcrypt-and-base64-with-php-and-c-sharp – hsuk

-1

Mã hóa sử dụng 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); 

Decrypt sử dụng 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); 

nơi pKeyFilename là một tập tin Thông tin cá nhân giao dịch tạo ra với các tập tin giấy chứng nhận cert.crt. Ví dụ này sử dụng mã hóa AES-256.

Các vấn đề liên quan