教学服务系统

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

信息计算2019-1易称清

[复制链接]

9

主题

24

帖子

118

积分

注册会员

Rank: 2

积分
118
发表于 2022-4-22 23:24:04 | 显示全部楼层 |阅读模式
4月22号截图

本帖子中包含更多资源

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

x
回复

使用道具 举报

9

主题

24

帖子

118

积分

注册会员

Rank: 2

积分
118
 楼主| 发表于 2022-4-29 22:13:01 | 显示全部楼层
4月29号截图

本帖子中包含更多资源

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

x
回复

使用道具 举报

9

主题

24

帖子

118

积分

注册会员

Rank: 2

积分
118
 楼主| 发表于 2022-5-3 21:20:33 | 显示全部楼层
5月3号截图

本帖子中包含更多资源

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

x
回复

使用道具 举报

9

主题

24

帖子

118

积分

注册会员

Rank: 2

积分
118
 楼主| 发表于 2022-5-11 20:07:19 | 显示全部楼层
  1. package test;

  2. import java.math.BigInteger;
  3. public class Prime {
  4.         public boolean sushu(double n) {
  5.                 // 判断素数
  6.                 boolean isPrime = true;
  7.                 if (n > 2) {

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

  18.                 }
  19.                 return isPrime;
  20.         }

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

  25.         public BigInteger nextPrime(BigInteger start) {
  26.                 // 产生一个比给定大整数 start 大的素数,错误率低于 1/2 的 ERR_VAL 次方
  27.                 if (isEven(start))
  28.                         start = start.add(ONE);
  29.                 else
  30.                         start = start.add(TWO);
  31.                 if (start.isProbablePrime(ERR_VAL))  //判断start有没有可能是素数
  32.                         return (start);
  33.                 else
  34.                         // 采用递归方式
  35.                         return (nextPrime(start));
  36.         }

  37.         private static boolean isEven(BigInteger n) {
  38.                 // 测试一个大整数是否为偶数
  39.                 return (n.mod(TWO).equals(ZERO));
  40.         }

  41.         public BigInteger bigRandom(int numDigits) {
  42.                 // 产生一个随机大整数,各位上的数字都是随机产生的,首位不为 0
  43.                 StringBuffer s = new StringBuffer("");
  44.                 for (int i = 0; i < numDigits; i++)
  45.                         if (i == 0)
  46.                                 s.append(randomDigit(false));
  47.                         else
  48.                                 s.append(randomDigit(true));
  49.                 return (new BigInteger(s.toString()));
  50.         }

  51.         private StringBuffer randomDigit(boolean isZeroOK) {
  52.                 // 产生一个随机的数字(字符串形式的),isZeroOK 决定这个数字是否可以为 0
  53.                 int index;
  54.                 if (isZeroOK)
  55.                         index = (int) Math.floor(Math.random() * 10);
  56.                 else
  57.                         index = 1 + (int) Math.floor(Math.random() * 9);
  58.                 return (digits[index]);
  59.         }

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

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

  66.         }
  67. }

  68. ********************************************************
  69. ************************************************************************************
  70. *******************************************************************************************************
  71. package test;

  72. import java.math.BigInteger;
  73. import java.util.Scanner;

  74. public class test0511{
  75.         static BigInteger x, y;

  76.         public static boolean fun(BigInteger x, BigInteger y) {
  77.                 BigInteger t;
  78.                 while (!(y.equals(new BigInteger("0")))) {
  79.                         t = x;
  80.                         x = y;
  81.                         y = t.mod(y);
  82.                 }
  83.                 if (x.equals(new BigInteger("1")))
  84.                         return false;
  85.                 else
  86.                         return true;
  87.         }

  88.         public static BigInteger siyao(BigInteger a, BigInteger b) {
  89.                 BigInteger temp;
  90.                 if (b.equals(new BigInteger("0"))) {
  91.                         x = new BigInteger("1");
  92.                         y = new BigInteger("0");
  93.                         return x;
  94.                 }
  95.                 siyao(b, a.mod(b));
  96.                 temp = x;
  97.                 x = y;
  98.                 y = temp.subtract((a.divide(b).multiply(y)));
  99.                 return x;
  100.         }

  101.         public static void main(String[] args) {
  102.                 BigInteger p = null;
  103.                 BigInteger e, d, n, t, c;
  104.                 BigInteger q = null;
  105.                 for (int i = 0; i < 2; i++) {// 生成p,q
  106.                         int numDigits;
  107.                         Prime pri = new Prime();
  108.                         numDigits = Integer.parseInt("10");
  109.                         BigInteger start = pri.bigRandom(numDigits);// 生成一个十位的大数
  110.                         BigInteger big = pri.nextPrime(start);// 求出比start大的素数
  111.                         if (i == 0)
  112.                                 p = big; // 把第一次生成的数赋值给p
  113.                         else
  114.                                 q = big; // 把第二次生成的数赋值给q
  115.                 }
  116.                
  117.                 Scanner sc = new Scanner(System.in); // 键盘输入明文
  118.                 n = p.multiply(q); // 求出φ(n)
  119.                 t = p.subtract(new BigInteger("1")).multiply( // 求出φ(n)=t=(p-1)*(q-1)
  120.                                 q.subtract(new BigInteger("1")));
  121.                 int numDigits;
  122.                 Prime pri = new Prime();
  123.                 numDigits = Integer.parseInt("10");
  124.                 BigInteger start = pri.bigRandom(numDigits);
  125.                 BigInteger big = pri.nextPrime(start); // 找出一个质数e
  126.                 e = big;
  127.                 // 求出私钥d
  128.                 d = siyao(e, t);
  129.                 System.out.println("Produces 2 prime numbers");
  130.                 System.out.println("p:"+p);
  131.                 System.out.println("q:"+q);
  132.                 System.out.println("n=p*q:"+n);
  133.                 System.out.println("e:"+e);
  134.                 System.out.println("φ(n)=(p-1)*(q-1):"+t);
  135.                 System.out.println("public key:"+e);
  136.                 System.out.println("privil key:"+d);
  137.                 System.out.println("give message:");
  138.                 String mess = sc.nextLine();
  139.                 BigInteger m = new BigInteger(mess.getBytes());
  140.                 System.out.println("public Large integers:" + m);
  141.                 c = m.modPow(e, n);
  142.                 System.out.println("privil large intergars:" + c);
  143.                 m = c.modPow(d, n);
  144.                 System.out.println("Decryption yields large integers in plaintext:" + c);
  145.                 String str = new String(m.toByteArray());
  146.                 System.out.println("Decryption yields the plaintext :" + str);
  147.         }

  148. }
