static void Demo3() { // SUMMARY: encrypt and decrypt object using a memory stream // create and populate a serializable demo object DemoObject DemoObject = new DemoObject() { SomeName = "Test Name", SomeInteger = 99999 }; DemoObject.SomeDictionary.Add("Test1", 1); DemoObject.SomeDictionary.Add("Test2", 2); DemoObject.SomeDictionary.Add("Test3", 3); // empty salt will be generated by encryption string SaltText; // need to assign a static password provider implemntation Aes256EncryptionProvider.PasswordProvider = new AppSettingPasswordProvider(); // create a stream to hold the cipher generated by encryption MemoryStream TargetCipherStream = new MemoryStream(); // encrypt the demo object into the stream. return the generated salt. Aes256EncryptionProvider.EncryptObject(TargetCipherStream, DemoObject, out SaltText); // convert the cipher to a string (not really needed, but good for visuaization string CipherText = Convert.ToBase64String(TargetCipherStream.ToArray()); // create a new stream and load the cipher MemoryStream SourceCipherStream = new MemoryStream(Convert.FromBase64String(CipherText)); // decrypt the stream to an object and cast as a demo object // we'll compare the serializations of original and decrypted demo objects, below DemoObject DecryptedDemoObject = (DemoObject)Aes256EncryptionProvider.DecryptObject(SourceCipherStream, SaltText); Console.WriteLine("DEMO #3"); Console.WriteLine("======================="); Console.WriteLine("\nDemoObject: {0}", new JavaScriptSerializer().Serialize(DemoObject)); Console.WriteLine("\nSalt: {0}", SaltText); Console.WriteLine("\nCipherText: {0}", CipherText); Console.WriteLine("\nDemoObject2: {0}", new JavaScriptSerializer().Serialize(DecryptedDemoObject)); Console.WriteLine(); }
static void Demo4() { // SUMMARY: encrypt and decrypt object to file // create and populate a serializable demo object DemoObject DemoObject = new DemoObject() { SomeName = "Test Name", SomeInteger = 99999 }; DemoObject.SomeDictionary.Add("Test1", 1); DemoObject.SomeDictionary.Add("Test2", 2); DemoObject.SomeDictionary.Add("Test3", 3); // set the file path where the encrypted binary data will be stored string FilePath = @"C:\Temporary\cipher.bin"; // empty salt will be generated by encryption string SaltText; // need to assign a static password provider implemntation Aes256EncryptionProvider.PasswordProvider = new AppSettingPasswordProvider(); // create the file stream FileStream TargetCipherStream = new FileStream(FilePath, FileMode.Create); // encrypt the demo object into the file stream. return the generated salt. Aes256EncryptionProvider.EncryptObject(TargetCipherStream, DemoObject, out SaltText); // create another file stream (same path) as the source of the cipher FileStream SourceCipherStream = new FileStream(FilePath, FileMode.Open); // decrypt the stream to an object and cast as a demo object // we'll compare the serializations of the original and decrypted demo objects, below DemoObject DecryptedDemoObject = (DemoObject)Aes256EncryptionProvider.DecryptObject(SourceCipherStream, SaltText); Console.WriteLine("DEMO #4"); Console.WriteLine("======================="); Console.WriteLine("\nDemoObject: {0}", new JavaScriptSerializer().Serialize(DemoObject)); Console.WriteLine("\nSalt: {0}", SaltText); Console.WriteLine("\nCipherText: Look in the file \"{0}\"", FilePath); Console.WriteLine("\nDemoObject2: {0}", new JavaScriptSerializer().Serialize(DecryptedDemoObject)); Console.WriteLine(); }