0% found this document useful (0 votes)
157 views3 pages

Java Caesar Cipher Program Guide

Caesar cipher is a type of substitution cipher where each letter in a message is replaced by a letter some fixed number of positions down the alphabet. The shift number is determined by a key between 1-26. For encryption, each letter is replaced by the letter n positions after it in the alphabet, wrapping around to the beginning. For decryption, each letter is replaced by the letter n positions before it. For example, with a key of 2, the encrypted text of "APPLE" is "CRRNG".

Uploaded by

oguiniandc19
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
157 views3 pages

Java Caesar Cipher Program Guide

Caesar cipher is a type of substitution cipher where each letter in a message is replaced by a letter some fixed number of positions down the alphabet. The shift number is determined by a key between 1-26. For encryption, each letter is replaced by the letter n positions after it in the alphabet, wrapping around to the beginning. For decryption, each letter is replaced by the letter n positions before it. For example, with a key of 2, the encrypted text of "APPLE" is "CRRNG".

Uploaded by

oguiniandc19
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 3

Caesar cipher is a type of substitution ciphers in which letters in a message (called plaintext)

are replaced with other letters to generate a ciphertext. The choice of replacement characters
is a function of a key (or a password). The key should be a number between 1 and 26. In
encryption (converting plaintext to ciphertext), the letter is replaced by another that is key
letters after it, whereas, in decryption (converting ciphertext to plaintext) the letter is replaced
by another that is key letters before. The letter after 'Z' is considered to be 'A'. For example, if
the key is 2: Encrypting plaintext Apple ciphertext CRRNG. Decrypting ciphertext CRRNG
plaintext: APPLE. Your program should do the following Ask the user whether he/she wants
to do encryption or decryption. Validate that the user inputs either 'e' for encryption or 'd' for
decryption. Ask the user for a message that only has alphabet letters with no spaces. Check
the message and remove any non-alphabet letter while displaying a warning, and make all
the alphabet letters uppercase. Ask the user for a key between 1 and 26 (inclusive). Validate
that the key is within this range. Compute and display the ciphertext if the user chose
encryption or the plaintext if the user chose decryption. Your program should display
messages identical to the examples below given the same input. Hello! Please press e for
encryption and d for decryption e Please enter your Message. The Message should have only
Alphabet letters, all spaces and non-alphabet characters will be deleted. I love you! Non
Alphabet Character Dropped: Non Alphabet Character Dropped: Non Alphabet Character
Dropped! Please input a key between 1 and 26: 7 The ciphertext is:PSVCLFVB

Solution

import javax.swing.JOptionPane;

public class CaesarCipher

   public static void main(String[] args)

   {

       String str = (JOptionPane.showInputDialog("Input Data to encypt:"));

       String key = (JOptionPane.showInputDialog("Input the key:"));

       int keyLength=key.length();

       //prints encryption

       String encrypted = encrypt(str, keyLength);


       System.out.println("Encrypted:" + encrypted);

       //prints decryption

       String decrypted = decrypt(encrypted, keyLength);

       System.out.println("Decrypted:" + decrypted);

       //prints key

       System.out.println("Key:" + key);

   }

   public static String encrypt(String str, int keyLength)

   {

       String encrypted = "";

       for(int i = 0; i < str.length(); i++)

       {

           int c = str.charAt(i);

           if (Character.isUpperCase(c))

           {

               //26 letters of the alphabet so mod by 26

               c = c + (keyLength % 26);

               if (c > 'Z')

               c = c - 26;

           }

           else if (Character.isLowerCase(c))

           {

               c = c + (keyLength % 26);

               if (c > 'z')

               c = c - 26;

           }
           encrypted += (char) c;

       }

   return encrypted;

   }

   public static String decrypt(String str, int keyLength)

   {

       String decrypted = "";

       for(int i = 0; i < str.length(); i++)

       {

           int c = str.charAt(i);

           if (Character.isUpperCase(c))

           {

               c = c - (keyLength % 26);

               if (c < 'A')

               c = c + 26;

           }

           else if (Character.isLowerCase(c))

           {

               c = c - (keyLength % 26);

               if (c < 'a')

               c = c + 26;

           }

           decrypted += (char) c;

       }

       return decrypted;

   }

You might also like