Keyword Cipher - GeeksforGeeks

18 Feb, 2022
A keyword cipher is a form of monoalphabetic substitution. A keyword is used as the key, and it determines the letter matchings of the cipher alphabet to the plain alphabet. Repeats of letters in the word are removed, then the cipher alphabet is generated with the keyword matching to A, B, C, etc. until the keyword is used up, whereupon the rest of the ciphertext letters are used in alphabetical order, excluding those already used in the key.  
Encryption
The first line of input contains the keyword which you wish to enter. The second line of input contains the string which you have to encrypt. Plaintext: 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 Encrypted: K R Y P T O S A B C D E F G H I J L M N Q U V W X ZWith KRYPTOS as the keyword, all As becoming Ks, all Bs becoming Rs, and so on. Encrypting the message “knowledge is power” using the keyword “Kryptos”: Encrypting the message: Knowledge is Power Encoded message: IlmWjbaEb GQ NmWbpExamples:  
Input :
Keyword : secret
Message : Zombie Here
Output :
Ciphered String : ZLJEFT DTOT

Take the first example, we used "secret" keyword there.
Plain Text : 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
When "secret" keyword is used, the new encrypting text becomes :
Encrypting : S E C R T A B D F G H I J K L M N O P Q U V W X Y Z
This means 'A' means 'S', 'B' means 'E' and 'C' means 'C' and so on.
Lets encode the given message "Zombie Here"
ZOMBIE HERE becomes ZLJEFT DTOT

Input :
Keyword : Star War
Message : Attack at dawn
Output :
Ciphered String : SPPSAG SP RSVJ
Few points to be noted in this method:  
All the messages are encoded in uppercase.
Whitespace, special character, and numbers do not take into consideration keywords although you can put them in there.
While encrypting the message, whitespace, special characters and numbers remain unaffected.
 
