954,515 Members — Technology Publication meets Social Media
Username:
Password:
Lost login information?
Have something to say? Contribute New Article Reply to this Article

C Sharp encryption

hi i am looking for some code and step by step instructions for the c sharp encryption and decryption process. if anyone is willing to send me some instructions i would be grateful. thanks

Neville

Nevillelajru
Newbie Poster
17 posts since Mar 2011
Reputation Points: 10
Solved Threads: 0
 

The only encryptions worth using are one-way encryptions. C# has a few classes that can do this, the only one I have used is the System.Security.Cryptography.Rijndael class, however you should use AES instead if you need a more secure algorithm. There is lots of sample code on the MSDN website in the documentation for the System.Security.Cryptography namespaces.

darkagn
Veteran Poster
1,197 posts since Aug 2007
Reputation Points: 404
Solved Threads: 200
 
private class EncryptionClass
        {
            public string Encrypt256(string Data, string Password)
            {
                byte[] clearBytes = System.Text.Encoding.Unicode.GetBytes(Data);


                PasswordDeriveBytes pdb = new PasswordDeriveBytes(Password,


                    new byte[] { 0x00, 0x01, 0x02, 0x1C, 0x1D, 0x1E, 0x03, 0x04, 0x05, 0x0F, 0x20, 0x21, 0xAD, 0xAF, 0xA4 });
                MemoryStream ms = new MemoryStream();

                Rijndael alg = Rijndael.Create();
                alg.Key = pdb.GetBytes(32);

                alg.IV = pdb.GetBytes(16);
                CryptoStream cs = new CryptoStream(ms, alg.CreateEncryptor(), CryptoStreamMode.Write);

                cs.Write(clearBytes, 0, clearBytes.Length);
                cs.Close();
                byte[] encryptedData = ms.ToArray();
                return Convert.ToBase64String(encryptedData);
            }
        }
CsharpChico
Junior Poster in Training
72 posts since May 2010
Reputation Points: 12
Solved Threads: 8
 

thanks alot for the code and all. i really need this for my assignment

Nevillelajru
Newbie Poster
17 posts since Mar 2011
Reputation Points: 10
Solved Threads: 0
 
thanks alot for the code and all. i really need this for my assignment

no problem but that code will only encrypt not decrypt are you looking for decrytpion also? Not that is 256 encryption so it will need to be dercyted as 256

if so to decrypt just use

public string Decrypt256(string Data, string Password)
        {
            byte[] clearBytes = Convert.FromBase64String(Data);


            PasswordDeriveBytes pdb = new PasswordDeriveBytes(Password,


                new byte[] { 0x00, 0x01, 0x02, 0x1C, 0x1D, 0x1E, 0x03, 0x04, 0x05, 0x0F, 0x20, 0x21, 0xAD, 0xAF, 0xA4 });
            MemoryStream ms = new MemoryStream();

            Rijndael alg = Rijndael.Create();
            alg.Key = pdb.GetBytes(32);

            alg.IV = pdb.GetBytes(16);
            CryptoStream cs = new CryptoStream(ms, alg.CreateDecryptor(), CryptoStreamMode.Write);

            cs.Write(clearBytes, 0, clearBytes.Length);
            cs.Close();
            byte[] decryptedData = ms.ToArray();
            return System.Text.Encoding.Unicode.GetString(decryptedData);


        }
CsharpChico
Junior Poster in Training
72 posts since May 2010
Reputation Points: 12
Solved Threads: 8
 

no i dont think i will need to decrypt... i will save the decrypted format in the database and when someone logs in i will encrypt the password from the textbox and match it with the encrypted pass from the database. anyways i will keep this to for reference. thanks alot for your time.

Nevillelajru
Newbie Poster
17 posts since Mar 2011
Reputation Points: 10
Solved Threads: 0
 

This question has already been solved

Post: Markdown Syntax: Formatting Help
You
View similar articles that have also been tagged: