Пример #1
0
        public string Save(bool saveAs = false)
        {
            if (_keyChain.Password == null || _keyChain.Password == "" || saveAs)
            {
                PasswordDialog pd = new PasswordDialog();
                pd.NewPassword = true;
                if (pd.ShowDialog() == DialogResult.OK)
                {
                    _keyChain.Password = pd.Password;
                }
                else
                {
                    return "";
                }
            }
            string exe = Assembly.GetExecutingAssembly().Location;
            string basename = "";
            string filePath = "";

            MemoryStream ms = new MemoryStream();
            DataContractSerializer serializer = new DataContractSerializer(typeof(KeyChain));
            serializer.WriteObject(ms, _keyChain);

            if (PreferencesMgr.Preference.SaveToCloud)
            {
                if (_keyChain.Filename == "" || _keyChain.Filename == KeyChain.DefaultFilename || saveAs)
                {
                    FilenameDialog fd = new FilenameDialog();
                    if (fd.ShowDialog() == DialogResult.OK)
                    {
                        _keyChain.Filename = fd.Filename;
                    }
                    else
                    {
                        return "";
                    }
                }
                basename = DropboxMgr.WriteToCloud(_keyChain.Filename, ms.ToArray());
                basename = basename.Substring(basename.LastIndexOf("/") + 1);
                filePath = Path.GetDirectoryName(exe) + "\\" + basename;
            }
            else
            {
                if (_keyChain.Filename == "" || _keyChain.Filename == KeyChain.DefaultFilename || saveAs)
                {
                    SaveFileDialog d = new SaveFileDialog();

                    d.InitialDirectory = Path.GetDirectoryName(exe);
                    d.RestoreDirectory = true;
                    d.AddExtension = true;
                    d.Filter = "KCF Documents (*.kcf)|*.kcf";
                    if (d.ShowDialog() != DialogResult.OK)
                        return "";
                    _keyChain.Filename = d.FileName;
                    basename = d.FileName.Substring(d.FileName.LastIndexOf("\\") + 1);
                    filePath = d.FileName;
                }
                else
                {

                }

            }

            byte[] cipherBytes = CryptoHelper.Encrypt(ms.ToArray());
            File.WriteAllBytes(filePath, cipherBytes);
            _keyChain.Saved = true;
            return _keyChain.Filename;
        }
Пример #2
0
        public string Open(string path)
        {
            byte[] data;
            if (path.StartsWith("dropbox://"))
            {
                string url = path.Substring(path.LastIndexOf("://") + 3);
                data = DropboxMgr.ReadFileFromCloud(url);
            }
            else
            {
                OpenFileDialog d = new OpenFileDialog();
                d.Filter = "KCF Documents (*.kcf)|*.kcf";
                if (d.ShowDialog() != DialogResult.OK)
                    return "";
                data = File.ReadAllBytes(d.FileName);
            }

            DataContractSerializer serializer = new DataContractSerializer(typeof(KeyChain));
            MemoryStream ms = new MemoryStream(data);
            KeyChain keychain = (KeyChain)serializer.ReadObject(ms);
            if (keychain.Password != null && keychain.Password != "")
            {
                PasswordDialog pd = new PasswordDialog();
                pd.Filename = path;
                pd.RealPassword = keychain.Password;
                if (pd.ShowDialog() == DialogResult.Abort)
                {
                    return KeyChain.WrongPassword;
                }
            }
            _keyChain = keychain;
            _keyChain.Filename = path;
            _keyChain.Saved = true;
            int limit = PreferencesMgr.Preference.RecentFileLimit;
            if (!PreferencesMgr.Preference.RecentFiles.Contains(path))
            {
                if (limit > PreferencesMgr.Preference.RecentFiles.Count)
                {
                    PreferencesMgr.Preference.RecentFiles.Add(path);
                }
                else
                {
                    PreferencesMgr.Preference.RecentFiles.Add(path);
                    PreferencesMgr.Preference.RecentFiles.RemoveAt(0);
                }
            }
            PreferencesMgr.SaveFile();
            return path;
        }