21DCS099-CRNS Practical 3

Download as docx, pdf, or txt
Download as docx, pdf, or txt
You are on page 1of 13

Faculty of Technology and Engineering

Devang Patel Institute of Advanced Research and Technology


Department of Computer Science & Engineering
Date: 05/02/2024

Laboratory Manual
Academic Year : 2023-24 Semester : 6
Course code : CS361 Course name : Cryptography & Network Security
Name : Purvam Seat Number : 21DCS099
Prajapati
(Softcopy to be prepared for this and to be submitted on teams)

Practical – 3

Date of Performance:

Aim:

The Rail Fence Cipher was invented in ancient times. It was used by the Greeks, who created
a special tool, called scytale, to make message encryption and decryption easier. The letters
are arranged in a way which is similar to the shape of the top edge of the rail fence. If king
Leonidas want to send message to Sparta as “300 achieved glory at hot gate, unite for Greece”
then what will be ciphertext when it is encrypted using 3 rows. Also implement decryption of
message.

[A]Cipher Encryption Algorithm:

 Please mention introduction of algorithm and background equations.


 Code and output of Cipher

[B] Cryptanalysis of Cipher:


 Mention in points attacks that can occur and the corresponding code.
 Also take screenshots of Output
Practical – 3

[A] Cipher Encryption Algorithm –

The Rail Fence Cipher was invented in ancient times. It was used by the Greeks, who created
a special tool, called scytale, to make message encryption and decryption easier. The letters
are arranged in a way which is similar to the shape of the top edge of the rail fence. If king
Leonidas want to send message to Sparta as “300 achieved glory at hot gate, unite for
Greece” then what will be ciphertext when it is encrypted using 3 rows. Also implement
decryption of message.

Code –

import java.util.*;
class RailFence {

// function to encrypt a message


public static String encryptRailFence(String text,
int key)
{

// create the matrix to cipher plain text


// key = rows , length(text) = columns
char[][] rail = new char[key][text.length()];

// filling the rail matrix to distinguish filled


// spaces from blank ones
for (int i = 0; i < key; i++)
Arrays.fill(rail[i], '\n');

boolean dirDown = false;


int row = 0, col = 0;

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

// check the direction of flow


// reverse the direction if we've just
// filled the top or bottom rail
if (row == 0 || row == key - 1)
dirDown = !dirDown;

// fill the corresponding alphabet


rail[row][col++] = text.charAt(i);
// find the next row using direction flag
if (dirDown)
row++;
else
row--;
}

// now we can construct the cipher using the rail


// matrix
StringBuilder result = new StringBuilder();
for (int i = 0; i < key; i++)
for (int j = 0; j < text.length(); j++)
if (rail[i][j] != '\n')
result.append(rail[i][j]);

return result.toString();
}

// This function receives cipher-text and key


// and returns the original text after decryption
public static String decryptRailFence(String cipher,
int key)
{

// create the matrix to cipher plain text


// key = rows , length(text) = columns
char[][] rail = new char[key][cipher.length()];

// filling the rail matrix to distinguish filled


// spaces from blank ones
for (int i = 0; i < key; i++)
Arrays.fill(rail[i], '\n');

// to find the direction


boolean dirDown = true;

int row = 0, col = 0;

// mark the places with '*'


for (int i = 0; i < cipher.length(); i++) {
// check the direction of flow
if (row == 0)
dirDown = true;
if (row == key - 1)
dirDown = false;

// place the marker


rail[row][col++] = '*';
// find the next row using direction flag
if (dirDown)
row++;
else
row--;
}

// now we can construct the fill the rail matrix


int index = 0;
for (int i = 0; i < key; i++)
for (int j = 0; j < cipher.length(); j++)
if (rail[i][j] == '*'
&& index < cipher.length())
rail[i][j] = cipher.charAt(index++);

StringBuilder result = new StringBuilder();

row = 0;
col = 0;
for (int i = 0; i < cipher.length(); i++) {
// check the direction of flow
if (row == 0)
dirDown = true;
if (row == key - 1)
dirDown = false;

// place the marker


if (rail[row][col] != '*')
result.append(rail[row][col++]);

// find the next row using direction flag


if (dirDown)
row++;
else
row--;
}
return result.toString();
}

