0% found this document useful (0 votes)
385 views

Caesar Cipher - Java Coding

This document describes a Caesar cipher program that can encrypt and decrypt text using a shift of 3. It includes: 1) Methods to encrypt plain text to cipher text and decrypt cipher text to plain text using a Caesar cipher with a shift of 3. 2) Sample test data showing plain text encrypted to cipher text and cipher text decrypted to plain text. 3) The main method that gets user input for encryption or decryption and calls the appropriate method.

Uploaded by

JASPER WESSLY
Copyright
© Attribution Non-Commercial (BY-NC)
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
385 views

Caesar Cipher - Java Coding

This document describes a Caesar cipher program that can encrypt and decrypt text using a shift of 3. It includes: 1) Methods to encrypt plain text to cipher text and decrypt cipher text to plain text using a Caesar cipher with a shift of 3. 2) Sample test data showing plain text encrypted to cipher text and cipher text decrypted to plain text. 3) The main method that gets user input for encryption or decryption and calls the appropriate method.

Uploaded by

JASPER WESSLY
Copyright
© Attribution Non-Commercial (BY-NC)
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 6

CAESAR CIPHER

Generate the Caesar cipher text of given plain text and also decrypt the generated cipher text. Also give the user the facility to change the value of key.

Testing Data

Encryption Plain Text Cipher Text :- meet me after the toga party :- PHHW PH DIWHU WKH WRJD SDUWB ( n = 3)

Decryption Cipher Text Plain Text : khoor eurwkhu : HELLO BROTHER

import java.util.*; class CeasarCipher { public static void findPlainText() { String letter[] = {"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" }; String plainText="", cipherTxt; System.out.println("Enter a ciphertext"); Scanner in = new Scanner(System.in); cipherTxt = in.nextLine(); int index = 0; for(int i=0; i<cipherTxt.length(); i++) { char c = cipherTxt.charAt(i); String val = String.valueOf(c); if(val.equals(" ")) { plainText = plainText + " ";} else { for(int j=0; j<letter.length; j++) { if(val.equalsIgnoreCase(letter[j]))

{ index = j;break;}}if(index < 3) {

index = (25 - index); plainText = plainText + letter[index]; } else { plainText = plainText + letter[index - 3];}}} System.out.println("Plain text of "+cipherTxt+" is "+ plainText); }

public static void findCeaserCipher() { String letter[] = {"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" }; String plainText, cipherTxt = ""; System.out.println("Enter a plaintext"); Scanner in = new Scanner(System.in);plainText = in.nextLine(); int index = 0;for(int i=0; i<plainText.length(); i++) { char c = plainText.charAt(i);

String val = String.valueOf(c); if(val.equals(" ")) { cipherTxt = cipherTxt + " ";} else { for(int j=0; j<letter.length; j++) { if(val.equalsIgnoreCase(letter[j])) { index = j;break;}} if(index > 22) { index = (index + 3) % 26; cipherTxt = cipherTxt + letter[index]; } else { cipherTxt = cipherTxt + letter[index + 3]; } }

} System.out.println("Cipher text of "+plainText+" is "+ cipherTxt); }

public static void main (String[] args) { Scanner in = new Scanner(System.in); System.out.println("Enter the choice:"); System.out.println("1. Plain text to cipher text."); System.out.println("2. cipher text to plain text."); int choice = in.nextInt();switch(choice) { case 1:findCeaserCipher(); break; case 2:findPlainText(); break; default:System.out.printf("Invalide Choice"); break;} } }

OUTPUT

meet me after the toga party

khoor eurwkhu

You might also like