Develop a program to implement both encryption and decryption using Caesar Cipher.

AIM:
To encrypt and decrypt the given message by using Ceaser Cipher encryption algorithm.

ALGORITHMS:

1. In Ceaser Cipher each letter in the plaintext is replaced by a letter some fixed number of positions down the alphabet.
2. For example, with a left shift of 3, D would be replaced by A, E would become B, and so on.
3. The encryption can also be represented using modular arithmetic by first transforming the letters into numbers, according to the scheme, A = 0, B = 1, Z = 25.
4. Encryption of a letter x by a shift n can be described mathematically as,
En(x) = (x + n) mod26
5. Decryption is performed similarly,
Dn (x)=(x – n) mod26

PROGRAM:
CaesarCipher.java
class caesarCipher {
public static String encode(String enc, int offset) { offset = offset % 26 + 26;
StringBuilder encoded = new StringBuilder(); for (char i : enc.toCharArray()) {
if (Character.isLetter(i)) {
if (Character.isUpperCase(i)) {
encoded.append((char) (‘A’ + (i – ‘A’ + offset) % 26));
} else {
encoded.append((char) (‘a’ + (i – ‘a’ + offset) % 26));
}
} else {
encoded.append(i);
}
}
return encoded.toString();
}

public static String decode(String enc, int offset) { return encode(enc, 26 – offset);
}

public static void main(String[] args) throws java.lang.Exception { String msg = “Anna University”;
System.out.println(“Simulating Caesar Cipher\n “);
System.out.println(“Input : ” + msg); System.out.printf(“Encrypted Message : “); System.out.println(caesarCipher.encode(msg, 3)); System.out.printf(“Decrypted Message : “);
System.out.println(caesarCipher.decode(caesarCipher.encode(msg, 3), 3));
}
}

OUTPUT:
Simulating Caesar Cipher

Input : Anna University
Encrypted Message : Dqqd Xqlyhuvlwb

Decrypted Message : Anna University

Leave a comment