| 网站首页 | 业界新闻 | 小组 | 威客 | 人才 | 下载频道 | 博客 | 代码贴 | 在线编程 | 编程论坛
欢迎加入我们,一同切磋技术
用户名:   
 
密 码:  
共有 2178 人关注过本帖
标题:请大侠帮忙:RSA加密算法
取消只看楼主 加入收藏
Crocodile_JX
Rank: 5Rank: 5
等 级:职业侠客
帖 子:161
专家分:335
注 册:2010-9-13
结帖率:100%
收藏
已结贴  问题点数:100 回复次数:16 
请大侠帮忙:RSA加密算法
代码如下:(运行的结果不对,不知错在哪里。请大侠指点,不胜感激)
#include <stdio.h>

int encryption(int m,int e,int n)         /*加密方法*/
{
  long int M=1;
  long int result_1=1;  /*用来存储加密后的数据*/
  int i;
  for(i=1;i<=e;i++)
    M=M*m;        /*计算m  的 e 次方*/
   /* (c-M)%n==0;      */
  while((result_1%n)!=M)
     result_1++;

   return result_1;
}

int decryption(int c,int d,int n)    /*解密方法*/
{
   long int M=1;
   int result_2=1;   /*存储解密后的数据*/
   int i;
   for(i=1;i<=d;i++)
     M=M*c;          /*计算 c 的 d 次方*/
   while(result_2%n!=M)    /*计算 result ,满足 result==c^d(mod n)*/
     result_2++;
   return result_2;
}
void main()
{
/*int p,q,e,d,m,n,t,c,r; */
int p,q; /*两个质数 p 与 q*/
int e,d;/*加密密钥 和  解密密钥*/
int n; /*存储 q 与 q 的 乘积*/
int t; /*存储 (q-1)与 (p-1)的 乘积 */
int m;/*消息数*/
int r;           /*变量*/
int c; /*存储运算的结果*/
int s;
printf("please input the p,q: ");  /*输入两个质数*/
scanf("%d%d",&p,&q);
n=p*q;
printf("the n is %3d\n",n);   /*打印输出n*/
t=(p-1)*(q-1);
printf("the t is %3d\n",t);            /*打印输出 t*/
printf("please input the e: ");
scanf("%d",&e);       /*输入解密密钥 e,e与t 互质。最大公约数为 1*/
while(e<1||e>t)    /*判断解密密钥是否正确*/
{
     printf("e is error,please input again (e<1||e>t): ");
     scanf("%d",&e);
}

d=1;
while(((e*d)%t)!=1)
d++;
printf("then caculate out that the d is %d\n",d);
   printf("input the m: ");  /*输入要加密的明文数字*/
            scanf("%d",&m);
            c=encryption(m,e,n);
            printf("the cipher is %d\n",c);

    printf("input the c: "); /*输入要解密的密文数字*/
            scanf("%d",&c);
           s=decryption(c,d,n);
            printf("the cipher is %d\n",s);



getch();
}

搜索更多相关主题的帖子: RSA 算法 
2010-11-29 15:55
Crocodile_JX
Rank: 5Rank: 5
等 级:职业侠客
帖 子:161
专家分:335
注 册:2010-9-13
收藏
得分:0 
RSA算法,主要过程如下:
取两素数p和q 。(保密)
计算n=p*q 。(公开)
计算t=(p-1)*(q-1) 。(保密)
取加密密钥e,要求e与t互素。 (公开)
取解密密钥d,要求d*e % t = 1。 (保密)

设消息为数M (要求M <n)
加密过程:c=(M^e) % n   (中间那里不是等号,他的意思是 c-M^e 模 n )
解密过程:m=(c^d) % n
则有m==M
2010-11-29 16:45
Crocodile_JX
Rank: 5Rank: 5
等 级:职业侠客
帖 子:161
专家分:335
注 册:2010-9-13
收藏
得分:0 
加密过程:c=(M^e) % n   (中间那里不是等号)
他的意思是 c-M^e被 m 整除。 也即是说 c 合同于 M^e 模 m
2010-11-29 16:48
Crocodile_JX
Rank: 5Rank: 5
等 级:职业侠客
帖 子:161
专家分:335
注 册:2010-9-13
收藏
得分:0 
int encryption(int m,int e,int n)         /*加密方法*/
{
  long int M=1;
  long int result_1=1;  /*用来存储加密后的数据*/
  int i;
  for(i=1;i<=e;i++)
    M=M*m;        /*计算m  的 e 次方*/
   /* (c-M)%n==0;      */
  while((result_1%n)!=M)  把这里改成 while((result_1-M)!=0)应该就可以了    下面的解密函数 也 改成 while((result_2-M)!=0)
     result_1++;

   return result_1;
}
不过 解密 这个函数 也有错误。
2010-11-29 16:57
Crocodile_JX
Rank: 5Rank: 5
等 级:职业侠客
帖 子:161
专家分:335
注 册:2010-9-13
收藏
得分:0 
回复 6楼 hahayezhe
int encryption(int m,int e,int n)         /*加密方法*/
{
  long int M=1;
  long int result_1=1;  /*用来存储加密后的数据*/
  int i;
  for(i=1;i<=e;i++)
    M=M*m;        /*计算m  的 e 次方*/
   /* (c-M)%n==0;      */
  while((result_1%n)!=M)  把这里改成 while((result_1-M)!=0)应该就可以了    下面的解密函数 也 改成 while((result_2-M)!=0)
     result_1++;

   return result_1;
}
不过 解密 这个函数 也有错误啊。
2010-11-29 17:02
Crocodile_JX
Rank: 5Rank: 5
等 级:职业侠客
帖 子:161
专家分:335
注 册:2010-9-13
收藏
得分:0 
改成 while((result_1-M)%n!=0)应该就可以了    下面的解密函数 也 改成 while((result_2-M)%n!=0)
2010-11-29 17:04
Crocodile_JX
Rank: 5Rank: 5
等 级:职业侠客
帖 子:161
专家分:335
注 册:2010-9-13
收藏
得分:0 
刚刚打错了 ...
嗯,对,代码没有完全解析清楚。呵呵
2010-11-29 17:05
Crocodile_JX
Rank: 5Rank: 5
等 级:职业侠客
帖 子:161
专家分:335
注 册:2010-9-13
收藏
得分:0 
运行的结果:
please input the p,q:  41 23
the n is 943
the t is 880
please input the e: 7
then calculate out that the d is  503
input the m: 2
the cipher is  128
input the c: 128
the cipher is 943    这一步的结果不对。应该是2 才对。 但是不知道为什么会有这个结果
2010-11-29 17:14
Crocodile_JX
Rank: 5Rank: 5
等 级:职业侠客
帖 子:161
专家分:335
注 册:2010-9-13
收藏
得分:0 
自己慢慢算了下之后,好像加密和解密的方法都不对呢!真是郁闷...
2010-11-29 17:39
Crocodile_JX
Rank: 5Rank: 5
等 级:职业侠客
帖 子:161
专家分:335
注 册:2010-9-13
收藏
得分:0 
如果是要用JAVA来做的话就会很简单了。 在求C的D次方的时候 数据会逸出呢。 哪位高手能用C语言给我修改下啊。
2010-11-30 09:58
快速回复:请大侠帮忙:RSA加密算法
数据加载中...
 
   



关于我们 | 广告合作 | 编程中国 | 清除Cookies | TOP | 手机版

编程中国 版权所有,并保留所有权利。
Powered by Discuz, Processed in 0.017020 second(s), 8 queries.
Copyright©2004-2024, BCCN.NET, All Rights Reserved