教学服务系统

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

信息计算2019级2班5号张欢

[复制链接]

8

主题

23

帖子

108

积分

注册会员

Rank: 2

积分
108
发表于 2022-4-19 11:45:12 | 显示全部楼层 |阅读模式
第五次作业提交


本帖子中包含更多资源

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

x
回复

使用道具 举报

8

主题

23

帖子

108

积分

注册会员

Rank: 2

积分
108
 楼主| 发表于 2022-4-22 15:29:04 | 显示全部楼层
本帖最后由 张欢 于 2022-4-22 15:30 编辑

4月22日  4.4-4.7



本帖子中包含更多资源

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

x
回复

使用道具 举报

8

主题

23

帖子

108

积分

注册会员

Rank: 2

积分
108
 楼主| 发表于 2022-4-29 19:41:47 | 显示全部楼层
本帖最后由 张欢 于 2022-4-29 19:42 编辑

4月29日  4.7-4.10

本帖子中包含更多资源

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

x
回复

使用道具 举报

8

主题

23

帖子

108

积分

注册会员

Rank: 2

积分
108
 楼主| 发表于 2022-5-2 23:19:30 | 显示全部楼层
本帖最后由 张欢 于 2022-5-8 18:38 编辑

大数版本的RSA
一.RSA算法简介
RSA公开密钥密码体制是一种使用不同的加密秘钥与解密密钥,“由已知加密密钥推导出解密密钥在计算上是不可行的”密码体质。它通常是先生成一对RSA密钥,其中之一是保密密钥,有用户保存;另一个为公开密钥,可对外公开,甚至可在网络服务器中注册。

二.RSA算法实现步骤


三.大数版本RSA算法代码
  1. import java.math.BigInteger;

  2. public class Prime {

  3.         private static final BigInteger ZERO = BigInteger.ZERO;
  4.         private static final BigInteger ONE = BigInteger.ONE;
  5.         private static final BigInteger TWO = new BigInteger("2");
  6.         private static final int ERR_VAL = 100;

  7.         public BigInteger nextPrime(BigInteger n) {
  8.                 if (isEven(n))
  9.                         n = n.add(ONE);//是偶数就+1
  10.                 else
  11.                         n = n.add(TWO);
  12.                 if (n.isProbablePrime(ERR_VAL)) // 判断n有没有可能是素数
  13.                         return (n);
  14.                 else
  15.                         // 采用递归方式
  16.                         return (nextPrime(n));
  17.         }

  18.         private static boolean isEven(BigInteger n) {
  19.                 // 判断一个大数是否为偶数
  20.                 return (n.mod(TWO).equals(ZERO));
  21.         }

  22.         public BigInteger bigRandom(int numDigits) {
  23.                 // 产生一个随机大整数,各位上的数字都是随机产生的
  24.                 StringBuffer s = new StringBuffer("");
  25.                 for (int i = 0; i < numDigits; i++)
  26.                         if (i == 0)
  27.                                 s.append(randomDigit(false));  //控制首位不为0,随机生成的数字在(1-9)之间
  28.                         else
  29.                                 s.append(randomDigit(true));  //其余位生成的数字在(0-9)之间
  30.                 return (new BigInteger(s.toString()));
  31.         }

  32.         private StringBuffer randomDigit(boolean isZeroOK) {
  33.                 // 产生一个随机的字符串形式的数字,isZeroOK 决定这个数字是否可以为 0
  34.                 int index;
  35.                 if (isZeroOK)  
  36.                         index = (int) Math.floor(Math.random() * 10);
  37.                 else
  38.                         index = 1 + (int) Math.floor(Math.random() * 9);
  39.                 return (digits[index]);
  40.         }

  41.         private static StringBuffer[] digits = { new StringBuffer("0"), new StringBuffer("1"), new StringBuffer("2"),
  42.                         new StringBuffer("3"), new StringBuffer("4"), new StringBuffer("5"), new StringBuffer("6"),
  43.                         new StringBuffer("7"), new StringBuffer("8"), new StringBuffer("9") };
  44. }