C++
Java
C#
Javascript
// CPP program for encoding the string// using classical cipher #include<bits/stdc++.h>using namespace std; // Function generates the encoded textstring encoder(string key){    string encoded = "";    // This array represents the    // 26 letters of alphabets    bool arr[26] = {0};     // This loop inserts the keyword    // at the start of the encoded string    for (int i=0; i<key.size(); i++)    {        if(key[i] >= 'A' && key[i] <= 'Z')        {            // To check whether the character is inserted            // earlier in the encoded string or not            if (arr[key[i]-65] == 0)            {                encoded += key[i];                arr[key[i]-65] = 1;            }        }        else if (key[i] >= 'a' && key[i] <= 'z')        {            if (arr[key[i]-97] == 0)            {                encoded += key[i] - 32;                arr[key[i]-97] = 1;            }        }    }     // This loop inserts the remaining    // characters in the encoded string.    for (int i=0; i<26; i++)    {        if(arr[i] == 0)        {            arr[i]=1;            encoded += char(i + 65);        }    }    return encoded;} // Function that generates encodes(cipher) the messagestring cipheredIt(string msg, string encoded){    string cipher="";     // This loop ciphered the message.    // Spaces, special characters and numbers remain same.    for (int i=0; i<msg.size(); i++)    {        if (msg[i] >='a' && msg[i] <='z')        {            int pos = msg[i] - 97;            cipher += encoded[pos];        }        else if (msg[i] >='A' && msg[i] <='Z')        {            int pos = msg[i] - 65;            cipher += encoded[pos];        }        else        {            cipher += msg[i];        }    }    return cipher;} // Driver codeint main(){    // Hold the Keyword    string key;    key = "Computer";    cout << "Keyword : " <<key << endl;     // Function call to generate encoded text    string encoded = encoder(key);     // Message that need to encode    string message = "GeeksforGeeks";    cout << "Message before Ciphering : " << message << endl;     // Function call to print ciphered text    cout << "Ciphered Text : " << cipheredIt(message,encoded) << endl;     return 0;}
// Java program for encoding the string// using classical cipher class GFG{     // Function generates the encoded text    static String encoder(char[] key)    {        String encoded = "";                 // This array represents the        // 26 letters of alphabets        boolean[] arr = new boolean[26];         // This loop inserts the keyword        // at the start of the encoded string        for (int i = 0; i < key.length; i++)        {            if (key[i] >= 'A' && key[i] <= 'Z')            {                // To check whether the character is inserted                // earlier in the encoded string or not                if (arr[key[i] - 65] == false)                {                    encoded += (char) key[i];                    arr[key[i] - 65] = true;                }            }            else if (key[i] >= 'a' && key[i] <= 'z')            {                if (arr[key[i] - 97] == false)                {                    encoded += (char) (key[i] - 32);                    arr[key[i] - 97] = true;                }            }        }         // This loop inserts the remaining        // characters in the encoded string.        for (int i = 0; i < 26; i++)        {            if (arr[i] == false)            {                arr[i] = true;                encoded += (char) (i + 65);            }        }        return encoded;    }     // Function that generates encodes(cipher) the message    static String cipheredIt(String msg, String encoded)    {        String cipher = "";         // This loop ciphered the message.        // Spaces, special characters and numbers remain same.        for (int i = 0; i < msg.length(); i++)        {            if (msg.charAt(i) >= 'a' && msg.charAt(i) <= 'z')            {                int pos = msg.charAt(i) - 97;                cipher += encoded.charAt(pos);            }            else if (msg.charAt(i) >= 'A' && msg.charAt(i) <= 'Z')            {                int pos = msg.charAt(i) - 65;                cipher += encoded.charAt(pos);            }            else            {                cipher += msg.charAt(i);            }        }        return cipher;    }     // Driver code    public static void main(String[] args)    {        // Hold the Keyword        String key;        key = "Computer";        System.out.println("Keyword : " + key);         // Function call to generate encoded text        String encoded = encoder(key.toCharArray());         // Message that need to encode        String message = "GeeksforGeeks";        System.out.println("Message before Ciphering : " + message);         // Function call to print ciphered text        System.out.println("Ciphered Text : " + cipheredIt(message,                encoded));    }} // This code is contributed by 29AjayKumar
// C# program for encoding the string// using classical cipherusing System;     class GFG{     // Function generates the encoded text    static String encoder(char[] key)    {        String encoded = "";                 // This array represents the        // 26 letters of alphabets        Boolean[] arr = new Boolean[26];         // This loop inserts the keyword        // at the start of the encoded string        for (int i = 0; i < key.Length; i++)        {            if (key[i] >= 'A' && key[i] <= 'Z')            {                // To check whether the character is inserted                // earlier in the encoded string or not                if (arr[key[i] - 65] == false)                {                    encoded += (char) key[i];                    arr[key[i] - 65] = true;                }            }            else if (key[i] >= 'a' && key[i] <= 'z')            {                if (arr[key[i] - 97] == false)                {                    encoded += (char) (key[i] - 32);                    arr[key[i] - 97] = true;                }            }        }         // This loop inserts the remaining        // characters in the encoded string.        for (int i = 0; i < 26; i++)        {            if (arr[i] == false)            {                arr[i] = true;                encoded += (char) (i + 65);            }        }        return encoded;    }     // Function that generates encodes(cipher) the message    static String cipheredIt(String msg, String encoded)    {        String cipher = "";         // This loop ciphered the message.        // Spaces, special characters and numbers remain same.        for (int i = 0; i < msg.Length; i++)        {            if (msg[i] >= 'a' && msg[i] <= 'z')            {                int pos = msg[i] - 97;                cipher += encoded[pos];            }            else if (msg[i] >= 'A' && msg[i] <= 'Z')            {                int pos = msg[i] - 65;                cipher += encoded[pos];            }            else            {                cipher += msg[i];            }        }        return cipher;    }     // Driver code    public static void Main(String[] args)    {        // Hold the Keyword        String key;        key = "Computer";        Console.WriteLine("Keyword : " + key);         // Function call to generate encoded text        String encoded = encoder(key.ToCharArray());         // Message that need to encode        String message = "GeeksforGeeks";        Console.WriteLine("Message before Ciphering : " + message);         // Function call to print ciphered text        Console.WriteLine("Ciphered Text : " + cipheredIt(message,                encoded));    }} /* This code contributed by PrinciRaj1992 */
<script> // JavaScript program for encoding the string// using classical cipher // Function generates the encoded textfunction encoder(key){    let encoded = "";                   // This array represents the        // 26 letters of alphabets        let arr = new Array(26);        for(let i=0;i<26;i++)        {            arr[i]=false;        }           // This loop inserts the keyword        // at the start of the encoded string        for (let i = 0; i < key.length; i++)        {            if (key[i].charCodeAt(0) >= 'A'.charCodeAt(0) &&            key[i].charCodeAt(0) <= 'Z'.charCodeAt(0))            {                // To check whether the character is inserted                // earlier in the encoded string or not                if (arr[key[i].charCodeAt(0) - 65] == false)                {                    encoded += ( key[i]);                    arr[key[i].charCodeAt(0) - 65] = true;                }            }            else if (key[i].charCodeAt(0) >= 'a'.charCodeAt(0) &&            key[i].charCodeAt(0) <= 'z'.charCodeAt(0))            {                if (arr[key[i].charCodeAt(0) - 97] == false)                {                    encoded +=                    String.fromCharCode(key[i].charCodeAt(0) - 32);                    arr[key[i].charCodeAt(0) - 97] = true;                }            }        }           // This loop inserts the remaining        // characters in the encoded string.        for (let i = 0; i < 26; i++)        {            if (arr[i] == false)            {                arr[i] = true;                encoded += String.fromCharCode(i + 65);            }        }        return encoded;} // Function that generates encodes(cipher) the messagefunction cipheredIt(msg,encoded){     let cipher = "";           // This loop ciphered the message.        // Spaces, special characters and numbers remain same.        for (let i = 0; i < msg.length; i++)        {            if (msg[i] >= 'a' && msg[i] <= 'z')            {                let pos = msg[i].charCodeAt(0) - 97;                cipher += encoded[pos];            }            else if (msg[i] >= 'A' && msg[i] <= 'Z')            {                let pos = msg[i].charCodeAt(0) - 65;                cipher += encoded[pos];            }            else            {                cipher += msg[i];            }                     }        return cipher;} // Driver code// Hold the Keywordlet key;key = "Computer";document.write("Keyword : " + key+"<br>"); // Function call to generate encoded textlet encoded = encoder(key.split("")); // Message that need to encodelet message = "GeeksforGeeks";document.write("Message before Ciphering : " + message+"<br>"); // Function call to print ciphered textdocument.write("Ciphered Text : " + cipheredIt(message,                                                   encoded));  // This code is contributed by rag2127 </script>
Output:  
Keyword : Computer
Message before Ciphering : GeeksforGeeks
Ciphered Text : EUUDNTILEUUDN
 
