You are on page 1of 9

TUTORIAL 1

1.A) Use the additive cipher with key=7 to encrypt the message “hello”.

#include<bits/stdc++.h>
using namespace std;
void encrypt()
{
string s;
int key;
cout<<"Enter string and key"<<endl;
cin>>s;
cin>>key;
int n = s.length();
for(int i=0;i<n;i++)
{
cout<<(char)((s[i] + 'a' + key)%26 +97);
}
cout<<endl<<endl;
}
void decrypt()
{
string s;
int key;
cin>>s;
cin>>key;
int n = s.length();
for(int i=0;i<n;i++)
{
cout<<(char)((s[i] - 'a' + key)%26 +97);
}
}
int main()
{
encrypt();
decrypt();
return 0;
}

OUTPUT
Enter string and key
hello 7
axeeh

1
1.B) Apply Brute force attack for the cipher text “jyfwavnyhwof”.

#include<bits/stdc++.h>
using namespace std;
int main()
{
string s;
cin>>s;
int n = s.length();
cout<<"List of possible words : "<<endl;
for(int i=0;i<30;i++)
{
for(int j=0;j<n;j++)
{
cout<<(char)((s[j] - 'a' + i)%26 +97);
}
cout<<endl;
}
return 0;

OUTPUT

Input String - jyfwavnyhwof

cryptography

2
TUTORIAL 2
2.A) Apply known plain text attack for –

“ P ht h alhjoly dvyrpun ha UPL. Aopz jvsslnl pz pu Tfzvyl. Aol zabkluaz zabkfpun ha UPL hyl
nvvk.”

Known Plain Text - students

Cipher Text - zabkluaz

Since we know that every alphabet in the cipher text is 7 letters ahead of the corresponding letter
in plain text then we can apply simple brute force to convert cipher text alphabets into plain text
alphabets.

Plain Text

“I am a teacher at NIE. This college is in Mysore. The students studying at NIE are good.”

3
2.B) Implement Multiplicative Cipher.

#include<bits/stdc++.h>
using namespace std;
void encrypt()
{
string s;
int key;
cout<<"Enter string and key"<<endl;
cin>>s>>key;
int n = s.length();
for(int i=0;i<n;i++)
cout<<(char)(((s[i] - 'a')*key)%26 + 97);
cout<<endl;
}
void decrypt()
{
string s;
int key;
cin>>s>>key;
int n = s.length();
int inverse;
for(inverse = 1 ;; inverse++)
{
if((inverse*key)%26 == 1)
break;
}
for(int i=0;i<n;i++)
cout<<(char)(((s[i] - 'a')*inverse)%26 +97);
}
int main()
{
encrypt();
decrypt();
return 0;
}

OUTPUT

Enter string and key

hello 7

xczzu

4
TUTORIAL 3
3.A) Implement Hill Cipher.

#include<bits/stdc++.h>
using namespace std;
float en[3][1], de[3][1], a[3][3], b[3][3], msg[3][1], m[3][3];
void getKeyMat() {
int i, j;
char mes[3];
cout<<"Enter 3x3 matrix for key (should have inverse):\n";
for(i = 0; i < 3; i++)
for(j = 0; j < 3; j++) {
cin>>a[i][j];
m[i][j] = a[i][j]; }
cout<<"\nEnter a string of 3 letter(use A through Z): ";
cin>>mes;
for(i = 0; i < 3; i++)
{msg[i][0] = mes[i] - 65;
}}
void encrypt() {
int i, j, k;
for(i = 0; i < 3; i++)
for(j = 0; j < 1; j++)
for(k = 0; k < 3; k++)
en[i][j] = en[i][j] + a[i][k] * msg[k][j];
cout<<"\nEncrypted string is: ";
for(i = 0; i < 3; i++)
cout<<(char)(fmod(en[i][0], 26) + 65);
}

void inversematrix()
{
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;

5
}
for(k = 0; k < 3; k++) {
for(i = 0; i < 3; i++) {
p = m[i][k];
q = m[k][k];
for(j = 0; j < 3; j++) {
if(i != k) {
m[i][j] = m[i][j]*q - p*m[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] / m[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";
}
}
void decrypt() {
int i, j, k;
inversematrix();
for(i = 0; i < 3; i++)
for(j = 0; j < 1; j++)
for(k = 0; k < 3; k++)
de[i][j] = de[i][j] + b[i][k] * en[k][j];
cout<<"\nDecrypted string is: ";
for(i = 0; i < 3; i++)
cout<<(char)(fmod(de[i][0], 26) + 65);
cout<<"\n";
}
int main() {
getKeyMat();
encrypt();
decrypt();
}

6
3.B) Apply know Plain Text Attack on Play Fair Cipher.

Key Matrix:

R E F A V

G H B C D

M N I/J K L

S T O P Q

Y Z U W X

Plaintext:

Commander in chief fleet to headquarters most immediate torpedo hit right aft

7
TUTORIAL 4
4.A) Implement Transposition Cipher.

#include<bits/stdc++.h>
using namespace std;
void encrypt()
{
string key;
string msg;
cout<<"Enter string and key"<<endl;
cin>>msg>>key;
int n=key.length();
char arr[n][n];
int k=0;
for(int i=0;i<n;i++)
for(int j=0;j<n;j++)
{
if((msg[k]-'a')<97 && (msg[k]-'a')>122)
arr[i][j]='_';
else
arr[i][j]=msg[k];
++k;
}
cout<<"Enter the order"<<endl;
string order,res="";
cin>>order;
for(int i=0;i<order.length();i++)
{
for(int j=0;j<n;j++){
int col=order[i]-'1';
res+=arr[j][col];
} }
cout<<"Encrypted string is "<<res<<endl;
}
int main()
{
encrypt();
return 0;
}

8
TUTORIAL 5

5.A) Implement RSA.

#include<bits/stdc++.h>
using namespace std;
int main()
{
int p,q,n,fin,i,j,d,e;
cout<<"Enter two unique prime numbers"<<endl;
cin>>p>>q;
n = p*q;
fin = (p-1)*(q-1);
cout<<"Choose public key (any prime number between 1 and "<<fin<<") :"<<endl;
cin>>e;
for(int i=1;;i++)
{
if(((1+i*fin)/e) == ceil((1 + i*fin)/e))
{
d = (1+i*fin)/e;
break;
}
}
cout<<"Private key : "<<d<<endl;
cout<<"Enter plain text\n";
char pt,ct;
cin>>pt;
int pit = pt - 'a';
int cit = ((int)(pow(pit,e))%n);
cit = cit + '0';
cout<<"Cipher Text :"<<ct<<endl;
cout<<"Decrypting now"<<endl;
pit = ((int)(pow(cit,d))%n);
pit = pit +'a';
cout<<pt;
return 0;
}

You might also like