#include
int
candp(int
a,int
b,int
c)
{
int
r=1
b=b+1
while(b!=1)
{
r=r*a
r=r%c
b--
}
printf("%d",r)
return
r
}
void
main()
{
int
p,q,e,d,m,n,t,c,r
char
s
{printf("input
the
p:\n")
scanf("%d\n",&p)
printf("input
the
q:\n")
scanf("%d%d\n",&p)
n=p*q
printf("so,the
n
is
%3d\n",n)
t=(p-1)*(q-1)
printf("so,the
t
is
%3d\n",t)
printf("please
intput
the
e:\n")
scanf("%d",&e)
if(e<1||e>t)
{printf("e
is
error,please
input
again")
scanf("%d",&e)}
d=1
while
(((e*d)%t)!=1)
d++
printf("then
caculate
out
that
the
d
is
%5d",d)
printf("if
you
want
to
konw
the
cipher
please
input
1\n
if
you
want
to
konw
the
plain
please
input
2\n")
scanf("%d",&r)
if(r==1)
{
printf("input
the
m
:"
)/*输入要加密的明文数字*/
scanf("%d\n",&m)
c=candp(m,e,n)
printf("so
,the
cipher
is
%4d",c)}
if(r==2)
{
printf("input
the
c
:"
)/*输入要解密的密文数字*/
scanf("%d\n",&c)
m=candp(c,d,n)
printf("so
,the
cipher
is
%4d\n",m)
printf("do
you
want
to
use
this
programe:yes
or
no")
scanf("%s",&s)
}while(s=='y')
}
}
程序修改如下:(主要是你的循环写的不对,输入的字符应该-'0'才能与正常的数字对应)
#include<stdio.h>
#include<math.h>
int
candp(int
a,int
b,int
c)
{int
r=1
int
s
int
i=1
for(i=1i<=bi++)r=r*a
printf("%d\
",r)
s=r%c
printf("%d\
",s)
return
s}
void
main()
{
int
p,q,e,d,m,n,t,c,r
char
s
printf("please
input
the
p,q:")
scanf("%d%d",&p,&q)
n=p*q
t=(p-1)*(q-1)
printf("the
n
is
%12d\
",n)
printf("please
input
the
e:")
scanf("%d",&e)
while(e<1||e>n)
//此处修改为while循环
{
printf("e
is
error,please
input
again:")
scanf("%d",&e)
}
d=1
while(((e*d)%t)!=1)
d++
printf("then
caculate
out
that
the
d
is
%d\
",d)
printf("the
cipher
please
input
1\
")
printf("the
plain
please
input
2\
")
scanf("%c",&s)
while((s-'0')!=1&&(s-'0')!=2)
//消除后面的getchar()
此处增加while循环注意括号内的字符
{scanf("%c",&s)}
switch(s-'0')
{
case
1:printf("intput
the
m:")
scanf("%d",&m)
c=candp(m,e,n)
printf("the
plain
is
%d\
",c)break
case
2:printf("input
the
c:")
scanf("%d",&c)
m=candp(c,d,n)
printf("the
cipher
is
%8d\
",m)
break
}
}
#include <stdlib.h>
#include <stdio.h>
#include <string.h>
#include <math.h>
#include <time.h>
#define PRIME_MAX 200 // 生成素数范围
#define EXPONENT_MAX 200 // 生成指数e范围
#define Element_Max 127 // 加密单元的最大值,这里为一个char, 即1Byte
char str_read[100]="hello world !" // 待加密的原文
int str_encrypt[100] // 加密后的内容
char str_decrypt[100] // 解密出来的内容
int str_read_len // str_read 的长度
int prime1, prime2 // 随机生成的两个质数
int mod, eular // 模数和欧拉数
int pubKey, priKey // 公钥指数和私钥指数
// 生成随机素数,实际应用中,这两个质数越大,就越难破解。
int randPrime()
{
int prime, prime2, i
next:
prime = rand() % PRIME_MAX // 随机产生数
if (prime <= 1) goto next // 不是质数,生成下一个随机数
if (prime == 2 || prime == 3) return prime
prime2 = prime / 2 // prime>=4, prime2 的平方必定大于 prime , 因此只检查小于等于prime2的数
for (i = 2i <= prime2i++) // 判断是否为素数
{
if (i * i >prime) return prime
if (prime % i == 0) goto next // 不是质数,生成下一个随机数
}
}
// 欧几里德算法,判断a,b互质
int gcd(int a, int b)
{
int temp
while (b != 0) {
temp = b
b = a % b
a = temp
}
return a
}
//生成公钥指数,条件是 1<e <欧拉数,且与欧拉数互质。
int randExponent()
{
int e
while (1)
{
e = rand() % eularif (e <EXPONENT_MAX) break
}
while (1)
{
if (gcd(e, eular) == 1) return ee = (e + 1) % eularif (e == 0 || e >EXPONENT_MAX) e = 2
}
}
//生成私钥指数
int inverse()
{
int d, x
while (1)
{
d = rand() % eular
x = pubKey * d % eular
if (x == 1)
{
return d
}
}
}
//加密函数
void jiami()
{
str_read_len = strlen(str_read) //从参数表示的地址往后找,找到第一个'\0',即串尾。计算'\0'至首地址的“距离”,即隔了几个字符,从而得出长度。
printf("密文是:")
for (int i = 0i <str_read_leni++)
{
int C = 1int a = str_read[i], b = a % mod
for (int j = 0j <pubKeyj++) //实现加密
{
C = (C*b) % mod
}
str_encrypt[i] = C
printf("%d ", str_encrypt[i])
}
printf("\n")
}
//解密函数
void jiemi()
{
int i=0 for (i = 0i <str_read_leni++)
{
int C = 1int a = str_encrypt[i], b=a%mod
for (int j = 0j <priKeyj++)
{
C = (C * b) % mod
}
str_decrypt[i] = C
}
str_decrypt[i] = '\0'printf("解密文是:%s \n", str_decrypt)
}
int main()
{
srand(time(NULL))
while (1)
{
prime1 = randPrime()prime2 = randPrime()printf("随机产生两个素数:prime1 = %d , prime2 = %d ", prime1, prime2)
mod = prime1 * prime2printf("模数:mod = prime1 * prime2 = %d \n", mod)if (mod >Element_Max) break// 模数要大于每个加密单元的值
}
eular = (prime1 - 1) * (prime2 - 1) printf("欧拉数:eular=(prime1-1)*(prime2-1) = %d \n", eular)
pubKey = randExponent()printf("公钥指数:pubKey = %d\n", pubKey)
priKey = inverse()printf("私钥指数:priKey = %d\n私钥为 (%d, %d)\n", priKey, priKey, mod)
jiami()jiemi()
return 0
}