private static DbPersistentData ReadDatabase( Stream stream, Headers headers, byte[] masterKey) { var decrypted = FileFormat.Decrypt( stream, headers, masterKey); if (decrypted == null) { return(null); } using (decrypted) using (var buffer = new MemoryStream()) { BufferEx.CopyStream(decrypted, buffer); return(new DbPersistentData { MasterKey = masterKey, Xml = buffer.ToArray(), Protection = CryptoSerializer .Serialize(headers), }); } }
public async Task Decrypt_should_decrypt_content() { using (var input = TestFiles.Read("IO.Demo7Pass.kdbx")) { input.Seek(222); var masterSeed = CryptographicBuffer.DecodeFromHexString( "2b4656399a5bdf9fdfe9e8705a34b6f484f9b1b940c3d7cfb7ffece3b634e0ae"); var masterKey = CryptographicBuffer.DecodeFromHexString( "87730050341ff55c46421f2f2a5f4e1e018d0443d19cacc8682f128f1874d0a4"); var encryptionIV = CryptographicBuffer.DecodeFromHexString( "f360c29e1a603a6548cfbb28da6fff50"); using (var decrypted = await FileFormat.Decrypt(input, masterKey, masterSeed, encryptionIV)) { var buffer = WindowsRuntimeBuffer.Create(32); buffer = await decrypted.ReadAsync(buffer, 32); Assert.Equal( "54347fe32f3edbccae1fc60f72c11dafd0a72487b315f9b174ed1073ed67a6e0", CryptographicBuffer.EncodeToHexString(buffer)); } } }
public async Task OpenDatabase() { var database = await OpenFileAsync(); using (var fs = await database.OpenReadAsync()) { // TODO: handle errors & display transformation progress var result = await FileFormat.Headers(fs); var headers = result.Headers; var masterKey = await _password .GetMasterKey(headers); using (var decrypted = await FileFormat .Decrypt(fs, masterKey, headers)) { // TODO: verify start bytes await FileFormat.VerifyStartBytes( decrypted, headers); // Parse content var doc = await FileFormat.ParseContent( decrypted, headers.UseGZip, headers); // TODO: verify headers integrity _cache.Cache(new CachedDatabase { Id = Id, Document = doc, Name = DisplayName, Headers = headers, }); _navigation .UriFor <GroupViewModel>() .Navigate(); _navigation.BackStack.Remove( _navigation.BackStack.Last()); } } }
public void LoadExisting(Stream existing, byte[] masterKey) { if (existing == null) { throw new ArgumentNullException("existing"); } if (masterKey == null) { throw new ArgumentNullException("masterKey"); } var reader = new BinaryReader(existing); if (!FileFormat.Sign(reader)) { throw new FormatException( "Invalid format detected"); } _version = FileFormat .ReadVersion(reader); _masterKey = masterKey; _headers = FileFormat .ReadHeaders(existing); _xmlWriter = new XmlWriter(); using (var decrypt = FileFormat.Decrypt( existing, _headers, masterKey)) { _xmlWriter.Load(decrypt); var crypto = CryptoSerializer .Create(_headers); _xmlWriter.Decrypt(crypto); } }
public async Task Database_decryption() { using (var kdbx = TestFiles.Read("IO.Demo7Pass.kdbx")) { // Headers var result = await FileFormat.Headers(kdbx); Assert.Equal(FileFormats.Supported, result.Format); var headers = result.Headers; Assert.NotNull(headers); // Master Key var password = new PasswordData { Password = "******", }; var masterKey = await password .GetMasterKey(headers); // Decrypt using (var decrypted = await FileFormat .Decrypt(kdbx, masterKey, headers)) { // Start bytes Assert.True(await FileFormat .VerifyStartBytes(decrypted, headers)); // Parse content var doc = await FileFormat.ParseContent( decrypted, headers.UseGZip, headers); Assert.True(FileFormat.VerifyHeaders(headers, doc)); } } }