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