教学服务系统

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

信息计算2019级1班22号李浩栋

[复制链接]

8

主题

16

帖子

72

积分

注册会员

Rank: 2

积分
72
发表于 2022-6-2 21:10:47 | 显示全部楼层 |阅读模式
RSA

算法原理:

  RSA公开密钥密码体制的原理是:根据数论,寻求两个大素数比较简单,而将它们的乘积进行因式分解却极其困难,因此可以将乘积公开作为加密密钥。



实现代码:

GCD.java:

  1. import java.math.BigInteger;

  2. public class GCD {

  3.     public BigInteger MaxFactor(BigInteger value1,BigInteger value2){


  4.         BigInteger zero = new BigInteger("0");

  5.         while (true){

  6.             BigInteger dividend = new BigInteger(value1.max(value2).toString());

  7.             BigInteger divisor = new BigInteger(value1.min(value2).toString());

  8.             if(dividend.remainder(divisor).equals(zero)){

  9.                 return divisor;

  10.             }
  11.             else{

  12.                 value1 = value1.min(value2);

  13.                 value2 = dividend.remainder(divisor);

  14.             }

  15.         }

  16.     }

  17. }
复制代码


HightEXPMod.java:

  1. import java.math.BigInteger;

  2. public class HightEXPMod {

  3.     public BigInteger computeHightEXPMod(BigInteger value, BigInteger exponent, BigInteger modValue){


  4.         BigInteger result = new BigInteger("1");

  5.         BigInteger zero = new BigInteger("0");

  6.         BigInteger two = new BigInteger("2");

  7.         BigInteger one = new BigInteger("1");

  8.         while (!exponent.equals(zero)){

  9.             if(!exponent.remainder(two).equals(zero)){

  10.                 result = result.multiply(value).remainder(modValue);

  11.                 exponent = exponent.subtract(one);

  12.             }
  13.             else {

  14.                 while (exponent.remainder(two).equals(zero)){

  15.                     exponent = exponent.divide(two);

  16.                     value = value.multiply(value).remainder(modValue);

  17.                 }

  18.             }

  19.         }

  20.         return result;
  21.     }
  22. }
复制代码


InverseMod.java:

  1. import java.math.BigInteger;

  2. public class InverseMod {

  3.     public BigInteger computeModInverse(BigInteger value, BigInteger modValue){

  4.         GCD gcd = new GCD();

  5.         BigInteger one = new BigInteger("1");

  6.         BigInteger zero = new BigInteger("0");

  7.         if(gcd.MaxFactor(value,modValue).equals(one)){

  8.             BigInteger n1 = new BigInteger(modValue.toString());

  9.             BigInteger n2 = new BigInteger(value.toString());

  10.             BigInteger b1 = new BigInteger("0");

  11.             BigInteger b2 = new BigInteger("1");

  12.             BigInteger q = n1.divide(n2);

  13.             BigInteger r = n1.subtract(q.multiply(n2));

  14.             BigInteger t = new BigInteger("0");

  15.             while(true){

  16.                 if(!r.equals(zero)){

  17.                     n1 = n2;

  18.                     n2 = r;

  19.                     t = b2;

  20.                     b2 = b1.subtract(q.multiply(b2));

  21.                     b1 = t;

  22.                     q = n1.divide(n2);

  23.                     r = n1.subtract(q.multiply(n2));

  24.                 }
  25.                 else{

  26.                     if(!n2.equals(one)){

  27.                         return zero;
  28.                     }
  29.                     else if(n2.equals(one)){

  30.                         if (b2.compareTo(zero) < 0){

  31.                             return b2.add(modValue);

  32.                         }
  33.                         else{

  34.                             return b2.remainder(modValue);
  35.                         }
  36.                     }

  37.                 }
  38.             }
  39.         }
  40.         else {

  41.             return zero;

  42.         }
  43.     }
  44. }
复制代码


PrimeNumber.java:

  1. import java.math.BigInteger;
  2. import java.util.Random;

  3. public class PrimeNumber {

  4.     public BigInteger getPrimeNumber(){

  5.         while(true){

  6.             Random random = new Random();

  7.             BigInteger primeNumber = BigInteger.probablePrime(1024,random);

  8.             if(judgePrimeNumber(primeNumber)){

  9.                 return primeNumber;

  10.             }

  11.         }

  12.     }

  13.     public static boolean judgePrimeNumber(BigInteger primeNumber_pro){

  14.         int s = 0;

  15.         BigInteger primeNumber_m = new BigInteger(primeNumber_pro.toString());

  16.         BigInteger one = new BigInteger("1");

  17.         BigInteger zero = new BigInteger("0");

  18.         BigInteger two = new BigInteger("2");

  19.         primeNumber_m = primeNumber_m.subtract(one);

  20.         while (true){

  21.             if(primeNumber_m.remainder(two).equals(zero)){

  22.                 s += 1;

  23.                 primeNumber_m = primeNumber_m.divide(two);

  24.             }
  25.             else {

  26.                 break;

  27.             }
  28.         }

  29.         while (true){

  30.             Random random = new Random();

  31.             int lenght = (int)(Math.random() * 1024);

  32.             BigInteger divide = BigInteger.probablePrime(lenght,random);

  33.             BigInteger b = primeNumber_pro.subtract(one).divide(divide);

  34.             int r = 0;

  35.             HightEXPMod hightEXPMod = new HightEXPMod();

  36.             BigInteger z = hightEXPMod.computeHightEXPMod(b,primeNumber_m,primeNumber_pro);

  37.             while(true){

  38.                 if(z.equals(one) || z.equals(primeNumber_pro.subtract(one))){

  39.                     return true;

  40.                 }
  41.                 else{

  42.                     while(true){

  43.                         if(r == s - 1){

  44.                             return false;

  45.                         }
  46.                         else{

  47.                             r += 1;

  48.                             z = hightEXPMod.computeHightEXPMod(z,two,primeNumber_pro);

  49.                             if(z.equals(primeNumber_pro.subtract(one))){

  50.                                 return true;

  51.                             }
  52.                             else {

  53.                             }

  54.                         }

  55.                     }

  56.                 }

  57.             }
  58.         }

  59.     }
  60. }
复制代码


RSAEncipher.java:

  1. import java.math.BigInteger;
  2. import java.util.Scanner;
  3. import java.util.zip.Inflater;

  4. public class RSAEncipher {

  5.     static String information = null;

  6.     public static void main(String[] args) {

  7.         System.out.print("input information to encipher: ");

  8.         Scanner inpute = new Scanner(System.in);

  9.         information = inpute.nextLine();

  10.         information = Encipher(information);

  11.         System.out.println(information);
  12.         

  13.     }

  14.     public static String Encipher(String information){

  15.         PrimeNumber primeNumber = new PrimeNumber();

  16.         BigInteger one = new BigInteger("1");

  17.         BigInteger zero = new BigInteger("0");

  18.         BigInteger p = primeNumber.getPrimeNumber();

  19.         BigInteger q = primeNumber.getPrimeNumber();

  20.         BigInteger n = p.multiply(q);

  21.         BigInteger $n = p.subtract(one).multiply(q.subtract(one));

  22.         BigInteger e = primeNumber.getPrimeNumber();

  23.         GCD gcd = new GCD();

  24.         InverseMod inverseMod = new InverseMod();

  25.         while(!gcd.MaxFactor(e,$n).equals(one) || inverseMod.computeModInverse(e,$n).equals(zero)){

  26.             e = primeNumber.getPrimeNumber();
  27.         }

  28.         BigInteger d = inverseMod.computeModInverse(e,$n);

  29.         HightEXPMod hightEXPMod = new HightEXPMod();

  30.         String information_encipher = "";

  31.         for(int i = 0; i < information.length();i++){

  32.             int c_value = (int)(information.charAt(i));

  33.             BigInteger information_m = new BigInteger(c_value + "");

  34.             information_m = hightEXPMod.computeHightEXPMod(information_m,e,n);

  35.             System.out.println(information_m.toString());

  36.             information_m = hightEXPMod.computeHightEXPMod(information_m,d,n);

  37.             char c = (char) Integer.parseInt(information_m.toString());

  38.             information_encipher += c;
  39.         }

  40.         return information_encipher;
  41.     }
  42. }
