示例#1
0
        public void PopulateDatabaseFromStream(PwDatabase db, Stream s, IStatusLogger slLogger)
        {
            #if !EXCLUDE_KEYTRANSFORM
            var importer = new Com.Keepassdroid.Database.Load.ImporterV3();

            var hashingStream = new HashingStreamEx(s, false, new SHA256Managed());

            _metaStreams = new List<PwEntryV3>();

            string password = "";//no need to distinguish between null and "" because empty passwords are invalid (and null is not allowed)
            KcpPassword passwordKey = (KcpPassword)db.MasterKey.GetUserKey(typeof(KcpPassword));
            if (passwordKey != null)
            {
                password = passwordKey.Password.ReadString();
            }

            KcpKeyFile passwordKeyfile = (KcpKeyFile)db.MasterKey.GetUserKey(typeof(KcpKeyFile));
            MemoryStream keyfileStream = null;
            if (passwordKeyfile != null)
            {
                keyfileStream = new MemoryStream(passwordKeyfile.RawFileData.ReadData());
            }

            try
            {
                var dbv3 = importer.OpenDatabase(hashingStream, password, keyfileStream);

                db.Name = dbv3.Name;
                db.KeyEncryptionRounds = (ulong) dbv3.NumKeyEncRounds;
                db.RootGroup = ConvertGroup(dbv3.RootGroup);
                if (dbv3.Algorithm == PwEncryptionAlgorithm.Rjindal)
                {
                    db.DataCipherUuid = StandardAesEngine.AesUuid;
                }
                else
                {
                    db.DataCipherUuid = new PwUuid(TwofishCipher.TwofishCipherEngine.TwofishCipherUuidBytes);
                }

            }
            catch (Java.IO.FileNotFoundException e)
            {
                throw new FileNotFoundException(
                    e.Message, e);
            }
            catch (Java.Lang.Exception e)
            {
                throw new Exception(e.LocalizedMessage ??
                e.Message ??
                e.GetType().Name, e);
            }

            HashOfLastStream = hashingStream.Hash;
            if (HashOfLastStream == null)
                throw new Exception("hashing didn't work"); //todo remove
            #else
            throw new Exception("Kdb is excluded with Key transform!");
            #endif
        }
示例#2
0
        public void PopulateDatabaseFromStream(PwDatabase db, Stream s, IStatusLogger slLogger)
        {
                        #if !EXCLUDE_KEYTRANSFORM
            var importer = new Com.Keepassdroid.Database.Load.ImporterV3();

            var hashingStream = new HashingStreamEx(s, false, new SHA256Managed());

            _metaStreams = new List <PwEntryV3>();

            string      password    = "";    //no need to distinguish between null and "" because empty passwords are invalid (and null is not allowed)
            KcpPassword passwordKey = (KcpPassword)db.MasterKey.GetUserKey(typeof(KcpPassword));
            if (passwordKey != null)
            {
                password = passwordKey.Password.ReadString();
            }

            KcpKeyFile   passwordKeyfile = (KcpKeyFile)db.MasterKey.GetUserKey(typeof(KcpKeyFile));
            MemoryStream keyfileStream   = null;
            if (passwordKeyfile != null)
            {
                keyfileStream = new MemoryStream(passwordKeyfile.RawFileData.ReadData());
            }


            try
            {
                var dbv3 = importer.OpenDatabase(hashingStream, password, keyfileStream);

                db.Name          = dbv3.Name;
                db.KdfParameters = (new AesKdf()).GetDefaultParameters();
                db.KdfParameters.SetUInt64(AesKdf.ParamRounds, (ulong)dbv3.NumKeyEncRounds);


                db.RootGroup = ConvertGroup(dbv3.RootGroup);
                if (dbv3.Algorithm == PwEncryptionAlgorithm.Rjindal)
                {
                    db.DataCipherUuid = StandardAesEngine.AesUuid;
                }
                else
                {
                    db.DataCipherUuid = new PwUuid(TwofishCipher.TwofishCipherEngine.TwofishCipherUuidBytes);
                }
            }
            catch (Java.IO.FileNotFoundException e)
            {
                throw new FileNotFoundException(
                          e.Message, e);
            }
            catch (Java.Lang.Exception e)
            {
                if (e.Message == "Invalid key!")
                {
                    throw new InvalidCompositeKeyException();
                }
                throw new Exception(e.LocalizedMessage ??
                                    e.Message ??
                                    e.GetType().Name, e);
            }

            HashOfLastStream = hashingStream.Hash;
            if (HashOfLastStream == null)
            {
                throw new Exception("hashing didn't work");                 //todo remove
            }
#else
            throw new Exception("Kdb is excluded with Key transform!");
#endif
        }