Posts
39
Comments
89
Trackbacks
0
C# Encryption

The .net framework contains useful classes for encryption found in the System.Security.Cryptography namespace. Even though these classes shield the developer from the complexity of the encryption algorithms, there is still a significant amount of implementation detail to handle.  Under most scenarios, a developer simply wants a function that takes a clear text string as a parameter and returns an encrypted string.  The C# code for this function is below

public string EncryptString(string ClearText)
        {

            byte[] clearTextBytes = Encoding.UTF8.GetBytes(ClearText);

            System.Security.Cryptography.SymmetricAlgorithm rijn =  SymmetricAlgorithm.Create();

            MemoryStream ms = new MemoryStream();
            byte[] rgbIV = Encoding.ASCII.GetBytes("ryojvlzmdalyglrj"); 
            byte[] key = Encoding.ASCII.GetBytes("hcxilkqbbhczfeultgbskdmaunivmfuo"); 
            CryptoStream cs = new CryptoStream(ms, rijn.CreateEncryptor(key, rgbIV), 
	   CryptoStreamMode.Write);

            cs.Write(clearTextBytes, 0, clearTextBytes.Length);

            cs.Close();

            return Convert.ToBase64String(ms.ToArray());
        }

The function to go from an encrypted string to clear text will do everything in the function above but in the opposite order. 

private string DecryptString(string EncryptedText)
        {
            byte[] encryptedTextBytes = Convert.FromBase64String(EncryptedText);

            MemoryStream ms = new MemoryStream();

            System.Security.Cryptography.SymmetricAlgorithm rijn = SymmetricAlgorithm.Create();


            byte[] rgbIV = Encoding.ASCII.GetBytes("ryojvlzmdalyglrj"); 
            byte[] key = Encoding.ASCII.GetBytes("hcxilkqbbhczfeultgbskdmaunivmfuo"); 

            CryptoStream cs = new CryptoStream(ms, rijn.CreateDecryptor(key, rgbIV), 
            CryptoStreamMode.Write);

            cs.Write(encryptedTextBytes, 0, encryptedTextBytes.Length);

            cs.Close();

            return Encoding.UTF8.GetString(ms.ToArray());

        }

Be sure to replace the encryption keys I provided above with your own unique values.  This site is an easy way to generate random letters of a given length.  Also, note that the encryption type used above is symmetric t.  If you need an asymmetric or hash algorithm use the appropriate class within the Cryptography namespace.

posted on Wednesday, January 28, 2009 8:15 PM Print
Comments
Gravatar
# re: C# Encryption
scarnavia
7/10/2009 11:13 AM
hi<br />the DecryptString method raise an error...<br />"padding is invalid and cannot be removed"<br />
Gravatar
# re: C# Encryption
Cale
8/3/2009 9:38 AM
The SymmetricAlgorithm class is abstract. It shouldn't be instantiated. Thats why this doesn't work. Not even sure why Microsoft allowed it to be instantiated (through the Create() method).<br /><br />Bottom line... this code does _not_ work and will _not_ work.
Gravatar
# re: C# Encryption
jamesqua
8/22/2009 5:55 PM
Cale, this code works just fine. I just ran it in a console application with no problem. Though it is odd for a class to be abstract with a method available for instantiation.
Gravatar
# re: C# Encryption
Andy
1/11/2010 9:37 AM
thanks
Gravatar
# re: C# Encryption
zdooxIE
5/11/2011 2:40 PM
works fine fo me
Gravatar
# re: C# Encryption
Dave
4/20/2012 1:05 PM
Works for me. Thank you!
Gravatar
# re: C# Encryption
Geoff Boult
12/5/2012 7:47 AM
Many thanks for a simple and understandable solution!
Gravatar
# re: C# Encryption
Billie
2/27/2013 1:27 AM
“Even though these classes shield the developer from the complexity of the encryption algorithms, there is still a significant amount of implementation detail to handle.” I tried it and it still works for me when I was cleaning up my www.custom-essay-writing.net site. Thanks for the post.
Gravatar
# re: C# Encryption
m
10/6/2013 6:43 PM
it also doesnt work for me.
Gravatar
# re: C# Encryption
m
10/6/2013 7:09 PM
actually i changed it to public static string and it worked fine. neat.
Gravatar
# re: C# Encryption
magicsux
10/17/2013 5:40 PM
Works awesome! Thx
Gravatar
# re: C# Encryption
dissertation writing help
11/20/2013 1:03 AM
I have to consult an IT expert just to explain this whole matter to me because it's quite confusing the first time I read it. I just wish that you could have made your point clearer and a bit more argumentative.
Gravatar
# re: C# Encryption
Imran ur Rehman
11/20/2013 5:48 AM
Awesome work, for those who r not able to run this code. kindly fix following things.

1) Make "private string DecryptString(string EncryptedText)" method Public so it should looks like "public string DecryptString(string EncryptedText)"

2) Length of keys should be as directed
i) rgbIV should be 16 characters long
ii)key should be 32 characters long

u can use extended or shorter lengths too but this could cause error.
first try to use same keys as provided by author. like

byte[] rgbIV = Encoding.ASCII.GetBytes("ryojvlzmdalyglrj");
byte[] key = Encoding.ASCII.GetBytes("hcxilkqbbhczfeultgbskdmaunivmfuo");

any ways,
thanks again for great work.

Gravatar
# re: C# Encryption
hyipbox
8/16/2014 10:26 AM
Good looking script, and works like charm. Thanks lot.

Post Comment

Title *
Name *
Email
Url
Comment *  
Please add 2 and 1 and type the answer here: