Keyword Cipher Java Program | ISC Computer Science 2024 Paper 2

Keyword cipher is a form of encryption technique. A keyword is used as the key, and it determines the letter matching 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 A, B, C, etc. until the keyword is used up, whereupon the rest of the cipher text 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.

PlaintextABCDEFGHIJKLMNOPQRSTUVWXYZ
EncryptedKRYPTOSABCDEFGHIJLMNQUVWXZ

With KRYPTOS as the keyword, all As become Ks, all Bs become Rs, and so on.

Example:

Encrypting the message: KNOWLEDGE IS POWER when, keyword is KRYPTOS
Encrypted message: DGHVETPST BM IHVTL

Write a program to accept a coded text in uppercase and a keyword. Using the above technique decrypt the text and display.

Note: All the messages are encoded in uppercase. Whitespace, special characters, and numbers remains unchanged.

Test your program for the following inputs:

Example 1
INPUT:
ENTER KEYWORD: SECRET
ENTER TEXT TO BE DECODED: ZLJEFT DTOT
OUTPUT:
DECODED TEXT: ZOMBIE HERE

Example 2
INPUT:
ENTER KEYWORD: STAR WARS
ENTER TEXT TO BE DECODED: SPPSAG SP RSVJ
OUTPUT: DECODED TEXT: ATTACK AT DAWN

Example 3
INPUT:
ENTER KEYWORD: PLAYERS
ENTER TEXT TO BE DECODED: Haln de yokl
OUTPUT: INVALID TEXT

import java.util.Scanner;
class Decrypt{
    public static void main(String[] args){
        Scanner in = new Scanner(System.in);
        System.out.print("ENTER KEYWORD: ");
        String keyword = in.nextLine().toUpperCase();
        System.out.print("ENTER TEXT TO BE DECODED: ");
        String text = in.nextLine();
        for(int i = 0; i < text.length(); i++){
            char ch = text.charAt(i);
            if(Character.isLowerCase(ch)){
                System.out.println("INVALID TEXT");
                return;
            }
        }
        keyword = process(keyword);
        String plainText = new String();
        for(char ch = 'A'; ch <= 'Z'; ch++)
            plainText += ch;
        String encrypted = new String(keyword);
        for(char ch = 'A'; ch <= 'Z'; ch++){
            if(keyword.indexOf(ch) == -1)
                encrypted += ch;
        }
        String decoded = new String();
        for(int i = 0; i < text.length(); i++){
            char ch = text.charAt(i);
            if(!Character.isLetter(ch))
                decoded += ch;
            else{
                int index = encrypted.indexOf(ch);
                decoded += plainText.charAt(index);
            }
        }
        System.out.println("DECODED TEXT: " + decoded);
    }
    public static String process(String k){
        String str = new String();
        for(int i = 0; i < k.length(); i++){
            char ch = k.charAt(i);
            if(!Character.isLetter(ch))
                continue;
            if(str.indexOf(ch) == -1)
                str += ch;
        }
        return str;
    }
}