教学服务系统

 找回密码
 立即注册
搜索
查看: 627|回复: 3

信息计算2019级2班17号戴隆俊

[复制链接]

11

主题

22

帖子

107

积分

注册会员

Rank: 2

积分
107
发表于 2022-5-3 00:43:19 | 显示全部楼层 |阅读模式
本帖最后由 戴隆俊 于 2022-5-26 21:41 编辑
  1. package kl;
复制代码
  1. package kl;

  2. import java.math.BigInteger;

  3. public class Prime {
  4.         public boolean sushu(double n) {
  5.                 // 判断素数
  6.                 boolean isPrime = true;
  7.                 // 如果n大于2 继续判断 否则 isPrime的值不变 2素数
  8.                 if (n > 2) {
  9.                         // 如果n是大于2的偶数 认定不是素数 修改变量值为false
  10.                         if (n % 2 == 0) {

  11.                                 isPrime = false;
  12.                         } else {
  13.                                 for (int i = 3; i <= (int) Math.sqrt(n); i += 2) {
  14.                                         if (n % i == 0) {
  15.                                                 isPrime = false;
  16.                                                 break;
  17.                                         }
  18.                                 }
  19.                         }

  20.                 }
  21.                 return isPrime;
  22.         }

  23.         private static final BigInteger ZERO = BigInteger.ZERO;
  24.         private static final BigInteger ONE = BigInteger.ONE;
  25.         private static final BigInteger TWO = new BigInteger("2");
  26.         private static final int ERR_VAL = 100;

  27.         public BigInteger nextPrime(BigInteger start) {

  28.                 if (isEven(start))
  29.                         start = start.add(ONE);
  30.                 else
  31.                         start = start.add(TWO);
  32.                 if (start.isProbablePrime(ERR_VAL))
  33.                         return (start);
  34.                 else

  35.                         return (nextPrime(start));
  36.         }

  37.         private static boolean isEven(BigInteger n) {

  38.                 return (n.mod(TWO).equals(ZERO));
  39.         }

  40.         public BigInteger bigRandom(int numDigits) {

  41.                 StringBuffer s = new StringBuffer("");
  42.                 for (int i = 0; i < numDigits; i++)
  43.                         if (i == 0)
  44.                                 s.append(randomDigit(false));
  45.                         else
  46.                                 s.append(randomDigit(true));
  47.                 return (new BigInteger(s.toString()));
  48.         }

  49.         private StringBuffer randomDigit(boolean isZeroOK) {

  50.                 int index;
  51.                 if (isZeroOK)
  52.                         index = (int) Math.floor(Math.random() * 10);
  53.                 else
  54.                         index = 1 + (int) Math.floor(Math.random() * 9);
  55.                 return (digits[index]);
  56.         }

  57.         private static StringBuffer[] digits = { new StringBuffer("0"),
  58.                         new StringBuffer("1"), new StringBuffer("2"),
  59.                         new StringBuffer("3"), new StringBuffer("4"),
  60.                         new StringBuffer("5"), new StringBuffer("6"),
  61.                         new StringBuffer("7"), new StringBuffer("8"), new StringBuffer("9") };

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

  63.         }
  64. }
  65. package kl;

  66. import java.math.BigInteger;
  67. import java.util.ArrayList;
  68. import java.util.Scanner;

  69. public class RSA {
  70.         static BigInteger x, y;

  71.         public static boolean fun(BigInteger x, BigInteger y) {
  72.                 BigInteger t;
  73.                 while (!(y.equals(new BigInteger("0")))) {
  74.                         t = x;
  75.                         x = y;
  76.                         y = t.mod(y);
  77.                 }
  78.                 if (x.equals(new BigInteger("1")))
  79.                         return false;
  80.                 else
  81.                         return true;
  82.         }

  83.         public static BigInteger siyue(BigInteger a, BigInteger b) {
  84.                 BigInteger temp;
  85.                 if (b.equals(new BigInteger("0"))) {
  86.                         x = new BigInteger("1");
  87.                         y = new BigInteger("0");
  88.                         return x;
  89.                 }
  90.                 siyue(b, a.mod(b));
  91.                 temp = x;
  92.                 x = y;
  93.                 y = temp.subtract((a.divide(b).multiply(y)));
  94.                 return x;
  95.         }

  96.         public static double entropy(String mess) {
  97.                 ArrayList<Node> jieguo = new ArrayList<Node>();
  98.                 jieguo.clear();
  99.                 double num = mess.length();
  100.                 for (int i = 0; i < num; i++) {
  101.                         boolean flag_exit = true;
  102.                         for (int j = 0; j < jieguo.size(); j++) {
  103.                                 if (jieguo.get(j).getalpha() == mess.charAt(i)) {
  104.                                         flag_exit = false;
  105.                                         jieguo.get(j).setp(jieguo.get(j).getp() + 1 / num);
  106.                                 }
  107.                         }
  108.                         if (flag_exit)
  109.                                 jieguo.add(new Node(1 / num, mess.charAt(i)));
  110.                 }
  111.                 /** 计算熵 */
  112.                 double entropy = 0;
  113.                 for (int i = 0; i < jieguo.size(); i++) {
  114.                         double p1 = jieguo.get(i).getp();
  115.                         entropy += (-p1 * (Math.log(p1) / Math.log(2)));
  116.                 }
  117.                 return entropy;
  118.         }

  119.         public static void main(String[] args) {
  120.                 BigInteger p = null;
  121.                 BigInteger e, d, n, t, c;
  122.                 BigInteger q = null;
  123.                 for (int i = 0; i < 2; i++) {
  124.                         int numDigits;
  125.                         Prime pri = new Prime();
  126.                         try {
  127.                                 numDigits = Integer.parseInt("15");
  128.                         } catch (Exception e1) {
  129.                                 numDigits = 128;
  130.                         }
  131.                         BigInteger start = pri.bigRandom(numDigits);
  132.                         BigInteger big = pri.nextPrime(start);
  133.                         if (i == 0)
  134.                                 p = big;
  135.                         else
  136.                                 q = big;
  137.                 }
  138.                 Scanner input = new Scanner(System.in);
  139.                 n = p.multiply(q);
  140.                 t = p.subtract(new BigInteger("1")).multiply(
  141.                                 q.subtract(new BigInteger("1")));
  142.                 int numDigits;
  143.                 Prime pri = new Prime();
  144.                 try {
  145.                         numDigits = Integer.parseInt("15");
  146.                 } catch (Exception e1) {
  147.                         numDigits = 128;
  148.                 }
  149.                 BigInteger start = pri.bigRandom(numDigits);
  150.                 BigInteger big = pri.nextPrime(start);
  151.                 e = big;
  152.                 while (e.compareTo(t) == 1 || fun(e, t)) {
  153.                         System.out.println("e不合要求,请重新产生" + (e.compareTo(t) == 1)
  154.                                         + fun(e, t));
  155.                         pri = new Prime();
  156.                         try {
  157.                                 numDigits = Integer.parseInt("15");
  158.                         } catch (Exception e1) {
  159.                                 numDigits = 128;
  160.                         }
  161.                         start = pri.bigRandom(numDigits);
  162.                         big = pri.nextPrime(start);
  163.                         e = big;
  164.                 }
  165.                 // 求出私钥d
  166.                 d = siyue(e, t);
  167.                 System.out.println("由计算机随机产生2个素数p,q,分别如下:");
  168.                 System.out.println(p);
  169.                 System.out.println(q);
  170.                 System.out.println("计算得到的n:");
  171.                 System.out.println(n);
  172.                 System.out.println("计算得到的t:");
  173.                 System.out.println(t);
  174.                 System.out.println("随机产生的公钥:");
  175.                 System.out.println(e);
  176.                 System.out.println("由扩展的欧几里德算法求得私钥:");
  177.                 System.out.println(d);
  178.                 while (true) {
  179.                         System.out.println("请输入明文:");
  180.                         String mess = input.nextLine();
  181.                         //
  182.                         BigInteger m = new BigInteger(mess.getBytes());// 把字串转成一个BigInteger对象
  183.                         System.out.println("明文的大整数表示形式:" + m);
  184.                         /** 调用函数计算信息,统计信息 */
  185.                         mess = m.toString();
  186.                         System.out.println("计算得明文熵:" + entropy(mess));
  187.                         // 修改第一位,+1
  188.                         c = m.modPow(e, n);// JAVA中的方法
  189.                         System.out.println("密文的大整数表示形式:" + c);
  190.                         System.out.println("计算得密文熵:" + entropy(c.toString()));
  191.                         System.out.println("密文的字符串表示形式:" + new String(c.toByteArray()));
  192.                         m = c.modPow(d, n);
  193.                         System.out.println("解密得到明文的大整数表示形式:" + c);
  194.                         String str = new String(m.toByteArray());// 把返回的结果还原成一个字串
  195.                         System.out.println("解密得到明文为:" + str);
  196.                 }
  197.         }
  198. }
