A random stream class. The class is initialized using random bytes provided by the caller. The produced stream has random properties, but for the same seed always the same stream is produced, i.e. this class can be used as stream cipher.
        public async Task TestProtectedDecrypt()
        {

            var protectedString = "QwzFTMLCpNY=";
            var protectedStringBytes = Convert.FromBase64String(protectedString);

           var rando = new CryptoRandomStream(CrsAlgorithm.Salsa20, Convert.FromBase64String("6tDlwZfwES4jAQzLisWdpNdnuTYyDZfflEdbshzdgi8="), new SHA256HasherRT());
           var getByte = rando.GetRandomBytes((uint)protectedStringBytes.Length);
           byte[] pbPlain = new byte[protectedStringBytes.Length];


           for (int i = 0; i < pbPlain.Length; ++i)
               pbPlain[i] = (byte)(protectedStringBytes[i] ^ getByte[i]);

           string mypass = UTF8Encoding.UTF8.GetString(pbPlain, 0, pbPlain.Length);

           Assert.AreEqual("Password", mypass);
        }
Exemple #2
0
        public async Task Write(PwDatabase databaseData, IFile databaseFile)
        {
             kdb4File = new Kdb4File(databaseData);

            ResetHeaderBytes();

            MemoryStream memStream = new MemoryStream();
            BinaryWriter writer = new BinaryWriter(memStream);
         
            _headerWriter.WriteHeaders(writer, kdb4File);

           var header = memStream.ToArray();
           var hashOfHeader = _hasher.Hash(header);


            var outStream = new MemoryStream();
            await outStream.WriteAsync(kdb4File.pbStreamStartBytes, 0, (int)kdb4File.pbStreamStartBytes.Length);
            var configuredStream = ConfigureStream(outStream);

            var persister = new Kdb4Persister(new CryptoRandomStream(CrsAlgorithm.Salsa20, kdb4File.pbProtectedStreamKey, _hasher));
            var data = persister.Persist(databaseData.Tree, hashOfHeader);
            try
            {
                await Task.Run(() =>
                    configuredStream.Write(data, 0, data.Length));
            }
            catch(Exception e)
            {
                
            }
         

            configuredStream.Dispose();
            var compressed = outStream.ToArray();

            var keyGenerator = new KeyGenerator(
                _hasher, 
                 _keyTransformer,
                databaseData.MasterKey, 
                databaseData.MasterKey.PercentComplete);
            var aesKey = await keyGenerator.GenerateHashedKeyAsync(kdb4File.pbMasterSeed, kdb4File.pbTransformSeed, (int)databaseData.KeyEncryptionRounds);
            var encrypted = await _databaseEncryptor.Encrypt(compressed, aesKey, kdb4File.pbEncryptionIV);
            
            writer.Write(encrypted);

            var streamToWriteToFile = writer.BaseStream;
            streamToWriteToFile.Position = 0;

            var bytesToWrite = streamToWriteToFile.ToArray();


            var databaseStream = await databaseFile.OpenAsync(FileAccess.ReadAndWrite);
            databaseStream.SetLength(0);
            await databaseStream.FlushAsync();
            await databaseStream.WriteAsync(bytesToWrite, 0, bytesToWrite.Length);
            await databaseStream.FlushAsync();
            databaseStream.Dispose();

            var cryptoStream = new CryptoRandomStream(CrsAlgorithm.Salsa20, kdb4File.pbProtectedStreamKey,_hasher);
            var parser = new Kdb4Parser(cryptoStream);
            databaseData.Tree = parser.ParseAndDecode(databaseData.Tree.Document);
        }
Exemple #3
0
 public Kdb4Parser(CryptoRandomStream crypto)
 {
     _cryptoStream = crypto;
 }
 public Kdb4Persister(CryptoRandomStream stream)
 {
     this.cryptoRandomStream = stream;
 }