教学服务系统

 找回密码
 立即注册
搜索
查看: 486|回复: 2

信息计算2019级1班30号刘毅扬

[复制链接]

8

主题

16

帖子

94

积分

注册会员

Rank: 2

积分
94
发表于 2022-6-2 15:18:10 | 显示全部楼层 |阅读模式
RSA加密和签名
(1)加密过程。在秘钥生成过程中,首先生成两个大的质数(素数)p和q,令n=p*q;m=(p-1)*(q-1),选取较小的数e,使e 和m互质,即e和m的最大公约数为1,然后生成d,使d*emod m=1,最后丢弃P,q,m,则公钥为e,n,私钥为d和n.

(2)加密过程。将明文x加密成密文y的计算公式为:x=y^d mod n。从公式可见,加密牵涉到明文和公钥。因此,密文即使被截获,也无法被解读。

(3) 解密过程。将密文y解密成明文x的计算公式为:x=y^d mod n。从公式可见,解密至牵涉到私钥和密文。因此,只要保管好私钥,不泄密,就可以放心地把密文和公钥公开。

  1. #include <stdio.h>  

  2. int candp(int a,int b,int c)  //计算密文

  3. {

  4.     int r=1;  

  5. b=b+1;  

  6. while(b!=1)  

  7. {  

  8.   r=r*a;  

  9.   r=r%c;  

  10.   b--;  

  11. }  

  12. return r;  

  13. }  



  14. int main()  

  15. {  

  16. int p,q,m,n,e,d,x,y,r;  

  17. printf("请输入 p,q: ");    //输入两个素数q,p

  18. scanf("%d%d",&p,&q);  

  19. n=p*q;  

  20. printf("n为%d\n",n);  



  21. m=(p-1)*(q-1);

  22. printf("m为%d\n",m);  



  23. printf("请输入e: ");  

  24. scanf("%d",&e);  

  25. if(e<1||e>m)  

  26. {  

  27.   printf("e输入错误,请重新输入: ");  

  28.   scanf("%d",&e);  

  29. }  

  30. d=1;  

  31. while(((e*d)%m)!=1) d++;  

  32. printf("计算出d为:%d\n",d);  

  33. printf("加密请输入1,解密请输入2,输入其他退出\n");   

  34. scanf("%d",&r);  

  35. while(1)

  36. {



  37.   if(r==1)

  38.   {

  39. printf("请输入x: ");  //计算密文

  40. scanf("%d",&x);  

  41. y=candp(x,e,n);  

  42. printf("密文为:%d\n",y);

  43.   }

  44.   else if(r==2)

  45. {

  46. printf("请输入密文: ");  //计算明文

  47. scanf("%d",&y);  

  48. x=candp(y,d,n);  

  49. printf("明文为%d\n",x);

  50.   }

  51.   else break;

  52. printf("加密请输入1,解密请输入2,输入其他退出\n");   

  53. scanf("%d",&r);

  54. }

  55.          return 0;

  56. }
复制代码



回复

使用道具 举报

8

主题

16

帖子

94

积分

注册会员

Rank: 2

积分
94
 楼主| 发表于 2022-6-2 15:34:11 | 显示全部楼层
本帖最后由 刘毅扬 于 2022-6-2 15:38 编辑

ECC椭圆曲线密码

公私钥生成:
1.Alice首先构造一条椭圆曲线E,在曲线上选择一点G作为生成元,并求G的阶为n,要求n必须为质数;
2.Alice选择一个私钥k(k<n),生成公钥Q=kG;
3.Alice将公钥组E、Q、G发送给Bob。
加密过程:
1.Bob收到信息后,将明文编码为M,M为曲线上一点,并选择一个随机数r rr(r < n , n r < n, nr<n,n为G GG的阶);
2.Bob计算点Cipher1与Cipher2即两段密文,计算方法如下
Cipher1=M+rQ
Cipher2=rG
3.Bob把Cipher1和Cipher2发给Alice。
解密过程:
Alice收到密文后,为了获得M,只需要Cipher1−k⋅Cipher2,因为Cipher1−k∗Cipher2=M+rQ−krG=M+rkG−krG=M
将M解码即可。

