Lab Assessment - 1: Classical Encryption
Lab Assessment - 1: Classical Encryption
Classical Encryption
a. Playfair cipher
C Code:
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#define SIZE 30
// a 26 character hashmap
// to store count of the alphabet
dicty = (int*)calloc(26, sizeof(int));
for (i = 0; i < ks; i++) {
if (key[i] != 'j')
dicty[key[i] - 97] = 2;
}
dicty['j' - 97] = 1;
i = 0;
j = 0;
if (a == 'j')
a = 'i';
else if (b == 'j')
b = 'i';
if (keyT[i][j] == a) {
arr[0] = i;
arr[1] = j;
}
else if (keyT[i][j] == b) {
arr[2] = i;
arr[3] = j;
}
}
}
}
if (a[0] == a[2]) {
str[i] = keyT[a[0]][mod5(a[1] + 1)];
str[i + 1] = keyT[a[0]][mod5(a[3] + 1)];
}
else if (a[1] == a[3]) {
str[i] = keyT[mod5(a[0] + 1)][a[1]];
str[i + 1] = keyT[mod5(a[2] + 1)][a[1]];
}
else {
str[i] = keyT[a[0]][a[3]];
str[i + 1] = keyT[a[2]][a[1]];
}
}
}
// Function to encrypt using Playfair Cipher
void encryptByPlayfairCipher(char str[], char key[])
{
char ps, ks, keyT[5][5];
// Key
ks = strlen(key);
ks = removeSpaces(key, ks);
Tolowercase(key, ks);
// Plaintext
ps = strlen(str);
Tolowercase(str, ps);
ps = removeSpaces(str, ps);
ps = prepare(str, ps);
// Function to decrypt
void decrypt(char str[], char keyT[5][5], int ps)
{
int i, a[4];
for (i = 0; i < ps; i += 2) {
search(keyT, str[i], str[i + 1], a);
if (a[0] == a[2]) {
str[i] = keyT[a[0]][mod5(a[1] - 1)];
str[i + 1] = keyT[a[0]][mod5(a[3] - 1)];
}
else if (a[1] == a[3]) {
str[i] = keyT[mod5(a[0] - 1)][a[1]];
str[i + 1] = keyT[mod5(a[2] - 1)][a[1]];
}
else {
str[i] = keyT[a[0]][a[3]];
str[i + 1] = keyT[a[2]][a[1]];
}
}
}
// ciphertext
ps = strlen(str);
Tolowercase(str, ps);
ps = removeSpaces(str, ps);
C++ Code:
#include<iostream>
#include<math.h>
using namespace std;
float en[3][1], de[3][1], a[3][3], b[3][3], msg[3][1], m[3][3];
void getKeyMatrix() { //get key and message from user
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() { //encrypts the message
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); //modulo 26 is taken for each element of the matrix
obtained by multiplication
}
void inversematrix() { //find inverse of key matrix
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 = 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() { //decrypt the message
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); //modulo 26 is taken to get the original message
cout<<"\n";
}
int main() {
getKeyMatrix();
encrypt();
decrypt();
}
Output:
c. Vigenere cipher
#include<stdio.h>
#include<string.h>
int main(){
char msg[] = "HELLOFROMTHEOTHERSIDE";
char key[] = "ASHNA";
int msgLen = strlen(msg), keyLen = strlen(key), i, j;
char newKey[msgLen], encryptedMsg[msgLen], decryptedMsg[msgLen];
for(i = 0, j = 0; i < msgLen; ++i, ++j){
if(j == keyLen)
j = 0;
newKey[i] = key[j];
}
newKey[i] = '\0';
for(i = 0; i < msgLen; ++i)
encryptedMsg[i] = ((msg[i] + newKey[i]) % 26) + 'A';
encryptedMsg[i] = '\0';
for(i = 0; i < msgLen; ++i)
decryptedMsg[i] = (((encryptedMsg[i] - newKey[i]) + 26) % 26) + 'A';
decryptedMsg[i] = '\0';
printf("Original Text: %s", msg);
printf("\nKey: %s", key);
printf("\nNew Generated Key: %s", newKey);
printf("\nEncrypted Message: %s", encryptedMsg);
printf("\nDecrypted Message: %s", decryptedMsg);
return 0;
}
Output:
d. Vernam Cipher
C Code:
#include<stdio.h>
char arr[26][26];
char message[22],key[22],emessage[22],retMessage[22];
int findRow(char);
int findColumn(char);
int findDecRow(char,int);
int main() {
int i=0,j,k,r,c;
k=96;
for (i=0;i<26;i++) {
k++;
for (j=0;j<26;j++) {
arr[i][j]=k++;
if(k==123)
k=97;
}
}
printf("\nEnter message\n");
gets(message);
printf("\nEnter the key\n");
gets(key);
// Encryption
for (i=0;key[i]!=NULL;i++) {
c=findRow(key[i]);
r=findColumn(message[i]);
emessage[i]=arr[r][c];
}
emessage[i]='\0';
printf("\n Encrypted message is:\n\n");
for (i=0;emessage[i]!=NULL;i++)
printf("%c",emessage[i]);
//decryption
for (i=0;key[i]!=NULL;i++) {
c=findColumn(key[i]);
r=findDecRow(emessage[i],c);
retMessage[i]=arr[r][0];
}
retMessage[i]='\0';
printf("\n\nMessage Retrieved is:\n\n");
for (i=0;retMessage[i]!=NULL;i++)
printf("%c",retMessage[i]);
getch();
return(0);
}
int findRow(char c) {
int i;
for (i=0;i<26;i++) {
if(arr[0][i]==c)
return(i);
}
}
int findColumn(char c) {
int i;
for (i=0;i<26;i++) {
if(arr[i][0]==c)
return(i);
}
}
int findDecRow(char c,int j) {
int i;
for (i=0;i<26;i++) {
if(arr[i][j]==c)
return(i);
}
}
Output:
2. Use single letter frequency attack to decipher the following message. You know that it has been
created with an additive (Shift) cipher.
PXPXKXENVDRUXVTNLXHYMXGMAXYKXJNXGVRFXMAHWGXXWLEHGZXKVBIAXKMXQM
Python Code:
message='PXPXKXENVDRUXVTNLXHYMXGMAXYKXJNXGVRFXMAHWGXXWLEHGZXKVBIAXKMXQM'
LETTERS='ABCDEFGHIJKLMNOPQRSTUVWXYZ'
FREQ='ETAOINSHRLDCUMWFGYPBVKJXQZ'
box=''
box2=[]
j=0
for q in FREQ:
w=LETTERS.find(q)
key=0
box2.append(w)
w=key
print('KEYS:-'+str(box2)+'\n\n\n DECRYPTION:-')
for key in box2:
if len(box)==len(message):
j=j+1
print('***'*10)
print(box)
box=''
for x in message:
y=LETTERS.find(x)
d=(y-key)%26
a=LETTERS[d]
box=box+str(a)
Output:
C Code:
#include <stdio.h>
#include <stdlib.h>
int main() {
char str[100],name[100];
int i,key;
int freq[256] = {0};
gets(str);
for(i = 0; str[i] != '\0'; i++)
{
freq[str[i]]++;
}
int m=freq[0];
char c=str[0];
for(i = 0; str[i]!='\0'; i++)
{
if(freq[str[i]]>m)
{
m=freq[str[i]];
c=str[i];
}
}
if(c<'E')
key='E'-c;
else
key=c-'E';
for(i=0;str[i]!='\0';i++)
{
char ch=str[i]-key;
if(ch<65)
ch=ch+26;
printf("%c",ch);
}
return 0;
}
Output: