# Java Program to Implement the Monoalphabetic Cypher

In this problem, we need to convert the text to cipher text using the Monoalphabetic Cipher technique.

All alphabetical characters are pre-mapped to their cipher character in the Monoalphabetic Cipher algorithm. So, we need to substitute all plain text characters with their mapped characters.

Here is the table containing the mapping of characters.

• a$\mathrm{\rightarrow;}$Q

• b $\mathrm{\rightarrow;}$ W

• c $\mathrm{\rightarrow;}$ E

• d $\mathrm{\rightarrow;}$ R

• e $\mathrm{\rightarrow;}$ T

• f $\mathrm{\rightarrow;}$ Y

• g $\mathrm{\rightarrow;}$ U

• h $\mathrm{\rightarrow;}$ I

• i $\mathrm{\rightarrow;}$ O

• j $\mathrm{\rightarrow;}$ P

• k $\mathrm{\rightarrow;}$ A

• l $\mathrm{\rightarrow;}$ S

• m $\mathrm{\rightarrow;}$ D

• n $\mathrm{\rightarrow;}$ F

• o $\mathrm{\rightarrow;}$ G

• p $\mathrm{\rightarrow;}$ H

• q $\mathrm{\rightarrow;}$ J

• r $\mathrm{\rightarrow;}$ K

• s $\mathrm{\rightarrow;}$ L

• t $\mathrm{\rightarrow;}$ Z

• u $\mathrm{\rightarrow;}$ X

• v $\mathrm{\rightarrow;}$ C

• w $\mathrm{\rightarrow;}$ V

• x $\mathrm{\rightarrow;}$ B

• y $\mathrm{\rightarrow;}$ N

• z $\mathrm{\rightarrow;}$ M

In the above mapping, we can observe that alphabetical characters are mapped to the sequence of characters in the keyboard.

Note - We need to keep all other characters as it is except alphabetical characters.

Problem statement - We have given plain text in the string format. We need to convert it to the cipher text using the Monoalphabetic Cipher technique.

### Sample examples

Input

str = 'This is a normal string!'

Output

'ZIOL OL Q FGKDQS LZKOFU!'

Explanation - According to the mapping table, all characters are mapped to a particular character.

Input

str = 'ab'

Output

'QW'

Explanation - Here, a -> Q, and b -> W. The cipher text is 'QW'.

Input

str = "How are you?"

Output

IGV QKT NGX?


Explanation - We mapped characters to encrypt them.

## Approach 1

In this approach, we can use a map or array data structure to store the mapping of each character. After that, we traverse through each character of plain text and substitute its character with the mapped text.

### Algorithm

Step 1 - Define the 'alphabeticalChars' array containing all alphabetical characters in the sequence.

Step 2 - Define the 'mappingChar' to store the mapped characters related to the alphabet.

Step 3 - Define the encryptStr() function to encrypt the plain text.

Step 3.1 - In the encryptStr() function, Initialize the 'e_srt' with an empty string to store the encrypted string.

Step 3.2 - Use the loop to traverse the string. Also, use a nested loop to find the index of the current character in the 'alphabeticalChars[]' array.

Step 3.3 - If the index of the character is found in the array, take a mapping character from the founded index, and append it to the e_str string.

Step 3.4 - If a character is not a lowercase alphabet, append it to the e_Str string as it is.

Step 3.5 - Return the e_str string.

Step 4 - Define the decryptStr() function to decrypt the encrypted string.

Step 4.1 - In the decryptStr() function, Initialize the 'd_str' string to store the plain text.

Step 4.2 - Start traversing the cipher text. Also, use the nested loop to traverse the 'mappingChars' array and find an index of the current character.

Step 4.3 - If a character is alphabetical, access the plain text character from the alphabeticalChars array according to the founded index and append it to the d_str string. If a character is a special character, append it as it is.

Step 4.4 - Return the d_str string.

### Example

import java.io.*;
public class Main {
// array of alphabets
public static char alphabeticalChars[] = { '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' };
// array of mapped alphabets
public static char mappingChars[] = { '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' };
// Encrypting the string
public static String encryptStr(String str) {
String e_str = "";
// traverse the string
for (int p = 0; p < str.length(); p++) {
for (int q = 0; q < 26; q++) {
// match the alphabets of the string with the array's alphabets
if (str.charAt(p) == alphabeticalChars[q]) {
e_str += mappingChars[q];
break;
}
// If the character is a special character, add it as it is
if (str.charAt(p) < 'a' || str.charAt(p) > 'z') {
e_str += str.charAt(p);
break;
}
}
}
return e_str;
}
public static String decryptStr(String str) {
String d_str = "";
// traverse the string
for (int p = 0; p < str.length(); p++) {
for (int q = 0; q < 26; q++) {
// match characters
if (str.charAt(p) == mappingChars[q]) {
d_str += alphabeticalChars[q];
break;
}
// For special characters
if (str.charAt(p) < 'A' || str.charAt(p) > 'Z') {
d_str += str.charAt(p);
break;
}
}
}
return d_str;
}
public static void main(String args[]) {
String str = "This is a normal string!";
System.out.println("Normal text is : " + str);
System.out.println(" ");
// Encryption part
String e_str = encryptStr(str.toLowerCase());
System.out.println("Encrypted string is : " + e_str);
// decryption part
System.out.println("Decrypted string is : " + decryptStr(e_str));
}
}


### Output

Normal text is : This is a normal string!
Encrypted string is : ZIOL OL Q FGKDQS LZKOFU!
Decrypted string is : this is a normal string!


Time complexity - O(N) as we traverse the plain text to encrypt and decrypt.

Space complexity - O(N) as we use string to store the cipher text.

The Monoalphabetic Cypher is easy to implement as we need to substitute the alphabetical characters with mapped characters. Also, it keeps the digits and special characters as it is.

The Monoalphabetic Cypher provides very low-level security as we substitute each plain text character with the fixed character. So, it keeps the frequency of characters the same.

Updated on: 24-Aug-2023

179 Views 