复制代码

本帖子中包含更多资源

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

x
回复

使用道具 举报

9

主题

24

帖子

118

积分

注册会员

Rank: 2

积分
118
 楼主| 发表于 2022-5-20 14:48:19 | 显示全部楼层
大数RSA




  1. package RSA2022;
  2. import java.math.BigInteger;
  3. import java.util.ArrayList;
  4. import java.util.Scanner;

  5. public class RSA {
  6.         static BigInteger x, y;
  7.          
  8.         public static boolean fun(BigInteger x, BigInteger y) {
  9.                 BigInteger t;
  10.                 while (!(y.equals(new BigInteger("0")))) {
  11.                         t = x;
  12.                         x = y;
  13.                         y = t.mod(y);
  14.                 }
  15.                 if (x.equals(new BigInteger("1")))
  16.                         return false;
  17.                 else
  18.                         return true;
  19.         }

  20.         public static BigInteger siyue(BigInteger a, BigInteger b) {
  21.                 BigInteger temp;
  22.                 if (b.equals(new BigInteger("0"))) {
  23.                         x = new BigInteger("1");
  24.                         y = new BigInteger("0");
  25.                         return x;
  26.                 }
  27.                 siyue(b, a.mod(b));
  28.                 temp = x;
  29.                 x = y;
  30.                 y = temp.subtract((a.divide(b).multiply(y)));
  31.                 return x;
  32.         }

  33.         public static double entropy(String mess) {
  34.                 ArrayList<Node> jieguo = new ArrayList<Node>();
  35.                 jieguo.clear();
  36.                 double num = mess.length();
  37.                 for (int i = 0; i < num; i++) {
  38.                         boolean flag_exit = true;
  39.                         for (int j = 0; j < jieguo.size(); j++) {
  40.                                 if (jieguo.get(j).getalpha() == mess.charAt(i)) {
  41.                                         flag_exit = false;
  42.                                         jieguo.get(j).setp(jieguo.get(j).getp() + 1 / num);
  43.                                 }
  44.                         }
  45.                         if (flag_exit)
  46.                                 jieguo.add(new Node(1 / num, mess.charAt(i)));
  47.                 }
  48.                 /** 计算熵 */
  49.                 double entropy = 0;
  50.                 for (int i = 0; i < jieguo.size(); i++) {
  51.                         double p1 = jieguo.get(i).getp();
  52.                         entropy += (-p1 * (Math.log(p1) / Math.log(2)));
  53.                 }
  54.                 return entropy;
  55.         }

  56.         public static void main(String[] args) {
  57.                 BigInteger p = null;
  58.                 BigInteger e, d, n, t, c;
  59.                 BigInteger q = null;
  60.                 for (int i = 0; i < 2; i++) {
  61.                         int numDigits;
  62.                         Prime pri = new Prime();
  63.                         try {
  64.                                 numDigits = Integer.parseInt("15");
  65.                         } catch (Exception e1) {
  66.                                 numDigits = 128;
  67.                         }
  68.                         BigInteger start = pri.bigRandom(numDigits);
  69.                         BigInteger big = pri.nextPrime(start);
  70.                         if (i == 0)
  71.                                 p = big;
  72.                         else
  73.                                 q = big;
  74.                 }
  75.                 Scanner input = new Scanner(System.in);
  76.                 n = p.multiply(q);
  77.                 t = p.subtract(new BigInteger("1")).multiply(
  78.                                 q.subtract(new BigInteger("1")));
  79.                 int numDigits;
  80.                 Prime pri = new Prime();
  81.                 try {
  82.                         numDigits = Integer.parseInt("15");
  83.                 } catch (Exception e1) {
  84.                         numDigits = 128;
  85.                 }
  86.                 BigInteger start = pri.bigRandom(numDigits);
  87.                 BigInteger big = pri.nextPrime(start);
  88.                 e = big;
  89.                 while (e.compareTo(t) == 1 || fun(e, t)) {
  90.                         System.out.println("e不合要求,请重新产生" + (e.compareTo(t) == 1)
  91.                                         + fun(e, t));
  92.                         pri = new Prime();
  93.                         try {
  94.                                 numDigits = Integer.parseInt("15");
  95.                         } catch (Exception e1) {
  96.                                 numDigits = 128;
  97.                         }
  98.                         start = pri.bigRandom(numDigits);
  99.                         big = pri.nextPrime(start);
  100.                         e = big;
  101.                 }
  102.                 // 求出私钥d
  103.                 d = siyue(e, t);
  104.                 System.out.println("由计算机随机产生2个素数p,q,分别如下:");
  105.                 System.out.println(p);
  106.                 System.out.println(q);
  107.                 System.out.println("计算得到的n:");
  108.                 System.out.println(n);
  109.                 System.out.println("计算得到的t:");
  110.                 System.out.println(t);
  111.                 System.out.println("随机产生的公钥:");
  112.                 System.out.println(e);
  113.                 System.out.println("由扩展的欧几里德算法求得私钥:");
  114.                 System.out.println(d);
  115.                 while (true) {
  116.                         System.out.println("请输入明文:");
  117.                         String mess = input.nextLine();
  118.                         //
  119.                         BigInteger m = new BigInteger(mess.getBytes());// 把字串转成一个BigInteger对象
  120.                         System.out.println("明文的大整数表示形式:" + m);
  121.                         /** 调用函数计算信息,统计信息 */
  122.                         mess = m.toString();
  123.                 //        
  124.                         // 修改第一位,+1
  125.                         c = m.modPow(e, n);// JAVA中的方法
  126.                         System.out.println("密文的大整数表示形式:" + c);
  127.                 //        
  128.                         System.out.println("密文的字符串表示形式:" + new String(c.toByteArray()));
  129.                         m = c.modPow(d, n);
  130.                         System.out.println("解密得到明文的大整数表示形式:" + c);
  131.                         String str = new String(m.toByteArray());// 把返回的结果还原成一个字串
  132.                         System.out.println("解密得到明文为:" + str);
  133.                 }
  134.         }

  135. }
