Click here to Skip to main content
16,011,784 members
Please Sign up or sign in to vote.
1.31/5 (3 votes)
See more:
Can someone give me the code to Encrypt and Decrypt a string in asp .net
Posted
Comments
Sergey Alexandrovich Kryukov 19-Nov-13 0:28am    
Not a question ("can someone" does not count :-). The meaning of encrypt and decrypt can be very different. You need to explain what do you want to achieve.
—SA

Hi Sameer,

I would suggest you to try your own encryption technology since C# has more number of encryption methodology.

C#
private string encodeStr =
            @"!#$%&'()*+,-./0123456789:;<=>?@ABCDEFGHIJKLMNOPQRSTUVWXYZ[\]^_`abcdefghijklmnopqrstuvwxyz{|}~";

        private string decodeStr =
            @"@ABCDEFGHIJKLMNO0123456789:;<=>?pqrstuvwxyz{|}~!#$%&'()*+,-./`abcdefghijklmnoPQRSTUVWXYZ[\]^_";


protected string encodeString(string rawText)
        {
            string cipherText = string.Empty;
            foreach (char rawChar in rawText)
            {
                cipherText += encodeStr.ElementAt(decodeStr.IndexOf(rawChar));
            }
            return cipherText;
        }

        //Custom Decryption method
        protected string decodeString(string cipherText)
        {
            string rawText = string.Empty;
            foreach (char cipherChar in cipherText)
            {
                rawText += decodeStr.ElementAt(encodeStr.IndexOf(cipherChar));
            }
            return rawText;
}


Function call:
encodeString("input String to encrypt");
decodeString("input String to decrypt");

Ok if you not ready to try your own encryption methods, have a look at here[^]andEncryption/Decryption with .NET[^]
 
Share this answer
 
 
Share this answer
 
Encryption Code
(Note if you are using Hasing the key must be 128 bit)

C#
public static string Encrypt(string ToEncrypt, bool useHasing)
        {
            byte[] keyArray;
            byte[] toEncryptArray = UTF8Encoding.UTF8.GetBytes(ToEncrypt);
            
            string Key = "asdfewrewqrss323"; //Must be a 123 bit key
            if (useHasing)
            {
                MD5CryptoServiceProvider hashmd5 = new MD5CryptoServiceProvider();
                keyArray = hashmd5.ComputeHash(UTF8Encoding.UTF8.GetBytes(Key));
                hashmd5.Clear();
            }
            else
            {
                keyArray = UTF8Encoding.UTF8.GetBytes(Key);
            }
            TripleDESCryptoServiceProvider tDes = new TripleDESCryptoServiceProvider();
            tDes.Key = keyArray;
            tDes.Mode = CipherMode.ECB;
            tDes.Padding = PaddingMode.PKCS7;
            ICryptoTransform cTransform = tDes.CreateEncryptor();
            byte[] resultArray = cTransform.TransformFinalBlock(toEncryptArray, 0, toEncryptArray.Length);
            tDes.Clear();
            return Convert.ToBase64String(resultArray, 0, resultArray.Length);
        }



Decryption Code
C#
public static string Decrypt(string cypherString, bool useHasing)
        {
            byte[] keyArray;
            byte[] toDecryptArray = Convert.FromBase64String(cypherString);
             string Key = "asdfewrewqrss323"; //Must be a 123 bit key
            if (useHasing)
            {
                MD5CryptoServiceProvider hashmd = new MD5CryptoServiceProvider();
                keyArray = hashmd.ComputeHash(UTF8Encoding.UTF8.GetBytes(key));
                hashmd.Clear();
            }
            else
            {
                keyArray = UTF8Encoding.UTF8.GetBytes(key);
            }
            TripleDESCryptoServiceProvider tDes = new TripleDESCryptoServiceProvider();
            tDes.Key = keyArray;
            tDes.Mode = CipherMode.ECB;
            tDes.Padding = PaddingMode.PKCS7;
            ICryptoTransform cTransform = tDes.CreateDecryptor();
            try
            {
                byte[] resultArray = cTransform.TransformFinalBlock(toDecryptArray, 0, toDecryptArray.Length);

                tDes.Clear();
                return UTF8Encoding.UTF8.GetString(resultArray, 0, resultArray.Length);
            }
            catch (Exception ex)
            {
                throw ex;
            }
        }


Usage

C#
textBox2.Text = Encrypt(textBox1.Text, false);
 
Share this answer
 
v2

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900