教学服务系统

 找回密码
 立即注册
搜索
查看: 600|回复: 5

信息计算2019级2班19号程志成

[复制链接]

7

主题

20

帖子

99

积分

注册会员

Rank: 2

积分
99
发表于 2022-4-29 22:01:17 | 显示全部楼层 |阅读模式
第一次

本帖子中包含更多资源

您需要 登录 才可以下载或查看,没有帐号?立即注册

x
回复

使用道具 举报

7

主题

20

帖子

99

积分

注册会员

Rank: 2

积分
99
 楼主| 发表于 2022-4-29 22:01:39 | 显示全部楼层
第二次.

本帖子中包含更多资源

您需要 登录 才可以下载或查看,没有帐号?立即注册

x
回复

使用道具 举报

7

主题

20

帖子

99

积分

注册会员

Rank: 2

积分
99
 楼主| 发表于 2022-4-29 22:01:55 | 显示全部楼层
第三次.

本帖子中包含更多资源

您需要 登录 才可以下载或查看,没有帐号?立即注册

x
回复

使用道具 举报

7

主题

20

帖子

99

积分

注册会员

Rank: 2

积分
99
 楼主| 发表于 2022-5-12 17:04:50 | 显示全部楼层
第四次.

本帖子中包含更多资源

您需要 登录 才可以下载或查看,没有帐号?立即注册

x
回复

使用道具 举报

7

主题

20

帖子

99

积分

注册会员

Rank: 2

积分
99
 楼主| 发表于 2022-5-12 17:09:48 | 显示全部楼层
  1. import java.math.BigInteger;
  2. import java.util.Random;
  3. import java.util.Scanner;
  4. //RSA算法
  5. public class TestRSA {
  6.         @SuppressWarnings("resource")
  7.         public static void main(String[] args) {
  8.                 TestRSA rsa=new TestRSA();
  9.                 BigInteger[] primeNumber = rsa.getPrimeNumber();
  10.                 //step1:产生两个大素数
  11.                 BigInteger p=primeNumber[0];
  12.                 BigInteger q=primeNumber[1];
  13.                 System.out.println("1、B产生的两个大素数是(保密):p="+p+" q="+q);
  14.                 //step2.1:计算n
  15.                 BigInteger n=p.multiply(q);//n=p*q
  16.                 System.out.println("2、计算的n是:"+n);
  17.                 //step2.2:计算sn
  18.                 BigInteger sn=(p.subtract(new BigInteger("1"))).multiply(q.subtract(new BigInteger("1")));//sn=(p-1)*(q-1)
  19.                 System.out.println("   计算的sn是:"+sn);
  20.                 //step3:随机选取e
  21.                 BigInteger e=rsa.getE(sn);//0<e<sn && e和 sn互素
  22.                 System.out.println("3、选取的e是:"+e);
  23.                 //step4:计算d
  24.                 BigInteger d=rsa.getD(sn, e).mod(sn);//d同时与n和sn互素
  25.                 System.out.println("4、计算的d是:"+d);
  26.                 //step5:B将n和e作为公钥公开
  27.                 System.out.println("5、公钥:n="+n+" e="+e);
  28.                 //step6:用户A获取到公钥
  29.                 System.out.println("6、用户A已经获取到公钥了,可以开始发数据了...");
  30.                 //step7:进行加密
  31.                 System.out.println("7、请输入要发送的明文(仅数字):");
  32.                 Scanner scanner=new Scanner(System.in);
  33.                 BigInteger m=new BigInteger(scanner.next());
  34.                 BigInteger c=rsa.getC(m,e,n);//计算密文
  35.                 System.out.println("加密后的密文是:"+c);
  36.                 //step8:进行解密
  37.                 BigInteger mm=rsa.getDecrypt(c,n,d);//进行解密
  38.                 System.out.println("8、解密后的的结果是:"+mm);
  39.         }
  40.         public BigInteger[] getPrimeNumber(){
  41.                 BigInteger p=null;
  42.                 BigInteger q=null;
  43.                 BigInteger[] res=new BigInteger[2];
  44.                 Random random = new Random();
  45.             p=BigInteger.probablePrime(64, random);
  46.             q=BigInteger.probablePrime(64, random);
  47.                 res[0]=p;
  48.                 res[1]=q;
  49.                 return res;
  50.         }
  51.         public BigInteger getE(BigInteger sn){
  52.                 BigInteger e = null;
  53.                 int length = sn.toString().length()-2;
  54.                 e=new BigInteger(sn.toString().subSequence(0, length-2).toString()).nextProbablePrime();
  55.                 return e;
  56.         }
  57.         public BigInteger getD(BigInteger sn,BigInteger e){
  58.                 BigInteger[] ret = new BigInteger[3];  
  59.             BigInteger u = BigInteger.valueOf(1), u1 = BigInteger.valueOf(0);  
  60.             BigInteger v = BigInteger.valueOf(0), v1 = BigInteger.valueOf(1);  
  61.             if (e.compareTo(sn) > 0) {  
  62.                 BigInteger tem = sn;  
  63.                 sn = e;  
  64.                 e = tem;  
  65.             }  
  66.             while (e.compareTo(BigInteger.valueOf(0)) != 0) {  
  67.                 BigInteger tq = sn.divide(e);  
  68.                 BigInteger tu = u;  
  69.                 u = u1;  
  70.                 u1 = tu.subtract(tq.multiply(u1));
  71.                 BigInteger tv = v;  
  72.                 v = v1;  
  73.                 v1 = tv.subtract(tq.multiply(v1));
  74.                 BigInteger tsn = sn;  
  75.                 sn = e;  
  76.                 e = tsn.subtract(tq.multiply(e));
  77.                 ret[0] = u;  
  78.                 ret[1] = v;  
  79.                 ret[2] = sn;  
  80.             }  
  81.                 return ret[1];
  82.         }
  83.         public BigInteger getC(BigInteger m,BigInteger e,BigInteger n){
  84.                 BigInteger c=null;
  85.                 c=m.modPow(e, n);
  86.                 return c;
  87.         }
  88.         public BigInteger getDecrypt(BigInteger c,BigInteger n,BigInteger d){
  89.                 BigInteger m=null;
  90.                 m=c.modPow(d, n);
  91.                 return m;
  92.         }
  93. }
