Ins Lab File
Ins Lab File
1
Index
2
Experiment-1
Aim - To implement Caesar cipher encryption and decryption.
Theory - The Caesar cipher is one of the simplest and oldest encryption techniques. It is a substitution
cipher where each letter in the plaintext is "shifted" a certain number of places down or up the alphabet.
For example, with a shift of 3, 'A' would be replaced by 'D', 'B' by 'E', and so on. If the end of the alphabet
is reached, it wraps around (e.g., 'Z' with a shift of 3 would become 'C').
Code -
#include <iostream>
#include <string>
using namespace std;
3
}
int main() {
string text;
int shift;
return 0;
}
Output :
4
Experiment-2
Aim - To implement Monoalphabetic Cipher decryption, where each letter in the alphabet is substituted by
exactly one other letter.
Theory - The Monoalphabetic Cipher is a substitution cipher where each character in the plaintext is
replaced by a corresponding character from a substitution alphabet (key). This substitution remains
constant throughout the encryption or decryption process. For decryption, each letter in the ciphertext is
replaced with the corresponding letter from the reverse mapping of the substitution alphabet.
Code -
#include <iostream>
#include <string>
#include <unordered_map>
using namespace std;
return decryptMap;
}
5
int main() {
string key = "QWERTYUIOPLKJHGFDSAZXCVBNM"; // Example substitution
key string ciphertext;
return 0;
}
Output:
6
Experiment-3
Aim - To implement PlayFair cipher using encryption decryption.
Theory - Each letter pair (digraph) in the plaintext is substituted based on rules derived from their
positions in the key square:
Same Row: Replace each letter with the letter to its immediate right (wrapping to the beginning of the
row if necessary).
Same Column: Replace each letter with the letter immediately below it (wrapping to the top if
necessary).
Rectangle: If the letters form a rectangle, replace each with the letter on the same row but in the column
of the other letter in the digraph.
Code -
#include <iostream>
#include <unordered_map>
#include <string>
using namespace std;
class PlayfairCipher {
char matrix[5][5];
unordered_map<char, pair<int, int>> pos;
7
string processDigraph(char a, char b, int dir) {
int r1 = pos[a].first, c1 = pos[a].second;
int r2 = pos[b].first, c2 = pos[b].second;
Output:
8
EXPERIMENT-4
Aim - To implement Polyalphabetic cipher encryption decryption. Encryption/Decryption: Based on
substitution, using multiple substitution Alphabets
Theory - The Polyalphabetic Cipher, represented here by the Vigenère Cipher, uses multiple substitution
alphabets to encrypt plaintext. The key is repeated to match the length of the plaintext, and each letter in
the plaintext is shifted according to the corresponding letter in the key. For example, if the key is "KEY"
and the plaintext is "HELLO", the key repeats to "KEYKE" to match the length of "HELLO".
Code :
#include<stdio.h>
#include<conio.h>
#include<string.h>
void main()
{
char msg[30],key[30],k[20],ct[20],pt[20];
int lenm,lenk,i,j;
clrscr();
9
printf("\nEncrypted Message : %s", ct);
printf("\nDecrypted Message : %s", pt);
getch();
}
Output:
10
Experiment-5
Aim - To implement Hill Cipher encryption decryption.
Theory - Hill cipher is a polygraphic substitution cipher based on linear algebra.Each letter is represented
by a number modulo 26. Often the simple scheme A = 0, B = 1….. Z = 25 is used, but this is not an
essential feature of the cipher. To encrypt a message, each block of n letters (considered as an n-
component vector) is multiplied by an invertible n × n matrix, against modulus 26. To decrypt the message,
each block is multiplied by the inverse of the matrix used for encryption.
Code -
#include<iostream>
#include<math.h>
using namespace std;
float encrypt[3][1], decrypt[3][1], a[3][3], b[3][3], mes[3][1], c[3][3];
void encryption(); //encrypts the message
void decryption(); //decrypts the message
void getKeyMessage(); //gets key and message from user
void inverse(); //finds inverse of key matrix
int main() {
getKeyMessage();
encryption();
decryption();
}
void encryption() {
int i, j, k;
for(i = 0; i< 3; i++)
for(j = 0; j < 1; j++)
for(k = 0; k < 3; k++)
encrypt[i][j] = encrypt[i][j] + a[i][k] * mes[k][j];
cout<<"\n Encrypted string is: ";
for(i = 0; i< 3; i++)
cout<<(char)(fmod(encrypt[i][0], 26) + 97);
}
void decryption() {
int i, j, k;
inverse();
for(i = 0; i< 3; i++)
for(j = 0; j < 1; j++)
for(k = 0; k < 3; k++)
decrypt[i][j] = decrypt[i][j] + b[i][k] * encrypt[k][j];
cout<<"\nDecrypted string is: ";
for(i = 0; i< 3; i++)
cout<<(char)(fmod(decrypt[i][0], 26) + 97);
cout<<"\n";
11
void getKeyMessage() {
int i, j;
char msg[3];
cout<<"Enter 3x3 matrix for key (It should be inversible):\n";
for(i = 0; i< 3; i++)
for(j = 0; j < 3; j++) {
cin>>a[i][j];
c[i][j] = a[i][j];
}
cout<<"\nEnter a 3 letter string: ";
cin>>msg;
for(i = 0; i< 3; i++)
mes[i][0] = msg[i] - 97;
}
void inverse() {
int i, j, k;
float p, q;
for(i = 0; i< 3; i++)
for(j = 0; j < 3; j++) {
if(i == j)
b[i][j]=1;
else
b[i][j]=0;
}
for(k = 0; k < 3; k++) {
for(i = 0; i< 3; i++) {
p = c[i][k];
q = c[k][k];
for(j = 0; j < 3; j++) {
if(i != k) {
c[i][j] = c[i][j]*q - p*c[k][j];
b[i][j] = b[i][j]*q - p*b[k][j];
}
}
}
}
for(i = 0; i< 3; i++)
for(j = 0; j < 3; j++)
b[i][j] = b[i][j] / c[i][i];
cout<<"\n\nInverse Matrix is:\n";
for(i = 0; i< 3; i++) {
for(j = 0; j < 3; j++)
cout<<b[i][j]<<" ";
cout<<"\n";
}
}
12
OUTPUT
13
Experiment-6
Aim - To implement the subkey generation for Simplified DES (S-DES).
Theory - Simplified Data Encryption Standard (S-DES) is a simple version of the DES Algorithm. It is
similar to the DES algorithm but is a smaller algorithm and has fewer parameters than DES. It was made
for educational purposes so that understanding DES would become simpler. It is a block cipher that
takes a block of plain text and converts it into ciphertext. It takes a block of 8bit.
It is a symmetric key cipher i.e. they use the same key for both encryption and decryption. In this article,
we are going to demonstrate key generation for s-des encryption and decryption algorithm. We take a
random 10-bit key and produce two 8-bit keys which will be used for encryption and decryption.
Code -
#include <iostream>
#include <vector>
using namespace std;
14
// Step 2: Split into two 5-bit halves
vector<int> leftHalf(permutedKey.begin(), permutedKey.begin() + 5);
vector<int> rightHalf(permutedKey.begin() + 5, permutedKey.end());
int main() {
// Input 10-bit key for S-DES (e.g., 1010000010)
vector<int> key = {1, 0, 1, 0, 0, 0, 0, 0, 1, 0};
// Display subkeys
displayKey(K1,
"K1");
displayKey(K2,
"K2");
return 0;
}
Output:
15
Experiment-7
Aim - To implement Diffie Hallman key exchange algorithm.
Theory - The Diffie–Hellman (DH) Algorithm is a key-exchange protocol that enables two parties
communicating over public channel to establish a mutual secret without it being transmitted over the
Internet. DH enables the two to use a public key to encrypt and decrypt their conversation or data using
symmetric cryptography.
Diffie-Hallman is generally explained by two sample parties, Alice and Bob, initiating a dialogue. Each has
a piece of information they want to share, while preserving its secrecy. To do that they agree on a public
piece of benign information that will be mixed with their privileged information as it travels over an
insecure channel.
Code –
#include <cmath>
#include <iostream>
using namespace std;
// Power function to return value of a ^ b mod P long long
int power(long long int a, long long int b,
long long int P)
{
if (b == 1)
return a;
else
(longint)pow(a, b)) % P);
}
return (((long
16
Output:
17
Experiment-8
Aim - To implement RSA encryption decryption
Theory - RSA algorithm is an asymmetric cryptography algorithm. Asymmetric actually means that it
works on two different keys i.e. Public Key and Private Key. As the name describes that the Public Key
is given to everyone and the Private key is kept private.
A client (for example browser) sends its public key to the server and requests some data. The server
encrypts the data using the client’s public key and sends the encrypted data. The client receives this
data and decrypts it.
Since this is asymmetric, nobody else except the browser can decrypt the data even if a third party has
the public key of the browser.
The idea! The idea of RSA is based on the fact that it is difficult to factorize a large integer. The public
key consists of two numbers where one number is a multiplication of two large prime numbers. And
private key is also derived from the same two prime numbers. So if somebody can factorize the large
number, the private key is compromised. Therefore encryption strength totally lies on the key size and
if we double or triple the key size, the strength of encryption increases exponentially. RSA keys can be
typically 1024 or 2048 bits long, but experts believe that 1024- bit keys could be broken in the near
future. But till now it seems to be an infeasible task.
ALGORITHM:
Begin
1. Choose two prime numbers p and q.
2. Compute n = p*q.
3. Calculate phi = (p-1) * (q-1).
4. Choose an integer e such that 1 < e < phi(n) and gcd(e, phi(n)) = 1; i.e., e and phi(n)
arecoprime.
5. Calculate d as d ≡ e−1 (mod phi(n)); here, d is the modular multiplicative inverse of e
modulo phi(n).
6. For encryption, c = me mod n, where m = original message.
7. For decryption, m = c d mod
n.End
18
Code -
#include<iostream>
#include<math.h> using
namespace std;
// find gcd
int gcd(int a, int b) {
int t;
while(1) {
t= a%b;
if(t==0)
return b;
a = b; b=
t;
}
}
int main() {
//2 random prime numbers
double p = 13;
double q = 11;
double n=p*q;//calculate n
double track;
double phi= (p-1)*(q-1);//calculate phi
//public key
//e stands for encrypt
double e=7;
//for checking that 1 < e < phi(n) and gcd(e, phi(n)) = 1; i.e., e and phi(n) are coprime.
while(e<phi) {
track = gcd(e,phi);
if(track==1)
break;
else
e++;
}
//private key
//d stands for decrypt
//choosing d such that it satisfies d*e = 1 mod phi
double d1=1/e;
double d=fmod(d1,phi);
double message = 9;
double c = pow(message,e); //encrypt the message
double m = pow(c,d);
c=fmod(c,n);
m=fmod(m,n);
cout<<"Original Message = "<<message;
cout<<"\n"<<"p = "<<p;
cout<<"\n"<<"q = "<<q;
cout<<"\n"<<"n = pq = "<<n;
cout<<"\n"<<"phi = "<<phi;
cout<<"\n"<<"e = "<<e;
cout<<"\n"<<"d = "<<d;
cout<<"\n"<<"Encrypted message = "<<c;
19
cout<<"\n"<<"Decrypted message = "<<m;
return 0;
Output:
20
Experiment-9
Aim - Write a program to generate SHA-1 hash.
Theory - SHA-1 or Secure Hash Algorithm 1 is a cryptographic hash function which takes an input
and produces a 160-bit (20-byte) hash value. This hash value is known as a message digest. This
message digest is usually then rendered as a hexadecimal number which is 40 digits long. It is a
U.S. Federal Information Processing Standard and was designed by the United States National
Security Agency. In cryptography, SHA-1 (Secure Hash Algorithm 1) is a cryptographic hash
function which takes an input and produces a 160-bit (20-byte) hash value known as a message
digest – typically rendered as a hexadecimal number, 40 digits long. It was designed by the United
States National Security Agency, and is a U.S. Federal Information Processing Standard.
Since 2005, SHA-1 has not been considered secure against well-funded opponents; as of 2010
many organizations have recommended its replacement. NIST formally deprecated use of SHA-1
in 2011 and disallowed its use for digital signatures in 2013. As of 2020, chosen-prefix attacks
against SHA-1 are practical. As such, it is recommended to remove SHA-1 from products as soon
as possible and instead use SHA-2 or SHA-3. Replacing SHA-1 is urgent where it is used for
digital signatures.
All major web browser vendors ceased acceptance of SHA-1 SSL certificates in 2017. In February
2017, CWI Amsterdam and Google announced they had performed a collision attack against SHA-
1, publishing two dissimilar PDF files which produced the same SHA-1 hash. But SHA-1 is still
secure for HMAC.
Code -
#include <iostream>
#include <vector>
#include <string>
#include <iomanip>
#include <sstream>
h0 += a; h1 += b; h2 += c; h3 += d; h4 += e;
return {h0, h1, h2, h3, h4};
}
ostringstream result;
for (unsigned int h : hash) {
result << hex << setw(8) << setfill('0') << h;
}
22
return result.str();
}
Output:
23
Experiment-10
Aim - Implement a digital signature algorithm
Theory - Digital Signature Algorithm (DSA) is one of the Federal Information Processing Standard
for making digital signatures depends on the mathematical concept or we can say the formulas of
modular exponentiation and the discrete logarithm problem to cryptograph the signature digitally in
this algorithm.
Therefore, a digital signature is a technique that binds a person or entity to the digital data of the
signature. Now, this will binding can be independently verified by the receiver as well as any third
party to access that data.
Here, Digital signature is a cryptographic value that is calculated from the data and a secret key
known only by the signer or the person whose signature is that.
In fact, in the real world, the receiver of message needs assurance that the message belongs to the
sender and he should not be able to hack the origination of that message for misuse or anything.
Their requirement is very crucial in business applications or any other things since the likelihood of
a dispute over exchanged data is very high to secure that data.
Code -
#include <iostream>
#include <cmath>
#include <random>
#include <tuple>
#include <string>
#include <iomanip>
#include <sstream>
24
// DSA Key Generation
tuple<long long, long long, long long> generateKeys(long long p, long long q, long long g) {
random_device rd;
mt19937 gen(rd()); uniform_int_distribution<long
long> dist(1, q - 1);
return v == r;
}
25
Output:
26
27
28
29
30