Hey! I already post this code on against many question about text encryption, but here everyone can access with out any problem I write two functions Encrypt and Decrypt For Encrypt function just pass a simple string and it will return encrypted string and vise verse with decrypt function
These function are not only for storing and retrieving passwords, but you can also use these functions on query string, when you pass value as a parameter with page redirection, you can encrypt your parameter, and at the when you want to use this parameter any other page, just use decrypt function, hope this little code help you I think this information is enough for using these functions, please feel free to ask if you have any query
using System; using System.Data; using System.Configuration; using System.Collections; using System.Web; using System.Web.Security; using System.Web.UI; using System.Web.UI.WebControls; using System.Web.UI.WebControls.WebParts; using System.Web.UI.HtmlControls; using System.Data.SqlClient; using System.IO; using System.Text; using System.Security.Cryptography;
public static string Encrypt(string pstrText) { string pstrEncrKey = "1239;[pewGKG)NisarFidesTech"; byte[] byKey ={ }; byte[] IV = { 18, 52, 86, 120, 144, 171, 205, 239 }; byKey = System.Text.Encoding.UTF8.GetBytes(pstrEncrKey.Substring(0, 8)); DESCryptoServiceProvider des = new DESCryptoServiceProvider(); byte[] inputByteArray = Encoding.UTF8.GetBytes(pstrText); MemoryStream ms = new MemoryStream(); CryptoStream cs = new CryptoStream(ms, des.CreateEncryptor(byKey, IV), CryptoStreamMode.Write); cs.Write(inputByteArray, 0, inputByteArray.Length); cs.FlushFinalBlock(); return Convert.ToBase64String(ms.ToArray()); }
public static string Decrypt(string pstrText) { pstrText = pstrText.Replace(" ", "+"); string pstrDecrKey = "1239;[pewGKG)NisarFidesTech"; byte[] byKey ={ }; byte[] IV = { 18, 52, 86, 120, 144, 171, 205, 239 }; byte[] inputByteArray = new byte[pstrText.Length];
byKey = System.Text.Encoding.UTF8.GetBytes(pstrDecrKey.Substring(0, 8)); DESCryptoServiceProvider des = new DESCryptoServiceProvider(); inputByteArray = Convert.FromBase64String(pstrText); MemoryStream ms = new MemoryStream(); CryptoStream cs = new CryptoStream(ms, des.CreateDecryptor(byKey, IV), CryptoStreamMode.Write); cs.Write(inputByteArray, 0, inputByteArray.Length); cs.FlushFinalBlock(); System.Text.Encoding encoding = System.Text.Encoding.UTF8; return encoding.GetString(ms.ToArray()); }
|
| Author: Miss Meetu Choudhary 16 Apr 2009 | Member Level: Diamond Points : 1 |
For description
Please Describe your code as well to make it more efficient we respect your contributions.
And it will be of great help to viewers if they are well described.
add the description and revert back to us
Thanks and Regards Meetu Choudhary Editor
|