0% found this document useful (0 votes)
156 views

Lab Fat Crypt

The document discusses several cipher techniques: Caesar cipher, which encrypts messages by shifting each letter by a key number of places; Playfair cipher, which encrypts pairs of letters based on their positions in a 5x5 grid keyed to a keyword; and Affine cipher, which encrypts messages using a mathematical function that multiplies each letter by a secret value and adds another secret value. Code examples in C/C++ are provided to implement encryption and decryption for each cipher technique.

Uploaded by

ruzzuq
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)
156 views

Lab Fat Crypt

The document discusses several cipher techniques: Caesar cipher, which encrypts messages by shifting each letter by a key number of places; Playfair cipher, which encrypts pairs of letters based on their positions in a 5x5 grid keyed to a keyword; and Affine cipher, which encrypts messages using a mathematical function that multiplies each letter by a secret value and adds another secret value. Code examples in C/C++ are provided to implement encryption and decryption for each cipher technique.

Uploaded by

ruzzuq
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/ 26

Caesar cipher

Encryption:
#include<stdio.h>

int main()
{
char message[100], ch;
int i, key;

printf("Enter a message to encrypt: ");


gets(message);
printf("Enter key: ");
scanf("%d", &key);

for(i = 0; message[i] != '\0'; ++i){


ch = message[i];

if(ch >= 'a' && ch <= 'z'){


ch = ch + key;

if(ch > 'z'){


ch = ch - 'z' + 'a' - 1;
}

message[i] = ch;
}
else if(ch >= 'A' && ch <= 'Z'){
ch = ch + key;

if(ch > 'Z'){


ch = ch - 'Z' + 'A' - 1;
}

message[i] = ch;
}
}

printf("Encrypted message: %s", message);

return 0;
}
Decryption:
#include<stdio.h>

