Encrypt and decrypt a string
How we can encrypt and decrypt a string in ASP. net ?
Know the answer? Post it. Somebody with the same question will find it here.
Answer a question in the forums and earn +20 SHARP for every answer.
See unanswered questions
DevSocialUpcoming · 3rd editionAI Agents ConferenceDec 1–3, 2026Live onlineFree to attend5days leftRegister
Posted Jun 15, 2011, 4:41 AM EDT
http://www.c-sharpcorner.com/UploadFile/Suprotim/CryptographyInDotNET11282005053700AM/CryptographyInDotNET.aspx
http://www.c-sharpcorner.com/UploadFile/krishnasarala/5184/
http://www.c-sharpcorner.com/UploadFile/ahsanshakir/EncryptFile12212006042816AM/EncryptFile.aspx
Please read our recommend article section.
Hope this helps you.
Mayur GujrathiRank #666 · 3.6K reputationPosted Jun 15, 2011, 9:41 PM EDT
there are many methods one of is following add following name space Using System.Security.Cryptography remember whenever you are encrypting and decrypting same word you will have to add same key parameter it means if you are encrypting word csharp and sending parameter c in string strKey parameter then while decrypting also you will havew to pass same key in decrypt function with decrypt string public static string Encrypt(string strToEncrypt, string strKey) { try { TripleDESCryptoServiceProvider objDESCrypto = new TripleDESCryptoServiceProvider(); MD5CryptoServiceProvider objHashMD5 = new MD5CryptoServiceProvider(); byte[] byteHash = null; byte[] byteBuff = null; string strTempKey = strKey; byteHash = objHashMD5.ComputeHash(ASCIIEncoding.ASCII.GetBytes(strTempKey)); objHashMD5 = null; objDESCrypto.Key = byteHash; objDESCrypto.Mode = CipherMode.ECB; //CBC, CFB byteBuff = ASCIIEncoding.ASCII.GetBytes(strToEncrypt); return Convert.ToBase64String(objDESCrypto.CreateEncryptor().TransformFinalBlock(byteBuff, 0, byteBuff.Length)); } catch (Exception ex) { return "Wrong Input. " + ex.Message; } } public static string Decrypt(string strEncrypted, string strKey) { try { TripleDESCryptoServiceProvider objDESCrypto = new TripleDESCryptoServiceProvider(); MD5CryptoServiceProvider objHashMD5 = new MD5CryptoServiceProvider(); byte[] byteHash = null; byte[] byteBuff = null; string strTempKey = strKey; byteHash = objHashMD5.ComputeHash(ASCIIEncoding.ASCII.GetBytes(strTempKey)); objHashMD5 = null; objDESCrypto.Key = byteHash; objDESCrypto.Mode = CipherMode.ECB; //CBC, CFB byteBuff = Convert.FromBase64String(strEncrypted); string strDecrypted = ASCIIEncoding.ASCII.GetString(objDESCrypto.CreateDecryptor().TransformFinalBlock(byteBuff, 0, byteBuff.Length)); objDESCrypto = null; return strDecrypted; } catch (Exception ex) { return "Wrong Input. " + ex.Message; } }