public static void Test() { Console.Out.WriteLine("Begin Example_060."); // Create the AES Service AESService aes = new AESService(); string password = "******"; SecureRandom random = new SecureRandom(); byte[] salt = new byte[AESService.SALT_SIZE]; random.NextBytes(salt); // Create the AES Key using password and salt. aes.GenerateKey(password, salt); // Encode and Decode a string then compare to verify they are the same. string clear_text = "This is a test"; byte[] enc_bytes = aes.Encode(UTF8Encoding.UTF8.GetBytes(clear_text)); byte[] dec_bytes = aes.Decode(enc_bytes); string dec_text = UTF8Encoding.UTF8.GetString(dec_bytes); /** * Compare the original and decrypted files. */ if (Compare.SafeEquals(UTF8Encoding.UTF8.GetBytes(clear_text), UTF8Encoding.UTF8.GetBytes(dec_text))) { Console.Out.WriteLine("Original and Decrypted are the same!"); } else { Console.Out.WriteLine("Original and Decrypted are NOT the same!"); } Console.Out.WriteLine("End Example_060."); }
public static void Test() { Console.Out.WriteLine("Begin Example_030."); // Create some data to test with. Support.TestData(TESTDATA_FILE); /** * Create a 256-bit AES key. AES keys are synchronous. One key can both * encrypt and decrypt data. */ Console.Out.WriteLine("Begin Create AES Key."); AESService aes = new AESService(); aes.GenerateKey(); Console.Out.WriteLine("End Create AES Key."); /** * Use AES key to encrypt a file stream directly to another file stream. */ Console.Out.WriteLine("Begin Encrypt Data."); using (FileStream outstream = new FileStream(TESTDATA_ENC_FILE, FileMode.Create), instream = new FileStream(TESTDATA_FILE, FileMode.Open)) { aes.Encode(instream, outstream); } Console.Out.WriteLine("End Encrypt Data."); /** * Now decrypt the encrypted file using the same AES key. */ Console.Out.WriteLine("Begin Decrypt Data."); using (FileStream outstream = new FileStream(TESTDATA_DEC_FILE, FileMode.Create), instream = new FileStream(TESTDATA_ENC_FILE, FileMode.Open)) { aes.Decode(instream, outstream); } Console.Out.WriteLine("End Decrypt Data."); /** * Compare the original and decrypted files. */ string shaOriginal = DigestSHA.Sha256(new FileStream(TESTDATA_FILE, FileMode.Open)); string shaDecripted = DigestSHA.Sha256(new FileStream(TESTDATA_DEC_FILE, FileMode.Open)); if (Compare.SafeEquals(UTF8Encoding.UTF8.GetBytes(shaOriginal), UTF8Encoding.UTF8.GetBytes(shaDecripted))) { Console.Out.WriteLine("Encrypted and decrypted files are the same."); } else { Console.Out.WriteLine("Encrypted and decrypted files are NOT the same."); } Console.Out.WriteLine("End Example_030."); }
public void TestEncodeAndDecode_InputStream_OutputStream() { Console.Out.WriteLine("encode and decode stream"); byte[] saltBytes = ReadSaltBytes(); AESService instance = new AESService(); instance.GenerateKey(password, saltBytes); byte[] decData; byte[] encData; using (MemoryStream outstream = new MemoryStream()) { using (MemoryStream instream = new MemoryStream(msgBytes)) { instance.Encode(instream, outstream); } encData = outstream.ToArray(); decData = instance.Decode(encData); Boolean bCompare = Compare.SafeEquals(msgBytes, decData); Assert.IsTrue(bCompare); } using (MemoryStream outstream = new MemoryStream()) { using (MemoryStream instream = new MemoryStream(encData)) { instance.Decode(instream, outstream); } decData = outstream.ToArray(); Boolean bCompare = Compare.SafeEquals(msgBytes, decData); Assert.IsTrue(bCompare); } }
public void TestEncodeAndDecode_String() { Console.Out.WriteLine("encode and decode byte string"); byte[] saltBytes = ReadSaltBytes(); AESService instance = new AESService(); instance.GenerateKey(password, saltBytes); byte[] encData = instance.Encode(msgString); string encString = Hex.Encode(encData); byte[] decData = instance.Decode(encString); Boolean bCompare = Compare.SafeEquals(msgBytes, decData); Assert.IsTrue(bCompare); }