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 static Stream DecryptFromStream(String _inputFile, Stream _s, byte[] _key, out bool _wasOk) { CryptoFormat ccf = CryptoFormatProviderFactory.INSTANCE.GetCryptoFormat(); byte[] data = ccf.DecryptFromStream(_inputFile, _s, _key, out _wasOk); if (!_wasOk) { return(null); } return(new MemoryStream(data)); }
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); }
public virtual IDictionary <string, NoteUpdate> GetNoteUpdatesSince(int revision) { Dictionary <string, NoteUpdate> noteUpdates = new Dictionary <string, NoteUpdate>(); if (IsValidXmlFile(manifestPath)) { // TODO: Permissions errors using (FileStream fs = new FileStream(manifestPath, FileMode.Open)) { Stream plainStream; { bool ok; plainStream = SecurityWrapper.DecryptFromStream(manifestPath, fs, myKey, out ok); if (!ok) { throw new Exception("ENCRYPTION ERROR!"); } } XmlDocument doc = new XmlDocument(); doc.Load(plainStream); string xpath = string.Format("//note[@rev > {0}]", revision.ToString()); XmlNodeList noteNodes = doc.SelectNodes(xpath); Logger.Debug("GetNoteUpdatesSince xpath returned {0} nodes", noteNodes.Count); foreach (XmlNode node in noteNodes) { string id = node.SelectSingleNode("@id").InnerText; int rev = Int32.Parse(node.SelectSingleNode("@rev").InnerText); if (noteUpdates.ContainsKey(id) == false) { // Copy the file from the server to the temp directory string revDir = GetRevisionDirPath(rev); string serverNotePath = Path.Combine(revDir, id + ".note"); //string noteTempPath = Path.Combine(tempPath, id + ".note"); // DON'T ENCRYPT HERE because we are getting the already encrypted file from the server //SecurityWrapper.CopyAndEncrypt(serverNotePath, noteTempPath, myKey); //File.Copy(serverNotePath, noteTempPath, true); // Get the title, contents, etc. string noteTitle = string.Empty; string noteXml = null; { // decrypt the note: bool ok; CryptoFormat ccf = CryptoFormatProviderFactory.INSTANCE.GetCryptoFormat(); byte[] contents = ccf.DecryptFile(serverNotePath, myKey, out ok); noteXml = Util.FromBytes(contents); // solve nasty BOM problem -__- int index = noteXml.IndexOf('<'); if (index > 0) { noteXml = noteXml.Substring(index, noteXml.Length - index); } } NoteUpdate update = new NoteUpdate(noteXml, noteTitle, id, rev); noteUpdates[id] = update; } } } } Logger.Debug("GetNoteUpdatesSince ({0}) returning: {1}", revision, noteUpdates.Count); return(noteUpdates); }