-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathencryption.c
More file actions
213 lines (184 loc) · 5.3 KB
/
encryption.c
File metadata and controls
213 lines (184 loc) · 5.3 KB
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
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
#include <stdio.h>
#include <string.h>
//convert the letters to numbers
void letterconversion(char *alphabet, char *input, int *intform);
//convert the numbers to letters
void numberconversion(char *alpha, char *output, int *modform);
//emcrypting function
void encrypt(int *numform, int *result);
//decrypting function
void decrypt(int *encryption, int *numberform);
//identity proof
void modproof();
int n = 29;
int main()
{
char letter[29] = {'*','A','B','C','D','E','F','G','H','I',
'J','K','L','M','N','O','P','Q','R','S',
'T','U','V','W','X','Y','Z','[','%'};
char plaintext[8];
char cyphertext[8];
//temporary variable
char cyphertemp[4];
int plain[4];
int cypher[4];
printf("Enter 8 digit string \n");
gets(plaintext);
//index variables
unsigned int i, j;
//convert lowercase letters to uppercase
for(i=0;i<8;i++)
{
if(plaintext[i]>=97)
{
plaintext[i] = plaintext[i] - 32;
}
}
printf("Input string (plaintext) : ");
for(i = 0; i < (sizeof(plaintext) / sizeof(char)); i+=4)
{
for(j = 0; j < 4; j++)
{
printf("%c", plaintext[i + j]);
//call function to convert letters
letterconversion(letter, plaintext + i, plain);
//encrypt the numbers
encrypt(plain, cypher);
//convert the numbers to the cypher text
numberconversion(letter, cyphertemp, cypher);
cyphertext[i + j] = cyphertemp[j];
}
}
printf("\nEncrypted (cyphertext) : ");
for(i = 0; i < (sizeof(plaintext) / sizeof(char)); i++)
{
printf("%c", cyphertext[i]);
}
printf("\nDecrypted (plaintext) : ");
for(i = 0; i < (sizeof(plaintext) / sizeof(char)); i+=4)
{
for(j = 0; j < 4; j++)
{
printf("%c", plaintext[i + j]);
//call function to convert letters
letterconversion(letter, (plaintext + i), plain);
//decrypt the numbers
decrypt(plain, cypher);
//convert the numbers to the cypher text
numberconversion(letter, cyphertemp, cypher);
cyphertext[i + j] = cyphertemp[j];
}
}
//call proof function
modproof();
getchar();
return 0;
}
void letterconversion(char *alphabet, char *input, int *intform)
{
//index variables
int i, j;
//compare letter string to inputted string
for(i=0; i<4; i++)
{
for(j=0; j<29; j++)
{
//if match, assign number to element i in plain and print to screen
if(*(alphabet+j) == *(input+i))
{
*(intform+i) = j;
}
}
}
}
void numberconversion(char *alpha, char *output, int *modform)
{
//index variables
int i, k;
//compare letter string to inputted string
for(k=0; k<4; k++)
{
for(i=0; i<29; i++)
{
//if modform is equal to index variable i, assign the corresponding letter into output
if(*(modform+k) == i)
{
*(output+k) = *(alpha+i);
}
}
}
}
//encryption function
void encrypt(int *numform, int *result)
{
int E[4][4] = {{9, 12, 23, 1},
{24, 5, 24, 19},
{13, 2, 21, 13},
{1, 3, 25, 15}};
//index variables
int i, k;
//multiplication of matrix elements
for(i=0;i<4;i++)
{
result[i] = 0;
for(k=0;k<4;k++)
{
result[i]+= (E[i][k]) * (numform[k]);
}
*(result+i) = *(result+i) % n;
}
}
//decryption function
void decrypt(int *encrypted, int *numberform)
{
int D[4][4] = {{10, 12, 8, 12},
{6, 21, 23, 13},
{20, 11, 19, 5},
{17, 25, 27, 27}};
//index variables
int i, k;
//multiplication of matrix elements
for(i=0;i<4;i++)
{
numberform[i] = 0;
for(k=0;k<4;k++)
{
//if element in modform is equal to index variable i, assign the corresponding alphabetical letter into output
numberform[i]+= (D[i][k]) * (encrypted[k]);
}
//get encrypted numbers mod 29
*(encrypted+i) = *(encrypted+i) % n;
}
}
void modproof()
{
int E[4][4] = {{9, 12, 23, 1},
{24, 5, 24, 19},
{13, 2, 21, 13},
{1, 3, 25, 15}};
int D[4][4] = {{10, 12, 8, 12},
{6, 21, 23, 13},
{20, 11, 19, 5},
{17, 25, 27, 27}};
//index variables
int i, j ,k;
int result[4][4];
int identity[4][4];
printf("\n\nIdentity matrix:");
for(i=0;i<4;i++)
{
printf("\n");
for(j=0;j<4;j++)
{
result[i][j] = 0;
for(k=0;k<4;k++)
{
//multiply the matrix elements and store them in variable called result
result[i][j]+= E[i][k] * D[k][j];
//get result mod 9 and store in identity
identity[i][j] = (result[i][j] % n);
}
printf("%d ", identity[i][j]);
}
}
}