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:

Form Validation with AngularJS and Spring MVC
Spring Boot - Twilio
REST Pagination in Spring
Finding the Differences Between Two Lists in Java
Java Program to Check Whether a Directed Graph Contains a Eulerian Cycle
Setting a Request Timeout for a Spring REST API
Java Program to Perform Preorder Non-Recursive Traversal of a Given Binary Tree
Interface trong Java 8 – Default method và Static method
Hướng dẫn Java Design Pattern – Facade
Java Program to Perform the Sorting Using Counting Sort
Java Program to Implement the MD5 Algorithm
Hướng dẫn Java Design Pattern – Abstract Factory
Reversing a Linked List in Java
Java Program to Check whether Graph is a Bipartite using DFS
Spring Security Basic Authentication
Spring Boot: Customize the Jackson ObjectMapper
Fixing 401s with CORS Preflights and Spring Security
Java Program to Implement Gauss Seidel Method
Java Program to Generate All Subsets of a Given Set in the Lexico Graphic Order
Java Program to Implement HashTable API
Tránh lỗi ConcurrentModificationException trong Java như thế nào?
Java Program to Implement Triply Linked List
Java equals() and hashCode() Contracts
Ép kiểu trong Java (Type casting)
Remove All Occurrences of a Specific Value from a List
Chuyển đổi Array sang ArrayList và ngược lại
Java Program to Find kth Largest Element in a Sequence
Spring Boot - Internationalization
Loại bỏ các phần tử trùng trong một ArrayList như thế nào trong Java 8?
OAuth 2.0 Resource Server With Spring Security 5
Java Program to Check Multiplicability of Two Matrices
Hướng dẫn Java Design Pattern – Object Pool