You are on page 1of 17

Dr B R Ambedkar National Institute of

Technology
Grand Trunk Road, Barnala - Amritsar Bypass Road,
Jalandhar, Punjab 144011

ITPC-321 Cryptography and Network Security LAB


B. TECH IT – III YEAR (5th SEMESTER)

Submitted by
Name Lanka sai suryachandra mouli chinnaapparao
Roll No 21124059
Submitted to

Department of Information Technology


S.NO NAME OF THE EXPERIMENT SUBMISSION DATE SIGN
Question 1:
Implement the encryption and decryption algorithms of a simple
additive/shift/Caeser cipher, with a key of your choice. The program should take
user input of the actual message, display the encrypted message and decrypted
message.
Code:
import java.util.*;
public class Main
{
static String encryption(String s,int key)
{
String ans="";
char ch;
int ref;
for(int i=0;i<s.length();i++)
{
ch=s.charAt(i);
if(ch==' ')
{
ans=ans+ch;
}
else if((int)ch<91)
{
ref=(int)ch-65;
ref=(ref+key)%26;
ref=ref+65;
ans=ans+(char)ref;
}
else if((int)ch<123)
{
ref=(int)ch-97;
ref=(ref+key)%26;
ref=ref+97;
ans=ans+(char)ref;
}
}
return ans;
}
static String decryption(String s,int key)
{
String ans="";
char ch;
int ref;
for(int i=0;i<s.length();i++)
{
ch=s.charAt(i);
if(ch==' ')
{
ans=ans+ch;
}
else if((int)ch<91)
{
ref=(int)ch-65;
ref=(ref-key)%26;
ref=ref+65;
ans=ans+(char)ref;
}
else if((int)ch<123)
{
ref=(int)ch-97;
if(ref-key>=0)
ref=(ref-key)%26;
else
ref=26+(ref-key)%26;
ref=ref+97;
ans=ans+(char)ref;
}
}
return ans;
}
public static void main(String[] args) {
String s;
System.out.println("enter the plain text");
Scanner in=new Scanner(System.in);
s=in.nextLine();
String ct;
int key;
System.out.println("enter the key");
key=in.nextInt();
ct=encryption(s,key);
System.out.println("the encrypted cipher text is "+ct);
String pt;
pt=decryption(ct,key);
System.out.println("the encrypted plain text is "+pt);
}
}

Output:
Question 2:
Implement the encryption and decryption algorithms of a multiplicative
cipher, with a key of 7. The program should take user input of the actual message,
display the encrypted message and decrypted message
Code:
import java.util.*;
public class Main
{
static int reverse(int key)
{
int a=1;
while(true)
{
if((key*a)%26==1)
{
return a;
}
a++;
}
}
static String fun(String s,int key)
{
String ans="";
char ch;
int ref;
for(int i=0;i<s.length();i++)
{
ch=s.charAt(i);
if(ch==' ')
{
ans=ans+' ';
}
else if((int)ch<91)
{
ref=(int)ch-65;
ref=ref*key;
ref=(ref%26)+65;
ans=ans+(char)ref;
}
else if((int)ch<123)
{
ref=(int)ch-97;
ref=ref*key;
ref=(ref%26)+97;
ans=ans+(char)ref;
}
}
return ans;
}
public static void main(String[] args) {
Scanner in=new Scanner(System.in);
String s;
System.out.println("enter the plain text");
s=in.nextLine();
int key;
System.out.println("enter the key");
key=in.nextInt();
String ct,pt;
ct=fun(s,key);
System.out.println("the cipher text is "+ct);
key=reverse(key);
pt=fun(ct,key);
System.out.println("the plain text is "+pt);
}
}

Output:
Question 3:
Implement the encryption and decryption algorithms of the rail fence
cipher. The program should take user input of the actual message, display the
encrypted message and decrypted message.
Code:
import java.util.*;
class thrid
{
static String encryption(String s)
{
String a="",b="";
for(int i=0;i<s.length();i++)
{
if(i%2==0)
{
a=a+s.charAt(i);
}
else
{
b=b+s.charAt(i);
}
}
String ans=a+b;
return ans;
}
static String decryption(String s)
{
String a,b;
int n=s.length();
if(n%2==0)
{
a=s.substring(0,n/2);
b=s.substring(n/2,n);
}
else
{
a=s.substring(0,(n+1)/2);
b=s.substring((n+1)/2,n);
}
int o=a.length();
int p=b.length();
String ans="";
for(int i=0;i<o-1;i++)
{
ans=ans+a.charAt(i)+b.charAt(i);
}
if(0==p)
{
ans=ans+a.charAt(o-1)+b.charAt(o-1);
}
else
{
ans=ans+a.charAt(o-1);
}
return ans;
}
public static void main(String args[])
{
String s;
Scanner in=new Scanner(System.in);
System.out.println("enter the plain text");
s=in.nextLine();
String ct=encryption(s);
System.out.println("the cipher text is "+ct);
String pt=decryption(ct);
System.out.println("the plain text is "+pt);
}
}

