Java Program to Implement the Monoalphabetic Cypher

This is a java program to implement monoalphabetic cypher. In cryptography, a substitution cipher is a method of encoding by which units of plaintext are replaced with ciphertext, according to a regular system; the “units” may be single letters (the most common), pairs of letters, triplets of letters, mixtures of the above, and so forth. The receiver deciphers the text by performing an inverse substitution.

Substitution ciphers can be compared with transposition ciphers. In a transposition cipher, the units of the plaintext are rearranged in a different and usually quite complex order, but the units themselves are left unchanged. By contrast, in a substitution cipher, the units of the plaintext are retained in the same sequence in the ciphertext, but the units themselves are altered.
There are a number of different types of substitution cipher. If the cipher operates on single letters, it is termed a simple substitution cipher; a cipher that operates on larger groups of letters is termed polygraphic. A monoalphabetic cipher uses fixed substitution over the entire message, whereas a polyalphabetic cipher uses a number of substitutions at different positions in the message, where a unit from the plaintext is mapped to one of several possibilities in the ciphertext and vice versa.

Here is the source code of the Java Program to Implement the Monoalphabetic Cypher. The Java program is successfully compiled and run on a Windows system. The program output is also shown below.

package com.maixuanviet.setandstring;
 
import java.util.Scanner;
 
public class MonoalphabeticCipher
{
    public static char p[]  = { 'a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'i',
            'j', 'k', 'l', 'm', 'n', 'o', 'p', 'q', 'r', 's', 't', 'u', 'v',
            'w', 'x', 'y', 'z' };
    public static char ch[] = { 'Q', 'W', 'E', 'R', 'T', 'Y', 'U', 'I', 'O',
            'P', 'A', 'S', 'D', 'F', 'G', 'H', 'J', 'K', 'L', 'Z', 'X', 'C',
            'V', 'B', 'N', 'M' };
 
    public static String doEncryption(String s)
    {
        char c[] = new char[(s.length())];
        for (int i = 0; i < s.length(); i++)
        {
            for (int j = 0; j < 26; j++)
            {
                if (p[j] == s.charAt(i))
                {
                    c[i] = ch[j];
                    break;
                }
            }
        }
        return (new String(c));
    }
 
    public static String doDecryption(String s)
    {
        char p1[] = new char[(s.length())];
        for (int i = 0; i < s.length(); i++)
        {
            for (int j = 0; j < 26; j++)
            {
                if (ch[j] == s.charAt(i))
                {
                    p1[i] = p[j];
                    break;
                }
            }
        }
        return (new String(p1));
    }
 
    public static void main(String args[])
    {
        Scanner sc = new Scanner(System.in);
        System.out.println("Enter the message: ");
        String en = doEncryption(sc.next().toLowerCase());
        System.out.println("Encrypted message: " + en);
        System.out.println("Decrypted message: " + doDecryption(en));
        sc.close();
    }
}

Output:

$ javac MonoalphabeticCipher.java
$ java MonoalphabeticCipher
 
Enter the message: 
Sanfoundry
Encrypted message: LQFYGXFRKN
Decrypted message: maixuanviet

Related posts:

Spring 5 WebClient
Chương trình Java đầu tiên
Java Program to Generate a Graph for a Given Fixed Degree Sequence
Inject Parameters into JUnit Jupiter Unit Tests
A Guide to the finalize Method in Java
Loại bỏ các phần tử trùng trong một ArrayList như thế nào?
How to use the Spring FactoryBean?
Add Multiple Items to an Java ArrayList
Java Program to Implement Maximum Length Chain of Pairs
Spring AMQP in Reactive Applications
Java Program to Construct a Random Graph by the Method of Random Edge Selection
Spring MVC Async vs Spring WebFlux
Java Program to Implement Red Black Tree
Getting Started with Stream Processing with Spring Cloud Data Flow
Java Program to Generate Random Partition out of a Given Set of Numbers or Characters
Hướng dẫn Java Design Pattern – Proxy
Java Program to Check whether Graph is a Bipartite using BFS
Immutable Map Implementations in Java
Quick Guide on Loading Initial Data with Spring Boot
Error Handling for REST with Spring
Java Program to Implement Selection Sort
Spring MVC Custom Validation
New Features in Java 13
Java Byte Array to InputStream
Java Program to Find a Good Feedback Edge Set in a Graph
Java Program to Check Whether a Directed Graph Contains a Eulerian Path
Java Program to Check if a Matrix is Invertible
Java Program to Implement the Program Used in grep/egrep/fgrep
Getting a File’s Mime Type in Java
Disable Spring Data Auto Configuration
Java Program to Implement SimpeBindings API
Java Program to Implement Hash Tables with Quadratic Probing