Hi,
I'm trying to create a C# Application that encrypts a message with the RijndaelManaged and then decrypt that message with perl, using PasswordDeriveByte to create random IV and Keys, but it seems that Perl reads the Salt but not the IV, so it can generate the key and can't decrypt the message.
This is the C# Code :
The variables are strOrig (PlainText) and strKey (Passphrase)
System.IO.MemoryStream ms = new System.IO.MemoryStream();
System.Security.Cryptography.RijndaelManaged rj = new Syst
+em.Security.Cryptography.RijndaelManaged();
rj.Mode = System.Security.Cryptography.CipherMode.CBC;
RandomNumberGenerator rng = RandomNumberGenerator.Create()
+;
byte[] salt = new byte[8];
rng.GetBytes(salt);
PasswordDeriveBytes pdb = new PasswordDeriveBytes(strKey,
+salt);
rj.IV = pdb.GetBytes(16);
rj.Key = pdb.GetBytes(32);
// Debug so I can see the IV, Key, and Salt Generated
string strIV = Encoding.UTF8.GetString(rj.IV);
string _strKey = Encoding.UTF8.GetString(rj.Key);
string strSalt = Encoding.UTF8.GetString(salt);
ms.Write(Encoding.UTF8.GetBytes("Salted__"), 0, Encoding.U
+TF8.GetByteCount("Salted__"));
ms.Write(salt, 0, salt.Length);
ms.Write(rj.IV, 0, rj.IV.Length);
byte[] bOrig = Encoding.UTF8.GetBytes(strOrig);
System.Security.Cryptography.CryptoStream cs = new System.
+Security.Cryptography.CryptoStream(ms, rj.CreateEncryptor(), System.S
+ecurity.Cryptography.CryptoStreamMode.Write);
cs.Write(bOrig, 0, bOrig.Length);
cs.FlushFinalBlock();
string bResult = Convert.ToBase64String(ms.ToArray());
bResult = bResult.Remove(0, 10);
ms.Flush();
return bResult;
And this is the Perl Code :
#!/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_Generated_in_C#";
my $enc_key = "same_as_in_c#_strKey";
my $enc_alg = "Rijndael";
my $cipher = Crypt::CBC->new({
'key' => $enc_key,
'cipher' => $enc_alg,
});
my $base64_decoded_msg = decode_base64($msg);
my $decrypted_msg = $cipher->decrypt($base64_decoded_msg);
// This is for debug
print STDERR " Salt:\n";
&hex_dump($cipher->salt());
print STDERR " Key:\n";
&hex_dump($cipher->key());
print STDERR " IV:\n";
&hex_dump($cipher->iv());
print STDERR " PassPhrase:\n";
&hex_dump($cipher->passphrase());
print STDERR " Block Size: " . $cipher->blocksize() ."\n",
" Key Size: " . $cipher->keysize(). "\n\n";
print "$decrypted_msg\n";
// Function for debug
sub hex_dump() {
my $data = shift;
my @chars = split //, $data;
my $ctr = 0;
my $ascii_str = '';
for my $char (@chars) {
if ($ctr % 16 == 0) {
print STDERR " $ascii_str\n" if $ascii_str;
printf STDERR " 0x%.4x: ", $ctr;
$ascii_str = '';
}
printf STDERR "%.2x", ord($char);
if ((($ctr+1) % 2 == 0) and ($ctr % 16 != 0)) {
print STDERR ' ';
}
if ($char =~ /[^\x20-\x7e]/) {
$ascii_str .= '.';
} else {
$ascii_str .= $char;
}
$ctr++;
}
if ($ascii_str) {
my $remainder = 1;
if ($ctr % 16 != 0) {
$remainder = 16 - $ctr % 16;
if ($remainder % 2 == 0) {
$remainder = 2*$remainder + int($remainder/2) + 1;
} else {
$remainder = 2*$remainder + int($remainder/2) + 2;
}
}
print STDERR ' 'x$remainder, $ascii_str;
}
print STDERR "\n";
return;
}
I believe that the problem is in the crypting process and the possition of the generated IV key, but I really don't know where to put it.
Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
Read Where should I post X? if you're not absolutely sure you're posting in the right place.
Please read these before you post! —
Posts may use any of the Perl Monks Approved HTML tags:
- a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
| |
For: |
|
Use: |
| & | | & |
| < | | < |
| > | | > |
| [ | | [ |
| ] | | ] |
Link using PerlMonks shortcuts! What shortcuts can I use for linking?
See Writeup Formatting Tips and other pages linked from there for more info.