0% found this document useful (0 votes)
78 views18 pages

Lab Assessment - 1: Classical Encryption

Here are the steps I would take to perform a single letter frequency attack to decipher the given message: 1. Count the frequency of each letter in the encrypted message and note which letter appears most often. In English text, the most common letter is E. 2. Look for letters in the encrypted text that appear much more frequently than other letters. This likely corresponds to the letter E in the original plaintext. 3. Identify which letters in the encrypted text correspond to other common letters like T, A, O, I, N, S, etc. based on their relative frequencies. 4. Using the letters identified in steps 2 and 3 as a starting point, make guesses about what words or phrases the
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
78 views18 pages

Lab Assessment - 1: Classical Encryption

Here are the steps I would take to perform a single letter frequency attack to decipher the given message: 1. Count the frequency of each letter in the encrypted message and note which letter appears most often. In English text, the most common letter is E. 2. Look for letters in the encrypted text that appear much more frequently than other letters. This likely corresponds to the letter E in the original plaintext. 3. Identify which letters in the encrypted text correspond to other common letters like T, A, O, I, N, S, etc. based on their relative frequencies. 4. Using the letters identified in steps 2 and 3 as a starting point, make guesses about what words or phrases the
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 18

LAB ASSESSMENT – 1

Fall Semester 2021-2022

Classical Encryption

1.Encode and decode a message Using

a. Playfair cipher

C Code:

#include <stdio.h>
#include <stdlib.h>
#include <string.h>

#define SIZE 30

// Function to convert the string to Tolowercase


void Tolowercase(char plain[], int ps)
{
int i;
for (i = 0; i < ps; i++) {
if (plain[i] > 64 && plain[i] < 91)
plain[i] += 32;
}
}

// Function to remove all spaces in a string


int removeSpaces(char* plain, int ps)
{
int i, count = 0;
for (i = 0; i < ps; i++)
if (plain[i] != ' ')
plain[count++] = plain[i];
plain[count] = '\0';
return count;
}

// Function to generate the 5x5 key square


void generateKeyTable(char key[], int ks, char keyT[5][5])
{
int i, j, k, flag = 0, *dicty;

// 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;

for (k = 0; k < ks; k++) {


if (dicty[key[k] - 97] == 2) {
dicty[key[k] - 97] -= 1;
keyT[i][j] = key[k];
j++;
if (j == 5) {
i++;
j = 0;
}
}
}

for (k = 0; k < 26; k++) {


if (dicty[k] == 0) {
keyT[i][j] = (char)(k + 97);
j++;
if (j == 5) {
i++;
j = 0;
}
}
}
}

// Function to search for the characters of a digraph


// in the key square and return their position
void search(char keyT[5][5], char a, char b, int arr[])
{
int i, j;

if (a == 'j')
a = 'i';
else if (b == 'j')
b = 'i';

for (i = 0; i < 5; i++) {

for (j = 0; j < 5; j++) {

if (keyT[i][j] == a) {
arr[0] = i;
arr[1] = j;
}
else if (keyT[i][j] == b) {
arr[2] = i;
arr[3] = j;
}
}
}
}

// Function to find the modulus with 5


int mod5(int a)
{
return (a % 5);
}

// Function to make the plain text length to be even


int prepare(char str[], int ptrs)
{
if (ptrs % 2 != 0) {
str[ptrs++] = 'z';
str[ptrs] = '\0';
}
return ptrs;
}

// Function for performing the encryption


void encrypt(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]];
}
}
}
// 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);

generateKeyTable(key, ks, keyT);

encrypt(str, keyT, 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]];
}
}
}

// Function to call decrypt


void decryptByPlayfairCipher(char str[], char key[])
{
char ps, ks, keyT[5][5];
// Key
ks = strlen(key);
ks = removeSpaces(key, ks);
Tolowercase(key, ks);

// ciphertext
ps = strlen(str);
Tolowercase(str, ps);
ps = removeSpaces(str, ps);

generateKeyTable(key, ks, keyT);

decrypt(str, keyT, ps);


}
// Driver code
int main()
{
int choice=0;
char str[SIZE], key[SIZE];
while(1){
printf("Enter your choice \n 1.Encryption \n 2.Decryption: \n 3.EXIT \n");
scanf("%d", &choice);
if(choice==1)
{
strcpy(key, "ashna");
printf("Key text: %s\n", key);
// Plaintext to be encrypted
strcpy(str, "ashnachoudhary");
printf("Plain text: %s\n", str);
encryptByPlayfairCipher(str, key);
printf("Cipher text: %s\n", str);
}
else if(choice==2)
{
// Key to be encrypted
strcpy(key, "ashna");
printf("Key text: %s\n", key);
// Ciphertext to be decrypted
strcpy(str, "shnbciblqgnstx");
printf("Plain text: %s\n", str);
// encrypt using Playfair Cipher
decryptByPlayfairCipher(str, key);
printf("Deciphered text: %s\n", str);
}
}
return 0;
}
Output Screen :
b. Hill cipher

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:

You might also like