-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathdiffie.c
More file actions
56 lines (52 loc) · 1010 Bytes
/
diffie.c
File metadata and controls
56 lines (52 loc) · 1010 Bytes
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
// Fast modular exponentiation
#include <stdio.h>
#include <stdlib.h>
#include <math.h>
int *tobinary(int a)
{
int *arr = (int *)malloc(sizeof(int)*100);
int k = 1;
while(a>0)
{
arr[k++] = a%2;
a = a/2;
}
// arr[0] = k-1;
return arr;
}
int fastmodular (int a, int b, int n)
{
int c = 1;
int *arr = tobinary(b);
int len = sizeof(arr)/arr[0];
int temp = a%n;
for (int i = 1;i<=n;i++)
{
if(arr[i]==1)
{
c = (c*temp)%n;
}
temp = (temp*temp)%n;
}
return c%n;
}
void diffie(int g, int p)
{
int a = 42;
int b = 33;
printf("The values of a and b: \n");
scanf("%d %d",&a,&b);
int x = fastmodular(g,a,p);
int y = fastmodular(g,b,p);
int ka = fastmodular(y,a,p);
int kb = fastmodular(x,b,p);
printf("ka: %d kb: %d",ka,kb);
}
int main()
{
int g,p;
printf("Enter the values of g and p: \n");
scanf("%d %d",&g,&p);
diffie(g,p);
return 0;
}