Created
February 15, 2012 07:14
-
-
Save cangencer/1833986 to your computer and use it in GitHub Desktop.
C# aes encrypt/decrypt
This file contains 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
public const int KEY_SIZE = 16; | |
public byte[] Encrypt (string password, string input) | |
{ | |
var sha256CryptoServiceProvider = new SHA256CryptoServiceProvider(); | |
var hash = sha256CryptoServiceProvider.ComputeHash(Encoding.UTF8.GetBytes(password)); | |
var key = new byte[KEY_SIZE]; | |
var iv = new byte[KEY_SIZE]; | |
Buffer.BlockCopy(hash, 0, key, 0, KEY_SIZE); | |
Buffer.BlockCopy(hash, KEY_SIZE, iv, 0, KEY_SIZE); | |
using (var cipher = new AesCryptoServiceProvider().CreateEncryptor(key, iv)) | |
using (var output = new MemoryStream()) | |
{ | |
using (var cryptoStream = new CryptoStream(output, cipher, CryptoStreamMode.Write)) | |
{ | |
var inputBytes = Encoding.UTF8.GetBytes(input); | |
cryptoStream.Write(inputBytes, 0, inputBytes.Length); | |
} | |
return output.ToArray(); | |
} | |
} | |
public string Decrypt(string password, byte[] encryptedBytes) | |
{ | |
var sha256CryptoServiceProvider = new SHA256CryptoServiceProvider(); | |
var hash = sha256CryptoServiceProvider.ComputeHash(Encoding.UTF8.GetBytes(password)); | |
var key = new byte[KEY_SIZE]; | |
var iv = new byte[KEY_SIZE]; | |
Buffer.BlockCopy(hash, 0, key, 0, KEY_SIZE); | |
Buffer.BlockCopy(hash, KEY_SIZE, iv, 0, KEY_SIZE); | |
using (var cipher = new AesCryptoServiceProvider().CreateDecryptor(key, iv)) | |
using (var source = new MemoryStream(encryptedBytes)) | |
using (var output = new MemoryStream()) | |
{ | |
using (var cryptoStream = new CryptoStream(source, cipher, CryptoStreamMode.Read)) | |
{ | |
cryptoStream.CopyTo(output); | |
} | |
return Encoding.UTF8.GetString(output.ToArray()); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Thanks.