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:

Java Program to Check whether Graph is Biconnected
Java Program to Implement Selection Sort
Một số từ khóa trong Java
Java Program to Perform Quick Sort on Large Number of Elements
Spring Boot - Tracing Micro Service Logs
Hướng dẫn Java Design Pattern – Proxy
“Stream has already been operated upon or closed” Exception in Java
Java Program to Implement Singly Linked List
Spring Data JPA Delete and Relationships
Recommended Package Structure of a Spring Boot Project
Guide to Dynamic Tests in Junit 5
HTTP Authentification and CGI/Servlet
A Guide to Java 9 Modularity
Count Occurrences of a Char in a String
Hướng dẫn Java Design Pattern – Command
Java Program to Check Cycle in a Graph using Graph traversal
Java Program to Search Number Using Divide and Conquer with the Aid of Fibonacci Numbers
Base64 encoding và decoding trong Java 8
Rest Web service: Filter và Interceptor với Jersey 2.x (P2)
The Order of Tests in JUnit
Java Program to Perform Searching Based on Locality of Reference
Spring Boot Application as a Service
Java Program to Find k Numbers Closest to Median of S, Where S is a Set of n Numbers
Java Program to Check Whether an Undirected Graph Contains a Eulerian Cycle
Java Program to Implement Red Black Tree
Một số nguyên tắc, định luật trong lập trình
Java Program to Implement ConcurrentHashMap API
Hướng dẫn Java Design Pattern – Intercepting Filter
Partition a List in Java
Java Program to Find the Shortest Path from Source Vertex to All Other Vertices in Linear Time
Java Program to Compute DFT Coefficients Directly
Java Program to Implement Kosaraju Algorithm