Location via proxy:   [ UP ]  
[Report a bug]   [Manage cookies]                

Caesar Cipher: Example

Download as pdf or txt
Download as pdf or txt
You are on page 1of 1

Caesar cipher

In cryptography, a Caesar cipher, also known as Caesar's cipher, the shift cipher, Caesar's code or
Caesar shift, is one of the simplest and most widely known encryption techniques. It is a type of
substitution cipher in which each letter in the plaintext is replaced by a letter some fixed number of
positions down the alphabet. For example, with a left shift of 3, D would be replaced by A, E would
become B, and so on. The method is named after Julius Caesar, who used it in his private
correspondence.

Example
The transformation can be represented by aligning two alphabets; the cipher alphabet is the plain
alphabet rotated left or right by some number of positions. For instance, here is a Caesar cipher
using a left rotation of three places, equivalent to a right shift of 23 (the shift parameter is used as
the key):
Plain: ABCDEFGHIJKLMNOPQRSTUVWXYZ
Cipher: XYZABCDEFGHIJKLMNOPQRSTUVW

When encrypting, a person looks up each letter of the message in the "plain" line and writes down
the corresponding letter in the "cipher" line.
Plaintext: THE QUICK BROWN FOX JUMPS OVER THE LAZY DOG
Ciphertext: QEB NRFZH YOLTK CLU GRJMP LSBO QEB IXWV ALD

Design a class CaesarCipher with methods to decrypt and encrypt a plain text to cipher text and
visa-versa given the key (shift parameter).

Create a TestCaesarCipher having the main( ) function to test the above methods.

Derive the class CaesarCipherFile from the super class CaesarCipherFile and add the methods
encryptFile and decryptFile which accepts the complete path of a text file containing the
plain/cipher text and the key(shift parameter) to encrypt and decrypt the file respectively.

Base class: CaesarCipher


Methods:
String encrypt( String plainText, int key )
String decrypt( String cipherText, int key )

Dervied class: CaesarCipherFile


void encryptFile( String pathToSourceFile, int key, String pathToTargetFile )
void decryptFile( String pathToSourceFile, int key, String pathToTargetFile )

Also create a TestCaesarCipherFile having the main( ) function to test the above methods.

You might also like