Monoalphabetic Cipher is a cipher where the letters of the plain text are mapped to ciphertext letters based on a single alphabetic key. It is a one to one mapping.
Given plain text, the task is to implement the Monoalphabetic Cipher.
Input: Welcome to geeksforgeeks Output: Plain text: Welcome to geeksforgeeks Encrypted message: VTSEGDT ZG UTTALYGKUTTAL Decrypted message: welcome to geeksforgeeks Input: Live the moment Output: Plain text: Live the moment Encrypted message: SOCT ZIT DGDTFZ Decrypted message: live the moment
Mapping characters to cipher text letters which we used in our code:
Explanation of Output:
|W – w – V||t – Z||g – U||g – U|
|e – T||o – G||e – T||e – T|
|l – S||e – T||e – T|
|c – E||k – A||k – A|
|o – G||s – L||s – L|
|m – D||f – Y|
|e – T||o – G|
|r – K|
Welcome to geeksforgeeks → VTSEGDT ZG UTTALYGKUTTAL
1. Create two char arrays, one for normal alphabets(say normalChar) and another is for encoding(say codedChar).
2. We will use two functions:
- stringEncryption: We pass string(string with all characters in lower case) as a parameter. Initialize an empty string(say encryptedString). We use for loop and compare each character with normal char array, whenever the condition is true, add the character with the corresponding index of codedChar to the encrypted string. In the case of special characters, we will add them directly to the string.
- stringDescryption: We pass the encrypted string as the parameter. Initialize an empty string(say descryptedString). In the same way, we run the for loop and add the character with the corresponding index of normalChar to the decrypted string. In the case of special characters, we will add them directly to the string.
Below is the implementation of the above approach:
Plain text: Welcome to geeksforgeeks Encrypted message: VTSEGDT ZG UTTALYGKUTTAL Decrypted message: welcome to geeksforgeeks
Attention reader! Don’t stop learning now. Get hold of all the important Java Foundation and Collections concepts with the Fundamentals of Java and Java Collections Course at a student-friendly price and become industry ready.