复制代码

本帖子中包含更多资源

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

x
回复

使用道具 举报

9

主题

24

帖子

118

积分

注册会员

Rank: 2

积分
118
 楼主| 发表于 2022-6-3 14:30:28 | 显示全部楼层
建立基于椭圆曲线的加密机制,需要找到类似RSA质因子分解或其他求离散对数这样的难题。而椭圆曲线上的已知G和xG求x,是非常困难的,此即为椭圆曲线上的的离散对数问题。此处x即为私钥,xG即为公钥。

椭圆曲线加密算法原理如下:

设私钥、公钥分别为k、K,即K = kG,其中G为G点。

公钥加密:  选择随机数r,将消息M生成密文C,该密文是一个点对,即:  C = {rG, M+rK},其中K为公钥

私钥解密:  M + rK - k(rG) = M + r(kG) - k(rG) = M  其中k、K分别为私钥、公钥。
  1. //曲线椭圆加密算法
  2. public static void ec() throws InvalidKeyException, SignatureException, NoSuchAlgorithmException {
  3.     //Create a key pair
  4.     KeyPairGenerator keyGen = KeyPairGenerator.getInstance("EC");
  5.     SecureRandom random = SecureRandom.getInstance("SHA1PRNG");
  6.     keyGen.initialize(256, random);

  7.     KeyPair keypair = keyGen.genKeyPair();
  8.     PrivateKey privateKey = keypair.getPrivate();
  9.     PublicKey publicKey = keypair.getPublic();

  10.     System.out.println("private key: " + Arrays.toString(privateKey.getEncoded()));
  11.     System.out.println("public key: " + Arrays.toString(publicKey.getEncoded()));



  12.     String input = "hello world";

  13.     //签名算法
  14.     Signature signer = Signature.getInstance("SHA256withECDSA");

  15.     //用私钥初始化签名
  16.     signer.initSign(privateKey);
  17.     //用签名更新输入
  18.     signer.update(input.getBytes());
  19.     byte[] sign = signer.sign();

  20.     //打印得到的签名, 可以将此签名传给第三方
  21.     System.out.println("input is:  " + input);
  22.     System.out.println("Sign: " + Arrays.toString(sign));


  23.     //第三方用公钥初始化,用签名做验证, 返回true表示验证正确
  24.     Signature verifier = Signature.getInstance("SHA256withECDSA");
  25.     verifier.initVerify(publicKey);

  26.     //第三方的数据abc,用签名来验证是否和数据一致
  27.     String input_again = "hello world";
  28.     verifier.update(input_again.getBytes());
  29.     boolean checkResult = verifier.verify(sign);
  30.     System.out.println("Sign result : " + checkResult);
  31. }
复制代码
回复

使用道具 举报

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

本版积分规则

教学服务系统

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

Powered by Discuz! X3.4

Copyright © 2001-2020, Tencent Cloud.

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