C语言的Diffie-Helman程序

时间:2019-04-14 13:15:33

标签: c diffie-hellman

我用C语言编写了Diffie Hellman程序的程序,在编译代码时遇到以下错误。我收到以下错误消息:

$gcc Diffie-Helman.c -o Diffie-Helman
/usr/bin/ld: /tmp/ccedcUj8.o: in function `power':
Diffie-Helman.c:(.text+0x2e): undefined reference to `pow'
collect2: error: ld returned 1 exit status

机器规格:Linux Ubuntu 18.10 该程序如下所示........

/* This program calculates the Key for two persons 
using the Diffie-Hellman Key exchange algorithm */
#include<stdio.h> 
#include<math.h> 

// Power function to return value of a ^ b mod P 
long long int power(long long int a, long long int b, long long 
int P) 
{ 
if (b == 1) 
    return a; 

else
    return (((long long int)pow(a, b)) % P); 
} 

//Driver program 
int main() 
{ 
long long int P, G, x, a, y, b, ka, kb; 

// Both the persons will be agreed upon the 
    // public keys G and P 
P = 23; // A prime number P is taken 
printf("The value of P : %lld\n", P); 

G = 9; // A primitve root for P, G is taken 
printf("The value of G : %lld\n\n", G); 

// Alice will choose the private key a 
a = 4; // a is the chosen private key 
printf("The private key a for Alice : %lld\n", a); 
x = power(G, a, P); // gets the generated key 

// Bob will choose the private key b 
b = 3; // b is the chosen private key 
printf("The private key b for Bob : %lld\n\n", b); 
y = power(G, b, P); // gets the generated key 

// Generating the secret key after the exchange 
    // of keys 
ka = power(y, a, P); // Secret key for Alice 
kb = power(x, b, P); // Secret key for Bob 

printf("Secret key for the Alice is : %lld\n", ka); 
printf("Secret Key for the Bob is : %lld\n", kb); 

return 0; 
} 

0 个答案:

没有答案