Wednesday, October 9, 2024

Programming Scripting Automation: GenKey V.2.0.0: Encrypted Password/Passphrase Generator and Manager

GenKey illustration.

Creating a stable and reliable program is far from simple. It requires many hours of testing and refining to ensure everything runs as expected. This program is my personal project, designed to help me store new credentials easily using GnuPG encryption, which I've been using for years. I understand that, as time goes on, unknown bugs may surface that I'm unaware of at the moment, but this program will continue to be developed and improved.

Hello everyone! Good morning. After a few days of working on the code for GenKey, I’ve finally completed my project to create the "Encrypted Password/Passphrase Generator and Manager"! This program helps users manage their logins from different sites and encrypts them into a file called passwords.txt.asc. Yes, they are encrypted using GnuPG standards for strong protection, based on a private/public key pair. Let's get started, and I’ll show you how I made it!


Program Design Base On MVC Standard For Easy Code Management

What is MVC design? 

The Model-View-Controller (MVC) design pattern is a software architectural pattern that separates an application into three main logical components: the Model, the View, and the Controller. It is widely used for organizing the code in web applications, GUIs, and software systems, providing a structure for managing code complexity.
GenKey Program.

The MVC pattern is a standard approach to program design.

Design blueprint.


GnuPG Encryption

This program employs GnuPG encryption to securely store user login credentials. As a result, users can access, read, write, and edit their credentials from various devices, including smartphones with Termux installed.

Encryption illustration.

What is GnuPG?

GnuPG, or GNU Privacy Guard, is an open-source implementation of the OpenPGP standard, designed for encrypting and signing data. It provides a robust framework for secure communication and data storage.

 

View or Interface and Why We Love CLI Base?

GenKey, starting from version 1.0.0, is a simple program consisting of a single file containing all its code, designed for flexibility across Windows, Linux, and smartphones (Termux). I decided to expand the design and add additional encryption features.

The standard CLI (Command Line Interface) design for people like me who prefer simplicity in programming. Without the distraction of elaborate GUI designs, we can focus on the code to create simple yet powerful programs. Flexibility is key; I want my program to run seamlessly across various devices and operating systems without the need to learn different programming languages, such as for Android. - myportfolioreview13.blogspot.com

Simple interface (CLI) display views.py.

It’s time to dive into the program design and code section. Let’s get started!


Logging Error and Debug Messages

To effectively manage errors and debug messages in my program, I need to implement logging functionality. In the file __init__.py, I set up standard Python logging to initialize the configuration for logging across my entire MVC program and its modules. This ensures that I can easily track and manage error messages throughout the application.

Logging function at __init__.py.

What Is the __init__.py File?

The __init__.py file is a special Python file that serves as an initializer for a Python package. When Python imports a package, it executes the code in __init__.py, allowing you to set up the package's namespace and define what is included when the package is imported. This file can also be used to initialize package-level variables, execute package-level code, or import submodules, making it an essential part of Python package development.


Input Validation

Input validation is a crucial component of my program that ensures the integrity and accuracy of user-provided data. I employ a comprehensive set of validation checks to handle potential errors and prevent misleading input. By implementing these validations, I can effectively manage situations where users may enter incorrect or unexpected data, thereby enhancing the overall reliability of the program.

The storage (init data) is integrated with validation logic.

The validation logic is encapsulated in an external module as part of my MVC (Model-View-Controller) design. This modular approach allows me to isolate validation functionality, making it easier to address and modify in the future if I discover any issues or if new validation requirements arise. This separation of concerns not only simplifies maintenance but also ensures that my code remains organized and adaptable to future changes.

validation.py.

With robust input validation in place, I can confidently handle user input and ensure that the program operates smoothly without encountering errors caused by invalid data.


Data Models

Data models serve as the foundational structures for managing various configurations, file handling, and database interactions within my program. These models encapsulate all essential data definitions and settings, allowing for streamlined management and easy access.

Snippet of models.py.

Whenever I need to modify default configurations or settings in my program, I can simply refer to the models.py file. This centralized approach not only simplifies the process of updating configurations but also ensures consistency throughout the application. Additionally, the data models facilitate efficient file handling and database operations, making it easier to manage data-related tasks.

Snippet of models.py.

By keeping all configuration and data management logic within the models.py file, I maintain a clean and organized codebase that is easy to navigate and update as needed. This modular design allows for greater flexibility and scalability in my program, ensuring that I can adapt to changing requirements without unnecessary complexity.


Controller Flow of Data