int main()
{
char message[100], ch;
int i, key;

printf("Enter a message to decrypt: ");


gets(message);
printf("Enter key: ");
scanf("%d", &key);

for(i = 0; message[i] != '\0'; ++i){


ch = message[i];

if(ch >= 'a' && ch <= 'z'){


ch = ch - key;

if(ch < 'a'){


ch = ch + 'z' - 'a' + 1;
}

message[i] = ch;
}
else if(ch >= 'A' && ch <= 'Z'){
ch = ch - key;

if(ch < 'A'){


ch = ch + 'Z' - 'A' + 1;
}

message[i] = ch;
}
}

Playfair:

#include<bits/stdc++.h>
using namespace std;

// *~~~~~~~~ variable declaration ~~~~~~~~~~* //


char grid[5][5]; // 5x5 matrix to encipher or decipher
char keyword[26]; // cypher key
char msg[100]; // message
int mark[130],len,r,c; // necessary variables

// *~~~~~~~~~ Function declaration ~~~~~~~~~~~* //


void createGrid();
void showGrid();
void encipher();
void decipher();
void initialize();
void menu()
{
system("CLS");
int n;
string op[]={"1. Encipher","2. Decipher","3. Exit"};
cout<<op[0]<<endl<<op[1]<<endl<<op[2]<<endl<<"Enter
choice: ";
cin>>n;
if(n==1) encipher();
else if(n==2) decipher();
else exit(1);
}
void initialize()
{
memset(mark,0,sizeof(mark));
system("CLS");
}
int main()
{
menu();
return 0;
}
void decipher()
{
initialize();
createGrid();
cout<<"Cypher text: ";
char cypText[150]; // cypher text
cin>>cypText;
getchar(); // flush buffer
int l=strlen(cypText); //take length

/*
for(int i=0;i<l;i+=2)
cout<<cypText[i]<<cypText[i+1]<<" ";
cout<<endl;
*/

cout<<"Decipher text: ";


//decipher starts
int P,Q,R,S,f1,f2;
char x,y;
for(int i=0;i<l;i+=2)
{
x=cypText[i];
y=cypText[i+1];
f1=f2=0;
for(int j=0;j<5;j++)
{
for(int k=0;k<5;k++)
{
if(x==grid[j][k])
{
P=j;
Q=k;
f1=1;
}
if(y==grid[j][k])
{
R=j;
S=k;
f2=1;
}
if(f1 && f2) break;
}
if(f1 && f2) break;
}
if(P==R) //same row
{
if(Q==0) cout<<grid[P][4];
else cout<<grid[P][Q-1];
if(S==0) cout<<grid[R][4];
else cout<<grid[R][S-1];
}
else if(Q==S ) // same column
{
if(P==0) cout<<grid[4][Q];
else cout<<grid[P-1][Q];
if(R==0) cout<<grid[4][S];
else cout<<grid[R-1][S];
}
else //opposite corner
{
cout<<grid[P][S]<<grid[R][Q];
}
}
cout<<endl<<endl;
system("PAUSE");
menu();
}
void encipher()
{
initialize();
createGrid();
cout<<"Message to cypher: ";
gets(msg);
int l=strlen(msg); // msg length
char reqText[150]; //generate required text to encipher
int in=0,j=0;
for(int i=0;i<l;i++)
{
j=i+1;
if(msg[i]==' ') //ignore all space from string
{
i++;
j++;
}
if(msg[j]==' ') j++; //ignore space
if(toupper(msg[i])=='J') msg[i]='i'; // ignore J
if(toupper(msg[i])==toupper(msg[j])) // if duplicate
add 'X' after the first letter
{
reqText[in]=toupper(msg[i]);
reqText[in+1]='X';
in++;
}
else
{
reqText[in]=toupper(msg[i]);
}
in++;
}
if(in%2!=0) reqText[in]='X'; // if one character left, add
'X' after it

/*
//show generated text in pair
for(int i=0;i<in;i+=2)
cout<<reqText[i]<<reqText[i+1]<<" ";
cout<<endl;
*/

cout<<"Cypher text: ";


//encipher starts
int P,Q,R,S,f1,f2;
char x,y;
for(int i=0;i<in;i+=2)
{
x=reqText[i];
y=reqText[i+1];
f1=f2=0;
for(int j=0;j<5;j++)
{
for(int k=0;k<5;k++)
{
if(x==grid[j][k])
{
P=j;
Q=k;
f1=1;
}
if(y==grid[j][k])
{
R=j;
S=k;
f2=1;
}
if(f1 && f2) break;
}
if(f1 && f2) break;
}
if(P==R) //same row
{
if(Q==4) cout<<grid[P][0];
else cout<<grid[P][Q+1];
if(S==4) cout<<grid[R][0];
else cout<<grid[R][S+1];
}
else if(Q==S ) // same column
{
if(P==4) cout<<grid[0][Q];
else cout<<grid[P+1][Q];
if(R==4) cout<<grid[0][S];
else cout<<grid[R+1][S];
}
else //opposite corner
{
cout<<grid[P][S]<<grid[R][Q];
}
}
cout<<endl<<endl;
system("PAUSE");
menu();
}
void createGrid()
{
cout<<"Keyword: ";
cin>>keyword; // key of the cypher
getchar();
len=strlen(keyword); // size of input string O(n) :3
mark['J']=1; // ignore J
r=0,c=0; //initialize row and column

// first populate the keyword


for(int i=0;i<len;i++)
{
if(!mark[toupper(keyword[i])]) // ignore duplicates
{
mark[toupper(keyword[i])]=1;
grid[r][c++]=toupper(keyword[i]);
if(c%5==0) //increase row column
{
c=0;
r++;
}
}
}

// complete rest of the matrix from unused characters


starting from A
for(int i='A';i<='Z';i++)
{
if(mark[i]==0) // taking values that are not in the
matrix already
{
grid[r][c++]=i;
mark[i]=1;
if(c%5==0)
{
if(r==4 && c==5) break; //matrix populated

// else increase row column


r++;
c=0;
}
}
}
}
void showGrid()
{
cout<<"5x5 Matrix"<<endl;
//show grid
for(int i=0;i<5;i++)
{
for(int j=0;j<5;j++)
{
cout<<grid[i][j]<<" ";
}
cout<<endl;
}
}
Affine cipher:

1. #include<iostream>
2. #include<string.h>
3. #include<stdlib.h>
4. using namespace std;
5. string encryptionMessage(string Msg)
6. {
7. string CTxt = "";
8. int a = 3;
9. int b = 6;
10. for (int i = 0; i < Msg.length(); i++)
11. {
12. CTxt = CTxt + (char) ((((a * Msg[i]) + b) % 26) + 65);
13. }
14. return CTxt;
15. }
16.
17. string decryptionMessage(string CTxt)
18. {
19. string Msg = "";
20. int a = 3;
21. int b = 6;
22. int a_inv = 0;
23. int flag = 0;
24. for (int i = 0; i < 26; i++)
25. {
26. flag = (a * i) % 26;
27. if (flag == 1)
28. {
29. a_inv = i;
30. }
31. }
32. for (int i = 0; i < CTxt.length(); i++)
33. {
34. Msg = Msg + (char) (((a_inv * ((CTxt[i] - b)) % 26)) + 65);
35. }
36. return Msg;
37. }
38. int main(int argc, char **argv)
39. {
40. cout << "Enter the message: ";
41. string message;
42. cin >> message;
43. cout << "Message is :" << message;
44. cout << "\nEncrypted Message is : " << encryptionMessage(message);
45.
46. cout << "\nDecrypted Message is: " << decryptionMessage(
47. encryptionMessage(message));
48. }

Hill cipher:
#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<<"\nEncrypted 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";
}

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";
}
}

Vigenere cipher:
#include<iostream>
#include<string.h>

using namespace std;

int main(){
char msg[] = "mohammedruzzuq";
char key[] = "hello";
int msgLen = strlen(msg), keyLen = strlen(key), i, j;

char newKey[msgLen], encryptedMsg[msgLen], decryptedMsg[msgLen];

//generating new key


for(i = 0, j = 0; i < msgLen; ++i, ++j){
if(j == keyLen)
j = 0;

newKey[i] = key[j];
}

newKey[i] = '\0';

//encryption
for(i = 0; i < msgLen; ++i)
encryptedMsg[i] = ((msg[i] + newKey[i]) % 26) + 'A';

encryptedMsg[i] = '\0';

//decryption
for(i = 0; i < msgLen; ++i)
decryptedMsg[i] = (((encryptedMsg[i] - newKey[i]) + 26) % 26) + 'A';

decryptedMsg[i] = '\0';

cout<<"Original Message: "<<msg;


cout<<"\nKey: "<<key;
cout<<"\nNew Generated Key: "<<newKey;
cout<<"\nEncrypted Message: "<<encryptedMsg;
cout<<"\nDecrypted Message: "<<decryptedMsg;

return 0;
}
SDES

#include
<iostream>
#include <string>
#include <ctime>
#include <stdio.h>
using namespace std;

string SDESEncryption(string, string, int);


string SDESDecryption(string, string, int);
string findKey(string, int);
string functionF(string, string);
string XOR(string, string);
string S1Box(string);
string S2Box(string);
string randomString(int);
string header();

int main()
{

string plaintext = randomString(12);


cout << "Generating your plaintext and key pair." << endl
<< "Please wait..." << endl;
system("pause");
system("CLS");
string key = randomString(9);
string ciphertext;
string decryption;
int numrounds = 4;
int rounds;

cout << header();

cout << "\tOriginal Plaintext:\t" << plaintext << endl


<< "\tKey:\t\t\t" << key << endl;

cout << "\nEncryption:\n";


for(int i = 0; i < numrounds; i++)
{
plaintext = SDESEncryption(key, plaintext, i+1);
cout << "\tC (after round " << i + 1 << "):\t" << plaintext
<< endl;
rounds = i + 1;
}

ciphertext.append(plaintext, 6, 6);
ciphertext.append(plaintext, 0, 6);

cout << endl << "Ciphertext after " << rounds << " rounds:\t" <<
ciphertext << endl << endl;

cout << "Proof by decryption:\n\n"


<< "Ciphertext:\t" << ciphertext << endl << endl;

decryption.append(ciphertext, 6, 6);
decryption.append(ciphertext, 0, 6);
//Now do [numrounds] of decryption
for(int j = numrounds; j > 0; j--)
{
decryption = SDESDecryption(key, decryption, j);
if(j != 1)
cout << "\tC(after round " << j << "):\t" <<
decryption << endl;
else if(j == 1)
cout << "\nPlaintext after:\t\t" << decryption <<
endl << endl;
}
return 0;
}

string SDESEncryption(string key, string plaintext, int round)


