Вы находитесь на странице: 1из 9

CRYPTOGRAPHY

FUNDAMENTALS

Lab report: Assessment- 2

Experiment : Affine Cipher and


Hill Cipher

Devang Pirta

Reg no: 17BCI0125


Problem:
To implement Affine Cipher technique.

Affine Cipher
Program:
#include <iostream>
#include <cstdio> using
namespace std;

int modInverse(int a, int m)


{
a = a % m;
for(int x = 1; x < m; x++) if
((a*x) % m == 1) return x;
}
string encrypt(int alpha, int beta, string text){ text =
text + ' ';
int lengthText = text.length(), i;
string cipher = "", temp = ""; for(i = 0; i
< lengthText; i++){ if(text[i] == ' '){
cipher = cipher + ' ' + temp; temp
= "";
}
else{
temp = temp + (char)(97 + (((int)text[i]- 97) * alpha + beta) % 26);
}
}
return cipher;
}
string decrypt(int alpha, int beta, string cipher){ cipher =
cipher + ' ';
int lengthCipher = cipher.length(), i;
string text = "", temp = ""; int mmi =
modInverse(alpha, 26); for(i = 0; i <
lengthCipher; i++){ if(cipher[i] == ' '){
text = text + ' ' + temp; temp = "";
}
else{
temp = temp + (char)(97 + ((((((int)cipher[i] - 97 - beta) * mmi) % 26) +
26) % 26));
}
}
return text;
}
int main(){
int alpha, beta, choice; string
text;
cout << "Enter the text: "; getline(cin,
text);
int alphaValues[12] = {1, 3, 5, 7, 9, 11, 15, 17, 19, 21, 23, 25}; bool
flag = false; do{
cout << "\nEnter the value of alpha and beta: ";
cin >> alpha >> beta; if(beta < 26 && beta >= 0){
int i = 0; for(;i < 12; i++){ if(alpha ==
alphaValues[i]){ flag = true;
break;
} }
if(flag){
continue;
}
else{
cout << "Invalid value of alpha" << endl;
} }
else{ flag = true;
cout << "Invalid value of beta" << endl;
}
}while(!flag);

cout << "\nEnter 0 for Encrypt and 1 for Decrypt: " << endl; cin >>
choice;
switch(choice){ case 0: cout <<
encrypt(alpha, beta, text) << endl; break; case
1: cout << decrypt(alpha, beta, text) << endl;
break; default:
cout << "Wrong choice" << endl;
}}

Sample runs:
Problem :
To implement Hill Cipher technique.

Hill Cipher
Program:
#include<iostream>
#include<math.h>
using namespace std;

float cipher[3][1], text[3][1], a[3][3], b[3][3], mes[3][1], c[3][3];


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];
} void
encryption(){ int
i, j, k;

for(i = 0; i < 3; i++) for(j =


0; j < 1; j++) for(k = 0; k <
3; k++)
cipher[i][j] = cipher[i][j] + a[i][k] * mes[k][j];
cout<<"\nEncrypted string is: ";
for(i = 0; i < 3; i++)
cout << (char)(fmod(cipher[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++)
text[i][j] = text[i][j] + b[i][k] * cipher[k][j];

cout<<"\nDecrypted string is: ";


for(i = 0; i < 3; i++)
cout<<(char)(fmod(text[i][0], 26) + 97);

cout<<"\n";
} void
getKeyMessage() {
int i, j; char msg[3];
cout << "Enter a 3 letter string: " << endl; cin
>> msg; for(i = 0; i < 3; i++){ mes[i][0] =
msg[i] - 97;
}
cout << "Enter 3x3 invertible key matrix: " << endl;
for(i = 0; i < 3; i++){ for(j = 0; j < 3; j++){
scanf("%f", &a[i][j]); c[i][j] = a[i][j];
}
}
} int main(){ int
choice;
getKeyMessage();
cout << "\nEnter 0 for Encrypt and 1 for Decrypt: " << endl; cin >>
choice;
switch(choice){
case 0:
encryption();
break; case 1:
decryption();
break; default:
cout << "Wrong choice" << endl;
}}

Sample runs:

Вам также может понравиться