Example #1
0
        static void Main(string[] args)
        {
            //Console.WriteLine(passwordManager.GetPassword());
            dict = new Dictionary <string, string>();
            Console.WriteLine("Checking for a pre-existing password record file...");

            try {
                using (System.IO.FileStream fileStream = new System.IO.FileStream("pass.bin", System.IO.FileMode.Open)) {
                    //fileStream.Lock(0, fileStream.Length);
                    Console.WriteLine("Password record file found, loading...");

                    /** Saving order
                     *
                     * Serialize
                     * Encrypt
                     * Write binary
                     */

                    // Read Binary
                    // Decrypt
                    // Deserialize
                    using (System.IO.BinaryReader br = new System.IO.BinaryReader(fileStream)) {
                        try {
                            string encryptedDictString = br.ReadString();
                            br.Close();
                            string dictString = EncryptionHelper.Decrypt(encryptedDictString, ask <string>("Enter master password: "******"Cannot write null to filestream. Why? I don't know. You just can't. Period.");
                                }
                                else if (e is JsonException)
                                {
                                    Console.WriteLine("Invalid JSON, maximum object depth allowed, or JSON is incompatible with password storage data type.");
                                }
                                else
                                {
                                    Console.WriteLine("An unexpexted error occured! If you see this message, you didn't screw" +
                                                      " up, I did. Just open up an issue at https://github.com/Catz1301/PasswordManager/issues and provide the following information:");
                                }
                                Console.WriteLine(Environment.NewLine + e.Message);
                                Console.WriteLine(Environment.NewLine + "-+-+-+-+-+-+-+-+-+-+-+-+-" + Environment.NewLine);
                                Console.WriteLine(e.StackTrace);
                                Console.WriteLine(Environment.NewLine + "Source: " + e.Source);
                                Console.WriteLine(Environment.NewLine + "Extra Data: ");
                                Console.WriteLine(e.Data);
                                Exception exception = new Exception("An error occured while deserializing JSON data. " + e.Message, e.InnerException);
                                throw exception;
                            }
                        } catch (Exception e) {
                            if (e is System.IO.IOException)
                            {
                                Console.WriteLine("An error occurred while writing to file.");
                            }
                            else if (e is System.IO.InternalBufferOverflowException)
                            {
                                Console.WriteLine("Internal buffer has overflown, maybe try saving more often?");
                            }
                            else if (e is System.IO.EndOfStreamException)
                            {
                                Console.WriteLine("Unexpectedly reached the end of stream. This may mean that the file is corrupted. Hope you have a backup or a plan!");
                            }
                            else if (e is ObjectDisposedException)
                            {
                                Console.WriteLine("Cannot write to disposed object! If you see this message, you didn't screw" +
                                                  " up, I did. Just open up an issue at https://github.com/Catz1301/PasswordManager/issues and provide the following information:");
                            }
                            else
                            {
                                Console.WriteLine("An unexpexted error occured! If you see this message, you didn't screw" +
                                                  " up, I did. Just open up an issue at https://github.com/Catz1301/PasswordManager/issues and provide the following information:");
                            }
                            Console.WriteLine(Environment.NewLine + e.Message);
                            Console.WriteLine(Environment.NewLine + "-+-+-+-+-+-+-+-+-+-+-+-+-" + Environment.NewLine);
                            Console.WriteLine(e.StackTrace);
                            Console.WriteLine(Environment.NewLine + "Source: " + e.Source);
                            Console.WriteLine(Environment.NewLine + "Extra Data: ");
                            Console.WriteLine(e.Data);
                            throw;
                        } finally {
                            br.Close();
                        }
                        //dict = (Dictionary<string, string>) bf.Deserialize(fileStream);
                    }
                }

                /*fileStream.Unlock(0, fileStream.Length);
                 * fileStream.Close();
                 * await fileStream.DisposeAsync();*/
            } catch (Exception e) {
                if (e is System.IO.FileNotFoundException)
                {
                    Console.WriteLine("No password record file found!");
                    Console.WriteLine("Creating a new one...");
                    dict = new Dictionary <string, string>();
                }
                else if (e is SerializationException)
                {
                    Console.WriteLine("Something went wrong.");
                    Console.WriteLine(e);
                }
            }

            System.Threading.Thread.Sleep(2000);
            Console.Clear();
            Console.WriteLine(helpText);
            bool quit = false;

            while (!quit)
            {
                ConsoleKeyInfo   keyInfo = Console.ReadKey(true);
                ConsoleKey       key     = keyInfo.Key;
                ConsoleModifiers modKeys = keyInfo.Modifiers;
                if (modKeys == ConsoleModifiers.Control)
                {
                    if (key == ConsoleKey.S)
                    {
                        if (saveConfirmation())
                        {
                            save();
                        }
                    }
                    else if (key == ConsoleKey.Q)
                    {
                        quit = singleCharConfirm("Are you sure?");
                    }
                    else
                    {
                        continue;
                    }
                }
                else
                {
                    if (key == ConsoleKey.N)
                    {
                        createEntry();
                    }
                    else if (key == ConsoleKey.R)
                    {
                        retrieveEntry();
                    }
                    else if (key == ConsoleKey.I)
                    {
                        printInfo();
                    }
                    else
                    {
                        Console.WriteLine("Invalid option. " + helpText);
                    }
                }
            }
        }
