0% found this document useful (0 votes)
56 views2 pages

Network Security Lab

The document describes an algorithm for encrypting and decrypting messages using Caesar cipher. It explains that in Caesar cipher each letter is shifted a fixed number of positions down the alphabet, and includes the mathematical formulas for encryption and decryption. It then provides a Java program that implements the Caesar cipher algorithm to encrypt and decrypt the message "Anna University" with a shift of 3. The program output verifies that the encryption and decryption was successful.

Uploaded by

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

Network Security Lab

The document describes an algorithm for encrypting and decrypting messages using Caesar cipher. It explains that in Caesar cipher each letter is shifted a fixed number of positions down the alphabet, and includes the mathematical formulas for encryption and decryption. It then provides a Java program that implements the Caesar cipher algorithm to encrypt and decrypt the message "Anna University" with a shift of 3. The program output verifies that the encryption and decryption was successful.

Uploaded by

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

Ex.

No : 1(a)
Encryption and Decryption Using Ceaser Cipher
Date :

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

RESULT:
Thus the program for ceaser cipher encryption and decryption algorithm has
been implemented and the output verified successfully.

You might also like