{

string Li;
string Ri;
string Ln;
string Rn;
string K;
string f;

K = findKey(key, round);
Li.append(plaintext, 0, 6);
Ri.append(plaintext, 6, 6);

Ln = Ri;

f.append(functionF(Ri, K));

Rn.append(f);
Rn = XOR(Li, f);

return (Ln + Rn);


}

string SDESDecryption(string key, string ciphertext, int round)


{

string Li;
string Ri;
string Ln;
string Rn;
string K;
string f;

K = findKey(key, round);

Li.append(ciphertext, 0, 6);
Ri.append(ciphertext, 6, 6);

Rn = Li;

f.append(functionF(Rn, K));

Ln.append(f);
Ln = XOR(Ri, f);

return (Ln + Rn);

}
string findKey(string key, int round)
{
//Get the key for the round
string K;
if(round == 1)
K.append(key, 0, 8);
else if(round == 2)
K.append(key, 1, 8);
else if(round == 3)
{
K.append(key, 2, 7);
K.append(key, 0, 1);
}
else if(round == 4)
{
K.append(key, 3, 6);
K.append(key, 0, 2);
}
return K;
}

string functionF(string R, string K)


{
char tmp;
string s1;
string s2;

R.append(R, 4, 2);
tmp = R[3];
R[5] = R[2];
R[4] = tmp;
R[3] = R[2];
R[2] = tmp;

R = XOR(R, K);
s1.append(R, 0, 4);
s2.append(R, 4, 4);
return S1Box(s1) + S2Box(s2);
}

string XOR(string x, string y)


{
for(int i = 0; i < x.length(); i++)
{
if(x[i] == y[i])
x[i] = '0';
else if(x[i] != y[i])
x[i] = '1';
}
return x;
}

string S1Box(string s1)


{

string row1[8] = {"101", "010", "001", "110", "011", "100", "111",


"000"};
string row2[8] = {"001", "100", "110", "010", "000", "111", "101",
"011"};
int column = 0;

if(s1[0] == '0')
{

if(s1[1] == '1')
column += 4;
if(s1[2] == '1')
column += 2;
if(s1[3] == '1')
column += 1;

return row1[column];
}

else if(s1[0] == '1')


{
//Convert remaining 3 bits to decimal
if(s1[1] == '1')
column += 4;
if(s1[2] == '1')
column += 2;
if(s1[3] == '1')
column += 1;

return row2[column];
}
else

return "ERROR";
}

string S2Box(string s2)


{

string row1[8] = {"100", "000", "110", "101", "111", "001", "011",


"010"};
string row2[8] = {"101", "011", "000", "111", "110", "010", "001",
"100"};
int column = 0;

if(s2[0] == '0')
{

if(s2[1] == '1')
column += 4;
if(s2[2] == '1')
column += 2;
if(s2[3] == '1')
column += 1;

return row1[column];
}

else if(s2[0] == '1')


{

if(s2[1] == '1')
column += 4;
if(s2[2] == '1')
column += 2;
if(s2[3] == '1')
column += 1;

return row2[column];
}
else

return "ERROR";
}

string randomString(int length)


{
//Use time as seed for PRNG
srand(time(0));
int randomNumber;
string randomBit;
string randomString;
for(int k = 0; k < length; k++)
{
randomNumber = rand() % 2;
if(randomNumber == 0)
randomBit = "0";
else if(randomNumber == 1)
randomBit = "1";
randomString.append(randomBit);
}
return randomString;
}

string header()
{
string header = "*****************\t\t*****************\n\t\t
SDES DEMO\t\t\t\n*****************\t\t*****************\n";
return header;
}
RSA:

1. #include<iostream>
2. #include<math.h>
3. #include<string.h>
4. #include<stdlib.h>
5.
6. using namespace std;
7.
8. long int p, q, n, t, flag, e[100], d[100], temp[100], j, m[100], en[100],
i;
9. char msg[100];
10. int prime(long int);
11. void ce();
12. long int cd(long int);
13. void encrypt();
14. void decrypt();
15. int prime(long int pr)
16. {
17. int i;
18. j = sqrt(pr);
19. for (i = 2; i <= j; i++)
20. {
21. if (pr % i == 0)
22. return 0;
23. }
24. return 1;
25. }
26. int main()
27. {
28. cout << "\nENTER FIRST PRIME NUMBER\n";
29. cin >> p;
30. flag = prime(p);
31. if (flag == 0)
32. {
33. cout << "\nWRONG INPUT\n";
34. exit(1);
35. }
36. cout << "\nENTER ANOTHER PRIME NUMBER\n";
37. cin >> q;
38. flag = prime(q);
39. if (flag == 0 || p == q)
40. {
41. cout << "\nWRONG INPUT\n";
42. exit(1);
43. }
44. cout << "\nENTER MESSAGE\n";
45. fflush(stdin);
46. cin >> msg;
47. for (i = 0; msg[i] != '\0'; i++)
48. m[i] = msg[i];
49. n = p * q;
50. t = (p - 1) * (q - 1);
51. ce();
52. cout << "\nPOSSIBLE VALUES OF e AND d ARE\n";
53. for (i = 0; i < j - 1; i++)
54. cout << e[i] << "\t" << d[i] << "\n";
55. encrypt();
56. decrypt();
57. return 0;
58. }
59. void ce()
60. {
61. int k;
62. k = 0;
63. for (i = 2; i < t; i++)
64. {
65. if (t % i == 0)
66. continue;
67. flag = prime(i);
68. if (flag == 1 && i != p && i != q)
69. {
70. e[k] = i;
71. flag = cd(e[k]);
72. if (flag > 0)
73. {
74. d[k] = flag;
75. k++;
76. }
77. if (k == 99)
78. break;
79. }
80. }
81. }
82. long int cd(long int x)
83. {
84. long int k = 1;
85. while (1)
86. {
87. k = k + t;
88. if (k % x == 0)
89. return (k / x);
90. }
91. }
92. void encrypt()
93. {
94. long int pt, ct, key = e[0], k, len;
95. i = 0;
96. len = strlen(msg);
97. while (i != len)
98. {
99. pt = m[i];
100. pt = pt - 96;
101. k = 1;
102. for (j = 0; j < key; j++)
103. {
104. k = k * pt;
105. k = k % n;
106. }
107. temp[i] = k;
108. ct = k + 96;
109. en[i] = ct;
110. i++;
111. }
112. en[i] = -1;
113. cout << "\nTHE ENCRYPTED MESSAGE IS\n";
114. for (i = 0; en[i] != -1; i++)
115. printf("%c", en[i]);
116. }
117. void decrypt()
118. {
119. long int pt, ct, key = d[0], k;
120. i = 0;
121. while (en[i] != -1)
122. {
123. ct = temp[i];
124. k = 1;
125. for (j = 0; j < key; j++)
126. {
127. k = k * ct;
128. k = k % n;
129. }
130. pt = k + 96;
131. m[i] = pt;
132. i++;
133. }
134. m[i] = -1;
135. cout << "\nTHE DECRYPTED MESSAGE IS\n";
136. for (i = 0; m[i] != -1; i++)
137. printf("%c", m[i]);
138. }

ElGAMMAl
import random
from math import pow

a = random.randint(2, 10)

def gcd(a, b):


if a < b:
return gcd(b, a)
elif a % b == 0:
return b;
else:
return gcd(b, a % b)

# Generating large random numbers


def gen_key(q):

key = random.randint(pow(10, 20), q)


while gcd(q, key) != 1:
key = random.randint(pow(10, 20), q)

return key

# Modular exponentiation
def power(a, b, c):
x = 1
y = a

while b > 0:
if b % 2 == 0:
x = (x * y) % c;
y = (y * y) % c
b = int(b / 2)

return x % c

# Asymmetric encryption
def encrypt(msg, q, h, g):

en_msg = []

k = gen_key(q)# Private key for sender


s = power(h, k, q)
p = power(g, k, q)

for i in range(0, len(msg)):


en_msg.append(msg[i])

print("g^k used : ", p)


print("g^ak used : ", s)
for i in range(0, len(en_msg)):
en_msg[i] = s * ord(en_msg[i])
return en_msg, p

def decrypt(en_msg, p, key, q):

dr_msg = []
h = power(p, key, q)
for i in range(0, len(en_msg)):
dr_msg.append(chr(int(en_msg[i]/h)))