Example #2
0
        static void save()
        {
            using (System.IO.FileStream fileStream = new System.IO.FileStream("pass.bin", System.IO.FileMode.Create)) {
                string dictString = JsonSerializer.Serialize(dict, dict.GetType());

                string encryptedDictString = EncryptionHelper.Encrypt(dictString, ask <string>("Enter password (If this is the first save, enter anything, but write it down!): "));
                try {
                    //bf.Serialize(fileStream, dict);
                    using (System.IO.BinaryWriter bw = new System.IO.BinaryWriter(fileStream)) {
                        try {
                            bw.Write(encryptedDictString);
                            bw.Close();
                            Console.WriteLine("Save successful!");
                            System.Threading.Thread.Sleep(1000);
                            Console.Clear();
                            Console.WriteLine(helpText);
                        } catch (Exception e) {
                            if (e is System.IO.IOException)
                            {
                                Console.WriteLine("An error occurred while writing to file.");
                            }
                            else if (e is System.IO.InternalBufferOverflowException)
                            {
                                Console.WriteLine("Internal buffer has overflown, maybe try saving more often?");
                            }
                            else if (e is ArgumentNullException)
                            {
                                Console.WriteLine("Cannot write null to filestream. Why? I don't know. You just can't. Period.");
                            }
                            else if (e is ObjectDisposedException)
                            {
                                Console.WriteLine("Cannot write to disposed object! If you see this message, you didn't screw" +
                                                  " up, I did. Just open up an issue at https://github.com/Catz1301/PasswordManager/issues and provide the following information:");
                            }
                            else
                            {
                                Console.WriteLine("An unexpexted error occured! If you see this message, you didn't screw" +
                                                  " up, I did. Just open up an issue at https://github.com/Catz1301/PasswordManager/issues and provide the following information:");
                            }
                            Console.WriteLine(Environment.NewLine + e.Message);
                            Console.WriteLine(Environment.NewLine + "-+-+-+-+-+-+-+-+-+-+-+-+-" + Environment.NewLine);
                            Console.WriteLine(e.StackTrace);
                            Console.WriteLine(Environment.NewLine + "Source: " + e.Source);
                            Console.WriteLine(Environment.NewLine + "Extra Data: ");
                            Console.WriteLine(e.Data);
                            throw;
                        } finally {
                            bw.Close();
                        }
                    }
                } catch (SerializationException e) {
                    Console.BackgroundColor = ConsoleColor.Red;
                    Console.ForegroundColor = ConsoleColor.Black;
                    Console.Clear();
                    Console.WriteLine("Failed to serialize! Reason: " + e.Message);
                    throw;
                } finally {
                    fileStream.Close();
                }
            }
        }