Introduction
In today’s digital world, encryption plays a significant role in ensuring the security of data.
Encryption is the process of converting data into a secret code to prevent unauthorized access.
This program demonstrates a simple approach to encrypt and decrypt a string using a basic encryption technique, commonly referred to as Caesar Cipher. It is an elementary encryption method that shifts the letters of the alphabet by a fixed number of positions.
Objective
The goal of this program is to:
- Implement a simple encryption and decryption algorithm.
- Show how basic cryptography works using Java.
- Allow users to encrypt and decrypt text using a shift value (key).
Java Code: Simple Encryption and Decryption
import java.util.Scanner; public class SimpleEncryption { // Method to encrypt the text public static String encrypt(String text, int key) { StringBuilder encryptedText = new StringBuilder(); for (int i = 0; i < text.length(); i++) { char c = text.charAt(i); if (Character.isLetter(c)) { char base = (Character.isUpperCase(c)) ? 'A' : 'a'; c = (char) ((c - base + key) % 26 + base); } encryptedText.append(c); } return encryptedText.toString(); } // Method to decrypt the text public static String decrypt(String text, int key) { return encrypt(text, 26 - key); // Decryption is just encryption with the inverse key } public static void main(String[] args) { Scanner scanner = new Scanner(System.in); System.out.print("Enter the text to encrypt: "); String inputText = scanner.nextLine(); System.out.print("Enter the shift key (1-25): "); int key = scanner.nextInt(); // Encrypting the text String encryptedText = encrypt(inputText, key); System.out.println("Encrypted Text: " + encryptedText); // Decrypting the text String decryptedText = decrypt(encryptedText, key); System.out.println("Decrypted Text: " + decryptedText); scanner.close(); } }
Explanation of the Program
The program consists of two main methods: encrypt
and decrypt
.
The encrypt
method performs a Caesar Cipher shift on each letter of the input text.
It checks whether each character is a letter and applies the shift accordingly. The key
determines how many positions the letters are shifted.
The decrypt
method simply reverses the encryption process by shifting the letters in the opposite direction using the inverse of the key (i.e., 26 - key
).
The program starts by prompting the user to enter a string and a key. Then it encrypts the string, prints the encrypted text, and immediately decrypts it back to the original string.
How to Run the Program
Follow these steps to run the program:
- Ensure you have Java Development Kit (JDK) installed on your system. If not, download and install it from here.
- Create a new Java file (e.g.,
SimpleEncryption.java
) and copy the code into it. - Open a terminal or command prompt and navigate to the directory containing your file.
- Compile the program using the command:
javac SimpleEncryption.java
- Run the program using the command:
java SimpleEncryption
- Follow the on-screen prompts to input the text and key for encryption and decryption.