return dr_msg

# Driver code
def main():

msg = 'encryption'
print("Original Message :", msg)

q = random.randint(pow(10, 20), pow(10, 50))


g = random.randint(2, q)

key = gen_key(q)# Private key for receiver


h = power(g, key, q)
print("g used : ", g)
print("g^a used : ", h)

en_msg, p = encrypt(msg, q, h, g)
dr_msg = decrypt(en_msg, p, key, q)
dmsg = ''.join(dr_msg)
print("Decrypted Message :", dmsg);

if __name__ == '__main__':
main()

Diffie hellman
#include<stdio.h>
#include<math.h>

// 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
return (((long long int)pow(a, b)) % P);
}

//Driver program
int main()
{
long long int P, G, x, a, y, b, ka, kb;

// Both the persons will be agreed upon the


// public keys G and P
P = 23; // A prime number P is taken
printf("The value of P : %lld\n", P);

G = 9; // A primitve root for P, G is taken


printf("The value of G : %lld\n\n", G);

// Alice will choose the private key a


a = 4; // a is the chosen private key
printf("The private key a for Alice : %lld\n", a);
x = power(G, a, P); // gets the generated key

// Bob will choose the private key b


b = 3; // b is the chosen private key
printf("The private key b for Bob : %lld\n\n", b);
y = power(G, b, P); // gets the generated key

// Generating the secret key after the exchange


// of keys
ka = power(y, a, P); // Secret key for Alice
kb = power(x, b, P); // Secret key for Bob

printf("Secret key for the Alice is : %lld\n", ka);


printf("Secret Key for the Bob is : %lld\n", kb);

return 0;
}

Hashing:
#include<stdio.h>
#include<limits.h>

void insert(int ary[],int hFn, int size){


int element,pos,n=0;

printf("Enter key element to insert\n");


scanf("%d",&element);

pos = element%hFn;

while(ary[pos]!= INT_MIN) { // INT_MIN and INT_MAX indicates that cell is empty. So if cell is empty loop
will break and goto bottom of the loop to insert element
if(ary[pos]== INT_MAX)
break;
pos = (pos+1)%hFn;
n++;
if(n==size)
break;
}

if(n==size)
printf("Hash table was full of elements\nNo Place to insert this element\n\n");
else
ary[pos] = element;
}

void delete(int ary[],int hFn,int size){

int element,n=0,pos;
printf("Enter element to delete\n");
scanf("%d",&element);

pos = element%hFn;

while(n++ != size){
if(ary[pos]==INT_MIN){
printf("Element not found in hash table\n");
break;
}
else if(ary[pos]==element){
ary[pos]=INT_MAX;
printf("Element deleted\n\n");
break;
}
else{
pos = (pos+1) % hFn;
}
}
if(--n==size)
printf("Element not found in hash table\n");
}

void search(int ary[],int hFn,int size){


int element,pos,n=0;

printf("Enter element you want to search\n");


scanf("%d",&element);

pos = element%hFn;

while(n++ != size){
if(ary[pos]==element){
printf("Element found at index %d\n",pos);
break;
}
else
if(ary[pos]==INT_MAX ||ary[pos]!=INT_MIN)
pos = (pos+1) %hFn;
}
if(--n==size) printf("Element not found in hash table\n");
}

void display(int ary[],int size){


int i;

printf("Index\tValue\n");

for(i=0;i<size;i++)
printf("%d\t%d\n",i,ary[i]);
}
int main(){
int size,hFn,i,choice;

printf("Enter size of hash table\n");


scanf("%d",&size);

int ary[size];

printf("Enter hash function [if mod 10 enter 10]\n");


scanf("%d",&hFn);

for(i=0;i<size;i++)
ary[i]=INT_MIN;

do{
printf("Enter your choice\n");
printf(" 1-> Insert\n 2-> Delete\n 3-> Display\n 4-> Searching\n 0-> Exit\n");
scanf("%d",&choice);

switch(choice){
case 1:
insert(ary,hFn,size);
break;
case 2:
delete(ary,hFn,size);
break;
case 3:
display(ary,size);
break;
case 4:
search(ary,hFn,size);
break;
default:
printf("Enter correct choice\n");
break;
}
}while(choice);

return 0;
}

You might also like