复制代码


运行截图:



本帖子中包含更多资源

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

x
回复

使用道具 举报

8

主题

16

帖子

72

积分

注册会员

Rank: 2

积分
72
 楼主| 发表于 2022-6-2 21:27:22 | 显示全部楼层
本帖最后由 李浩栋 于 2022-6-2 21:31 编辑

椭圆曲线加密(ECC)

ecc简介:
  椭圆曲线加密,全称EllipseCurve Cryptography,简称ECC。与传统的基于大素数因数分解难题的方式不同,ECC通过椭圆曲线的方式产生密钥。相比RSA,ECC优势是可以使用更短的密钥,来实现与RSA相当或更高的安全。

题目设计:
(1)编程计算该椭圆曲线上所有在有限域GF(89)上的点;
(2)编程实现椭圆曲线上任意一个点P(例如P=(12,5))的倍点运算的递归算法,即计算k*P( k=2,3,…);(重点!)
(3)利用此递归算法找出椭圆曲线上的所有生成元G以及它们的阶n,即满足n*G=O;
(4)设计实现某一用户B的公钥、私钥算法,即得到public key=(n, G, PB, Ep(a, b))
secure key=nB(小于n)
(5)假如用户A发送明文消息“yes”并加密传输给用户B,用户B接收消息后要能解密为明文。试用ECC密码体制实现此功能。


回复

使用道具 举报

8

主题

16

帖子

72

积分

注册会员

Rank: 2

积分
72
 楼主| 发表于 2022-6-2 21:33:25 | 显示全部楼层
