static void CreateNewList() { if (File.Exists(GetAbsolutePath("list"))) { if (!PromptConfirm("You already have a keylist. Overwrite?")) { return; } } string password = DiscreetPrompt("Enter password"); byte[] salt = CryptoBox.GetRandomBytes(16); File.WriteAllBytes(GetAbsolutePath("salt"), salt); Console.Write("Generating key..."); byte[] key = CryptoBox.GenerateKeyFromPassword(password, salt, CryptoBox.DefaultKeySize); Key = key; Console.WriteLine("done."); PasswordList list = new PasswordList(); CryptoBox.SafeSerialize(list, key, GetAbsolutePath("list")); List = list; Console.WriteLine("Created new encrypted keylist."); AfterUnlock(); }
public string Generate() { string range = GetRangeFromCharacters(Characters); byte[] random = CryptoBox.GetRandomBytes(Length); StringBuilder sb = new StringBuilder(); for (int i = 0; i < random.Length; i++) { byte b = random[i]; b = (byte)(b * ((double)range.Length / 256d)); sb.Append(range[b]); } return(sb.ToString()); }
static void UnlockList() { try { if (!File.Exists(GetAbsolutePath("list"))) { Console.WriteLine("Keylist doesn't exist!"); return; } if (!File.Exists(GetAbsolutePath("salt"))) { Console.WriteLine("Salt doesn't exist!"); return; } string password = DiscreetPrompt("Enter password"); byte[] salt = File.ReadAllBytes(GetAbsolutePath("salt")); File.WriteAllBytes(GetAbsolutePath("salt"), salt); Console.Write("Generating key..."); byte[] key = CryptoBox.GenerateKeyFromPassword(password, salt, CryptoBox.DefaultKeySize); Key = key; Console.WriteLine("done."); Console.Write("Decrypting keylist..."); List = CryptoBox.SafeDeserialize <PasswordList>(key, GetAbsolutePath("list")); Console.WriteLine("done."); AfterUnlock(); } catch (Exception ex) { Console.WriteLine("Failed!"); Console.WriteLine(ex); } }
static void Save() { CryptoBox.SafeSerialize(List, Key, GetAbsolutePath("list")); }