-
-
Save blowdart/979051 to your computer and use it in GitHub Desktop.
Revisions
-
blowdart revised this gist
May 18, 2011 . 1 changed file with 10 additions and 5 deletions.There are no files selected for viewing
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 charactersOriginal file line number Diff line number Diff line change @@ -9,7 +9,7 @@ class Program { static void Main(string[] args) { string text = "Buy my book, available now from Wrox Press"; byte[] data = Encoding.UTF8.GetBytes(text); byte[] encrypted; @@ -24,11 +24,16 @@ static void Main(string[] args) encrypted = ms.ToArray(); } // "Correct" approach. By initialising the MemoryStream with the encrypted data you're fixing the length // So instead do the same as you did during encryption, except create a decryptor. // Start with an empty MemoryStream, attach the CryptoStream to it, then write the encrypted data // into the CryptoStream, flush, and tada. using (var ms = new MemoryStream()) using (var decrypt = new CryptoStream(ms, encryptor.CreateDecryptor(), CryptoStreamMode.Write)) { decrypt.Write(encrypted, 0, encrypted.Length); decrypt.FlushFinalBlock(); decrypted = ms.ToArray(); } Console.WriteLine("Data length: {0}", data.Length); -
follesoe created this gist
May 18, 2011 .There are no files selected for viewing
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 charactersOriginal file line number Diff line number Diff line change @@ -0,0 +1,55 @@ using System; using System.IO; using System.Security.Cryptography; using System.Text; namespace CryptoTest { class Program { static void Main(string[] args) { string text = "Hello World"; byte[] data = Encoding.UTF8.GetBytes(text); byte[] encrypted; byte[] decrypted; var encryptor = GetEncryptor(); using (var ms = new MemoryStream()) using (var encrypt = new CryptoStream(ms, encryptor.CreateEncryptor(), CryptoStreamMode.Write)) { encrypt.Write(data, 0, data.Length); encrypt.FlushFinalBlock(); encrypted = ms.ToArray(); } using (var ms = new MemoryStream(encrypted)) using (var decrypt = new CryptoStream(ms, encryptor.CreateDecryptor(), CryptoStreamMode.Read)) { decrypted = new byte[ms.Length]; decrypt.Read(decrypted, 0, decrypted.Length); } Console.WriteLine("Data length: {0}", data.Length); Console.WriteLine("Encrypted length: {0}", encrypted.Length); Console.WriteLine("Decrypted length: {0}", decrypted.Length); Console.WriteLine(); Console.WriteLine("Original content: -{0}-", text); Console.WriteLine("Decrypted content: -{0}-", Encoding.UTF8.GetString(decrypted)); Console.ReadLine(); } private static AesManaged GetEncryptor() { string key = "this is my super secret key bojah!"; byte[] keyBytes = Encoding.UTF8.GetBytes(key); var rfc = new Rfc2898DeriveBytes(key, keyBytes, 1000); var encryptor = new AesManaged(); encryptor.Key = rfc.GetBytes(16); encryptor.IV = rfc.GetBytes(16); return encryptor; } } }