Last active
February 22, 2019 08:06
-
-
Save arnabdas/e7ada42c3094aae99a0b47b3463c58aa to your computer and use it in GitHub Desktop.
AES encryption/decryption
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
using System; | |
using System.IO; | |
using System.Security.Cryptography; | |
using System.Text; | |
namespace CodeRuse | |
{ | |
// https://bitlush.com/blog/symmetric-encryption-in-c-sharp | |
public static class CypherExtensions | |
{ | |
static string _aesSalt = "really-long-alphanumeric-string"; | |
static string _aesPassword = "really-long-alphanumeric-string"; | |
static string Encrypt<T>(this string value, string password, string salt) | |
where T : SymmetricAlgorithm, new() | |
{ | |
DeriveBytes rgb = new Rfc2898DeriveBytes(password, Encoding.Unicode.GetBytes(salt)); | |
SymmetricAlgorithm algorithm = new T(); | |
byte[] rgbKey = rgb.GetBytes(algorithm.KeySize >> 3); | |
byte[] rgbIV = rgb.GetBytes(algorithm.BlockSize >> 3); | |
ICryptoTransform transform = algorithm.CreateEncryptor(rgbKey, rgbIV); | |
using (MemoryStream buffer = new MemoryStream()) | |
{ | |
using (CryptoStream stream = new CryptoStream(buffer, transform, CryptoStreamMode.Write)) | |
{ | |
using (StreamWriter writer = new StreamWriter(stream, Encoding.Unicode)) | |
{ | |
writer.Write(value); | |
} | |
} | |
return Convert.ToBase64String(buffer.ToArray()); | |
} | |
} | |
static string Decrypt<T>(this string text, string password, string salt) | |
where T : SymmetricAlgorithm, new() | |
{ | |
DeriveBytes rgb = new Rfc2898DeriveBytes(password, Encoding.Unicode.GetBytes(salt)); | |
SymmetricAlgorithm algorithm = new T(); | |
byte[] rgbKey = rgb.GetBytes(algorithm.KeySize >> 3); | |
byte[] rgbIV = rgb.GetBytes(algorithm.BlockSize >> 3); | |
ICryptoTransform transform = algorithm.CreateDecryptor(rgbKey, rgbIV); | |
using (MemoryStream buffer = new MemoryStream(Convert.FromBase64String(text))) | |
{ | |
using (CryptoStream stream = new CryptoStream(buffer, transform, CryptoStreamMode.Read)) | |
{ | |
using (StreamReader reader = new StreamReader(stream, Encoding.Unicode)) | |
{ | |
return reader.ReadToEnd(); | |
} | |
} | |
} | |
} | |
public static string AesEncrypt(this string value) | |
{ | |
return value.Encrypt<AesManaged>(_aesPassword, _aesSalt); | |
} | |
public static string AesDecrypt(this string text) | |
{ | |
return text.Decrypt<AesManaged>(_aesPassword, _aesSalt); | |
} | |
} | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
using Newtonsoft.Json; | |
string encryptedString = "I want to be encrypted".AesEncrypt(); | |
string encryptedObj = JsonConvert.SerializeObject(new User { name = "Foo", TimeStamp = DateTime.UtcNow }).AesEncrypt(); | |
string decryptedString = encryptedString.AesDecrypt(); | |
User decryptedObj = JsonConvert.DeserializeObject<User>(encryptedObj.AesDecrypt()); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment