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:
- Ensure you have Java installed on your machine. You can download it from the official Java website.
- Save the code in a file named
BasicAuthSystem.java
. - Open your terminal (command prompt) and navigate to the directory where the file is saved.
- Compile the program using the command:
javac BasicAuthSystem.java
- 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.