复制代码
  1. import java.math.BigInteger;
  2. import java.util.Scanner;

  3. public class RSA {
  4.         static BigInteger x, y;
  5.         public static BigInteger siyao(BigInteger a, BigInteger b) {//生成私钥
  6.                 BigInteger temp;
  7.                 if (b.equals(new BigInteger("0"))) {
  8.                         x = new BigInteger("1");
  9.                         y = new BigInteger("0");
  10.                         return x;
  11.                 }
  12.                 siyao(b, a.mod(b));
  13.                 temp = x;
  14.                 x = y;
  15.                 y = temp.subtract((a.divide(b).multiply(y)));
  16.                 return x;
  17.         }

  18.         public static void main(String[] args) {
  19.                 BigInteger p = null;
  20.                 BigInteger e, d, n, φ, c;
  21.                 BigInteger q = null;

  22.                 for (int i = 0; i < 2; i++) {// 生成p,q
  23.                         int numDigits;
  24.                         Prime pri = new Prime();
  25.                         numDigits = Integer.parseInt("10");
  26.                         BigInteger start = pri.bigRandom(numDigits);// 生成一个十位的大数
  27.                         BigInteger big = pri.nextPrime(start);// 开始找一个比start大的可能的素数
  28.                         if (i == 0)
  29.                                 p = big; // 把第一次生成的数赋值给p
  30.                         else
  31.                                 q = big; // 把第二次生成的数赋值给q
  32.                 }

  33.                 Scanner sc = new Scanner(System.in); // 键盘输入明文
  34.                 n = p.multiply(q); // 求出n=p*q
  35.                 φ = p.subtract(new BigInteger("1")).multiply(  // 求出φ(n)=(p-1)*(q-1)
  36.                                 q.subtract(new BigInteger("1")));

  37.                 int numDigits;
  38.                 Prime pri = new Prime();
  39.                 numDigits = Integer.parseInt("10");
  40.                 BigInteger start = pri.bigRandom(numDigits);
  41.                 BigInteger big = pri.nextPrime(start);
  42.                 e = big;// 找出一个质数e

  43.                 d = siyao(e, φ);// d*e mod φ(n)=1  求出私钥d

  44.                 System.out.print("产生2个素数p,q:");
  45.                 System.out.println(p+"  "+q);
  46.                 System.out.print("n=p*q:");
  47.                 System.out.println(n);
  48.                 System.out.print("φ(n)=(p-1)*(q-1):");
  49.                 System.out.println(φ);
  50.                 System.out.print("公钥:");
  51.                 System.out.println(e);
  52.                 System.out.print("私钥:");
  53.                 System.out.println(d);
  54.                 System.out.print("请输入明文:");
  55.                 String mess = sc.nextLine();
  56.                 BigInteger m = new BigInteger(mess.getBytes());
  57.                 System.out.println("明文的大整数:" + m);
  58.                 c = m.modPow(e, n);
  59.                 System.out.println("密文的大整数:" + c);
  60.                 m = c.modPow(d, n);
  61.                 System.out.println("解密得到明文的大整数:" + m);
  62.                 String str = new String(m.toByteArray());
  63.                 System.out.println("解密得到明文为:" + str);
  64.                 sc.close();
  65.         }

  66. }
复制代码

四.运行结果截图


本帖子中包含更多资源

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

x
回复

使用道具 举报

8

主题

23

帖子

108

积分

注册会员

Rank: 2

积分
108
 楼主| 发表于 2022-5-3 12:48:22 | 显示全部楼层
本帖最后由 张欢 于 2022-5-3 12:49 编辑

5月3号  4.8-4.11

本帖子中包含更多资源

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

x
回复

使用道具 举报

8

主题

23

帖子

108

积分

注册会员

Rank: 2

积分
108
 楼主| 发表于 2022-5-26 15:39:37 | 显示全部楼层
本帖最后由 张欢 于 2022-5-26 15:43 编辑

大数版本的RSA
(之前重新写的不使用BigInteger的大数版本的RSA算法实现,忘记上传了)
一.RSA算法简介
RSA公开密钥密码体制是一种使用不同的加密秘钥与解密密钥,“由已知加密密钥推导出解密密钥在计算上是不可行的”密码体质。它通常是先生成一对RSA密钥,其中之一是保密密钥,有用户保存;另一个为公开密钥,可对外公开,甚至可在网络服务器中注册。

二.RSA算法实现步骤


三.大数版本RSA算法代码
  1. package rsa;

  2. import java.util.Random;
  3. import java.util.Scanner;
  4. import java.math.BigInteger;
  5. import java.security.SecureRandom;

  6. public class Rsa1 {
  7.         private static final int ORDER = 100000;// 随机数的数量级
  8.         private static final int MIN = 10000; // 选择的随机数的最小值

  9.         public static long getPrime() { // 获取一个随机数,判断是否为素数,并用MillerRabin方法检验
  10.                 long x = 0;
  11.                 while (x % 2 == 0 || !MillerRabin(x, 5)) {
  12.                         x = getRandom();
  13.                 }
  14.                 return x;
  15.         }

  16.         public static long getRandom() {// 随机生成一个奇数
  17.                 long x = 3;
  18.                 Random rd = new Random();
  19.                 do {
  20.                         x = rd.nextInt(ORDER);
  21.                 } while (x < MIN || x % 2 == 0);
  22.                 return x;
  23.         }

  24.         public static boolean MillerRabin(long n, int t) {
  25.                 for (int i = 0; i < t; i++)
  26.                         if (!isPrime(n))
  27.                                 return false;
  28.                 return true;
  29.         }

  30.         public static boolean isPrime(long n) {
  31.                 long k, q;
  32.                 SecureRandom random = new SecureRandom();
  33.                 for (k = 0; (((n - 1) >> k) & 1) == 0; k++)
  34.                         ;
  35.                 q = (n - 1) >> k;
  36.                 long a = random.nextLong(n);
  37.                 if (squareMultiply(a, q, n) == 1)
  38.                         return true;
  39.                 for (int j = 0; j < k; j++)
  40.                         if (squareMultiply(a, (int) Math.pow(2, j) * q, n) == n - 1)
  41.                                 return true;
  42.                 return false;
  43.         }

  44.         public static long squareMultiply(long a, long b, long p) {
  45.                 long x = 1, y = a;
  46.                 long len = (long) Math.ceil((Math.log(b) / Math.log(2)));
  47.                 for (int i = 0; i < len; i++) {
  48.                         if (((b >> i) & 1) == 1) {
  49.                                 x = (x * y) % p;
  50.                         }
  51.                         y = (y * y) % p;
  52.                 }
  53.                 return x;
  54.         }

  55.         public static long powMod(long x, long y, long z) {//模幂运算

  56.                 if (y == 0)
  57.                         return 1 % z;

  58.                 long half = powMod(x, y >> 1, z);

  59.                 half = (half * half) % z;

  60.                 if ((y & 1) == 0) { // y是偶数

  61.                         return half;

  62.                 } else { // y是奇数

  63.                         return (half * (x % z)) % z;

  64.                 }

  65.         }

  66.         public static long Inverse(long a, long b) { // 模逆运算
  67.                 long[] m = { 1, 0, a };
  68.                 long[] n = { 0, 1, b };
  69.                 long[] temp = new long[3];
  70.                 long q = 0; // 初始化
  71.                 boolean flag = true;
  72.                 while (flag) {
  73.                         q = m[2] / n[2];
  74.                         for (int i = 0; i < 3; i++) {
  75.                                 temp[i] = m[i] - q * n[i];
  76.                                 m[i] = n[i];
  77.                                 n[i] = temp[i];
  78.                         }
  79.                         if (n[2] == 1) {
  80.                                 if (n[1] < 0) {
  81.                                         n[1] = n[1] + a;
  82.                                 }
  83.                                 return n[1];
  84.                         }
  85.                         if (n[2] == 0) {
  86.                                 flag = false;
  87.                         }
  88.                 }
  89.                 return 0;
  90.         }

  91.         public static void main(String[] args) {
  92.                 long p = getPrime(); // 生成两个素数
  93.                 long q = getPrime();
  94.                 while (p == q) {
  95.                         q = getPrime();
  96.                 }
  97.                 long n = p * q;
  98.                 long φ = (p - 1) * (q - 1);
  99.                 long e = getPrime();
  100.                 long d = Inverse(φ, e);
  101.                 System.out.println("p:"+p);
  102.                 System.out.println("q:"+q);
  103.                 System.out.println("n:"+n);
  104.                 System.out.println("φ:"+φ);
  105.                 System.out.println("e:"+e);
  106.                 System.out.println("d:"+d);

  107.                 System.out.print("请输入明文:");
  108.                 Scanner sc = new Scanner(System.in);
  109.                 long m=sc.nextLong();
  110.                 long c=powMod(m, e, n);
  111.                 System.out.println("密文:" + c);
  112.                 m = powMod(c, d, n);
  113.                 System.out.println("解密得到明文:" + m);
  114.                 sc.close();
  115.         }
  116. }
复制代码
四、运行结果

本帖子中包含更多资源

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

x
回复

使用道具 举报

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

本版积分规则

教学服务系统

GMT+8, 2025-9-20 02:53 , Processed in 0.016294 second(s), 19 queries .

Powered by Discuz! X3.4

Copyright © 2001-2020, Tencent Cloud.

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