Java Program to Implement the Monoalphabetic Cipher

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.

  1.  
  2. package com.sanfoundry.setandstring;
  3.  
  4. import java.util.Scanner;
  5.  
  6. public class MonoalphabeticCipher
  7. {
  8.     public static char p[]  = { 'a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'i',
  9.             'j', 'k', 'l', 'm', 'n', 'o', 'p', 'q', 'r', 's', 't', 'u', 'v',
  10.             'w', 'x', 'y', 'z' };
  11.     public static char ch[] = { 'Q', 'W', 'E', 'R', 'T', 'Y', 'U', 'I', 'O',
  12.             'P', 'A', 'S', 'D', 'F', 'G', 'H', 'J', 'K', 'L', 'Z', 'X', 'C',
  13.             'V', 'B', 'N', 'M' };
  14.  
  15.     public static String doEncryption(String s)
  16.     {
  17.         char c[] = new char[(s.length())];
  18.         for (int i = 0; i < s.length(); i++)
  19.         {
  20.             for (int j = 0; j < 26; j++)
  21.             {
  22.                 if (p[j] == s.charAt(i))
  23.                 {
  24.                     c[i] = ch[j];
  25.                     break;
  26.                 }
  27.             }
  28.         }
  29.         return (new String(c));
  30.     }
  31.  
  32.     public static String doDecryption(String s)
  33.     {
  34.         char p1[] = new char[(s.length())];
  35.         for (int i = 0; i < s.length(); i++)
  36.         {
  37.             for (int j = 0; j < 26; j++)
  38.             {
  39.                 if (ch[j] == s.charAt(i))
  40.                 {
  41.                     p1[i] = p[j];
  42.                     break;
  43.                 }
  44.             }
  45.         }
  46.         return (new String(p1));
  47.     }
  48.  
  49.     public static void main(String args[])
  50.     {
  51.         Scanner sc = new Scanner(System.in);
  52.         System.out.println("Enter the message: ");
  53.         String en = doEncryption(sc.next().toLowerCase());
  54.         System.out.println("Encrypted message: " + en);
  55.         System.out.println("Decrypted message: " + doDecryption(en));
  56.         sc.close();
  57.     }
  58. }

Output:

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

Sanfoundry Global Education & Learning Series – 1000 Java Programs.

advertisement
advertisement

Here’s the list of Best Books in Java Programming, Data Structures and Algorithms.

If you find any mistake above, kindly email to [email protected]

advertisement
advertisement
Subscribe to our Newsletters (Subject-wise). Participate in the Sanfoundry Certification contest to get free Certificate of Merit. Join our social networks below and stay updated with latest contests, videos, internships and jobs!

Youtube | Telegram | LinkedIn | Instagram | Facebook | Twitter | Pinterest
Manish Bhojasia - Founder & CTO at Sanfoundry
Manish Bhojasia, a technology veteran with 20+ years @ Cisco & Wipro, is Founder and CTO at Sanfoundry. He lives in Bangalore, and focuses on development of Linux Kernel, SAN Technologies, Advanced C, Data Structures & Alogrithms. Stay connected with him at LinkedIn.

Subscribe to his free Masterclasses at Youtube & discussions at Telegram SanfoundryClasses.