Java

 

 

 

Introduction

This project demonstrates how to create a simple login and registration system using Java. It covers the basics of implementing a basic authentication mechanism, allowing users to register their details and log in securely. The program stores user information in memory and uses password verification to ensure secure access.

Objective

The objective of this project is to create a simple, text-based login and registration system that stores user credentials securely. The system will prompt users for their username and password during login, and it will ensure that the provided password matches the stored password after encryption. Additionally, users will be able to register new accounts, and the system will maintain a collection of registered users.

Code Implementation


import java.util.HashMap;
import java.util.Scanner;
import java.security.MessageDigest;
import java.security.NoSuchAlgorithmException;

public class BasicAuthSystem {
// HashMap to store registered users (username -> password)
private static HashMap<String, String> users = new HashMap<>();
private static Scanner scanner = new Scanner(System.in);

public static void main(String[] args) {
while (true) {
System.out.println(“Welcome to the Basic Authentication System”);
System.out.println(“1. Register”);
System.out.println(“2. Login”);
System.out.println(“3. Exit”);
System.out.print(“Choose an option: “);
int choice = scanner.nextInt();
scanner.nextLine(); // Consume newline

switch (choice) {
case 1:
register();
break;
case 2:
login();
break;
case 3:
System.out.println(“Exiting… Goodbye!”);
return;
default:
System.out.println(“Invalid choice. Please try again.”);
}
}
}

// Method for registering a new user
private static void register() {
System.out.print(“Enter a username: “);
String username = scanner.nextLine();

// Check if username already exists
if (users.containsKey(username)) {
System.out.println(“Username already exists. Please choose a different one.”);
return;
}

System.out.print(“Enter a password: “);
String password = scanner.nextLine();

// Encrypt the password before storing
String encryptedPassword = encryptPassword(password);

// Store the username and encrypted password
users.put(username, encryptedPassword);

System.out.println(“Registration successful!”);
}

// Method for logging in
private static void login() {
System.out.print(“Enter your username: “);
String username = scanner.nextLine();

// Check if username exists
if (!users.containsKey(username)) {
System.out.println(“Username not found. Please register first.”);
return;
}

System.out.print(“Enter your password: “);
String password = scanner.nextLine();

// Encrypt the entered password and compare with stored password
String encryptedPassword = encryptPassword(password);

if (users.get(username).equals(encryptedPassword)) {
System.out.println(“Login successful!”);
} else {
System.out.println(“Incorrect password. Please try again.”);
}
}

// Method to encrypt password using SHA-256
private static String encryptPassword(String password) {
try {
MessageDigest md = MessageDigest.getInstance(“SHA-256”);
byte[] hash = md.digest(password.getBytes());
StringBuilder hexString = new StringBuilder();

for (byte b : hash) {
hexString.append(String.format(“%02x”, b));
}

return hexString.toString();
} catch (NoSuchAlgorithmException e) {
throw new RuntimeException(“Error while encrypting password”, e);
}
}
}

Explanation of the Program Structure

The program is structured as a simple console application with the following key components:

  • users HashMap: A HashMap is used to store registered users where the key is the username, and the value is the encrypted password.
  • register() method: This method allows new users to register by entering a username and password. The password is encrypted using SHA-256 before being stored in the HashMap.
  • login() method: The login method checks if the entered username exists. If it does, the entered password is encrypted and compared with the stored password to verify the user’s identity.
  • encryptPassword() method: This method takes a plain text password and returns its encrypted version using the SHA-256 hashing algorithm.

How to Run the Program

To run the program, follow these steps:

  1. Ensure you have Java installed on your machine. You can download it from the official Java website.
  2. Save the code in a file named BasicAuthSystem.java.
  3. Open your terminal (command prompt) and navigate to the directory where the file is saved.
  4. Compile the program using the command: javac BasicAuthSystem.java
  5. Run the program using the command: java BasicAuthSystem

The program will prompt you to either register or log in. You can exit by selecting the option to quit.

 

© 2025 Learn Programming. All rights reserved.

 

By Aditya Bhuyan

I work as a cloud specialist. In addition to being an architect and SRE specialist, I work as a cloud engineer and developer. I have assisted my clients in converting their antiquated programmes into contemporary microservices that operate on various cloud computing platforms such as AWS, GCP, Azure, or VMware Tanzu, as well as orchestration systems such as Docker Swarm or Kubernetes. For over twenty years, I have been employed in the IT sector as a Java developer, J2EE architect, scrum master, and instructor. I write about Cloud Native and Cloud often. Bangalore, India is where my family and I call home. I maintain my physical and mental fitness by doing a lot of yoga and meditation.

Leave a Reply

Your email address will not be published. Required fields are marked *

error

Enjoy this blog? Please spread the word :)