C# TripleDES and Perl CPAN:Crypt
-
Hi, I am trying to encrypt data with TripleDES algorithm using given key and given iv in c#, so I can get (correct) results that Perl code provides, but unfortunately I am failing in my attemps. Does anyone know what I am doing wrong? Thanks in advance, D Here are sources: Encryption in C#
public void encrypt() { string keyS = "W-3lee7#AA345ll812345678"; string ivS = "00000000"; string testText = "20091217140739-1" byte[] key = new MD5CryptoServiceProvider().ComputeHash(Encoding.UTF8.GetBytes(keyS)); byte[] iv = Encoding.UTF8.GetBytes(ivS); byte[] data = Encoding.UTF8.GetBytes(testText); byte[] enc = new byte[0]; byte[] dec = new byte[0]; TripleDES tdes = TripleDES.Create(); tdes.IV = iv; tdes.Key = key; tdes.Mode = CipherMode.CBC; tdes.Padding = PaddingMode.PKCS7; ICryptoTransform ict = tdes.CreateEncryptor(); enc = ict.TransformFinalBlock(data, 0, data.Length); return BytesToHex(enc); } public string BytesToHex(byte[] bytes) { StringBuilder hexString = new StringBuilder(bytes.Length); for (int i = 0; i < bytes.Length; i++) { hexString.Append(bytes[i].ToString("X2")); } return hexString.ToString(); }
Perl code:#!/usr/bin/perl use strict; use warnings; use Crypt::CBC; my $key = "W-3lee7#AA345ll812345678"; my $cbc = Crypt::CBC->new( -key => $key, -header => 'none', -iv => '00000000', -cipher => 'DES_EDE3', ); my $plaintext = "20091217140739-1"; print "plaintext: " . $plaintext . "\n\n"; my $encrypted = join('',unpack('H*',$cbc->encrypt($plaintext))); print "encrypted: " . $encrypted . "\n\n"; my $decrypted = $cbc->decrypt(pack'H*',$encrypted); print "decrypted: " . $decrypted . "\n\n"; 1;
-
Hi, I am trying to encrypt data with TripleDES algorithm using given key and given iv in c#, so I can get (correct) results that Perl code provides, but unfortunately I am failing in my attemps. Does anyone know what I am doing wrong? Thanks in advance, D Here are sources: Encryption in C#
public void encrypt() { string keyS = "W-3lee7#AA345ll812345678"; string ivS = "00000000"; string testText = "20091217140739-1" byte[] key = new MD5CryptoServiceProvider().ComputeHash(Encoding.UTF8.GetBytes(keyS)); byte[] iv = Encoding.UTF8.GetBytes(ivS); byte[] data = Encoding.UTF8.GetBytes(testText); byte[] enc = new byte[0]; byte[] dec = new byte[0]; TripleDES tdes = TripleDES.Create(); tdes.IV = iv; tdes.Key = key; tdes.Mode = CipherMode.CBC; tdes.Padding = PaddingMode.PKCS7; ICryptoTransform ict = tdes.CreateEncryptor(); enc = ict.TransformFinalBlock(data, 0, data.Length); return BytesToHex(enc); } public string BytesToHex(byte[] bytes) { StringBuilder hexString = new StringBuilder(bytes.Length); for (int i = 0; i < bytes.Length; i++) { hexString.Append(bytes[i].ToString("X2")); } return hexString.ToString(); }
Perl code:#!/usr/bin/perl use strict; use warnings; use Crypt::CBC; my $key = "W-3lee7#AA345ll812345678"; my $cbc = Crypt::CBC->new( -key => $key, -header => 'none', -iv => '00000000', -cipher => 'DES_EDE3', ); my $plaintext = "20091217140739-1"; print "plaintext: " . $plaintext . "\n\n"; my $encrypted = join('',unpack('H*',$cbc->encrypt($plaintext))); print "encrypted: " . $encrypted . "\n\n"; my $decrypted = $cbc->decrypt(pack'H*',$encrypted); print "decrypted: " . $decrypted . "\n\n"; 1;
I see you've set C#
draskosaric wrote:
tdes.Padding = PaddingMode.PKCS7;
what is Perl using for padding ? I see at a very quick glance at Crypt::CBC " -padding The padding method, one of "standard" (default), "space", "oneandzeroes", "rijndael_compat", or "null" (default "standard"). " Im not sure which of those equates to PKCS7, it would be the first thing I'd check 'g'
-
I see you've set C#
draskosaric wrote:
tdes.Padding = PaddingMode.PKCS7;
what is Perl using for padding ? I see at a very quick glance at Crypt::CBC " -padding The padding method, one of "standard" (default), "space", "oneandzeroes", "rijndael_compat", or "null" (default "standard"). " Im not sure which of those equates to PKCS7, it would be the first thing I'd check 'g'
Hi Garth, thank you for you quick reply. I think I've sold it. Perl is using PKCS5 as its standard padding(default), which, according to some posts on the net, will not cause problems with PKCS7, so that wasn't the root of my problem. The solution lies in cofiguring perl script with extra parameter "literal_key" and now everything works fine. D