回复

使用道具 举报

8

主题

16

帖子

94

积分

注册会员

Rank: 2

积分
94
 楼主| 发表于 2022-6-2 15:37:26 | 显示全部楼层
  1. #include<stdio.h>
  2. #include<stdlib.h>
  3. #include<string.h>
  4. #include<math.h>
  5. #include<time.h>
  6. #define MAX 100

  7. typedef struct point{
  8.         int point_x;
  9.         int point_y;
  10. }Point;
  11. typedef struct ecc{
  12.         struct point p[MAX];
  13.         int len;
  14. }ECCPoint;
  15. typedef struct generator{
  16.         Point p;
  17.         int p_class;
  18. }GENE_SET;

  19. void get_all_points();
  20. int int_sqrt(int s);
  21. Point timesPiont(int k,Point p);
  22. Point add_two_points(Point p1,Point p2);
  23. int inverse(int n,int b);
  24. void get_generetor_class();
  25. void encrypt_ecc();
  26. void decrypt_ecc();
  27. int mod_p(int s);
  28. void print();
  29. int isPrime(int n);
  30. char alphabet[26]="abcdefghijklmnopqrstuvwxyz";
  31. int a=-1,b=0,p=89;
  32. ECCPoint eccPoint;
  33. GENE_SET geneSet[MAX];
  34. int geneLen;
  35. char plain[]="yes";
  36. int m[MAX];
  37. int cipher[MAX];
  38. int nB;
  39. Point P1,P2,Pt,G,PB;
  40. Point Pm;
  41. int C[MAX];
  42. int main()
  43. {        
  44.         get_generetor_class();
  45.         encrypt_ecc();
  46.         decrypt_ecc();
  47.         return 0;
  48. }
  49. void encrypt_ecc()
  50. {
  51.         int num,i,j;
  52.         int gene_class;
  53.         int num_t;
  54.         int k;
  55.         srand(time(NULL));
  56.         for(i=0;i<strlen(plain);i++)
  57.         {
  58.                 for(j=0;j<26;j++)
  59.                 {
  60.                         if(plain[i]==alphabet[j])
  61.                         {
  62.                                 m[i]=j;
  63.                         }
  64.                 }         
  65.         }
  66.         num=rand()%geneLen;
  67.         gene_class=geneSet[num].p_class;
  68.         while(isPrime(gene_class)==-1)
  69.         {
  70.                  num=rand()%(geneLen-3)+3;
  71.                  gene_class=geneSet[num].p_class;
  72.         }
  73.         G=geneSet[num].p;
  74.         nB=rand()%(gene_class-1)+1;
  75.         PB=timesPiont(nB,G);
  76.         printf("\n公钥:\n");
  77.         printf("{y^2=x^3%d*x+%d,%d,(%d,%d),(%d,%d)}\n",a,b,gene_class,G.point_x,G.point_y,PB.point_x,PB.point_y);
  78.         printf("私钥:\n");
  79.         printf("nB=%d\n",nB);
  80.         k=rand()%(gene_class-2)+1;
  81.         P1=timesPiont(k,G);
  82.         num_t=rand()%eccPoint.len;
  83.         Pt=eccPoint.p[num_t];
  84.         P2=timesPiont(k,PB);
  85.         Pm=add_two_points(Pt,P2);
  86.         printf("加密数据:\n");
  87.         printf("kG=(%d,%d),Pt+kPB=(%d,%d),C={",P1.point_x,P1.point_y,Pm.point_x,Pm.point_y);
  88.         for(i=0;i<strlen(plain);i++)
  89.         {
  90.                 C[i]=m[i]*Pt.point_x+Pt.point_y;
  91.                 printf("{%d}",C[i]);
  92.         }        
  93.         printf("}\n");
  94. }
  95. void decrypt_ecc()
  96. {
  97.         Point temp,temp1;
  98.         int m,i;
  99.         temp=timesPiont(nB,P1);
  100.         temp.point_y=0-temp.point_y;
  101.         temp1=add_two_points(Pm,temp);
  102.         printf("\n解密结果:\n");
  103.         for(i=0;i<strlen(plain);i++)
  104.         {
  105.                 m=(C[i]-temp1.point_y)/temp1.point_x;
  106.                 printf("%c",alphabet[m]);
  107.         }
  108.         printf("\n");
  109. }
  110. int isPrime(int n)
  111. {
  112.         int i,k;
  113.     k = sqrt(n);
  114.     for (i = 2; i <= k;i++)
  115.     {
  116.             if (n%i == 0)
  117.                         break;
  118.         }      
  119.     if (i <=k){
  120.             return -1;
  121.         }
  122.     else {
  123.              return 0;
  124.         }
  125. }
  126. void get_generetor_class()
  127. {        
  128.         int i,j=0;
  129.         int count=1;
  130.         Point p1,p2;
  131.         get_all_points();
  132.         printf("\n**********************************输出生成元以及阶:*************************************\n");
  133.         for(i=0;i<eccPoint.len;i++)
  134.         {
  135.                 count=1;
  136.                 p1.point_x=p2.point_x=eccPoint.p[i].point_x;
  137.                 p1.point_y=p2.point_y=eccPoint.p[i].point_y;               
  138.                 while(1)
  139.                 {        
  140.                         p2=add_two_points(p1,p2);                                       
  141.                         if(p2.point_x==-1 && p2.point_y==-1)
  142.                         {
  143.                                 break;
  144.                         }
  145.                         count++;
  146.                         if(p2.point_x==p1.point_x)
  147.                         {
  148.                                 break;
  149.                         }                                                
  150.                 }
  151.                 count++;
  152.                 if(count<=eccPoint.len+1)
  153.                 {
  154.                         geneSet[j].p.point_x=p1.point_x;
  155.                         geneSet[j].p.point_y=p1.point_y;
  156.                         geneSet[j].p_class=count;
  157.                         printf("(%d,%d)--->>%d\t",geneSet[j].p.point_x,geneSet[j].p.point_y,geneSet[j].p_class);
  158.                         j++;
  159.                         if(j % 6 ==0){
  160.                                 printf("\n");
  161.                         }
  162.                 }
  163.                 geneLen=j;        
  164.         }
  165. }
  166. Point timesPiont(int k,Point p0)
  167. {
  168.         if(k==1){
  169.                 return p0;
  170.         }                 
  171.         else if(k==2){
  172.                 return add_two_points(p0,p0);
  173.         }else{
  174.                 return add_two_points(p0,timesPiont(k-1,p0));
  175.         }
  176. }
  177. Point add_two_points(Point p1,Point p2)
  178. {
  179.         long t;
  180.         int x1=p1.point_x;
  181.         int y1=p1.point_y;
  182.         int x2=p2.point_x;
  183.         int y2=p2.point_y;
  184.         int tx,ty;
  185.         int x3,y3;
  186.         int flag=0;
  187.         if((x2==x1)&& (y2==y1) )
  188.         {
  189.                 if(y1==0)
  190.                 {
  191.                         flag=1;
  192.                 }else{
  193.                         t=(3*x1*x1+a)*inverse(p,2*y1) % p;
  194.                 }
  195.         }else{
  196.                 ty=y2-y1;
  197.                 tx=x2-x1;
  198.                 while(ty<0)
  199.                 {
  200.                         ty+=p;
  201.                 }
  202.                 while(tx<0)
  203.                 {
  204.                         tx+=p;
  205.                 }
  206.                 if(tx==0 && ty !=0)
  207.                 {
  208.                         flag=1;
  209.                 }else{
  210.                         t=ty*inverse(p,tx) % p;
  211.                 }
  212.         }
  213.         if(flag==1)
  214.         {
  215.                 p2.point_x=-1;
  216.                 p2.point_y=-1;
  217.         }else{
  218.                 x3=(t*t-x1-x2) % p;
  219.                 y3=(t*(x1-x3)-y1) % p;
  220.                 while(x3<0)
  221.                 {
  222.                         x3+=p;
  223.                 }
  224.                 while(y3<0)
  225.                 {
  226.                         y3+=p;
  227.                 }
  228.                 p2.point_x=x3;
  229.                 p2.point_y=y3;
  230.         }        
  231.         return p2;
  232. }
  233. int inverse(int n,int b)
  234. {
  235.         int q,r,r1=n,r2=b,t,t1=0,t2=1,i=1;
  236.         while(r2>0)
  237.         {
  238.                 q=r1/r2;
  239.                 r=r1%r2;
  240.                 r1=r2;
  241.                 r2=r;
  242.                 t=t1-q*t2;
  243.                 t1=t2;
  244.                 t2=t;
  245.         }
  246.         if(t1>=0)
  247.                 return t1%n;
  248.         else{  
  249.                 while((t1+i*n)<0)
  250.                         i++;
  251.                 return t1+i*n;
  252.         }
  253. }
  254. void get_all_points()
  255. {
  256.         int i=0;
  257.         int j=0;
  258.         int s,y=0;
  259.         int n=0,q=0;
  260.         int modsqrt=0;
  261.         int flag=0;
  262.         if (4 * a * a * a + 27 * b * b != 0)
  263.         {
  264.                 for(i=0;i<=p-1;i++)
  265.                 {
  266.                         flag=0;
  267.                         n=1;
  268.                         y=0;
  269.                         s= i * i * i + a * i + b;
  270.                         while(s<0)
  271.                         {
  272.                                 s+=p;
  273.                         }
  274.                         s=mod_p(s);
  275.                         modsqrt=int_sqrt(s);
  276.                         if(modsqrt!=-1)
  277.                         {
  278.                                 flag=1;
  279.                                 y=modsqrt;
  280.                         }else{
  281.                                 while(n<=p-1)
  282.                                 {
  283.                                         q=s+n*p;
  284.                                         modsqrt=int_sqrt(q);
  285.                                         if(modsqrt!=-1)
  286.                                         {
  287.                                                 y=modsqrt;
  288.                                                 flag=1;
  289.                                                 break;
  290.                                         }
  291.                                                 flag=0;
  292.                                                 n++;
  293.                                 }
  294.                         }
  295.                         if(flag==1)
  296.                         {
  297.                                 eccPoint.p[j].point_x=i;
  298.                                 eccPoint.p[j].point_y=y;
  299.                                 j++;
  300.                                 if(y!=0)
  301.                                 {
  302.                                         eccPoint.p[j].point_x=i;
  303.                                         eccPoint.p[j].point_y=(p-y) % p;
  304.                                         j++;
  305.                                 }                                
  306.                         }
  307.                 }
  308.                 eccPoint.len=j;
  309.                 print();
  310.         }        
  311. }
  312. int mod_p(int s)
  313. {
  314.         int i;
  315.         int result;
  316.         i = s / p;
  317.         result = s - i * p;
  318.         if (result >= 0)
  319.         {
  320.                 return result;
  321.         }
  322.         else
  323.         {
  324.                 return result + p;
  325.         }
  326. }

  327. int int_sqrt(int s)
  328. {
  329.         int temp;
  330.         temp=(int)sqrt(s);
  331.         if(temp*temp==s)
  332.         {
  333.                 return temp;
  334.         }else{
  335.                 return -1;
  336.         }
  337. }
  338. void print()
  339. {
  340.         int i;
  341.         int len=eccPoint.len;
  342.         printf("\n该椭圆曲线上共有%d个点(包含无穷远点)\n",len+1);
  343.         for(i=0;i<len;i++)
  344.         {
  345.                 if(i % 8==0)
  346.                 {
  347.                         printf("\n");
  348.                 }
  349.                 printf("(%2d,%2d)\t",eccPoint.p[i].point_x,eccPoint.p[i].point_y);
  350.         }
  351.         printf("\n");
  352. }
复制代码
回复

使用道具 举报

您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

教学服务系统

GMT+8, 2025-4-30 01:24 , Processed in 0.022732 second(s), 18 queries .

Powered by Discuz! X3.4

Copyright © 2001-2020, Tencent Cloud.

快速回复 返回顶部 返回列表