复制代码




本帖子中包含更多资源

您需要 登录 才可以下载或查看,没有帐号?立即注册

x
回复

使用道具 举报

7

主题

20

帖子

99

积分

注册会员

Rank: 2

积分
99
 楼主| 发表于 2022-6-2 18:26:04 | 显示全部楼层
ecc算法

  1. #include <stdio.h>
  2. #include <math.h>
  3. int xy[22];

  4. #define N 23
  5. #define A 1
  6. //#define M 29
  7. #define a1 1
  8. #define b1 4
  9. typedef struct point {
  10.         int x;
  11.         int y;
  12. }Point;
  13. typedef struct ecc {
  14.         struct point p[100];
  15.         int len;
  16. }ECCPoint;
  17. typedef struct generator {
  18.         Point p;
  19.         int p_class;
  20. }GENE_SET;
  21. ECCPoint eccPoint;
  22. Point mul(Point p1, Point p2);
  23. Point add_two_points(Point p1, Point p2);
  24. GENE_SET geneSet[100];
  25. int geneLen;
  26. //判断平方根是否为整数
  27. int int_sqrt(int s)
  28. {
  29.         int temp;
  30.         temp = (int)sqrt(s);//转为整型
  31.         if (temp * temp == s)
  32.         {
  33.                 return temp;
  34.         }
  35.         else {
  36.                 return -1;
  37.         }
  38. }
  39. //取模函数
  40. int mod_p(int s)
  41. {
  42.         int i;        //保存s/p的倍数
  43.         int result;        //模运算的结果
  44.         i = s / N;
  45.         result = s - i * N;
  46.         if (result >= 0)
  47.         {
  48.                 return result;
  49.         }
  50.         else
  51.         {
  52.                 return result + N;
  53.         }
  54. }
  55. //打印点集
  56. void print()
  57. {
  58.         int i;
  59.         int len = eccPoint.len;
  60.         printf("\n该椭圆曲线上共有%d个点(包含无穷远点)\n", len + 1);
  61.         for (i = 0; i < len; i++)
  62.         {
  63.                 if (i % 8 == 0)
  64.                 {
  65.                         printf("\n");
  66.                 }
  67.                 printf("(%2d,%2d)\t", eccPoint.p[i].x, eccPoint.p[i].y);
  68.         }
  69.         printf("\n");
  70. }
  71. //task1:求出椭圆曲线上所有点
  72. void get_all_points()
  73. {
  74.         int i = 0;
  75.         int j = 0;
  76.         int s, y = 0;
  77.         int n = 0, q = 0;
  78.         int modsqrt = 0;
  79.         int flag = 0;
  80.         if (a1 * a1 * a1 + b1 * b1 + 4 != 0)
  81.         {
  82.                 for (i = 0; i <= N - 1; i++)
  83.                 {
  84.                         flag = 0;
  85.                         n = 1;
  86.                         y = 0;
  87.                         s = i * i * i + a1 * i + b1;
  88.                         while (s < 0)
  89.                         {
  90.                                 s += N;
  91.                         }
  92.                         s = mod_p(s);
  93.                         modsqrt = int_sqrt(s);
  94.                         if (modsqrt != -1)
  95.                         {
  96.                                 flag = 1;
  97.                                 y = modsqrt;
  98.                         }
  99.                         else {
  100.                                 while (n <= N - 1)
  101.                                 {
  102.                                         q = s + n * N;
  103.                                         modsqrt = int_sqrt(q);
  104.                                         if (modsqrt != -1)
  105.                                         {
  106.                                                 y = modsqrt;
  107.                                                 flag = 1;
  108.                                                 break;
  109.                                         }
  110.                                         flag = 0;
  111.                                         n++;
  112.                                 }
  113.                         }
  114.                         if (flag == 1)
  115.                         {
  116.                                 eccPoint.p[j].x = i;
  117.                                 eccPoint.p[j].y = y;
  118.                                 j++;
  119.                                 if (y != 0)
  120.                                 {
  121.                                         eccPoint.p[j].x = i;
  122.                                         eccPoint.p[j].y = (N - y) % N;
  123.                                         j++;
  124.                                 }
  125.                         }
  126.                 }
  127.                 eccPoint.len = j;//点集个数
  128.                 print(); //打印点集
  129.         }
  130. }

  131. //task3:求生成元以及阶
  132. void get_generetor_class()
  133. {
  134.         int i, j = 0;
  135.         int count = 1;
  136.         Point p1, p2;
  137.         get_all_points();
  138.         printf("\n**********************************输出生成元以及阶:*************************************\n");
  139.         for (i = 0; i < eccPoint.len; i++)
  140.         {
  141.                 count = 1;
  142.                 p1.x = p2.x = eccPoint.p[i].x;
  143.                 p1.y = p2.y = eccPoint.p[i].y;
  144.                 while (1)
  145.                 {
  146.                         p2 = add_two_points(p1, p2);
  147.                         if (p2.x == -1 && p2.y == -1)
  148.                         {
  149.                                 break;
  150.                         }
  151.                         count++;
  152.                         if (p2.x == p1.x)
  153.                         {
  154.                                 break;
  155.                         }
  156.                 }
  157.                 count++;
  158.                 if (count <= eccPoint.len + 1)
  159.                 {
  160.                         geneSet[j].p.x = p1.x;
  161.                         geneSet[j].p.y = p1.y;
  162.                         geneSet[j].p_class = count;
  163.                         printf("(%d,%d)--->>%d\t", geneSet[j].p.x, geneSet[j].p.y, geneSet[j].p_class);
  164.                         j++;
  165.                         if (j % 6 == 0) {
  166.                                 printf("\n");
  167.                         }
  168.                 }
  169.                 geneLen = j;
  170.         }
  171. }

  172. // 求 a mod b 的逆元
  173. void exGcd(int a, int b) {
  174.         if (b == 0) {
  175.                 xy[0] = 1;
  176.                 xy[1] = 0;
  177.         }
  178.         else {
  179.                 exGcd(b, a % b);
  180.                 int x = xy[0];
  181.                 xy[0] = xy[1];
  182.                 xy[1] = x - (a / b) * xy[1];
  183.         }

  184. }
  185. int calculate3(int y, int k, int p) {

  186.         int l = 1;
  187.         for (int i = 0; i < k; i++) {
  188.                 l = l * y;
  189.                 l = l % p;
  190.         }

  191.         return l;
  192. }

  193. Point eccmutiply(int n, Point p) {
  194.         int a, b, l, k, m;
  195.         a = p.x;
  196.         b = p.y;
  197.         for (int i = 0; i < n - 1; i++) {

  198.                 if (a == p.x && b == p.y) {
  199.                         exGcd(2 * p.y, N);
  200.                         k = xy[0];
  201.                         if (k < 0)k = k + N;
  202.                         printf("逆元=%d\n", k);
  203.                         l = (3 * p.x * p.x + A) * k;
  204.                         l = calculate3(l, 1, N);
  205.                         if (l < 0) {
  206.                                 l = l + N;
  207.                         }
  208.                 }
  209.                 else {
  210.                         exGcd(a - p.x + N, N);
  211.                         k = xy[0];
  212.                         if (k < 0)k = k + N;
  213.                         printf("else逆元=%d\n", k);
  214.                         l = (b - p.y) * k;
  215.                         l = calculate3(l, 1, N);
  216.                         if (l < 0) {
  217.                                 l = l + N;
  218.                         }
  219.                         printf("l=%d\n", l);
  220.                 }
  221.                 m = p.x;
  222.                 a = l * l - a - p.x;
  223.                 a = calculate3(a, 1, N);
  224.                 if (a < 0) {
  225.                         a = a + N;
  226.                 }
  227.                 b = l * (m - a) - p.y;
  228.                 b = calculate3(b, 1, N);

  229.                 if (b < 0) {
  230.                         b = b + N;
  231.                 }
  232.                 printf("%d(a,b)=(%d,%d)\n", i + 2, a, b);
  233.                 //if(a==4&&b==5)break;
  234.         }
  235.         Point p3;
  236.         p3.x = a;
  237.         p3.y = b;
  238.         return p3;
  239. }
  240. Point mul(Point p1, Point p2) {
  241.         int k, l;
  242.         exGcd(p2.x - p1.x + N, N);
  243.         k = xy[0];
  244.         if (k < 0)k = k + N;
  245.         //printf("else逆元=%d\n",k);
  246.         l = (p2.y - p1.y) * k;
  247.         l = calculate3(l, 1, N);
  248.         if (l < 0) {
  249.                 l = l + N;
  250.         }
  251.         //printf("l=%d\n",l);       
  252.         Point p3;
  253.         p3.x = l * l - p1.x - p2.x;
  254.         p3.x = calculate3(p3.x, 1, N);
  255.         if (p3.x < 0)p3.x = p3.x + N;

  256.         p3.y = l * (p1.x - p3.x) - p1.y;
  257.         p3.y = calculate3(p3.y, 1, N);
  258.         if (p3.y < 0)p3.y = p3.y + N;
  259.         return p3;
  260. }

  261. //求b关于n的逆元
  262. int inverse(int n, int b)
  263. {
  264.         int q, r, r1 = n, r2 = b, t, t1 = 0, t2 = 1, i = 1;
  265.         while (r2 > 0)
  266.         {
  267.                 q = r1 / r2;
  268.                 r = r1 % r2;
  269.                 r1 = r2;
  270.                 r2 = r;
  271.                 t = t1 - q * t2;
  272.                 t1 = t2;
  273.                 t2 = t;
  274.         }
  275.         if (t1 >= 0)
  276.                 return t1 % n;
  277.         else {
  278.                 while ((t1 + i * n) < 0)
  279.                         i++;
  280.                 return t1 + i * n;
  281.         }
  282. }
  283. //两点的加法运算
  284. Point add_two_points(Point p1, Point p2)
  285. {
  286.         long t;
  287.         int x1 = p1.x;
  288.         int y1 = p1.y;
  289.         int x2 = p2.x;
  290.         int y2 = p2.y;
  291.         int tx, ty;
  292.         int x3, y3;
  293.         int flag = 0;
  294.         //求
  295.         if ((x2 == x1) && (y2 == y1))
  296.         {
  297.                 //相同点相加
  298.                 if (y1 == 0)
  299.                 {
  300.                         flag = 1;
  301.                 }
  302.                 else {
  303.                         t = (3 * x1 * x1 + a1) * inverse(N, 2 * y1) % N;
  304.                 }
  305.                 //printf("inverse(p,2*y1)=%d\n",inverse(p,2*y1));
  306.         }
  307.         else {
  308.                 //不同点相加
  309.                 ty = y2 - y1;
  310.                 tx = x2 - x1;
  311.                 while (ty < 0)
  312.                 {
  313.                         ty += N;
  314.                 }
  315.                 while (tx < 0)
  316.                 {
  317.                         tx += N;
  318.                 }
  319.                 if (tx == 0 && ty != 0)
  320.                 {
  321.                         flag = 1;
  322.                 }
  323.                 else {
  324.                         t = ty * inverse(N, tx) % N;
  325.                 }
  326.         }
  327.         if (flag == 1)
  328.         {
  329.                 p2.x = -1;
  330.                 p2.y = -1;
  331.         }
  332.         else {
  333.                 x3 = (t * t - x1 - x2) % N;
  334.                 y3 = (t * (x1 - x3) - y1) % N;
  335.                 //使结果在有限域GF(P)上
  336.                 while (x3 < 0)
  337.                 {
  338.                         x3 += N;
  339.                 }
  340.                 while (y3 < 0)
  341.                 {
  342.                         y3 += N;
  343.                 }
  344.                 p2.x = x3;
  345.                 p2.y = y3;
  346.         }
  347.         return p2;
  348. }
  349. //task2:倍点运算的递归算法
  350. Point timesPiont(int k, Point p0)
  351. {
  352.         if (k == 1) {
  353.                 return p0;
  354.         }
  355.         else if (k == 2) {
  356.                 return add_two_points(p0, p0);
  357.         }
  358.         else {
  359.                 return add_two_points(p0, timesPiont(k - 1, p0));
  360.         }
  361. }
  362. main() {
  363.         get_generetor_class();
  364.         Point p1, p2, p, p4, p5, p6, p7, p8, p9, p10;
  365.         int na, k, h, r, s, u1, u2;
  366.         printf("选择生成元");
  367.         scanf_s("%d%d", &p1.x, &p1.y);
  368.         int j = 0;
  369.         while (j < N) {

  370.                 if (geneSet[j].p.x == p1.x && geneSet[j].p.y == p1.y) {
  371.                         break;
  372.                 }
  373.                 printf("j=%d\n", j);
  374.                 ++j;
  375.         }
  376.         int M = geneSet[j].p_class;
  377.         printf("M=%d", M);
  378.         //        p1.x=0;
  379.         //        p1.y=2;
  380.                 //p.x=20;
  381.                 //p.y=1;
  382.         printf("请输入私钥");
  383.         scanf_s("%d", &na);
  384.         p2 = eccmutiply(na, p1);
  385.         printf("公钥为(%d,%d)", p2.x, p2.y);
  386.         //p2=mul(p,p1);

  387.         //printf("(%d,%d)",p2.x,p2.y);

  388.         //签名过程
  389.         printf("输入随机数k\n");
  390.         scanf_s("%d", &k);
  391.         printf("输入hash\n");
  392.         scanf_s("%d", &h);


  393.         p4 = eccmutiply(k, p1);
  394.         r = calculate3(p4.x, 1, N);
  395.         if (r < 0)r = r + N;
  396.         s = inverse(M, k) * (h + na * r) % M;
  397.         if (s < 0)s = s + N;
  398.         printf("签名为(%d,%d)\n", r, s);
  399.         printf("========================");
  400.         //验证过程
  401.         u1 = h * inverse(M, s) % M;

  402.         if (u1 < 0)u1 = u1 + M;
  403.         u2 = r * inverse(M, s) % M;

  404.         if (u2 < 0)u2 = u2 + M;

  405.         printf("u1u2=%d,%d\n", u1, u2);

  406.         p5 = eccmutiply(u1, p1);
  407.         printf("sG=(%d,%d)\n", p5.x, p5.y);

  408.         p6 = eccmutiply(u2, p2);
  409.         printf("HP=(%d,%d)\n", p6.x, p6.y);

  410.         p7 = add_two_points(p5, p6);
  411.         printf("sG+HP=(%d,%d)\n", p7.x, p7.y);
  412.         if (calculate3(p7.x, 1, N) == r) {
  413.                 printf("通过");
  414.         }
  415.         else {
  416.                 printf("失败");
  417.         }

  418. }
复制代码
回复

使用道具 举报

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

本版积分规则

教学服务系统

GMT+8, 2025-4-30 07:46 , Processed in 0.014522 second(s), 19 queries .

Powered by Discuz! X3.4

Copyright © 2001-2020, Tencent Cloud.

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