Java

 

 

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:

  1. Ensure you have Java Development Kit (JDK) installed on your system. If not, download and install it from here.
  2. Create a new Java file (e.g., SimpleEncryption.java) and copy the code into it.
  3. Open a terminal or command prompt and navigate to the directory containing your file.
  4. Compile the program using the command: javac SimpleEncryption.java
  5. Run the program using the command: java SimpleEncryption
  6. Follow the on-screen prompts to input the text and key for encryption and decryption.
© 2025 Learn Programming

 

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 :)