复制代码
RSA公开密钥密码体制是一种使用不同的加密秘钥与解密密钥,“由已知加密密钥推导出解密密钥在计算上是不可行的”密码体质。它通常是先生成一对RSA密钥,其中之一是保密密钥,有用户保存;另一个为公开密钥,可对外公开,甚至可在网络服务器中注册。

本帖子中包含更多资源

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

x
回复

使用道具 举报

11

主题

22

帖子

107

积分

注册会员

Rank: 2

积分
107
 楼主| 发表于 2022-5-6 14:38:51 | 显示全部楼层
mooc学习

本帖子中包含更多资源

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

x
回复

使用道具 举报

11

主题

22

帖子

107

积分

注册会员

Rank: 2

积分
107
 楼主| 发表于 2022-6-2 15:18:41 | 显示全部楼层
本帖最后由 戴隆俊 于 2022-6-2 15:20 编辑

  大数RSA算法的实现
一.原理:
1. 随意选择两个大的质数p和q,p不等于q,计算N = pq.
2. 根据欧拉函数,求得r=φ(N)=φ(p)φ(q)=(p-1)(q-1)。
3. 选择一个小于r的整数e,是e与r互质。并求得e关于r的模反元素,命名为d。(求d令ed≡1(mod r))。(模反元素存在,当且仅当e与r互质)
4. 将p和q的记录销毁。


