Using TripleDES Encryption [modified]
-
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
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.
-
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
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.
-
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.
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
-
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
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.
-
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
What is the StrValue that you pass to the streamwriter?
-
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.
Hi, The size is 7. Its there in the declration of the array. Regards, Mahen
-
What is the StrValue that you pass to the streamwriter?
Hi, strValue is the text that needs to be encrypted. Regards, Mahen
-
Hi, strValue is the text that needs to be encrypted. Regards, Mahen
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?
-
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?
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
-
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
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.
-
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
Please dont reinvent the wheel. Use SSL. :|**
How xacc.ide transforms text to colored words on the screen
Intel PentuimM (aka Centrino) undervolting**