实现代码:
ecc.cpp:
  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[27] = "abcdefghijklmnopqrstuvwxyz";
  31. int a = -1, b = 0, p = 89;//椭圆曲线为E89(-1,0): y2=x3-x (mod 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. //task4:加密
  50. void encrypt_ecc()
  51. {
  52.         int num, i, j;
  53.         int gene_class;
  54.         int num_t;
  55.         int k;
  56.         srand(time(NULL));
  57.         //明文转换过程
  58.         for (i = 0; i < strlen(plain); i++)
  59.         {
  60.                 for (j = 0; j < 26; j++) //for(j=0;j<26;j++)
  61.                 {
  62.                         if (plain[i] == alphabet[j])
  63.                         {
  64.                                 m[i] = j;//将字符串明文换成数字,并存到整型数组m里面
  65.                         }
  66.                 }
  67.         }
  68.         //选择生成元
  69.         num = rand() % geneLen;
  70.         gene_class = geneSet[num].p_class;
  71.         while (isPrime(gene_class) == -1)//不是素数
  72.         {
  73.                 num = rand() % (geneLen - 3) + 3;
  74.                 gene_class = geneSet[num].p_class;
  75.         }
  76.         //printf("gene_class=%d\n",gene_class);
  77.         G = geneSet[num].p;
  78.         //printf("G:(%d,%d)\n",geneSet[num].p.point_x,geneSet[num].p.point_y);
  79.         nB = rand() % (gene_class - 1) + 1;//选择私钥
  80.         PB = timesPiont(nB, G);
  81.         printf("\n公钥:\n");
  82.         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);
  83.         printf("私钥:\n");
  84.         printf("nB=%d\n", nB);
  85.         //加密
  86.         //
  87.         k = rand() % (gene_class - 2) + 1;
  88.         P1 = timesPiont(k, G);
  89.         //
  90.         num_t = rand() % eccPoint.len; //选择映射点
  91.         Pt = eccPoint.p[num_t];
  92.         //printf("Pt:(%d,%d)\n",Pt.point_x,Pt.point_y);
  93.         P2 = timesPiont(k, PB);
  94.         Pm = add_two_points(Pt, P2);
  95.         printf("加密数据:\n");
  96.         printf("kG=(%d,%d),Pt+kPB=(%d,%d),C={", P1.point_x, P1.point_y, Pm.point_x, Pm.point_y);
  97.         for (i = 0; i < strlen(plain); i++)
  98.         {
  99.                 //                num_t=rand()%eccPoint.len; //选择映射点
  100.                 //                Pt=eccPoint.p[num_t];
  101.                 C[i] = m[i] * Pt.point_x + Pt.point_y;
  102.                 printf("{%d}", C[i]);
  103.         }
  104.         printf("}\n");
  105. }
  106. //task5:解密
  107. void decrypt_ecc()
  108. {
  109.         Point temp, temp1;
  110.         int m, i;
  111.         temp = timesPiont(nB, P1);
  112.         temp.point_y = 0 - temp.point_y;
  113.         temp1 = add_two_points(Pm, temp);//求解Pt
  114. //        printf("(%d,%d)\n",temp.point_x,temp.point_y);
  115. //        printf("(%d,%d)\n",temp1.point_x,temp1.point_y);
  116.         printf("\n解密结果:\n");
  117.         for (i = 0; i < strlen(plain); i++)
  118.         {
  119.                 m = (C[i] - temp1.point_y) / temp1.point_x;
  120.                 printf("%c", alphabet[m]);//输出密文
  121.         }
  122.         printf("\n");
  123. }
  124. //判断是否为素数
  125. int isPrime(int n)
  126. {
  127.         int i, k;
  128.         k = sqrt(n);
  129.         for (i = 2; i <= k; i++)
  130.         {
  131.                 if (n % i == 0)
  132.                         break;
  133.         }
  134.         if (i <= k) {
  135.                 return -1;
  136.         }
  137.         else {
  138.                 return 0;
  139.         }
  140. }
  141. //task3:求生成元以及阶
  142. void get_generetor_class()
  143. {
  144.         int i, j = 0;
  145.         int count = 1;
  146.         Point p1, p2;
  147.         get_all_points();
  148.         //        p1.point_x=p2.point_x=3;
  149.         //        p1.point_y=p2.point_y=2;
  150.         //        while(1)
  151.         //        {
  152.         //                printf("(%d,%d)+(%d,%d)---%d\n",p1.point_x,p1.point_y,p2.point_x,p2.point_y,count);
  153.         //                p2=add_two_points(p1,p2);
  154.         //                count++;
  155.         //                if(p2.point_x==-1 && p2.point_y==-1)
  156.         //                {
  157.         //                        break;
  158.         //                }
  159.         //        }
  160.         //        printf("\n\n(%d,%d)---%d\n",p1.point_x,p1.point_y,count);
  161.                 //
  162.         //        do{
  163.         //                        printf("(%d,%d)+(%d,%d)---%d\n",p1.point_x,p1.point_y,p2.point_x,p2.point_y,count);
  164.         //                        p2=add_two_points(p1,p2);
  165.         //                        count++;
  166.         //                       
  167.         //        } while(!((p2.point_x==p1.point_x)&&(p2.point_y==p1.point_y)));
  168.         //        printf("(%d,%d)+(%d,%d)---%d\n",p1.point_x,p1.point_y,p2.point_x,p2.point_y,count);
  169.         //        count ++ ;
  170.         //        printf("\n\n(%d,%d)---%d\n",p1.point_x,p1.point_y,count);
  171.         printf("\n**********************************输出生成元以及阶:*************************************\n");
  172.         for (i = 0; i < eccPoint.len; i++)
  173.         {
  174.                 count = 1;
  175.                 p1.point_x = p2.point_x = eccPoint.p[i].point_x;
  176.                 p1.point_y = p2.point_y = eccPoint.p[i].point_y;
  177.                 while (1)
  178.                 {
  179.                         p2 = add_two_points(p1, p2);
  180.                         if (p2.point_x == -1 && p2.point_y == -1)
  181.                         {
  182.                                 break;
  183.                         }
  184.                         count++;
  185.                         if (p2.point_x == p1.point_x)
  186.                         {
  187.                                 break;
  188.                         }
  189.                 }
  190.                 count++;
  191.                 if (count <= eccPoint.len + 1)
  192.                 {
  193.                         geneSet[j].p.point_x = p1.point_x;
  194.                         geneSet[j].p.point_y = p1.point_y;
  195.                         geneSet[j].p_class = count;
  196.                         printf("(%d,%d)--->>%d\t", geneSet[j].p.point_x, geneSet[j].p.point_y, geneSet[j].p_class);
  197.                         j++;
  198.                         if (j % 6 == 0) {
  199.                                 printf("\n");
  200.                         }
  201.                 }
  202.                 geneLen = j;
  203.         }
  204. }