Output:
Question 5:
Implement the encryption and decryption algorithms of an affine cipher
with the key pair (7, 2). The program should take user input of the actual message,
display the encrypted message and decrypted message

Code:
import java.util.Scanner;
public class Main {
public static int gcd(int a, int b) {
while (b != 0) {
int temp = b;
b = a % b;
a = temp;
}
return a;
}
public static int modInverse(int a, int m) {
for (int x = 1; x < m; x++) {
if ((a * x) % m == 1) {
return x;
}
}
return -1; // No mod inverse exists
}
public static String affineEncrypt(String plainText, int keyA, int keyB) {
StringBuilder encryptedText = new StringBuilder();
for (char character : plainText.toCharArray()) {
if (Character.isLetter(character)) {
char base = Character.isUpperCase(character) ? 'A' : 'a';
int charIndex = character - base;
int encryptedCharIndex = (keyA * charIndex + keyB) % 26;
char encryptedChar = (char) (encryptedCharIndex + base);
encryptedText.append(encryptedChar);
} else {
encryptedText.append(character);
}
}
return encryptedText.toString();
}
public static String affineDecrypt(String encryptedText, int keyA, int keyB) {
StringBuilder decryptedText = new StringBuilder();
int modInverseKeyA = modInverse(keyA, 26);
for (char character : encryptedText.toCharArray()) {
if (Character.isLetter(character)) {
char base = Character.isUpperCase(character) ? 'A' : 'a';
int encryptedCharIndex = character - base;
int decryptedCharIndex = (modInverseKeyA * (encryptedCharIndex - keyB + 26))
% 26;
char decryptedChar = (char) (decryptedCharIndex + base);
decryptedText.append(decryptedChar);
} else {
decryptedText.append(character);
}
}
return decryptedText.toString();
}
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
System.out.print("Enter the message: ");
String message = scanner.nextLine();
int keyA = 7;
int keyB = 2;
String encryptedMessage = affineEncrypt(message, keyA, keyB);
System.out.println("Encrypted Message: " + encryptedMessage);
String decryptedMessage = affineDecrypt(encryptedMessage, keyA, keyB);
System.out.println("Decrypted Message: " + decryptedMessage);
}
}

Output:
Question 4:
Implement a transposition cipher where:
a) the plaintext is written into a table (2D array) column by column and
then transmitted row by row.
b) the plaintext is written into a table row by row and then transmitted
column by column. Number of rows = number of columns = 4. If P-Boxes are
implemented using the above methods, what kind of P-Box would they be? Draw
the permutation tables for both.

Code:
a)
import java.util.*;
public class Main {
public static String encrypt(String plaintext, int numRows) {
StringBuilder ciphertext = new StringBuilder();
int numCols = (int) Math.ceil((double) plaintext.length() / numRows);
char[][] table = new char[numRows][numCols];
int index = 0;
for (int col = 0; col < numCols; col++) {
for (int row = 0; row < numRows; row++) {
if (index < plaintext.length()) {
table[row][col] = plaintext.charAt(index++);
} else {
table[row][col] = 'X'; // Padding with 'X' if needed
}
}
}
for (int row = 0; row < numRows; row++) {
for (int col = 0; col < numCols; col++) {
ciphertext.append(table[row][col]);
}
}
return ciphertext.toString();
}
public static void main(String[] args) {
Scanner in=new Scanner(System.in);
System.out.println("enter the palin text");
String plaintext=in.nextLine();
int numRows = 4;
String encryptedText = encrypt(plaintext, numRows);
System.out.println("Encrypted Text: " + encryptedText);
}
}

Output:

Code:
b)
import java.util.*;
public class RowByRowCipher {
public static String encrypt(String plaintext, int numRows) {
StringBuilder ciphertext = new StringBuilder();
int numCols = (int) Math.ceil((double) plaintext.length() / numRows);
char[][] table = new char[numRows][numCols];
int index = 0;
for (int row = 0; row < numRows; row++) {
for (int col = 0; col < numCols; col++) {
if (index < plaintext.length()) {
table[row][col] = plaintext.charAt(index++);
} else {
table[row][col] = 'X'; // Padding with 'X' if needed
}
}
}
for (int col = 0; col < numCols; col++) {
for (int row = 0; row < numRows; row++) {
ciphertext.append(table[row][col]);
}
}
return ciphertext.toString();
}
public static void main(String[] args) {
System.out.println("enter the plain text");
Scanner in=new Scanner(System.in);
String plaintext=in.nextLine();
int numRows = 4;
String encryptedText = encrypt(plaintext, numRows);
System.out.println("Encrypted Text: " + encryptedText);
}
}

Output:

You might also like