Documente Academic
Documente Profesional
Documente Cultură
Encryption:
#include<stdio.h>
int main()
{
char message[100], ch;
int i, key;
message[i] = ch;
}
else if(ch >= 'A' && ch <= 'Z'){
ch = ch + key;
message[i] = ch;
}
}
return 0;
}
Decryption:
#include<stdio.h>
int main()
{
char message[100], ch;
int i, key;
message[i] = ch;
}
else if(ch >= 'A' && ch <= 'Z'){
ch = ch - key;
message[i] = ch;
}
}
Playfair:
#include<bits/stdc++.h>
using namespace std;
/*
for(int i=0;i<l;i+=2)
cout<<cypText[i]<<cypText[i+1]<<" ";
cout<<endl;
*/
/*
//show generated text in pair
for(int i=0;i<in;i+=2)
cout<<reqText[i]<<reqText[i+1]<<" ";
cout<<endl;
*/
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>
int main() {
getKeyMessage();
encryption();
decryption();
}
void encryption() {
int i, j, k;
void decryption() {
int i, j, k;
inverse();
cout<<"\n";
}
void getKeyMessage() {
int i, j;
char msg[3];
void inverse() {
int i, j, k;
float p, q;
Vigenere cipher:
#include<iostream>
#include<string.h>
int main(){
char msg[] = "mohammedruzzuq";
char key[] = "hello";
int msgLen = strlen(msg), keyLen = strlen(key), i, j;
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';
return 0;
}
SDES
#include
<iostream>
#include <string>
#include <ctime>
#include <stdio.h>
using namespace std;
int main()
{
ciphertext.append(plaintext, 6, 6);
ciphertext.append(plaintext, 0, 6);
cout << endl << "Ciphertext after " << rounds << " rounds:\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 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);
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);
}
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;
}
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);
}
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];
}
return row2[column];
}
else
return "ERROR";
}
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];
}
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 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)
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 = []
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)
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>
else
return (((long long int)pow(a, b)) % P);
}
//Driver program
int main()
{
long long int P, G, x, a, y, b, ka, kb;
return 0;
}
Hashing:
#include<stdio.h>
#include<limits.h>
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;
}
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");
}
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");
}
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;
int ary[size];
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;
}