public void TestDifferentDataLenghts() { CryptoFormat cff = CryptoFormatProviderFactory.INSTANCE.GetCryptoFormat(); String file = @"D:\test.txt"; byte[] pw = Util.GetBytes("password"); String data = "this is my base test data "; for (int i = 0; i < 10; i++) { byte[] salt; byte[] key = AESUtil.CalculateSaltedHash(pw, out salt); if (cff.Version() == 0) { // if "defective" old version is used, the key is passed in directly key = pw; } cff.WriteCompatibleFile(file, Util.GetBytes(data), key, salt); bool ok; byte[] decrypted = cff.DecryptFile(file, pw, out ok); Assert.NotNull(decrypted); Assert.IsTrue(data.Equals(Util.FromBytes(decrypted))); Assert.IsTrue(ok); data = data + (char)('a' + i); } }
public static void SaveAsEncryptedFile(String _fileName, byte[] _data, byte[] _password) { CryptoFormat ccf = CryptoFormatProviderFactory.INSTANCE.GetCryptoFormat(); byte[] salt; byte[] key = AESUtil.CalculateSaltedHash(_password, out salt); ccf.WriteCompatibleFile(_fileName, _data, key, salt); }
public void basicSaltTest() { Random r = new Random(); for (int i = 0; i < 1000; i++) { byte[] salt = null; byte[] key = new byte[32]; r.NextBytes(key); byte[] saltedHash = AESUtil.CalculateSaltedHash(key, out salt); byte[] saltedHashCompare = AESUtil.CalculateSaltedHash(key, salt); Assert.IsTrue(Util.ArraysAreEqual(saltedHash, saltedHashCompare)); } }
public static void CopyAndEncrypt(String _inputFile, String _toFile, byte[] _password) { FileStream input = File.OpenRead(_inputFile); MemoryStream membuf = new MemoryStream(); int b = input.ReadByte(); while (b >= 0) { membuf.WriteByte((byte)b); b = input.ReadByte(); } input.Close(); byte[] salt; byte[] key = AESUtil.CalculateSaltedHash(_password, out salt); CryptoFormat ccf = CryptoFormatProviderFactory.INSTANCE.GetCryptoFormat(); ccf.WriteCompatibleFile(_toFile, membuf.ToArray(), key, salt); }
public void testCrypt(String _file, String _data, byte[] _pw) { CryptoFormat cff = CryptoFormatProviderFactory.INSTANCE.GetCryptoFormat(); byte[] salt; byte[] key = AESUtil.CalculateSaltedHash(_pw, out salt); if (cff.Version() == 0) { // if "defective" old version is used, the key is passed in directly key = _pw; } cff.WriteCompatibleFile(_file, Util.GetBytes(_data), key, salt); bool ok; byte[] decrypted = cff.DecryptFile(_file, _pw, out ok); Assert.NotNull(decrypted); Assert.IsTrue(_data.Equals(Util.FromBytes(decrypted)), "value doesn't match after decryption"); Assert.IsTrue(ok); }
public void TestCrpytoFiles() { CryptoFormat cff = CryptoFormatProviderFactory.INSTANCE.GetCryptoFormat(); byte[] pw = Util.GetBytes("password"); String file = @"D:\test.txt"; byte[] salt; byte[] key = AESUtil.CalculateSaltedHash(pw, out salt); if (cff.Version() == 0) { // if "defective" old version is used, the key is passed in directly key = pw; } cff.WriteCompatibleFile(file, Util.GetBytes("Hello there..."), key, salt); bool ok; byte[] data = cff.DecryptFile(file, pw, out ok); Assert.IsTrue(ok); }