复制代码
接上:
  1. //task2:倍点运算的递归算法
  2. Point timesPiont(int k, Point p0)
  3. {
  4.         if (k == 1) {
  5.                 return p0;
  6.         }
  7.         else if (k == 2) {
  8.                 return add_two_points(p0, p0);
  9.         }
  10.         else {
  11.                 return add_two_points(p0, timesPiont(k - 1, p0));
  12.         }
  13. }

  14. //两点的加法运算
  15. Point add_two_points(Point p1, Point p2)
  16. {
  17.         long t;
  18.         int x1 = p1.point_x;
  19.         int y1 = p1.point_y;
  20.         int x2 = p2.point_x;
  21.         int y2 = p2.point_y;
  22.         int tx, ty;
  23.         int x3, y3;
  24.         int flag = 0;
  25.         //求
  26.         if ((x2 == x1) && (y2 == y1))
  27.         {
  28.                 //相同点相加
  29.                 if (y1 == 0)
  30.                 {
  31.                         flag = 1;
  32.                 }
  33.                 else {
  34.                         t = (3 * x1 * x1 + a) * inverse(p, 2 * y1) % p;
  35.                 }
  36.                 //printf("inverse(p,2*y1)=%d\n",inverse(p,2*y1));
  37.         }
  38.         else {
  39.                 //不同点相加
  40.                 ty = y2 - y1;
  41.                 tx = x2 - x1;
  42.                 while (ty < 0)
  43.                 {
  44.                         ty += p;
  45.                 }
  46.                 while (tx < 0)
  47.                 {
  48.                         tx += p;
  49.                 }
  50.                 if (tx == 0 && ty != 0)
  51.                 {
  52.                         flag = 1;
  53.                 }
  54.                 else {
  55.                         t = ty * inverse(p, tx) % p;
  56.                 }
  57.         }
  58.         if (flag == 1)
  59.         {
  60.                 p2.point_x = -1;
  61.                 p2.point_y = -1;
  62.         }
  63.         else {
  64.                 x3 = (t * t - x1 - x2) % p;
  65.                 y3 = (t * (x1 - x3) - y1) % p;
  66.                 //使结果在有限域GF(P)上
  67.                 while (x3 < 0)
  68.                 {
  69.                         x3 += p;
  70.                 }
  71.                 while (y3 < 0)
  72.                 {
  73.                         y3 += p;
  74.                 }
  75.                 p2.point_x = x3;
  76.                 p2.point_y = y3;
  77.         }
  78.         return p2;
  79. }
  80. //求b关于n的逆元
  81. int inverse(int n, int b)
  82. {
  83.         int q, r, r1 = n, r2 = b, t, t1 = 0, t2 = 1, i = 1;
  84.         while (r2 > 0)
  85.         {
  86.                 q = r1 / r2;
  87.                 r = r1 % r2;
  88.                 r1 = r2;
  89.                 r2 = r;
  90.                 t = t1 - q * t2;
  91.                 t1 = t2;
  92.                 t2 = t;
  93.         }
  94.         if (t1 >= 0)
  95.                 return t1 % n;
  96.         else {
  97.                 while ((t1 + i * n) < 0)
  98.                         i++;
  99.                 return t1 + i * n;
  100.         }
  101. }
  102. //task1:求出椭圆曲线上所有点
  103. void get_all_points()
  104. {
  105.         int i = 0;
  106.         int j = 0;
  107.         int s, y = 0;
  108.         int n = 0, q = 0;
  109.         int modsqrt = 0;
  110.         int flag = 0;
  111.         if (4 * a * a * a + 27 * b * b != 0)
  112.         {
  113.                 for (i = 0; i <= p - 1; i++)
  114.                 {
  115.                         flag = 0;
  116.                         n = 1;
  117.                         y = 0;
  118.                         s = i * i * i + a * i + b;
  119.                         while (s < 0)
  120.                         {
  121.                                 s += p;
  122.                         }
  123.                         s = mod_p(s);
  124.                         modsqrt = int_sqrt(s);
  125.                         if (modsqrt != -1)
  126.                         {
  127.                                 flag = 1;
  128.                                 y = modsqrt;
  129.                         }
  130.                         else {
  131.                                 while (n <= p - 1)
  132.                                 {
  133.                                         q = s + n * p;
  134.                                         modsqrt = int_sqrt(q);
  135.                                         if (modsqrt != -1)
  136.                                         {
  137.                                                 y = modsqrt;
  138.                                                 flag = 1;
  139.                                                 break;
  140.                                         }
  141.                                         flag = 0;
  142.                                         n++;
  143.                                 }
  144.                         }
  145.                         if (flag == 1)
  146.                         {
  147.                                 eccPoint.p[j].point_x = i;
  148.                                 eccPoint.p[j].point_y = y;
  149.                                 j++;
  150.                                 if (y != 0)
  151.                                 {
  152.                                         eccPoint.p[j].point_x = i;
  153.                                         eccPoint.p[j].point_y = (p - y) % p;
  154.                                         j++;
  155.                                 }
  156.                         }
  157.                 }
  158.                 eccPoint.len = j;//点集个数
  159.                 print(); //打印点集
  160.         }
  161. }

  162. //取模函数
  163. int mod_p(int s)
  164. {
  165.         int i;        //保存s/p的倍数
  166.         int result;        //模运算的结果
  167.         i = s / p;
  168.         result = s - i * p;
  169.         if (result >= 0)
  170.         {
  171.                 return result;
  172.         }
  173.         else
  174.         {
  175.                 return result + p;
  176.         }
  177. }

  178. //判断平方根是否为整数
  179. int int_sqrt(int s)
  180. {
  181.         int temp;
  182.         temp = (int)sqrt(s);//转为整型
  183.         if (temp * temp == s)
  184.         {
  185.                 return temp;
  186.         }
  187.         else {
  188.                 return -1;
  189.         }
  190. }
  191. //打印点集
  192. void print()
  193. {
  194.         int i;
  195.         int len = eccPoint.len;
  196.         printf("\n该椭圆曲线上共有%d个点(包含无穷远点)\n", len + 1);
  197.         for (i = 0; i < len; i++)
  198.         {
  199.                 if (i % 8 == 0)
  200.                 {
  201.                         printf("\n");
  202.                 }
  203.                 printf("(%2d,%2d)\t", eccPoint.p[i].point_x, eccPoint.p[i].point_y);
  204.         }
  205.         printf("\n");
  206. }
复制代码


运行截图:

本帖子中包含更多资源

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

x
回复

使用道具 举报

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

本版积分规则

教学服务系统

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

Powered by Discuz! X3.4

Copyright © 2001-2020, Tencent Cloud.

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