// driver program to check the above functions


public static void main(String[] args)
{

// Encryption
System.out.println("Encrypted Message: ");
System.out.println(
encryptRailFence("attack at once", 2));
System.out.println(
encryptRailFence("GeeksforGeeks ", 3));
System.out.println(
encryptRailFence("defend the east wall", 3));

// Now decryption of the same cipher-text


System.out.println("\nDecrypted Message: ");
System.out.println(
decryptRailFence("atc toctaka ne", 2));
System.out.println(
decryptRailFence("GsGsekfrek eoe", 3));
System.out.println(
decryptRailFence("dnhaweedtees alf tl", 3));
}
}

Code Output -

[B] Cryptanalysis of Cipher –


1. Brute Force Attack: This attack involves trying all possible keys until the correct one is
found. It is a time-consuming method but can be effective against weak ciphers.

2. Known Plaintext Attack: In this attack, the attacker has access to both the plaintext and its
corresponding ciphertext. By analyzing multiple pairs of plaintext-ciphertext, the attacker
tries to deduce the encryption key.

3. Chosen Plaintext Attack: Here, the attacker can choose specific plaintexts and obtain their
corresponding ciphertexts. By analyzing the patterns in the ciphertexts, the attacker tries to
deduce the encryption key.

4. Chosen Ciphertext Attack: In this attack, the attacker has the ability to choose specific
ciphertexts and obtain their corresponding plaintexts. By analyzing the patterns in the
plaintexts, the attacker tries to deduce the encryption key.

5. Differential Cryptanalysis: This attack involves comparing pairs of plaintexts and their
corresponding ciphertexts to find patterns in the encryption algorithm. By analyzing the
differences between the plaintexts and ciphertexts, the attacker tries to deduce information
about the encryption key.
6. Linear Cryptanalysis: This attack exploits linear approximations in the encryption
algorithm to deduce information about the encryption key. By analyzing the linear
relationships between the plaintext, ciphertext, and key, the attacker tries to break the cipher.

7. Meet-in-the-Middle Attack: This attack involves encrypting the plaintext with all possible
keys and decrypting the ciphertext with all possible keys. By comparing the results, the
attacker tries to find a matching pair of keys, which can then be used to decrypt the
ciphertext.

CASE 1: Including Digits: It will remain same in encryption and decryption.

PlainText = " 123HelloWorld"

Code:
#include <bits/stdc++.h>
using namespace std;

string encryptRailFence(string text, int key)


{

char rail[key][(text.length())];

for (int i=0; i < key; i++)


for (int j = 0; j < text.length(); j++)
rail[i][j] = '\n';

bool dir_down = false;


int row = 0, col = 0;

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


{
if (row == 0 || row == key-1)
dir_down = !dir_down;

rail[row][col++] = text[i];

dir_down?row++ : row--;
}

string result;
for (int i=0; i < key; i++)
for (int j=0; j < text.length(); j++)
if (rail[i][j]!='\n')
result.push_back(rail[i][j]);

return result;
}

string decryptRailFence(string cipher, int key)


{
char rail[key][cipher.length()];

for (int i=0; i < key; i++)


for (int j=0; j < cipher.length(); j++)
rail[i][j] = '\n';

bool dir_down;

int row = 0, col = 0;

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


{
if (row == 0)
dir_down = true;
if (row == key-1)
dir_down = false;

rail[row][col++] = '*';

dir_down?row++ : row--;
}

int index = 0;
for (int i=0; i<key; i++)
for (int j=0; j<cipher.length(); j++)
if (rail[i][j] == '*' && index<cipher.length())
rail[i][j] = cipher[index++];

string result;

row = 0, col = 0;
for (int i=0; i< cipher.length(); i++)
{
if (row == 0)
dir_down = true;
if (row == key-1)
dir_down = false;

if (rail[row][col] != '*')
result.push_back(rail[row][col++]);

dir_down?row++: row--;
}
return result;
}

int main()
{
int key =3;
string plainText = "123HelloWorld";
cout<<"PlainText: "<<plainText<<endl;

string cipherText = encryptRailFence(plainText, key);


cout << "Encrypted Text: "<< cipherText << endl;

string decryptedText = decryptRailFence(cipherText,key);


cout << "Decrypted Text: "<< decryptedText << endl;

return 0;
}

