public void EncryptAesManaged() { string text = "You've fallen for one of the two classic blunders! The first being never get involved in a land war in Asia but only slightly lesser known: never go in against a cicelean when DEATH is on the line! HAHAHAHAHAHAHA *dies* You only think I guessed wrong! That's what's so funny! I switched glasses when your back was turned! Ha ha! You fool! You fell victim to one of the classic blunders - The most famous of which is 'never get involved in a land war in Asia' - but only slightly less well - known is this: 'Never go against a Sicilian when death is on the line!' Ha ha ha ha ha ha ha!"; // Create Aes that generates a new key and initialization vector (IV). // Same key must be used in encryption and decryption using (AesManaged aes = new AesManaged()) { // Encrypt string byte[] encrypted = Cryptographer.EncryptAsync(text, aes.Key, aes.IV).Result; // Decrypt the bytes to a string. string decrypted = Cryptographer.DecryptAsync(encrypted, aes.Key, aes.IV).Result; // Print decrypted string. It should be same as raw data Assert.AreEqual <string>(text, decrypted); } }
public async Task EncryptFilesAsync_FilesInCurrentDirectory_EncryptFilesSuccessfully() { Parallel.ForEach(Directory.EnumerateFiles(Directory.GetCurrentDirectory(), "*.encrypt"), item => { // Delete each file with the *.encrypt extension. File.Delete(item); }); IEnumerable <string> files = Directory.EnumerateFiles( Directory.GetCurrentDirectory(), "*.*"); // Count files to start int count = files.Count(); using Cryptographer cryptographer = new Cryptographer(); try { // string text = "You've fallen for one of the two classic blunders! The first being never get involved in a land war in Asia but only slightly lesser known: never go in against a cicelean when DEATH is on the line! HAHAHAHAHAHAHA *dies* You only think I guessed wrong! That's what's so funny! I switched glasses when your back was turned! Ha ha! You fool! You fell victim to one of the classic blunders - The most famous of which is 'never get involved in a land war in Asia' - but only slightly less well - known is this: 'Never go against a Sicilian when death is on the line!' Ha ha ha ha ha ha ha!"; await foreach (string fileName in Program.EncryptFilesAsync(files, cryptographer)) { byte[] encryptedData = await File.ReadAllBytesAsync(fileName); string decryptedData = await cryptographer.DecryptAsync(encryptedData); string decryptedFileName = Path.GetFileNameWithoutExtension(fileName); if (File.Exists(decryptedFileName)) { string expected = File.ReadAllText(decryptedFileName); Assert.AreEqual <string>(expected, decryptedData); } } } catch (AggregateException exception) { TestContext !.WriteLine(exception.Flatten().ToString()); throw; } Assert.AreEqual <int>(count, Directory.EnumerateFiles(Directory.GetCurrentDirectory(), "*.encrypt").Count()); }