- Java Programs - Advanced
- Java Programs - Home
- Pernicious Number Program
- Check Matrix Column Sum Program
- Flipgram (Heterogram) Program
- Circular Queue Program
- Flight Passenger Inheritance Program
- Goldbach Number Program
- Shift Matrix Rows Up Program
- Pangram String Program
- Later Date Program
- Symmetric Matrix Program
- Cell Phone Keypad Program
- Unique Digit Integer Program
- Max & Min Elements in Matrix Program
- Anagram Strings Program
- Mask String Program Specimen
- Mix Array Program Specimen
- LCM using Recursion Program Specimen
- Recycle Dequeue Program Specimen
- Library Compute Inheritance Program Specimen
- Linked List & Binary Tree Question Specimen
- Decimal to Hexadecimal Program
- Insertion Sort Program
- Lowest & Highest ASCII Value Program
- Card Game Stack Program
- Employee Salary Overtime Inheritance Program
- Binary Tree 2024 Questions
- Hamming Number Program
- Doubly Markov Matrix Program
- Snowball String Program
- Future Date Program
- Lucky Number Program
- Bar Graph of Vowels & Consonants Program
- Vampire Number Program
- Rotate Matrix by 270 Degree Anticlockwise Program
- Keyword Cipher Program
- Increasing, Decreasing & Bouncy Number Program
- Fill Matrix with Characters Program
- Decode Encoded Text Program
- Project Submission Date Program
- Sort Matrix Boundary Elements Program
- Insert Word in a Sentence Program
- Composite Magic Number Program
- Mirror Image of a Matrix Program
- Common Words in a Paragraph Program
- Dudeney Number Recursive Program
- Matrix Transpose Program
- Sort Words Alphabetically Program
- DeQueue Data Structure Program
- Demand Supply Inheritance Program
- Linked List & Binary Tree Question
- Pronic Number in Java using Recursion
- Unique Word Program
- No Repeated Alphabets Program
- Calculate Series Program
- Reverse Number using Recursion Program
- Item Taxable Inheritance Program
- Stack Data Structure Program
- Evil Number Program
- Merge Two Arrays Program
- Remove Repeated Alphabets Program
- Rack Stack Data Structure Program
- Stock Sales Inheritance Program
- Date and Month Program
- Binary Search using Recursion Program
- Mix Two Words Program
- Circular Queue Program
- Data Interface Program
- Prime Adam Number Program
- Octal to Decimal Conversion Program
- Arrange Words Length-wise Program
- Armstrong Number Program
- Reverse Matrix Elements Program
- Rearrange Vowels & Consonants Program
- Record & Highest Inheritance Program
- Diary Class Queue Program
- Linked List and Binary Tree Program
- Generate Date Program
- Array Format Program
- Generate Palindrome Words
- Perfect Number Program on Recursion
- Checking for Equal Matrices
- Words Beginning with Capital Letter
- Number Series Inheritance Program
- Register Stack Program
- Goldbach Number Program
- Sorting Two-Dimensional Matrix Program
- Vertical Banner Program
- Palindrome using Recursion Program
- Adder Program
- Swap Sort Program
- Product Sales Inheritance Program
- Queue Program on Array
- Cartons Program
- Quiz Program
- Caesar Cipher Program
- Disarium Number Recursive Program
- Shift Matrix Shuffle Program
- ConsChange Program
- Bank Account Inheritance Program
- Bookshelf Stack Program
- Circular Prime Program
- Sort Non-boundary Matrix Elements Program
- Words Beginning and Ending with Vowels Program
- Admission Recursive Binary Search Program
- Merger Class Concatenation Program
- String Frequency Program
- WordPile Stack Program
- Plane Circle Inheritance Program
- Smallest Integer Program
- Rotate Matrix 90 Degrees Program
- Vowels and Consonants Per Word Program
- Merging Sorted Arrays Program
- Series Sum Program
- Fibonacci Strings Program
- Stock Purchase Inheritance Program
- Array to Stack Program
- Composite Magic Program
- Symmetric Matrix Program
- Deleting Word from String Program
- Emirp Number Program
- Exchange Alphabets Program
- Matrix Difference Program
- Perimeter Area Inheritance Program
- Dequeue Queue Program
- ISBN Code Program
- Mirror Image Matrix Program
- Palindrome Words Program
- Combine Array Program
- Vowel Word Program
- Happy Number Program
- Link Queue Program
- Detail Bill Inheritance Program
- Prime Palindrome Program
- Arrange Words Alphabetically Program
- Sort Matrix Elements Program
- Frequency of “And” and “An” Program
- Decimal to Octal Conversion Program
- Pseudo Arithmetic Sequence Program
- Record Rank Inheritance Program
- Stack Program Storing Names
- Number to Words Conversion Program
- Magic Number Program
- Special Number Program
- Triangular Number Program
Java Programs - Advanced
![]() Share with a Friend |
Keyword Cipher Program - Java Program
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.
|
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 |
Z |
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; } }
Output
OUTPUT 1:
ENTER KEYWORD: SECRET
ENTER TEXT TO BE DECODED: ZLJEFT DTOT
DECODED TEXT: ZOMBIE HERE
OUTPUT 2:
ENTER KEYWORD: STAR WARS
ENTER TEXT TO BE DECODED: SPPSAG SP RSVJ
DECODED TEXT: ATTACK AT DAWN
OUTPUT 3:
ENTER KEYWORD: PLAYERS
ENTER TEXT TO BE DECODED: Haln de yokl
INVALID TEXT
