예제 #1
0
        // passing
        public void AddNewPassword()
        {
            // 表单验证
            if (string.IsNullOrEmpty(TagNameAdded))
            {
                WindowToolTip = "Please choose or add a tag for the password";
                return;
            }
            if (string.IsNullOrEmpty(PasswordForm.Title))
            {
                WindowToolTip = "Please fill the title";
                return;
            }
            if (string.IsNullOrEmpty(PasswordForm.Password))
            {
                WindowToolTip = "Please enter the password!";
            }
            if (!PasswordForm.Website.StartsWith("https://"))
            {
                PasswordForm.Website = "https://" + PasswordForm.Website;
            }

            if (DbHelper.IfTagedPasswordListContain(TagList, TagNameAdded))
            {
                // 如果该 tag 在列表中已存在
                int tagId = DbHelper.GetTagId(TagNameAdded);
                // 保留明文密码
                string password          = PasswordForm.Password;
                string encryptedPassword = Encryptor.AESEncrypt(PasswordForm.Password, KeyPassword);
                PasswordForm.TagId    = tagId;
                PasswordForm.Avatar   = AvatarDictionary.GetAvatarPath(PasswordForm.Title);
                PasswordForm.Password = encryptedPassword;
                DbHelper.InsertPasswordItem(PasswordForm);
                // 处理好后将密码明文添加进TagList
                PasswordForm.Password = password;
                AddPasswordToTagList(PasswordForm, TagNameAdded);
            }
            else
            {
                // tag 不存在, 将新来的 tag 入表
                // 插入新 tag 返回 tag Id
                int tagId = DbHelper.InsertTagName(TagNameAdded);
                // 重复,待优化
                string password          = PasswordForm.Password;
                string encryptedPassword = Encryptor.AESEncrypt(PasswordForm.Password, KeyPassword);
                PasswordForm.TagId    = tagId;
                PasswordForm.Avatar   = AvatarDictionary.GetAvatarPath(PasswordForm.Title);
                PasswordForm.Password = encryptedPassword;
                DbHelper.InsertPasswordItem(PasswordForm);
                // 处理好后将密码明文添加进TagList
                PasswordForm.Password = password;
                AddPasswordToTagList(PasswordForm, TagNameAdded);
            }
            UpdateTagList();

            PasswordForm.Clean();
            Switcher.AddNewPassword();
        }
예제 #2
0
파일: FileHelper.cs 프로젝트: jaywhen/Pmer
        /// <summary>
        /// 将 *.csv 文件中的密码转换为一个Tag
        /// </summary>
        /// <param name="fileName"></param>
        /// <returns></returns>
        public static Tag ChromeCSVToTag(string filePath, string tagName, byte[] key)
        {
            var reader = new StreamReader(File.OpenRead(filePath));

            // 跳过第一行(已知第一行的内容)
            reader.ReadLine();
            // chrome csv pw file head:
            // [0]name(Title) [1]url(website) [2]username(account) [3]password
            string line;
            List <PasswordItem> passwordItems = new List <PasswordItem>();
            int tagId = DbHelper.GetMaxTagId() + 1;

            while ((line = reader.ReadLine()) != null)
            {
                string[]     lines        = line.Split(',');
                PasswordItem passwordItem = new PasswordItem
                {
                    Title    = lines[0],
                    Account  = lines[2],
                    Password = Encryptor.AESEncrypt(lines[3], key),
                    Website  = lines[1],
                    Avatar   = AvatarDictionary.GetAvatarPath(lines[0]),
                    TagId    = tagId
                };
                passwordItems.Add(passwordItem);
            }
            reader.Close();

            Tag retTag = new Tag
            {
                PasswordItems = passwordItems,
                TagId         = tagId,
                TagName       = tagName
            };

            return(retTag);
        }