示例#1
0
        public static PasswordsModel ReadFile()
        {
            PasswordsModel retVal       = null;
            string         path         = Environment.GetFolderPath(Environment.SpecialFolder.MyDocuments);
            string         fullFileName = string.Format("{0}\\{1}", path, "sugar.data");

            try
            {
                String encryptedData;
                String json;
                using (StreamReader sr = new StreamReader(fullFileName))
                {
                    encryptedData = sr.ReadToEnd();
                    json          = CryptoHelper.DecryptStringAES(encryptedData, SharedSecret);
                }

                retVal = JsonConvert.DeserializeObject <PasswordsModel>(json);
            }
            catch (Exception e)
            {
                Console.WriteLine("The file could not be read:");
                Console.WriteLine(e.Message);
            }

            return(retVal);
        }
示例#2
0
 override public bool Execute(string[] args)
 {
     if (string.IsNullOrWhiteSpace(Passwordfile.SharedSecret))
     {
         MessageBox.Show("Password not set. Use 'Open Password' command to set password.");
     }
     else
     {
         if (args.Length > 1)
         {
             string         name = args[1];
             PasswordsModel data = Passwordfile.ReadFile();
             if (data != null)
             {
                 if (data.Passwords != null)
                 {
                     string password = data.Passwords.Where(o => o.Name.ToLower() == name.ToLower()).Select(o => o.Password).FirstOrDefault();
                     if (string.IsNullOrWhiteSpace(password))
                     {
                         MessageBox.Show("Password not found.");
                     }
                     else
                     {
                         Clipboard.SetText(password, TextDataFormat.Text);
                     }
                 }
             }
             else
             {
                 MessageBox.Show("Password not set. Use 'Open Password' command to set password.");
             }
         }
     }
     return(true); // hide command window
 }
示例#3
0
        public static bool SaveFile(PasswordsModel data)
        {
            bool retVal = false;

            string json         = JsonConvert.SerializeObject(data, new IsoDateTimeConverter());
            string path         = Environment.GetFolderPath(Environment.SpecialFolder.MyDocuments);
            string fullFileName = string.Format("{0}\\{1}", path, "sugar.data");

            string encryptedData = CryptoHelper.EncryptStringAES(json, SharedSecret);

            encryptedData.SaveAsTextFile(fullFileName);
            return(retVal);
        }
示例#4
0
        override public bool Execute(string[] args)
        {
            if (string.IsNullOrWhiteSpace(Passwordfile.SharedSecret))
            {
                MessageBox.Show("Password not set. Use 'Open Password' command to set password.");
            }
            else
            {
                if (args.Length > 2)
                {
                    PasswordModel pw = new PasswordModel();
                    pw.Name     = args[1];
                    pw.Password = args[2];
                    if (args.Length > 3)
                    {
                        pw.UserName = args[3];
                        if (args.Length > 4)
                        {
                            pw.Url = args[4];
                            if (args.Length > 5)
                            {
                                pw.Email = args[5];
                            }
                        }
                    }

                    PasswordsModel data = Passwordfile.ReadFile();
                    if (data == null)
                    {
                        data = new PasswordsModel();
                    }
                    if (data.Passwords != null)
                    {
                        data.Passwords.Add(pw);
                    }
                    Passwordfile.SaveFile(data);
                }
            }
            return(true); // hide command window
        }