Decryption
To decode the message you check the position of the given message in encrypting text with the plain text.Plaintext: 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 Encrypted: K R Y P T O S A B C D E F G H I J L M N Q U V W X ZMessage: PTYBIATLEP Deciphered Text: DECIPHEREDNow, how do we generate the deciphered string? We search for ‘P’ in Encrypted Text and compare its position with plain text letter and generate that letter. So ‘P’ becomes ‘D’, ‘T’ becomes ‘E’, ‘Y’ becomes ‘C’, and so on.Examples:  
Input :
Keyword : secret
Message : zljeft dtOT
Output :
Deciphered String : ZOMBIE HERE

Input :
Keyword : joker0O7hack123
Message : QjTijl
Output :
Deciphered String : BATMAN
 
CPP
// CPP program for decoding the string// which generate using classical cipher #include<bits/stdc++.h>using namespace std; // Original Set of lettersstring plaintext = "ABCDEFGHIJKLMNOPQRSTUVWXYZ"; // Function generates the encoded textstring encoder(string key){    string encoded = "";    bool arr[26] = {0};     // This loop inserts the keyword    // at the start of the encoded string    for (int i=0; i<key.size(); i++)    {        if(key[i] >= 'A' && key[i] <= 'Z')        {            // To check whether the character is inserted            // earlier in the encoded string or not            if (arr[key[i]-65] == 0)            {                encoded += key[i];                arr[key[i]-65] = 1;            }        }        else if (key[i] >= 'a' && key[i] <= 'z')        {            if (arr[key[i]-97] == 0)            {                encoded += key[i] - 32;                arr[key[i]-97] = 1;            }        }    }     // This loop inserts the remaining    // characters in the encoded string.    for (int i=0; i<26; i++)    {        if(arr[i] == 0)        {            arr[i]=1;            encoded += char(i + 65);        }    }    return encoded;} // This function will decode the messagestring decipheredIt(string msg, string encoded){    // Hold the position of every character (A-Z)    // from encoded string    map <char,int> enc;    for(int i=0; i<encoded.size(); i++)    {        enc[encoded[i]]=i;    }     string decipher="";     // This loop deciphered the message.    // Spaces, special characters and numbers remain same.    for (int i=0; i<msg.size(); i++)    {        if (msg[i] >='a' && msg[i] <='z')        {            int pos = enc[msg[i]-32];            decipher += plaintext[pos];        }        else if(msg[i] >='A' && msg[i] <='Z')        {            int pos = enc[msg[i]];            decipher += plaintext[pos];        }        else        {            decipher += msg[i];        }    }    return decipher;} // Driver codeint main(){    // Hold the Keyword    string key;    key = "Computer";    cout << "Keyword : "<< key << endl;     // Function call to generate encoded text    string encoded = encoder(key);     // Message that need to decode    string message = "EUUDN TIL EUUDN";    cout << "Message before Deciphering : " << message << endl;     // Function call to print deciphered text    cout << "Deciphered Text : " << decipheredIt(message,encoded) << endl;     return 0;}
Output:  
Keyword : Computer
Message before Deciphering : EUUDN TIL EUUDN
Deciphered Text : GEEKS FOR GEEKS
You can improve this Classical Cipher: Keyword also. Here we are only taking A-Z in plain text. You can take uppercase, lowercase, and numbers also into consideration.Ways to attack a keyword cipher: The best ways to attack a keyword cipher without knowing the keyword are through the known-plaintext attack, frequency analysis, and discovery of the keyword (often a cryptanalyst will combine all three techniques). Keyword discovery allows immediate decryption since the table can be made immediately.This article is contributed by Sachin Bisht. If you like GeeksforGeeks and would like to contribute, you can also write an article using write.geeksforgeeks.org or mail your article to review-team@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.Please write comments if you find anything incorrect, or you want to share more information about the topic discussed above. 
29AjayKumar
princiraj1992
rag2127
kumaripunam984122
simranarora5sos
cryptography
Strings
Strings
cryptography
Writing code in comment?
Please use ide.geeksforgeeks.org,
generate link and share the link here.
Array of Strings in C++ (5 Different Ways to Create)
Convert string to char array in C++
Check whether two strings are anagram of each other
Top 50 String Coding Problems for Interviews
Length of the longest substring without repeating characters
Reverse words in a given string
How to split a string in C/C++, Python and Java?
Remove duplicates from a given string
Print all the duplicates in the input string
stringstream in C++ and its applications