-
-
Save jeffersonvventura/5e215694924b473e2a058ef6a71e8bf2 to your computer and use it in GitHub Desktop.
C# AES sample code
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; | |
namespace AES | |
{ | |
class Program | |
{ | |
static string aes_key = "AXe8YwuIn1zxt3FPWTZFlAa14EHdPAdN9FaZ9RQWihc="; | |
static string aes_iv = "bsxnWolsAyO7kCfWuyrnqg=="; | |
static void Main(string[] args) | |
{ | |
string original = "Here is some data to encrypt!"; | |
try | |
{ | |
// Encrypt the string to an array of bytes. | |
byte[] encrypted = EncryptStringToBytes(original, Convert.FromBase64String(aes_key), Convert.FromBase64String(aes_iv)); | |
// Decrypt the bytes to a string. | |
string roundtrip = DecryptStringFromBytes(encrypted, Convert.FromBase64String(aes_key), Convert.FromBase64String(aes_iv)); | |
//Display the original data and the decrypted data. | |
Console.WriteLine("Original: {0}", original); | |
Console.WriteLine("Round Trip: {0}", roundtrip); | |
Console.WriteLine("==========================="); | |
// Encrypt the string to an array of bytes. | |
string str_encrypted = EncryptAES(original); | |
// Decrypt the bytes to a string. | |
string str_roundtrip = DecryptAES(str_encrypted); | |
Console.WriteLine("Original: {0}", str_encrypted); | |
Console.WriteLine("Round Trip: {0}", str_roundtrip); | |
Console.ReadLine(); | |
} | |
catch (Exception e) | |
{ | |
Console.WriteLine("Error: {0}", e.Message); | |
} | |
} | |
public static string EncryptAES(string plainText) | |
{ | |
byte[] encrypted; | |
using (AesCryptoServiceProvider aes = new AesCryptoServiceProvider()) | |
{ | |
aes.Key = Convert.FromBase64String(aes_key); | |
aes.IV = Convert.FromBase64String(aes_iv); | |
aes.Mode = CipherMode.CBC; | |
aes.Padding = PaddingMode.PKCS7; | |
ICryptoTransform enc = aes.CreateEncryptor(aes.Key, aes.IV); | |
using (MemoryStream ms = new MemoryStream()) | |
{ | |
using (CryptoStream cs = new CryptoStream(ms, enc, CryptoStreamMode.Write)) | |
{ | |
using (StreamWriter sw = new StreamWriter(cs)) | |
{ | |
sw.Write(plainText); | |
} | |
encrypted = ms.ToArray(); | |
} | |
} | |
} | |
return Convert.ToBase64String(encrypted); | |
} | |
public static string DecryptAES(string encryptedText) | |
{ | |
string decrypted = null; | |
byte[] cipher = Convert.FromBase64String(encryptedText); | |
using (AesCryptoServiceProvider aes = new AesCryptoServiceProvider()) | |
{ | |
aes.Key = Convert.FromBase64String(aes_key); | |
aes.IV = Convert.FromBase64String(aes_iv); | |
aes.Mode = CipherMode.CBC; | |
aes.Padding = PaddingMode.PKCS7; | |
ICryptoTransform dec = aes.CreateDecryptor(aes.Key, aes.IV); | |
using (MemoryStream ms = new MemoryStream(cipher)) | |
{ | |
using (CryptoStream cs = new CryptoStream(ms, dec, CryptoStreamMode.Read)) | |
{ | |
using (StreamReader sr = new StreamReader(cs)) | |
{ | |
decrypted = sr.ReadToEnd(); | |
} | |
} | |
} | |
} | |
return decrypted; | |
} | |
static byte[] EncryptStringToBytes(string plainText, byte[] Key, byte[] IV) | |
{ | |
// Check arguments. | |
if (plainText == null || plainText.Length <= 0) | |
throw new ArgumentNullException("plainText"); | |
if (Key == null || Key.Length <= 0) | |
throw new ArgumentNullException("Key"); | |
if (IV == null || IV.Length <= 0) | |
throw new ArgumentNullException("Key"); | |
byte[] encrypted; | |
// Create an RijndaelManaged object | |
// with the specified key and IV. | |
using (RijndaelManaged rijAlg = new RijndaelManaged()) | |
{ | |
rijAlg.Key = Key; | |
rijAlg.IV = IV; | |
// Create a decrytor to perform the stream transform. | |
ICryptoTransform encryptor = rijAlg.CreateEncryptor(rijAlg.Key, rijAlg.IV); | |
// Create the streams used for encryption. | |
using (MemoryStream msEncrypt = new MemoryStream()) | |
{ | |
using (CryptoStream csEncrypt = new CryptoStream(msEncrypt, encryptor, CryptoStreamMode.Write)) | |
{ | |
using (StreamWriter swEncrypt = new StreamWriter(csEncrypt)) | |
{ | |
//Write all data to the stream. | |
swEncrypt.Write(plainText); | |
} | |
encrypted = msEncrypt.ToArray(); | |
} | |
} | |
} | |
// Return the encrypted bytes from the memory stream. | |
return encrypted; | |
} | |
static string DecryptStringFromBytes(byte[] cipherText, byte[] Key, byte[] IV) | |
{ | |
// Check arguments. | |
if (cipherText == null || cipherText.Length <= 0) | |
throw new ArgumentNullException("cipherText"); | |
if (Key == null || Key.Length <= 0) | |
throw new ArgumentNullException("Key"); | |
if (IV == null || IV.Length <= 0) | |
throw new ArgumentNullException("Key"); | |
// Declare the string used to hold | |
// the decrypted text. | |
string plaintext = null; | |
// Create an RijndaelManaged object | |
// with the specified key and IV. | |
using (RijndaelManaged rijAlg = new RijndaelManaged()) | |
{ | |
rijAlg.Key = Key; | |
rijAlg.IV = IV; | |
// Create a decrytor to perform the stream transform. | |
ICryptoTransform decryptor = rijAlg.CreateDecryptor(rijAlg.Key, rijAlg.IV); | |
// Create the streams used for decryption. | |
using (MemoryStream msDecrypt = new MemoryStream(cipherText)) | |
{ | |
using (CryptoStream csDecrypt = new CryptoStream(msDecrypt, decryptor, CryptoStreamMode.Read)) | |
{ | |
using (StreamReader srDecrypt = new StreamReader(csDecrypt)) | |
{ | |
// Read the decrypted bytes from the decrypting stream | |
// and place them in a string. | |
plaintext = srDecrypt.ReadToEnd(); | |
} | |
} | |
} | |
} | |
return plaintext; | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment