public void TestFileMegabyte() { var testEncryptionHandler = new AesCryptoManager(); var iv = new byte[] { 1, 2, 3, 4, 5, 6, 7, 8, 1, 2, 3, 4, 5, 6, 7, 8 }; var key = new byte[32]; AesCryptoManager.FillWithSecureValues(key); testEncryptionHandler.EncryptFileBytes(_assetsFolder + "TestFile.txt", _assetsFolder + "EncryptedTestFile.txt", key, iv); testEncryptionHandler.DecryptFileBytes(_assetsFolder + "EncryptedTestFile.txt", _assetsFolder + "DecryptedTestFile.txt", key, iv); using (var unencryptedFileReader = new BinaryReader(File.OpenRead(_assetsFolder + "TestFile.txt"))) using (var decryptedFileReader = new BinaryReader(File.OpenRead(_assetsFolder + "DecryptedTestFile.txt"))) { Debug.Assert(new FileInfo(_assetsFolder + "TestFile.txt").Length == new FileInfo(_assetsFolder + "DecryptedTestFile.txt").Length); while (true) { try { Debug.Assert(unencryptedFileReader.ReadByte() == decryptedFileReader.ReadByte(), "Failed decrypting - file corrupted"); } catch (EndOfStreamException) { break; } } } }
public void TestBadFile() { var testEncryptionHandler = new AesCryptoManager(); var iv = new byte[] { 1, 2, 3, 4, 5, 6, 7, 8, 1, 2, 3, 4, 5, 6, 7, 8 }; var key = new byte[32]; AesCryptoManager.FillWithSecureValues(key); var data = new byte[1024 * 1024 * 4]; var rng = new Random(); rng.NextBytes(data); File.WriteAllBytes(_assetsFolder + "EncryptedTestFile.txt", data); void Fail() => testEncryptionHandler.DecryptFileBytes(_assetsFolder + "EncryptedTestFile.txt", _assetsFolder + "DecryptedTestFile.txt", key, iv); Assert.Throws <CryptographicException>((Action)Fail); }
public void TestBadKey() { var testEncryptionHandler = new AesCryptoManager(); var iv = new byte[] { 1, 2, 3, 4, 5, 6, 7, 8, 1, 2, 3, 4, 5, 6, 7, 8 }; var key = new byte[32]; var badKey = new byte[32]; AesCryptoManager.FillWithSecureValues(key); AesCryptoManager.FillWithSecureValues(badKey); if (key.SequenceEqual(badKey)) { throw new ExternalException("What the $@#%"); } testEncryptionHandler.EncryptFileBytes(_assetsFolder + "testFile.txt", _assetsFolder + "EncryptedTestFile.txt", key, iv); void Fail() => testEncryptionHandler.DecryptFileBytes(_assetsFolder + "EncryptedTestFile.txt", _assetsFolder + "DecryptedTestFile.txt", badKey, iv); Assert.Throws <CryptographicException>((Action)Fail); }