There is one problem that many of the links above ignore, and this is just before the return of the encrypted string, the URL Encode (see below, before the string is returned). I am using IIS 7.5 and it will automatically “decrypt” the string for you, so the decryption “should” be ok. The encryption and decryption code is shown below.
public string EncryptQueryString(string inputText, string key, string salt) { byte[] plainText = Encoding.UTF8.GetBytes(inputText); using (RijndaelManaged rijndaelCipher = new RijndaelManaged()) { PasswordDeriveBytes secretKey = new PasswordDeriveBytes(Encoding.ASCII.GetBytes(key), Encoding.ASCII.GetBytes(salt)); using (ICryptoTransform encryptor = rijndaelCipher.CreateEncryptor(secretKey.GetBytes(32), secretKey.GetBytes(16))) { using (MemoryStream memoryStream = new MemoryStream()) { using (CryptoStream cryptoStream = new CryptoStream(memoryStream, encryptor, CryptoStreamMode.Write)) { cryptoStream.Write(plainText, 0, plainText.Length); cryptoStream.FlushFinalBlock(); string base64 = Convert.ToBase64String(memoryStream.ToArray());
source share