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. Using TripleDES Encryption [modified]

Using TripleDES Encryption [modified]

Scheduled Pinned Locked Moved C#
csharpsecurity
14 Posts 4 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.
  • C Offline
    C Offline
    cybersurferdev
    wrote on last edited by
    #1

    Hi All I need to use TripleDES encyption for my Web Application. I want to store the key used for encryption in the web.config file. How would I go about choosing a key, storing the key in the web.config, retrieving the key for encryption and decryption, and then doing the encryption itself. I'm relatively new at doing cryptography in C#. Thanks:) -- modified at 2:46 Wednesday 7th June, 2006

    L K L 3 Replies Last reply
    0
    • C cybersurferdev

      Hi All I need to use TripleDES encyption for my Web Application. I want to store the key used for encryption in the web.config file. How would I go about choosing a key, storing the key in the web.config, retrieving the key for encryption and decryption, and then doing the encryption itself. I'm relatively new at doing cryptography in C#. Thanks:) -- modified at 2:46 Wednesday 7th June, 2006

      L Offline
      L Offline
      Lost User
      wrote on last edited by
      #2

      You can see the sample from microsoft in your visual studio directory. C:\Program Files\Microsoft Visual Studio .NET 2003\SDK\v1.1\QuickStart\howto\samples\cryptography\fileencrypt\cs

      C 1 Reply Last reply
      0
      • C cybersurferdev

        Hi All I need to use TripleDES encyption for my Web Application. I want to store the key used for encryption in the web.config file. How would I go about choosing a key, storing the key in the web.config, retrieving the key for encryption and decryption, and then doing the encryption itself. I'm relatively new at doing cryptography in C#. Thanks:) -- modified at 2:46 Wednesday 7th June, 2006

        K Offline
        K Offline
        Kodanda Pani
        wrote on last edited by
        #3

        Hi, You can use the following code. TripleDESCryptoServiceProvider provider = null; MemoryStream ms= null; CryptoStream cs = null; StreamWriter sw = null; try { provider = new TripleDESCryptoServiceProvider(); ms = new MemoryStream(); cs = new CryptoStream(ms,provider.CreateEncryptor(Key,Vector),CryptoStreamMode.Write); sw = new StreamWriter(cs); sw.Write(strValue); sw.Flush(); cs.FlushFinalBlock(); ms.Flush(); } catch(Exception) { //do exception handling } finally { sw.Close(); cs.Close(); ms.Close(); } Guess this will help you. Regards, Mahen

        C 2 Replies Last reply
        0
        • L Lost User

          You can see the sample from microsoft in your visual studio directory. C:\Program Files\Microsoft Visual Studio .NET 2003\SDK\v1.1\QuickStart\howto\samples\cryptography\fileencrypt\cs

          C Offline
          C Offline
          cybersurferdev
          wrote on last edited by
          #4

          I don't want to read data in from a file, then encrypt and decrypt it. I want to retrieve a value from the HTTP HEADERS and encrypt that, then redirect from that page with the ecrypted value appended to the URL and decrypt it on another page.

          1 Reply Last reply
          0
          • K Kodanda Pani

            Hi, You can use the following code. TripleDESCryptoServiceProvider provider = null; MemoryStream ms= null; CryptoStream cs = null; StreamWriter sw = null; try { provider = new TripleDESCryptoServiceProvider(); ms = new MemoryStream(); cs = new CryptoStream(ms,provider.CreateEncryptor(Key,Vector),CryptoStreamMode.Write); sw = new StreamWriter(cs); sw.Write(strValue); sw.Flush(); cs.FlushFinalBlock(); ms.Flush(); } catch(Exception) { //do exception handling } finally { sw.Close(); cs.Close(); ms.Close(); } Guess this will help you. Regards, Mahen

            C Offline
            C Offline
            cybersurferdev
            wrote on last edited by
            #5

            That's great, thank you. But I would like to use keys (validationKey and decryptionKey) stored in my web.config file. Also, I want to retrieve a value from the HTTP HEADERS and encrypt that. Then append the encrypted string to a URL and redirect to another page and decrypt it there.

            K 1 Reply Last reply
            0
            • C cybersurferdev

              That's great, thank you. But I would like to use keys (validationKey and decryptionKey) stored in my web.config file. Also, I want to retrieve a value from the HTTP HEADERS and encrypt that. Then append the encrypted string to a URL and redirect to another page and decrypt it there.

              K Offline
              K Offline
              Kodanda Pani
              wrote on last edited by
              #6

              Hi, If you want you can store the keys in web.config or else you can use registry etc. Store them as strings and covert them to Byte array while doing the encryption. private static void GetKeys(byte[] Key, byte[] Vector) { string[] keystring; string[] vectorstring; byte[] intKey = new byte[24]; byte[] intVector = new byte[7]; strKey = System.Configuration.ConfigurationSettings.AppSettings["Key"]; strVector = System.Configuration.ConfigurationSettings.AppSettings["Vector"]; keystring = strKey.Split(new char[]{','}); vectorstring= strVector.Split(new char[]{','}); //build the keys for(int i=0; i<23; i++) { intKey[i] = Convert.ToByte(strKey[i]); } for(int i=0; i<7; i++) { intVector[i] = Convert.ToByte(strVector[i]); } byte[] intLocalKey = {intKey[0], intKey[1], intKey[2], intKey[3], intKey[4], intKey[5], intKey[6], intKey[7], intKey[8], intKey[9], intKey[10], intKey[11], intKey[12], intKey[13], intKey[14], intKey[15], intKey[16], intKey[17], intKey[18], intKey[19], intKey[20], intKey[21], intKey[22], intKey[23]}; byte[] intLocalVec = {intVector[0], intVector[1], intVector[2], intVector[3], intVector[4], intVector[5], intVector[6], intVector[7]}; Key = intLocalKey; Vector = intLocalVec; } Regards, Mahen

              C 1 Reply Last reply
              0
              • K Kodanda Pani

                Hi, If you want you can store the keys in web.config or else you can use registry etc. Store them as strings and covert them to Byte array while doing the encryption. private static void GetKeys(byte[] Key, byte[] Vector) { string[] keystring; string[] vectorstring; byte[] intKey = new byte[24]; byte[] intVector = new byte[7]; strKey = System.Configuration.ConfigurationSettings.AppSettings["Key"]; strVector = System.Configuration.ConfigurationSettings.AppSettings["Vector"]; keystring = strKey.Split(new char[]{','}); vectorstring= strVector.Split(new char[]{','}); //build the keys for(int i=0; i<23; i++) { intKey[i] = Convert.ToByte(strKey[i]); } for(int i=0; i<7; i++) { intVector[i] = Convert.ToByte(strVector[i]); } byte[] intLocalKey = {intKey[0], intKey[1], intKey[2], intKey[3], intKey[4], intKey[5], intKey[6], intKey[7], intKey[8], intKey[9], intKey[10], intKey[11], intKey[12], intKey[13], intKey[14], intKey[15], intKey[16], intKey[17], intKey[18], intKey[19], intKey[20], intKey[21], intKey[22], intKey[23]}; byte[] intLocalVec = {intVector[0], intVector[1], intVector[2], intVector[3], intVector[4], intVector[5], intVector[6], intVector[7]}; Key = intLocalKey; Vector = intLocalVec; } Regards, Mahen

                C Offline
                C Offline
                cybersurferdev
                wrote on last edited by
                #7

                Thanks so much, you've been a really great help. But one last thing, what does the size of the Initialisation vector need to be and what characters may it store? Cos I need this for the web.config too.

                K 1 Reply Last reply
                0
                • K Kodanda Pani

                  Hi, You can use the following code. TripleDESCryptoServiceProvider provider = null; MemoryStream ms= null; CryptoStream cs = null; StreamWriter sw = null; try { provider = new TripleDESCryptoServiceProvider(); ms = new MemoryStream(); cs = new CryptoStream(ms,provider.CreateEncryptor(Key,Vector),CryptoStreamMode.Write); sw = new StreamWriter(cs); sw.Write(strValue); sw.Flush(); cs.FlushFinalBlock(); ms.Flush(); } catch(Exception) { //do exception handling } finally { sw.Close(); cs.Close(); ms.Close(); } Guess this will help you. Regards, Mahen

                  C Offline
                  C Offline
                  cybersurferdev
                  wrote on last edited by
                  #8

                  What is the StrValue that you pass to the streamwriter?

                  K 1 Reply Last reply
                  0
                  • C cybersurferdev

                    Thanks so much, you've been a really great help. But one last thing, what does the size of the Initialisation vector need to be and what characters may it store? Cos I need this for the web.config too.

                    K Offline
                    K Offline
                    Kodanda Pani
                    wrote on last edited by
                    #9

                    Hi, The size is 7. Its there in the declration of the array. Regards, Mahen

                    1 Reply Last reply
                    0
                    • C cybersurferdev

                      What is the StrValue that you pass to the streamwriter?

                      K Offline
                      K Offline
                      Kodanda Pani
                      wrote on last edited by
                      #10

                      Hi, strValue is the text that needs to be encrypted. Regards, Mahen

                      C 1 Reply Last reply
                      0
                      • K Kodanda Pani

                        Hi, strValue is the text that needs to be encrypted. Regards, Mahen

                        C Offline
                        C Offline
                        cybersurferdev
                        wrote on last edited by
                        #11

                        Can I not convert the string to a byte array and then pass it to the memorystream? Won't this work too? How do I retrieve the encrypted string once the process is complete?

                        K 1 Reply Last reply
                        0
                        • C cybersurferdev

                          Can I not convert the string to a byte array and then pass it to the memorystream? Won't this work too? How do I retrieve the encrypted string once the process is complete?

                          K Offline
                          K Offline
                          Kodanda Pani
                          wrote on last edited by
                          #12

                          Hi, In the code used to encrypt add the following statement to get the encrypted text into a string variable strRtnvalue. strRtnvalue = Convert.ToBase64String(ms.GetBuffer(),0,Convert.ToInt32(ms.Length)); Regards, Mahen

                          C 1 Reply Last reply
                          0
                          • K Kodanda Pani

                            Hi, In the code used to encrypt add the following statement to get the encrypted text into a string variable strRtnvalue. strRtnvalue = Convert.ToBase64String(ms.GetBuffer(),0,Convert.ToInt32(ms.Length)); Regards, Mahen

                            C Offline
                            C Offline
                            cybersurferdev
                            wrote on last edited by
                            #13

                            What text encoding must be used for the byte arrays? I'm using the UniCodeEncoding. But I keep getting an error that the key length is wrong for this algorithm. But my key is 48characters in length and the IV is 16cahracters in length.

                            1 Reply Last reply
                            0
                            • C cybersurferdev

                              Hi All I need to use TripleDES encyption for my Web Application. I want to store the key used for encryption in the web.config file. How would I go about choosing a key, storing the key in the web.config, retrieving the key for encryption and decryption, and then doing the encryption itself. I'm relatively new at doing cryptography in C#. Thanks:) -- modified at 2:46 Wednesday 7th June, 2006

                              L Offline
                              L Offline
                              leppie
                              wrote on last edited by
                              #14

                              Please dont reinvent the wheel. Use SSL. :|**

                              How xacc.ide transforms text to colored words on the screen
                              Intel PentuimM (aka Centrino) undervolting

                              **

                              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