-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.c
96 lines (73 loc) · 1.78 KB
/
main.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
#include<stdio.h>
#include<string.h>
#include<stdlib.h>
void stringToHex (char *message, long pow, long mod);
long encrypt (long encryptNum, long pow, long mod);
long encrypt (long encryptNum, long pow, long mod)
{
long result;
result = 1;
if (pow < 1)
return result;
if (pow == 1)
return encryptNum % mod;
result = encrypt(encryptNum, pow / 2, mod) % mod;
result = (result * result) % mod;
if (pow & 1)
result = (result * encryptNum) % mod;
return result;
}
void stringToHex(char *message, long pow, long mod)
{
int i, j, block, rest;
long threeChar = 0, encryptNum;
block = strlen(message)/3;
rest = strlen(message)%3;
i =0;
printf("\n");
while(block !=0)
{
threeChar = message[i * 3] * 256 * 256 +
message[i * 3+1] * 256 +
message[i * 3+2];
encryptNum = encrypt(threeChar, pow, mod);
printf("%08lX ", encryptNum);
if(((i + 1) % 8) == 0)
{
printf("\n");
}
block = block -1;
i++;
j = i;
}
if (rest == 1)
{
threeChar = message[j * 3] *256*256 + ' ' *256 + ' ';
encryptNum = encrypt(threeChar, pow, mod);
printf("%08lX ", encryptNum);
}
else if (rest ==2)
{
threeChar = message[j * 3] *256*256+ message[j *3+1] * 256 + ' ';
encryptNum = encrypt(threeChar, pow, mod);
printf("%08lX ", encryptNum);
}
printf("\n");
}
int main (int argc, char **argv)
{
long pow, mod;
char text;
char message[4097];
int i = 0;
scanf("%ld", &pow);
scanf("%lX", &mod);
scanf("%c", &text);
while(((scanf("%c", &text)) != EOF))
{
message[i++] = text;
}
message[i] = 0;
stringToHex(message,pow,mod);
return 0;
}