For hours, I have been searching for the perfect way to encrypt strings for URLs. Here is the method I found
stack overflow
using System; using System.IO; using System.Security.Cryptography; using System.Text; using System.Web; public class SimplerAES { private static byte[] key = { 123, 217, 19, 11, 24, 26, 85, 45, 114, 184, 27, 162, 37, 112, 222, 209, 241, 24, 175, 144, 173, 53, 196, 29, 24, 26, 17, 218, 131, 236, 53, 209 }; private static byte[] vector = { 146, 64, 191, 111, 23, 3, 113, 119, 231, 121, 221, 112, 79, 32, 114, 156 }; private ICryptoTransform encryptor, decryptor; private UTF8Encoding encoder; public SimplerAES() { RijndaelManaged rm = new RijndaelManaged(); encryptor = rm.CreateEncryptor(key, vector); decryptor = rm.CreateDecryptor(key, vector); encoder = new UTF8Encoding(); } public string Encrypt(string unencrypted) { return Convert.ToBase64String(Encrypt(encoder.GetBytes(unencrypted))); } public string Decrypt(string encrypted) { return encoder.GetString(Decrypt(Convert.FromBase64String(encrypted))); } public string EncryptToUrl(string unencrypted) { return HttpUtility.UrlEncode(Encrypt(unencrypted)); } public string DecryptFromUrl(string encrypted) { return Decrypt(HttpUtility.UrlDecode(encrypted)); } public byte[] Encrypt(byte[] buffer) { MemoryStream encryptStream = new MemoryStream(); using (CryptoStream cs = new CryptoStream(encryptStream, encryptor, CryptoStreamMode.Write)) { cs.Write(buffer, 0, buffer.Length); } return encryptStream.ToArray(); } public byte[] Decrypt(byte[] buffer) { MemoryStream decryptStream = new MemoryStream(); using (CryptoStream cs = new CryptoStream(decryptStream, decryptor, CryptoStreamMode.Write)) { cs.Write(buffer, 0, buffer.Length); } return decryptStream.ToArray(); } }
Now when I look at the encrypted strings created with
var a1 = _aes.EncryptToUrl("aaa"); var a2 = _aes.EncryptToUrl("aaaa"); var a3 = _aes.EncryptToUrl("aaaaa"); var a4 = _aes.EncryptToUrl("aaaaaa"); var a5 = _aes.EncryptToUrl("aaaaaaa"); var a6 = _aes.EncryptToUrl("aaaaaaaa"); var a7 = _aes.EncryptToUrl("aaaaaaaaa"); var a8 = _aes.EncryptToUrl("aaaaaaaaaa"); var a9 = _aes.EncryptToUrl("aaaaaaaaaaa"); var a10 = _aes.EncryptToUrl("aaaaaaaaaaa"); var a11 = _aes.EncryptToUrl("aaaaaaaaaaaa"); var a12 = _aes.EncryptToUrl("aaaaaaaaaaaaa"); var a13 = _aes.EncryptToUrl("aaaaaaaaaaaaaa");
All lines (var a1 - a13) end with "% 3d% 3d"
Since preserving the storage space is very important for this application, I wonder if I can just delete "% 3d% 3d" and add it later before decoding to return to the original URL.
Can anyone give me some advice on this.
Thanks,