教学服务系统

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

信息计算2019级2班9号范承蝶

[复制链接]

9

主题

20

帖子

127

积分

注册会员

Rank: 2

积分
127
发表于 2022-4-22 14:52:38 | 显示全部楼层 |阅读模式
本帖最后由 范承蝶 于 2022-4-22 14:54 编辑

2022年4月19日以及4月22日慕课截图:

本帖子中包含更多资源

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

x
回复

使用道具 举报

9

主题

20

帖子

127

积分

注册会员

Rank: 2

积分
127
 楼主| 发表于 2022-4-29 20:58:37 | 显示全部楼层
2022年4月29日慕课截图:

本帖子中包含更多资源

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

x
回复

使用道具 举报

9

主题

20

帖子

127

积分

注册会员

Rank: 2

积分
127
 楼主| 发表于 2022-5-3 10:56:52 | 显示全部楼层
2022年5月3日慕课截图:

本帖子中包含更多资源

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

x
回复

使用道具 举报

9

主题

20

帖子

127

积分

注册会员

Rank: 2

积分
127
 楼主| 发表于 2022-5-3 10:58:22 | 显示全部楼层
本帖最后由 范承蝶 于 2022-5-10 18:01 编辑
  1. import java.math.BigInteger;
  2. import java.util.Random;

  3. public class rsa {
  4.     private final static int numLength = 128;//素数长度
  5.     private final static int accuracy = 100;//素数的准确率为1-(2^(-accuracy))

  6.     //获取最大公约数
  7.     private BigInteger getG(BigInteger a, BigInteger b) {
  8.         if (b.byteValue() == 0)
  9.                 return a;
  10.         return getG(b, a.mod(b));
  11.     }

  12.     //扩展欧几里得方法,计算 ax + by = 1中的x与y的整数解(a与b互质)
  13.     private static BigInteger[] extGCD(BigInteger a, BigInteger b) {
  14.         if (b.signum() == 0) {
  15.             return new BigInteger[]{a, new BigInteger("1"), new BigInteger("0")};
  16.         } else {
  17.             BigInteger[] bigIntegers = extGCD(b, a.mod(b));
  18.             BigInteger y = bigIntegers[1].subtract(a.divide(b).multiply(bigIntegers[2]));
  19.             return new BigInteger[]{bigIntegers[0], bigIntegers[2], y};
  20.         }
  21.     }

  22.     //超大整数超大次幂然后对超大的整数取模
  23.     private static BigInteger expMode(BigInteger base, BigInteger exp, BigInteger mod) {
  24.         BigInteger res = BigInteger.ONE;
  25.         BigInteger tempBase = new BigInteger(base.toString());
  26.         for (int i = 0; i < exp.bitLength(); i++) {
  27.             if (exp.testBit(i)) {
  28.                     //判断对应二进制位是否为1
  29.                 res = (res.multiply(tempBase)).mod(mod);
  30.             }
  31.             tempBase = tempBase.multiply(tempBase).mod(mod);
  32.         }
  33.         return res;
  34.     }

  35.     //产生公钥与私钥
  36.     public static SecretKey generateKey(BigInteger p, BigInteger q) {
  37.         //令n = p * q。取 φ(n) = (p-1) * (q-1)。
  38.         BigInteger n = p.multiply(q);
  39.         //计算与n互质的整数个数 欧拉函数
  40.         BigInteger fy = p.subtract(BigInteger.ONE).multiply(q.subtract(BigInteger.ONE));
  41.         //取 e ∈ [1 < e < φ(n) ] ,( n , e )作为公钥对,这里取65537
  42.         BigInteger e = new BigInteger("65537");
  43.         BigInteger[] bigIntegers = extGCD(e, fy);
  44.         BigInteger x = bigIntegers[1];
  45.         if (x.signum() == -1) {
  46.             x = x.add(fy);
  47.         }
  48.         return new SecretKey(n, e, x);
  49.     }

  50.     public static SecretKey generateKey() {
  51.         BigInteger[] pq = getRandomPQ();
  52.         return generateKey(pq[0], pq[1]);
  53.     }

  54.     //加密
  55.     public static BigInteger encrypt(BigInteger text, SecretKey.PublicKey publicKey) {
  56.         return expMode(text, publicKey.e, publicKey.n);
  57.     }

  58.     //解密
  59.     public static BigInteger decrypt(BigInteger cipher, SecretKey.PrivateKey privateKey) {
  60.         return expMode(cipher, privateKey.d, privateKey.n);
  61.     }

  62.     //加密
  63.     public static String encrypt(String text, SecretKey.PublicKey publicKey) {
  64.         return encrypt(new BigInteger(text.getBytes()), publicKey).toString();
  65.     }

  66.     //解密
  67.     public static String decrypt(String chipper, SecretKey.PrivateKey privateKey) {
  68.         BigInteger bigInteger = expMode(new BigInteger(chipper), privateKey.d, privateKey.n);
  69.         byte[] bytes = new byte[bigInteger.bitLength() / 8 + 1];
  70.         for (int i = 0; i < bytes.length; i++) {
  71.             for (int j = 0; j < 8; j++) {
  72.                 if (bigInteger.testBit(j + i * 8)) {
  73.                     bytes[bytes.length - 1 - i] |= 1 << j;
  74.                 }
  75.             }
  76.         }
  77.         return new String(bytes);
  78.     }

  79.     //产生两个随机128位大质数
  80.     public static BigInteger[] getRandomPQ() {
  81.         BigInteger p = BigInteger.probablePrime(numLength, new Random());
  82.         while (!p.isProbablePrime(accuracy)) {
  83.             p = BigInteger.probablePrime(numLength, new Random());
  84.         }
  85.         BigInteger q = BigInteger.probablePrime(numLength, new Random());
  86.         while (!q.isProbablePrime(accuracy)) {
  87.             q = BigInteger.probablePrime(numLength, new Random());
  88.         }
  89.         return new BigInteger[]{p, q};
  90.     }

  91.     //密匙对
  92.     static class SecretKey {
  93.         BigInteger n, e, d;

  94.         public SecretKey(BigInteger n, BigInteger e, BigInteger d) {
  95.             this.n = n;
  96.             this.e = e;
  97.             this.d = d;
  98.         }

  99.         public PublicKey getPublicKey() {
  100.             return new PublicKey(n, e);
  101.         }

  102.         public PrivateKey getPrivateKey() {
  103.             return new PrivateKey(n, d);
  104.         }

  105.         //密钥
  106.         static class PrivateKey {
  107.             public BigInteger n, d;

  108.             public PrivateKey(BigInteger n, BigInteger d) {
  109.                 this.n = n;
  110.                 this.d = d;
  111.             }
  112.         }

  113.         //公钥
  114.         static class PublicKey {
  115.             public BigInteger n, e;

  116.             public PublicKey(BigInteger n, BigInteger e) {
  117.                 this.n = n;
  118.                 this.e = e;
  119.             }
  120.         }
  121.     }


  122.     public static void main(String[] args) {
  123.         SecretKey secretKey = rsa.generateKey();
  124.         //明文内容不要超过128位,超过后需要分段加密
  125.         String text = "qwertyuiop";
  126.         String chipper = rsa.encrypt(text, secretKey.getPublicKey());

  127.       //密文长度可能会随着随机密钥的改变而改变,最长不超过256位
  128.         System.out.println("加密后:\n" +
  129.                 "密文二进制长度:" + new BigInteger(chipper).bitLength()
  130.                 + "\n"
  131.                 + chipper);
  132.         String origin = rsa.decrypt(chipper, secretKey.getPrivateKey());
  133.         System.out.println("解密后:\n" + origin);
  134.     }
  135. }

复制代码



本帖子中包含更多资源

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

x
回复

使用道具 举报

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

本版积分规则

教学服务系统

GMT+8, 2025-4-30 11:06 , Processed in 0.016848 second(s), 19 queries .

Powered by Discuz! X3.4

Copyright © 2001-2020, Tencent Cloud.

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