21DCS099-CRNS Practical 3
21DCS099-CRNS Practical 3
21DCS099-CRNS Practical 3
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.
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 {
return result.toString();
}
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;
// 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));
Code Output -
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.
Code:
#include <bits/stdc++.h>
using namespace std;
char rail[key][(text.length())];
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;
}
bool dir_down;
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;
return 0;
}
Output:
CASE 2: Including Special Characters: It will remain same in encryption and decryption.
Code:
#include <bits/stdc++.h>
using namespace std;
char rail[key][(text.length())];
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;
}
bool dir_down;
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;
return 0;
}
Output:
CASE 3: Including White Space: It will remain same in encryption and decryption.
Code:
#include <bits/stdc++.h>
using namespace std;
char rail[key][(text.length())];
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;
}
bool dir_down;
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;
return 0;
}
Output: