教学服务系统

 找回密码
 立即注册
搜索
查看: 533|回复: 4

信息计算2019级1班8号林荟萃

[复制链接]

8

主题

20

帖子

92

积分

注册会员

Rank: 2

积分
92
发表于 2022-4-22 14:06:27 | 显示全部楼层 |阅读模式
4月19日慕课学习截图:

本帖子中包含更多资源

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

x
回复

使用道具 举报

8

主题

20

帖子

92

积分

注册会员

Rank: 2

积分
92
 楼主| 发表于 2022-4-29 21:32:46 | 显示全部楼层
4.22慕课截图:

本帖子中包含更多资源

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

x
回复

使用道具 举报

8

主题

20

帖子

92

积分

注册会员

Rank: 2

积分
92
 楼主| 发表于 2022-4-29 21:33:32 | 显示全部楼层
4.29慕课截图:

本帖子中包含更多资源

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

x
回复

使用道具 举报

8

主题

20

帖子

92

积分

注册会员

Rank: 2

积分
92
 楼主| 发表于 2022-5-3 11:22:05 | 显示全部楼层
本帖最后由 108林荟萃 于 2022-6-2 16:03 编辑

RSA算法实现

RSA算法常用于非对称加密,非对称加密流程如下:
  • (1)乙方生成两把密钥(公钥和私钥)。公钥是公开的,任何人都可以获得,私钥则是保密的。
  • (2)甲方获取乙方的公钥,然后用它对信息加密。
  • (3)乙方得到加密后的信息,用私钥解密。

1.Encryptors.java

  1. package RSAEncryptionAlgorithm;
  2. import java.io.UnsupportedEncodingException;
  3. import java.math.BigInteger;
  4. import java.util.Random;
  5. import java.util.Base64;
  6. import java.util.Scanner;

  7. public class Encryptors {
  8.     public static void main(String[] args) throws UnsupportedEncodingException {
  9.         //System.out.println(RSATolls.GetGcd(new BigInteger("168468468"), new BigInteger("489416814641")));;
  10.         //System.out.println(RSATolls.GetModInv(new BigInteger("168468468"), new BigInteger("489416814641")));
  11.         BigInteger p = BigInteger.probablePrime(256, new Random());
  12.         BigInteger q = BigInteger.probablePrime(256, new Random());
  13.         BigInteger n = p.multiply(q);
  14.         String nn = Base64.getEncoder().encodeToString(n.toByteArray());//编码之后的n
  15.         System.out.println("n:");
  16.         System.out.println(nn);
  17.         BigInteger fai_n = p.subtract(BigInteger.ONE).multiply(q.subtract(BigInteger.ONE));
  18.         BigInteger e = new BigInteger(fai_n.bitLength(), new Random());
  19.         //System.out.println(n);
  20.         //System.out.println(fai_n);
  21.         while (!RSATolls.GetGcd(e, fai_n).equals(BigInteger.ONE)) {
  22.             do {
  23.                 e = new BigInteger(fai_n.bitLength(), new Random());
  24.             } while (e.compareTo(fai_n) >= 1);
  25.         }//公钥
  26.         BigInteger d = RSATolls.GetModInv(e, fai_n);//私钥
  27.         String ee = Base64.getEncoder().encodeToString(e.toByteArray());//编码之后的公钥
  28.         System.out.println("公钥:");
  29.         System.out.println(ee);
  30.         String dd = Base64.getEncoder().encodeToString(d.toByteArray());//编码之后的密钥
  31.         System.out.println("私钥:");
  32.         System.out.println(dd);
  33.         //加密
  34.         System.out.println("加密:");
  35.         System.out.println("请输入需要加密的明文:");
  36.         Scanner scan = new Scanner(System.in);
  37.         String userInput = scan.nextLine();
  38.         byte[] plaintextByte = userInput.getBytes("UTF-8");
  39.         byte[] plaintextByteCode = Base64.getEncoder().encode(plaintextByte);
  40.         BigInteger plaintextBI = new BigInteger(plaintextByteCode);
  41.         //System.out.println(plaintextBI);
  42.         System.out.println("输入n:" + nn);
  43.         System.out.println("输入公钥e:" + ee);
  44.         n = new BigInteger(Base64.getDecoder().decode(nn.getBytes()));
  45.         e = new BigInteger(Base64.getDecoder().decode(ee.getBytes()));
  46.         BigInteger c = plaintextBI.modPow(e, n);
  47.         System.out.println("密文:");
  48.         System.out.println(Base64.getEncoder().encodeToString(c.toByteArray()));
  49.         //解密
  50.         System.out.println("解密:");
  51.         String cipher = Base64.getEncoder().encodeToString(c.toByteArray());
  52.         System.out.println("输入密文" + cipher);
  53.         c = new BigInteger(Base64.getDecoder().decode(cipher));
  54.         System.out.println("输入n:" + nn);
  55.         System.out.println("输入私钥d:" + dd);
  56.         n = new BigInteger(Base64.getDecoder().decode(nn.getBytes()));
  57.         d = new BigInteger(Base64.getDecoder().decode(dd.getBytes()));
  58.         BigInteger m = c.modPow(d, n);
  59.         System.out.println("得到明文:");
  60.         System.out.println(new String(Base64.getDecoder().decode(m.toByteArray()), "UTF-8"));
  61.     }
  62. }
复制代码

2.RSATolls.java

  1. package RSAEncryptionAlgorithm;

  2. import java.math.BigInteger;

  3. public class RSATolls {
  4.     //求最大公因数
  5.     public static BigInteger GetGcd(BigInteger n, BigInteger u) {
  6.         BigInteger n1, n2;
  7.         n1 = n;
  8.         n2 = u;
  9.         while (true) {
  10.             //q = BigInteger.DivRem(n1, n2, out r);
  11.             BigInteger[] c = n1.divideAndRemainder(n2);
  12.             if (!c[1].equals(BigInteger.ZERO)) {
  13.                 n1 = n2;
  14.                 n2 = c[1];
  15.             } else
  16.                 break;
  17.         }
  18.         return n2;
  19.     }

  20.     //求模逆
  21.     public static BigInteger GetModInv(BigInteger value, BigInteger modulus) {
  22.         BigInteger n1, n2, b1 = BigInteger.ZERO, b2 = BigInteger.ONE, t;
  23.         n1 = modulus;
  24.         n2 = value;
  25.         while (true) {
  26.             BigInteger[] c = n1.divideAndRemainder(n2);
  27.             if (!c[1].equals(BigInteger.ZERO)) {
  28.                 n1 = n2;
  29.                 n2 = c[1];
  30.                 t = b2;
  31.                 b2 = b1.subtract(c[0].multiply(b2));
  32.                 b1 = t;
  33.             } else if (!n2.equals(BigInteger.ONE)) {
  34.                 b2 = modulus.add(new BigInteger("-1"));
  35.                 break;
  36.             } else {
  37.                 break;
  38.             }
  39.         }
  40.         if (b2.equals(modulus.add(new BigInteger("-1")))) {
  41.             return new BigInteger("-1");
  42.         }
  43.         while (b2.compareTo(BigInteger.ZERO) == -1) {
  44.             b2 = b2.add(modulus);
  45.         }
  46.         return b2.remainder(modulus);
  47.     }
  48. }
复制代码

3.运行截图:


本帖子中包含更多资源

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

x
回复

使用道具 举报

8

主题

20

帖子

92

积分

注册会员

Rank: 2

积分
92
 楼主| 发表于 2022-5-3 11:24:37 | 显示全部楼层
5.3慕课视频学习截图

本帖子中包含更多资源

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

x
回复

使用道具 举报

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

本版积分规则

教学服务系统

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

Powered by Discuz! X3.4

Copyright © 2001-2020, Tencent Cloud.

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