本帖子中包含更多资源

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

x
回复

使用道具 举报

11

主题

22

帖子

107

积分

注册会员

Rank: 2

积分
107
 楼主| 发表于 2022-6-2 15:20:24 | 显示全部楼层
本帖最后由 戴隆俊 于 2022-6-3 12:51 编辑
  1. package kl;

  2. import java.math.BigInteger;

  3. public class Prime {
  4.         public boolean sushu(double n) {
  5.                 // 判断素数
  6.                 boolean isPrime = true;
  7.                 // 如果n大于2 继续判断 否则 isPrime的值不变 2素数
  8.                 if (n > 2) {
  9.                         // 如果n是大于2的偶数 认定不是素数 修改变量值为false
  10.                         if (n % 2 == 0) {

  11.                                 isPrime = false;
  12.                         } else {
  13.                                 for (int i = 3; i <= (int) Math.sqrt(n); i += 2) {
  14.                                         if (n % i == 0) {
  15.                                                 isPrime = false;
  16.                                                 break;
  17.                                         }
  18.                                 }
  19.                         }

  20.                 }
  21.                 return isPrime;
  22.         }

  23.         private static final BigInteger ZERO = BigInteger.ZERO;
  24.         private static final BigInteger ONE = BigInteger.ONE;
  25.         private static final BigInteger TWO = new BigInteger("2");
  26.         private static final int ERR_VAL = 100;

  27.         public BigInteger nextPrime(BigInteger start) {

  28.                 if (isEven(start))
  29.                         start = start.add(ONE);
  30.                 else
  31.                         start = start.add(TWO);
  32.                 if (start.isProbablePrime(ERR_VAL))
  33.                         return (start);
  34.                 else

  35.                         return (nextPrime(start));
  36.         }

  37.         private static boolean isEven(BigInteger n) {

  38.                 return (n.mod(TWO).equals(ZERO));
  39.         }

  40.         public BigInteger bigRandom(int numDigits) {

  41.                 StringBuffer s = new StringBuffer("");
  42.                 for (int i = 0; i < numDigits; i++)
  43.                         if (i == 0)
  44.                                 s.append(randomDigit(false));
  45.                         else
  46.                                 s.append(randomDigit(true));
  47.                 return (new BigInteger(s.toString()));
  48.         }

  49.         private StringBuffer randomDigit(boolean isZeroOK) {

  50.                 int index;
  51.                 if (isZeroOK)
  52.                         index = (int) Math.floor(Math.random() * 10);
  53.                 else
  54.                         index = 1 + (int) Math.floor(Math.random() * 9);
  55.                 return (digits[index]);
  56.         }

  57.         private static StringBuffer[] digits = { new StringBuffer("0"),
  58.                         new StringBuffer("1"), new StringBuffer("2"),
  59.                         new StringBuffer("3"), new StringBuffer("4"),
  60.                         new StringBuffer("5"), new StringBuffer("6"),
  61.                         new StringBuffer("7"), new StringBuffer("8"), new StringBuffer("9") };

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

  63.         }
  64. }
  65. package kl;

  66. import java.math.BigInteger;
  67. import java.util.ArrayList;
  68. import java.util.Scanner;

  69. public class RSA {
  70.         static BigInteger x, y;

  71.         public static boolean fun(BigInteger x, BigInteger y) {
  72.                 BigInteger t;
  73.                 while (!(y.equals(new BigInteger("0")))) {
  74.                         t = x;
  75.                         x = y;
  76.                         y = t.mod(y);
  77.                 }
  78.                 if (x.equals(new BigInteger("1")))
  79.                         return false;
  80.                 else
  81.                         return true;
  82.         }

  83.         public static BigInteger siyue(BigInteger a, BigInteger b) {
  84.                 BigInteger temp;
  85.                 if (b.equals(new BigInteger("0"))) {
  86.                         x = new BigInteger("1");
  87.                         y = new BigInteger("0");
  88.                         return x;
  89.                 }
  90.                 siyue(b, a.mod(b));
  91.                 temp = x;
  92.                 x = y;
  93.                 y = temp.subtract((a.divide(b).multiply(y)));
  94.                 return x;
  95.         }

  96.         public static double entropy(String mess) {
  97.                 ArrayList<Node> jieguo = new ArrayList<Node>();
  98.                 jieguo.clear();
  99.                 double num = mess.length();
  100.                 for (int i = 0; i < num; i++) {
  101.                         boolean flag_exit = true;
  102.                         for (int j = 0; j < jieguo.size(); j++) {
  103.                                 if (jieguo.get(j).getalpha() == mess.charAt(i)) {
  104.                                         flag_exit = false;
  105.                                         jieguo.get(j).setp(jieguo.get(j).getp() + 1 / num);
  106.                                 }
  107.                         }
  108.                         if (flag_exit)
  109.                                 jieguo.add(new Node(1 / num, mess.charAt(i)));
  110.                 }
  111.                 /** 计算熵 */
  112.                 double entropy = 0;
  113.                 for (int i = 0; i < jieguo.size(); i++) {
  114.                         double p1 = jieguo.get(i).getp();
  115.                         entropy += (-p1 * (Math.log(p1) / Math.log(2)));
  116.                 }
  117.                 return entropy;
  118.         }

  119.         public static void main(String[] args) {
  120.                 BigInteger p = null;
  121.                 BigInteger e, d, n, t, c;
  122.                 BigInteger q = null;
  123.                 for (int i = 0; i < 2; i++) {
  124.                         int numDigits;
  125.                         Prime pri = new Prime();
  126.                         try {
  127.                                 numDigits = Integer.parseInt("15");
  128.                         } catch (Exception e1) {
  129.                                 numDigits = 128;
  130.                         }
  131.                         BigInteger start = pri.bigRandom(numDigits);
  132.                         BigInteger big = pri.nextPrime(start);
  133.                         if (i == 0)
  134.                                 p = big;
  135.                         else
  136.                                 q = big;
  137.                 }
  138.                 Scanner input = new Scanner(System.in);
  139.                 n = p.multiply(q);
  140.                 t = p.subtract(new BigInteger("1")).multiply(
  141.                                 q.subtract(new BigInteger("1")));
  142.                 int numDigits;
  143.                 Prime pri = new Prime();
  144.                 try {
  145.                         numDigits = Integer.parseInt("15");
  146.                 } catch (Exception e1) {
  147.                         numDigits = 128;
  148.                 }
  149.                 BigInteger start = pri.bigRandom(numDigits);
  150.                 BigInteger big = pri.nextPrime(start);
  151.                 e = big;
  152.                 while (e.compareTo(t) == 1 || fun(e, t)) {
  153.                         System.out.println("e不合要求,请重新产生" + (e.compareTo(t) == 1)
  154.                                         + fun(e, t));
  155.                         pri = new Prime();
  156.                         try {
  157.                                 numDigits = Integer.parseInt("15");
  158.                         } catch (Exception e1) {
  159.                                 numDigits = 128;
  160.                         }
  161.                         start = pri.bigRandom(numDigits);
  162.                         big = pri.nextPrime(start);
  163.                         e = big;
  164.                 }
  165.                 // 求出私钥d
  166.                 d = siyue(e, t);
  167.                 System.out.println("由计算机随机产生2个素数p,q,分别如下:");
  168.                 System.out.println(p);
  169.                 System.out.println(q);
  170.                 System.out.println("计算得到的n:");
  171.                 System.out.println(n);
  172.                 System.out.println("计算得到的t:");
  173.                 System.out.println(t);
  174.                 System.out.println("随机产生的公钥:");
  175.                 System.out.println(e);
  176.                 System.out.println("由扩展的欧几里德算法求得私钥:");
  177.                 System.out.println(d);
  178.                 while (true) {
  179.                         System.out.println("请输入明文:");
  180.                         String mess = input.nextLine();
  181.                         //
  182.                         BigInteger m = new BigInteger(mess.getBytes());// 把字串转成一个BigInteger对象
  183.                         System.out.println("明文的大整数表示形式:" + m);
  184.                         /** 调用函数计算信息,统计信息 */
  185.                         mess = m.toString();
  186.                         System.out.println("计算得明文熵:" + entropy(mess));
  187.                         // 修改第一位,+1
  188.                         c = m.modPow(e, n);// JAVA中的方法
  189.                         System.out.println("密文的大整数表示形式:" + c);
  190.                         System.out.println("计算得密文熵:" + entropy(c.toString()));
  191.                         System.out.println("密文的字符串表示形式:" + new String(c.toByteArray()));
  192.                         m = c.modPow(d, n);
  193.                         System.out.println("解密得到明文的大整数表示形式:" + c);
  194.                         String str = new String(m.toByteArray());// 把返回的结果还原成一个字串
  195.                         System.out.println("解密得到明文为:" + str);
  196.                 }
  197.         }
  198. }
复制代码


本帖子中包含更多资源

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

x
回复

使用道具 举报

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

本版积分规则

教学服务系统

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

Powered by Discuz! X3.4

Copyright © 2001-2020, Tencent Cloud.

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