输出一个C语言编程的EIGamal算法给我
时间: 2023-03-31 18:00:36 浏览: 81
EIGamal算法是一种非对称加密算法,它可以用于加密和解密数据。下面是一个C语言编程的EIGamal算法示例:
#include <stdio.h>
#include <stdlib.h>
#include <time.h>
#include <math.h>
#define MAX 100
int prime(int);
int gcd(int, int);
int power(int, int, int);
int main()
{
int p, g, x, y, k, a, b, i;
char msg[MAX];
printf("Enter a prime number (p): ");
scanf("%d", &p);
if (!prime(p))
{
printf("%d is not a prime number.\n", p);
exit(1);
}
printf("Enter a primitive root of %d (g): ", p);
scanf("%d", &g);
printf("Enter a secret key (x): ");
scanf("%d", &x);
y = power(g, x, p);
printf("Public key (y): %d\n", y);
printf("Enter a message to encrypt: ");
scanf("%s", msg);
srand(time(NULL));
k = rand() % (p - 2) + 1;
a = power(g, k, p);
b = power(y, k, p);
for (i = ; msg[i] != '\'; i++)
printf("%c", (b * msg[i]) % p);
printf("\n");
a = power(a, p - 1 - x, p);
for (i = ; msg[i] != '\'; i++)
printf("%c", (a * (b * msg[i]) % p) % p);
printf("\n");
return ;
}
int prime(int n)
{
int i;
for (i = 2; i <= sqrt(n); i++)
if (n % i == )
return ;
return 1;
}
int gcd(int a, int b)
{
if (b == )
return a;
return gcd(b, a % b);
}
int power(int a, int b, int p)
{
int res = 1;
while (b > )
{
if (b % 2 == 1)
res = (res * a) % p;
a = (a * a) % p;
b /= 2;
}
return res;
}
相关推荐
![pdf](https://img-home.csdnimg.cn/images/20210720083512.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![java](https://img-home.csdnimg.cn/images/20210720083646.png)
![zip](https://img-home.csdnimg.cn/images/20210720083736.png)
![pdf](https://img-home.csdnimg.cn/images/20210720083512.png)