Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. General Programming
  3. C#
  4. C# TripleDES and Perl CPAN:Crypt

C# TripleDES and Perl CPAN:Crypt

Scheduled Pinned Locked Moved C#
perlcsharpalgorithmssecurityquestion
3 Posts 2 Posters 0 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • D Offline
    D Offline
    draskosaric
    wrote on last edited by
    #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;

    G 1 Reply Last reply
    0
    • D draskosaric

      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;

      G Offline
      G Offline
      Garth J Lancaster
      wrote on last edited by
      #2

      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'

      D 1 Reply Last reply
      0
      • G Garth J Lancaster

        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'

        D Offline
        D Offline
        draskosaric
        wrote on last edited by
        #3

        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

        1 Reply Last reply
        0
        Reply
        • Reply as topic
        Log in to reply
        • Oldest to Newest
        • Newest to Oldest
        • Most Votes


        • Login

        • Don't have an account? Register

        • Login or register to search.
        • First post
          Last post
        0
        • Categories
        • Recent
        • Tags
        • Popular
        • World
        • Users
        • Groups