The controller serves as the backbone of data management within the program. It is responsible for implementing internal validation of data flows and handling user input through customized prompts. Essentially, it encompasses the most logical methods for managing how data moves through the application. This structured approach ensures that data is consistently validated and processed, maintaining the integrity and reliability of the overall system.

Snippet of custom input prompt.

Snippet of the password generator within the controller.

The GnuPG Encryption

GnuPG encryption is a robust encryption method that I use to secure all my credentials, whether stored on my smartphone, notebook, or external storage. Based on my secret key, I can import my private key on various platforms, including Virtual Machines, smartphones (with Termux installed), PCs, and notebooks. As long as I remember my secret key, I can easily decrypt all my credentials across any device.

Snippet of encryption method.

Before users can utilize encryption, they must generate a private/public key pair, which will be used to encrypt and decrypt the credentials file. By default, the file is named passwords.txt, and when encrypted, the .asc extension will be automatically added, resulting in passwords.txt.asc.

Although the program requires GnuPG to be installed for encryption, it's optional. Some users may choose not to encrypt with GnuPG after generating a password or passphrase. Instead, they can use a different password manager to store their credentials. All they need to do is open the passwords.txt file and copy the generated credentials into their preferred password manager.
 
Snippet of decryption method.

I placed the encryption functionality in an external module (whether an external file or whatever you prefer to call it) because it’s easier to manage. If a bug arises in the future, I won’t have to worry about the complexity of the entire program. This is why learning MVC (Model-View-Controller) is so valuable for programmers, it helps keep code organized and maintainable.
 
 

Running The Program!

This is the moment I’ve been eagerly waiting for and also worried about because of potential bugs! I can write a program even within a tight deadline, but why did this particular program take me about a week to complete? The answer is: bugs. When you rush to create a program in a shorter time, it’s more likely to have bugs or malformed functionality when it's used. Taking the time to properly test and debug ensures a more reliable and robust result.

Interface of GenKey.

The generated password/passphrase is automatically copied to the clipboard for convenience, allowing you to paste it directly into the desired site. (Note: For added security, remember to routinely clear your computer's temporary files and cache.)

Generating passphrase with GenKey.

Yes, encryption is optional! As mentioned earlier, if a user chooses not to encrypt the passwords.txt file for any reason, they are free to do so. Now, let's try encrypting the file! But first, let's explore the logic when I enter 'n' (short for NO) to skip encryption.

Skipping the encryption will result in the passwords.txt file remaining unencrypted.

The credentials in passwords.txt.

Don't you see that there is a file for the passphrase? This file contains words from articles to help generate Janis's passphrase. Users should fill the file with random words or phrases from the internet, books, blogs, etc. Don’t worry; I’ve included a warning if the file is empty. If the file does not exist, it will be automatically created, but it will be empty. Users will need to fill it with their own words. My passphrase has a total length of 1,201,104 words.

My passphrase file.

Now, let's try generating a password and encrypting the file password.txt:

I tested an invalid recipient from the GnuPG database, and the recipient validation functioned as expected. It prompts the user to re-enter the recipient's information and limits the number of attempts to three. If the user exceeds this limit, the program will notify them and terminate the input process, leaving the file password.txt unencrypted.
 
Testing encryption.

Also the not required field like phone, notes if empty it will be initialized to default "n/a" value.

And now let's see the encrypted passwords.txt.asc file:
 
The encrypted credentials after encrypted.

The credentials are now unreadable because they are encrypted. Let's try to decrypt them using the GnuPG standard from the terminal to read the credentials.


File passwords.txt.asc when decrypted.

How about adding more credentials to the passwords.txt.asc file? Since it's already encrypted, we'll need to decrypt it using the secret key. I've also implemented validation to check if the entered secret key is incorrect or if the input is left empty.


Appending credentials to password.txt.asc.

Decrypting the file passwords.txt.asc to view the recently appended credentials.

Some of the validations happen behind the scenes, and each user interaction is managed through validation processes. However, for simplicity in this post, I believe the demonstrations provided are enough to prove that this blog indeed showcases examples of my work.


End...

Creating a stable and reliable program is far from simple. It requires many hours of testing and refining to ensure everything runs as expected. This program is my personal project, designed to help me store new credentials easily using GnuPG encryption, which I've been using for years. I understand that, as time goes on, unknown bugs may surface that I'm unaware of at the moment, but this program will continue to be developed and improved.

I’m also considering creating a GUI for this program! Stay connected with my blog, and see you in the next article.

P.S:

Friendly reminder:
"They realized that they weren't looking for a set of skills and credentials; they were looking to hire a human being with the motivation and ability to learn." - Adam Grant