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. Help with DES encryption & decryption

Help with DES encryption & decryption

Scheduled Pinned Locked Moved C#
helpcsharphtmlcomgraphics
2 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.
  • S Offline
    S Offline
    Sreerag Gopinath
    wrote on last edited by
    #1

    Hi, Could you spare some time to help me with a small problem? I need to perform encryption and decryption using the DES algorithm. This is the way I want the program to work : My form has 4 textboxes - 1) tbPlaintext --- For the user to enter the plaintext 2) tbCiphertext --- For displaying he ciphertext 3) tbKey --- For displaying the key 4) tbIV --- For displaying the Initialization Vector and 2 buttons - 1) Button1 --- When clicked performs encryption using key form tbKey and the IV from the string - "init vec" 2) Button2 --- When clicked performs decryption using the key in tbKey and the IV "init vec" The user enters the plaintext -"Hello world" and the key - "qwertyui" and clicks the Button1. The ciphertext appears in the textbox tbCiphertext ("SGVsbG8gd29ybGQ="). This part works fine. Now when the user clicks the Button2 , after clearing the text in tbPlaintext, the plaintext "Hello world" should appear in tbPlaintext. But what I get is "U0dWc2JHOGdkMjl5YkdRPQ==". How do you explain that? What should I do to get my original plaintext? The MSDN article "How To: Create an Encryption Library in .NET 1.1" at the following URL helped me with the code- http://msdn.microsoft.com/library/default.asp?url=/library/en-us/dnnetsec/html/SecNetHT10.asp Is there something I'm missing? Please help me as soon as possible so I can proceed with my project. - Sreerag Gopinath P C EMAIL : sreerag.gopinath@gmail.com PHONE : +919447489699 ********************************************************* The relevant source code is as follows - ----------------------------------------------- /* ENCRYPT USING DES ALGORITHM */ private void button1_Click(object sender, System.EventArgs e) { if (tbKey.Text == "") MessageBox.Show("Enter some key"); else { if (tbKey.Text.Length == 8) { DES des = new DESCryptoServiceProvider(); des.Mode = CipherMode.CBC; byte[] plaintext = Encoding.ASCII.GetBytes(tbplaintext.Text.ToString()); byte[] key = Encoding.ASCII.GetBytes(tbKey.Text.Trim()); byte[] IV = Encoding.ASCII.GetBytes("init vec"); des.Key = key; des.IV = IV; ICryptoTransform transform = des.CreateEncryptor(des.Key,des.IV); MemoryStream memStreamEncryptedData = new MemoryStream(); memStreamEncryptedData.Write(plaintext,0,plaintext.L

    G 1 Reply Last reply
    0
    • S Sreerag Gopinath

      Hi, Could you spare some time to help me with a small problem? I need to perform encryption and decryption using the DES algorithm. This is the way I want the program to work : My form has 4 textboxes - 1) tbPlaintext --- For the user to enter the plaintext 2) tbCiphertext --- For displaying he ciphertext 3) tbKey --- For displaying the key 4) tbIV --- For displaying the Initialization Vector and 2 buttons - 1) Button1 --- When clicked performs encryption using key form tbKey and the IV from the string - "init vec" 2) Button2 --- When clicked performs decryption using the key in tbKey and the IV "init vec" The user enters the plaintext -"Hello world" and the key - "qwertyui" and clicks the Button1. The ciphertext appears in the textbox tbCiphertext ("SGVsbG8gd29ybGQ="). This part works fine. Now when the user clicks the Button2 , after clearing the text in tbPlaintext, the plaintext "Hello world" should appear in tbPlaintext. But what I get is "U0dWc2JHOGdkMjl5YkdRPQ==". How do you explain that? What should I do to get my original plaintext? The MSDN article "How To: Create an Encryption Library in .NET 1.1" at the following URL helped me with the code- http://msdn.microsoft.com/library/default.asp?url=/library/en-us/dnnetsec/html/SecNetHT10.asp Is there something I'm missing? Please help me as soon as possible so I can proceed with my project. - Sreerag Gopinath P C EMAIL : sreerag.gopinath@gmail.com PHONE : +919447489699 ********************************************************* The relevant source code is as follows - ----------------------------------------------- /* ENCRYPT USING DES ALGORITHM */ private void button1_Click(object sender, System.EventArgs e) { if (tbKey.Text == "") MessageBox.Show("Enter some key"); else { if (tbKey.Text.Length == 8) { DES des = new DESCryptoServiceProvider(); des.Mode = CipherMode.CBC; byte[] plaintext = Encoding.ASCII.GetBytes(tbplaintext.Text.ToString()); byte[] key = Encoding.ASCII.GetBytes(tbKey.Text.Trim()); byte[] IV = Encoding.ASCII.GetBytes("init vec"); des.Key = key; des.IV = IV; ICryptoTransform transform = des.CreateEncryptor(des.Key,des.IV); MemoryStream memStreamEncryptedData = new MemoryStream(); memStreamEncryptedData.Write(plaintext,0,plaintext.L

      G Offline
      G Offline
      geo_m
      wrote on last edited by
      #2

      you're mixing with the base64 encoding. Your sequence is: Encryption: get bytes of plaintext encrypt display base64 encoded ciphertext Decryption: get bytes of ciphertext decrypt display base64 encoded result but better and working solution ought to be Decryption: decode the ciphertext bytes from the base64 (FromBase64String) decrypt get string from bytes

      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