Output:

CASE 2: Including Special Characters: It will remain same in encryption and decryption.

PlainText = “@HelloWorld# "

Code:
#include <bits/stdc++.h>
using namespace std;

string encryptRailFence(string text, int key)


{

char rail[key][(text.length())];

for (int i=0; i < key; i++)


for (int j = 0; j < text.length(); j++)
rail[i][j] = '\n';

bool dir_down = false;


int row = 0, col = 0;

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


{
if (row == 0 || row == key-1)
dir_down = !dir_down;

rail[row][col++] = text[i];

dir_down?row++ : row--;
}

string result;
for (int i=0; i < key; i++)
for (int j=0; j < text.length(); j++)
if (rail[i][j]!='\n')
result.push_back(rail[i][j]);

return result;
}

string decryptRailFence(string cipher, int key)


{
char rail[key][cipher.length()];

for (int i=0; i < key; i++)


for (int j=0; j < cipher.length(); j++)
rail[i][j] = '\n';

bool dir_down;

int row = 0, col = 0;

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


{
if (row == 0)
dir_down = true;
if (row == key-1)
dir_down = false;

rail[row][col++] = '*';

dir_down?row++ : row--;
}

int index = 0;
for (int i=0; i<key; i++)
for (int j=0; j<cipher.length(); j++)
if (rail[i][j] == '*' && index<cipher.length())
rail[i][j] = cipher[index++];

string result;

row = 0, col = 0;
for (int i=0; i< cipher.length(); i++)
{
if (row == 0)
dir_down = true;
if (row == key-1)
dir_down = false;

if (rail[row][col] != '*')
result.push_back(rail[row][col++]);
dir_down?row++: row--;
}
return result;
}

int main()
{
int key =3;
string plainText = "@HelloWorld#";
cout<<"PlainText: "<<plainText<<endl;

string cipherText = encryptRailFence(plainText, key);


cout << "Encrypted Text: "<< cipherText << endl;

string decryptedText = decryptRailFence(cipherText,key);


cout << "Decrypted Text: "<< decryptedText << endl;

return 0;
}

Output:

CASE 3: Including White Space: It will remain same in encryption and decryption.

PlainText = “ Hello World "

Code:
#include <bits/stdc++.h>
using namespace std;

string encryptRailFence(string text, int key)


{

char rail[key][(text.length())];

for (int i=0; i < key; i++)


for (int j = 0; j < text.length(); j++)
rail[i][j] = '\n';

bool dir_down = false;


int row = 0, col = 0;

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


{
if (row == 0 || row == key-1)
dir_down = !dir_down;

rail[row][col++] = text[i];

dir_down?row++ : row--;
}

string result;
for (int i=0; i < key; i++)
for (int j=0; j < text.length(); j++)
if (rail[i][j]!='\n')
result.push_back(rail[i][j]);

return result;
}

string decryptRailFence(string cipher, int key)


{
char rail[key][cipher.length()];

for (int i=0; i < key; i++)


for (int j=0; j < cipher.length(); j++)
rail[i][j] = '\n';

bool dir_down;

int row = 0, col = 0;

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


{
if (row == 0)
dir_down = true;
if (row == key-1)
dir_down = false;

rail[row][col++] = '*';

dir_down?row++ : row--;
}

int index = 0;
for (int i=0; i<key; i++)
for (int j=0; j<cipher.length(); j++)
if (rail[i][j] == '*' && index<cipher.length())
rail[i][j] = cipher[index++];

string result;
row = 0, col = 0;
for (int i=0; i< cipher.length(); i++)
{
if (row == 0)
dir_down = true;
if (row == key-1)
dir_down = false;

if (rail[row][col] != '*')
result.push_back(rail[row][col++]);

dir_down?row++: row--;
}
return result;
}

int main()
{
int key =3;
string plainText = " Hello World ";
cout<<"PlainText: "<<plainText<<endl;

string cipherText = encryptRailFence(plainText, key);


cout << "Encrypted Text: "<< cipherText << endl;

string decryptedText = decryptRailFence(cipherText,key);


cout << "Decrypted Text: "<< decryptedText << endl;

return 0;
}

Output:

You might also like