-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathPermutationCipher.c
120 lines (89 loc) · 2.39 KB
/
PermutationCipher.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
#include <stdio.h>
#include <time.h>
#include <string.h>
#include <stdlib.h>
int* generate_random_key(int n){
int* r = malloc(n * sizeof(int));
for(int i=0;i<n;++i){
r[i]=i+1;
}
for (int i = n-1; i >= 0; --i){
srand(time(0));
int j = rand() % (i+1);
int temp = r[i];
r[i] = r[j];
r[j] = temp;
}
return r;
}
char* Encrypt(char* plaintext, char* key, int m){
int i=0;
while(plaintext[i]!='\0') i++;
int len = i;
int temp_len = len;
while(temp_len%m!=0) temp_len++;
char str[temp_len+1];
// x stand for buffer padding
for(int i=0;i<temp_len;i++){
str[i] = 'x';
}
str[temp_len] = '\0';
i = 0;
for(char* c=plaintext; *c!='\0'; c++){
str[(i/m)*m+key[i%m]-'1'] = *c-'a'+'A';
i++;
}
strcpy(plaintext, str);
return plaintext;
}
char* Decrypt(char ciphertext[], char* key, int m){
int i=0;
while(ciphertext[i]!='\0') i++;
int len = i;
char plaintext[len+1];
for(int i=0;i<len;i++){
plaintext[i] = 'x';
}
char pi_inverse[m];
for(int i=0;i<m;i++){
pi_inverse[key[i]-'1'] = i+'1';
}
i = 0;
for(char* c=ciphertext; *c!='\0'; c++){
if(*c=='x'){
plaintext[(i/m)*m+pi_inverse[i%m]-'1'] = '\0';
}
else{
plaintext[(i/m)*m+pi_inverse[i%m]-'1'] = *c-'A'+'a';
}
i++;
}
strcpy(ciphertext, plaintext);
ciphertext[len] = '\0';
return ciphertext;
}
int main(){
int m;
printf("Enter the value of m: ");
scanf("%d",&m);
printf("Generating random key: ");
int *key = generate_random_key(m);
for(int i=0;i<m;i++){
printf("%d", key[i]);
}
printf("\n");
char plaintext[1000];
char keyword[m];
getchar();
printf("\nEnter Plain text: ");
scanf("%s", plaintext);
printf("Enter key: ");
scanf("%s", keyword);
printf("\nEncrypting Plain Text...\n");
char* ciphertext = Encrypt(plaintext, keyword, m);
printf("Cipher text: %s\n", ciphertext);
printf("\nDecrypting Cipher Text...\n");
char* real_message = Decrypt(ciphertext, keyword, m);
printf("Real message: %s\n", real_message);
return 0;
}