المساعد الشخصي الرقمي

مشاهدة النسخة كاملة : C# Rijndael Encrypt and Pel Crypt::CBC



C# Programming
07-09-2009, 05:21 PM
Hi,

I'm trying to encrypt a string in c# that would be decrypted with perl. The problem is that in the perl script there is not IV specified so, when I encrypt the result is not readable be the perl decryptor.

Is there any workaround to know who perl auto generate the IV (I suppose that's what it does) ?



C# Code to Encrypt :

private string strCryptoMessage(string strOrig, string strKey)
{
System.Security.Cryptography.RijndaelManaged rj = new System.Security.Cryptography.RijndaelManaged();
rj.Mode = System.Security.Cryptography.CipherMode.CBC;
rj.Key = Encoding.UTF8.GetBytes(strKey);

byte[] bOrig = Encoding.UTF8.GetBytes(strOrig);

System.IO.MemoryStream ms = new System.IO.MemoryStream();

System.Security.Cryptography.CryptoStream cs = new System.Security.Cryptography.CryptoStream(ms, rj.CreateEncryptor(), System.Security.Cryptography.CryptoStreamMode.Write);

cs.Write(bOrig, 0, bOrig.Length);
cs.FlushFinalBlock();

string bResult = Convert.ToBase64String(ms.ToArray());
ms.Flush();


return bResult;
}




Perl Code To Encrypt :

#!/opt/local/bin/perl -w

use IO::Socket;
use IO::Handle;
use MIME::Base64;
use Data::Dumper;
use POSIX;
use Getopt::Long;
use strict;


require Crypt::CBC;

my $msg = "MESSAGE_TO_ENCRYPT";
my $enc_key = "SAME_AS_strKey";
my $enc_alg = "Rijndael";

my $cipher = Crypt::CBC->new({
'key' => $enc_key,
'cipher' => $enc_alg,
});

my $encrypted_msg = $cipher->encrypt($msg);
print "$msg\n";

my $encoded_msg = encode_base64($encrypted_msg, '');
print "$encoded_msg\n";


Both should generate the same result, but I think that the perl Crypt::CBC generate the IV code using the key in some way.