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:

SOAP Web service: Upload và Download file sử dụng MTOM trong JAX-WS
Apache Commons Collections BidiMap
Luồng Daemon (Daemon Thread) trong Java
Java Program to Implement Hash Tables
Java Program to Implement Pollard Rho Algorithm
Spring Data MongoDB – Indexes, Annotations and Converters
Java Program to Find SSSP (Single Source Shortest Path) in DAG (Directed Acyclic Graphs)
Consuming RESTful Web Services
Java Program to Search Number Using Divide and Conquer with the Aid of Fibonacci Numbers
Thao tác với tập tin và thư mục trong Java
Spring Boot - Quick Start
Java Program to Implement Adjacency List
Spring WebClient and OAuth2 Support
How to Use if/else Logic in Java 8 Streams
Java – Random Long, Float, Integer and Double
Model, ModelMap, and ModelAndView in Spring MVC
Use Liquibase to Safely Evolve Your Database Schema
Java Program to Represent Graph Using Adjacency Matrix
Java Program to Compute Discrete Fourier Transform Using Naive Approach
Java Program to Implement Binary Search Tree
Java Program to Perform Inorder Non-Recursive Traversal of a Given Binary Tree
Java Program to Permute All Letters of an Input String
Lập trình đa luồng trong Java (Java Multi-threading)
Introduction to Spring Cloud Rest Client with Netflix Ribbon
A Guide to HashSet in Java
How to Change the Default Port in Spring Boot
Introduction to Spring Boot CLI
Java Program to Implement Queue using Linked List
A Guide to EnumMap
So sánh HashSet, LinkedHashSet và TreeSet trong Java
Từ khóa this và super trong Java
Chuyển đổi Array sang ArrayList và ngược lại