-
Notifications
You must be signed in to change notification settings - Fork 0
/
rabin.c
141 lines (141 loc) · 2.63 KB
/
rabin.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
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
#include <stdio.h>
#include <stdlib.h>
#include <ctype.h>
#include <math.h>
#include <limits.h>
int e = 2, n;
int p, q;
int gcd(int a,int b){
int q,r1,r2,r;
if(a>b){
r1=a;
r2=b;
}
else{
r1=b;
r2=a;
}
while(r2>0){
q=r1/r2;
r=r1-q*r2;
r1=r2;
r2=r;
}
return r1;
}
int FastExponention(int bit,int n,int *y,int *a){
if(bit==1){
*y=(*y * (*a))%n;
}
*a=(*a)*(*a) %n;
}
int FindT(int a,int m,int n){
int r;
int y=1;
while(m>0){
r=m%2;
FastExponention(r,n,&y,&a);
m=m/2;
}
return y;
}
int PrimarityTest(int a,int i){
int n=i-1;
int k=0;
int j,m,T;
while(n%2==0){
k++;
n=n/2;
}
m=n;
T=FindT(a,m,i);
if(T==1||T==i-1){
return 1;
}
for(j=0;j<k;j++){
T=FindT(T,2,i);
if(T==1){
return 0;
}
if(T==i-1){
return 1;
}
}
return 0;
}
int KeyGeneration(){
do{
do{
p=rand()%500;
}while(!((p%2==1)&&(p%4==3)));
}while(!PrimarityTest(2,p));
do{
do{
q=rand()%500;
}while(!((q%2==1)&&(q%4==3)));
}while(!PrimarityTest(2,p));
n=p*q;
printf("p=%d, q=%d,n=%d\n",p,q,n);
}
int Encryption(int PlainText){
printf("PlainText is %d\n", PlainText);
int cipher = FindT(PlainText, e, n);
fprintf(stdout, "Cipher Text is %d\n", cipher);
return cipher;
}
int inverse(int a,int b){
int inv;
int q,r,r1=a,r2=b,t,t1=0,t2=1;
while(r2>0){
q=r1/r2;
r=r1-q*r2;
r1=r2;
r2=r;
t=t1-q*t2;
t1=t2;
t2=t;
}
if (r1==1){
inv=t1;
}
if(inv<0){
inv=inv+a;
}
return inv;
}
int ChineseRemainderTheorem(int a,int b,int m1,int m2){
int M,M1,M2,M1_inv,M2_inv;
int result;
M=m1*m2;
M1=M/m1;
M2=M/m2;
M1_inv=inverse(m1,M1);
M2_inv=inverse(m2,M2);
result=(a*M1*M1_inv+b*M2*M2_inv)%M;
return result;
}
int Decryption(int Cipher){
int P1,P2,P3,P4;
int a1,a2,b1,b2;
a1=FindT(Cipher,(p+1)/4,p);
a2=p-a1;
b1=FindT(Cipher,(q + 1)/4,q);
b2=q-b1;
P1=ChineseRemainderTheorem(a1,b1,p,q);
P2=ChineseRemainderTheorem(a1,b2,p,q);
P3=ChineseRemainderTheorem(a2,b1,p,q);
P4 = ChineseRemainderTheorem(a2, b2, p, q);
printf("\nP1 = %d\nP2 = %d\nP3 = %d\nP4 = %d\n",P1,P2,P3,P4);
}
int main(void)
{
int PlainText;
KeyGeneration();
do {
PlainText=rand()%(n - 1)+1;
}
while (gcd(PlainText,n)!=1);
int Cipher=Encryption(PlainText);
Decryption(Cipher);
return 0;
}