Skip to content

Instantly share code, notes, and snippets.

@ChrisMcKee
Last active December 10, 2015 15:08
Show Gist options
  • Select an option

  • Save ChrisMcKee/4451906 to your computer and use it in GitHub Desktop.

Select an option

Save ChrisMcKee/4451906 to your computer and use it in GitHub Desktop.
AES Encryption Helper
namespace Helper
{
using System;
using System.IO;
using System.Security.Cryptography;
using System.Text;
using System.Web;
public class AESEncryptionHelper
{
private static readonly byte[] Key = { 111, 222, 33, 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 readonly byte[] Vector = { 155, 68, 222, 111, 23, 3, 113, 119, 231, 121, 221, 112, 79, 32, 114, 156 };
private readonly ICryptoTransform _encryptor;
private readonly ICryptoTransform _decryptor;
public AESEncryptionHelper()
{
var rm = new RijndaelManaged();
_encryptor = rm.CreateEncryptor(Key, Vector);
_decryptor = rm.CreateDecryptor(Key, Vector);
}
private string Encrypt(string unencrypted)
{
return Convert.ToBase64String(Encrypt(Encoding.Unicode.GetBytes(unencrypted)));
}
private string Decrypt(string encrypted)
{
return Encoding.Unicode.GetString(Decrypt(Convert.FromBase64String(encrypted.Replace(" ", "+"))));
}
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)
{
var encryptStream = new MemoryStream();
using (var cs = new CryptoStream(encryptStream, _encryptor, CryptoStreamMode.Write))
{
cs.Write(buffer, 0, buffer.Length);
}
return encryptStream.ToArray();
}
public byte[] Decrypt(byte[] buffer)
{
var decryptStream = new MemoryStream();
using (var cs = new CryptoStream(decryptStream, _decryptor, CryptoStreamMode.Write))
{
cs.Write(buffer, 0, buffer.Length);
}
return decryptStream.ToArray();
}
}
}
// Needs the Base65 Encoding Class (In gists)
namespace Utils
{
using System.Text;
using System.Web;
public sealed class EncryptionHelper
{
private static string Encrypt(string unencrypted)
{
var parsed = Encoding.Unicode.GetBytes(unencrypted);
return Base65Encoding.Encode(parsed);
}
private static string Decrypt(string encrypted)
{
return Encoding.Unicode.GetString(Base65Encoding.Decode(encrypted));
}
public static string EncryptToUrl(string unencrypted)
{
return HttpUtility.UrlEncode(Encrypt(unencrypted));
}
public static string DecryptFromUrl(string encrypted)
{
return Decrypt(HttpUtility.UrlDecode(encrypted));
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment