教学服务系统

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

信息计算2019级2班31号吴明霞

[复制链接]

8

主题

20

帖子

152

积分

注册会员

Rank: 2

积分
152
发表于 2022-4-19 15:19:41 | 显示全部楼层 |阅读模式
本帖最后由 吴明霞 于 2022-4-19 15:24 编辑

2022.4.19观看截图:

本帖子中包含更多资源

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

x
回复

使用道具 举报

8

主题

20

帖子

152

积分

注册会员

Rank: 2

积分
152
 楼主| 发表于 2022-4-22 17:24:50 | 显示全部楼层
2022.4.22网课观看截图:

本帖子中包含更多资源

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

x
回复

使用道具 举报

8

主题

20

帖子

152

积分

注册会员

Rank: 2

积分
152
 楼主| 发表于 2022-4-29 20:11:15 | 显示全部楼层
2022.4.29观看截图:

本帖子中包含更多资源

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

x
回复

使用道具 举报

8

主题

20

帖子

152

积分

注册会员

Rank: 2

积分
152
 楼主| 发表于 2022-5-3 11:44:44 | 显示全部楼层
2022.5.3观看截图:

本帖子中包含更多资源

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

x
回复

使用道具 举报

8

主题

20

帖子

152

积分

注册会员

Rank: 2

积分
152
 楼主| 发表于 2022-5-3 14:59:03 | 显示全部楼层
本帖最后由 吴明霞 于 2022-5-13 15:21 编辑

RSA算法

一、RSA算法的简介
  1977年,三位数学家Rivest、Shamir 和 Adleman 设计了一种算法,可以实现非对称加密。这种算法用他们三个人的名字命名,叫做RSA算法。从那时直到现在,RSA算法一直是最广为使用的"非对称加密算法"。非对称加密需要两个密钥来进行加密和解密,这两个秘钥是公开密钥(简称公钥)和私有密钥(简称私钥)。利用公钥对信息进新加密,私钥进行解密。RSA公开密钥密码体制的原理是:根据数论,寻求两个大素数比较简单,而将它们的乘积进行因式分解却极其困难,因此可以将乘积公开作为加密密钥。
二、RSA算法的原理
RSA密钥生成算法具体如下:
(1)随机选取两个素数,作为p和q;
(2)计算n=q*p,m=(q-1)*(p-1);
(3)随机选取e,使e与m互质;
(4)利用扩展欧几里得算法,计算d使d*e mod m=1;
(5)得到公钥(e,n)和私钥(d,n).
RSA加密算法具体如下:
(1)输入明文x(数字);
(2)利用模运算的性质,计算密文y=x^e mod n.
RSA解密算法具体如下:
(1)输入密文y(数字);
(2)利用模运算的性质,计算密文x=y^ mod n.
三、代码实现
Utils.java
  1. package rsa;
  2. import java.io.UnsupportedEncodingException;
  3. import java.math.BigInteger;
  4. import java.util.*;

  5. public class Utils {

  6.     public static List<Integer> setInformation(java.lang.String str) {
  7.         List<Integer> list = new ArrayList<Integer>();
  8.         try {
  9.             byte[] bytes = str.getBytes("utf-8");
  10.             //System.out.println(Arrays.toString(bytes));
  11.             for (byte byt : bytes) {
  12.                 list.add((int)(byt));
  13.             }
  14.         } catch (UnsupportedEncodingException e) {
  15.             e.printStackTrace();
  16.         }
  17.         return list;
  18.     }

  19.     /**
  20.      * 判断是否为素数
  21.      *
  22.      */
  23.     public static boolean isPrime(int n) {
  24.         boolean flag = true;
  25.         for (int i = 2; i < n; i++) {
  26.             if (n % i == 0) {
  27.                 flag = false;
  28.                 break;
  29.             }
  30.         }
  31.         return flag;
  32.     }

  33.     /**
  34.      * 随机选取两个素数
  35.      */
  36.     public static int[] getTwoPrime() {
  37.         int prime[] = new int[2];
  38.         int anInt;
  39.         int i = 0;
  40.         while (i < 2) {
  41.             anInt = new Random().nextInt(200);
  42.             if (isPrime(anInt) && anInt > 10) {
  43.                 prime[i] = anInt;
  44.                 i++;
  45.             }

  46.         }
  47.         return prime;
  48.     }

  49.     /**
  50.      * 获得N和M
  51.      */
  52.     public static HashMap<Character, Integer> get_N_And_M(int[] twoPrime) {
  53.         HashMap<Character, Integer> map = new HashMap<Character, Integer>();
  54.         int N = twoPrime[0] * twoPrime[1];
  55.         int M = (twoPrime[0] - 1) * (twoPrime[1] - 1);
  56.         System.out.println("素数1:" + twoPrime[0] + "\t\t素数2:" + twoPrime[1]);
  57.         System.out.println("M:" + M + "\t\tN:" + N);
  58.         map.put('N', N);
  59.         map.put('M', M);

  60.         return map;
  61.     }

  62.     /**
  63.      * 随机选取e使得和m互质
  64.      *
  65.      */
  66.     public static int mCoprime(int M) {
  67.         //生成一个介于1和M之间的质数,此质数一定和M互质
  68.         int E;
  69.         while (true) {
  70.             E = new Random().nextInt(200);
  71.             if (isPrime(E) && E > 0 && E < M){
  72.                 System.out.println("生成的E是\t:"+E);
  73.                 break;
  74.             }
  75.         }
  76.         return E;
  77.     }

  78.     /**
  79.      * 获得D
  80.      * @param M
  81.      * @param E
  82.      * @return
  83.      */
  84.     public static int getD(int M,int E){
  85.         int D=1;
  86.         while(true){
  87.             if((D*E)%M==1){
  88.                 break;
  89.             }
  90.             D++;
  91.         }
  92.         return D;
  93.     }


  94.     /**
  95.      * 加密
  96.      */
  97.     public static BigInteger encryption(int msg,int E,int N){
  98.         BigInteger bigInteger = new BigInteger(msg+"");
  99.         BigInteger encryptedText = bigInteger.modPow(new BigInteger(E+""), new BigInteger(N+""));
  100. //        System.out.println("密文:"+encryptedText);
  101.         return encryptedText;
  102.     }

  103.     /**
  104.      * 解密
  105.      */
  106.     public static BigInteger decrypt(BigInteger encryptedText,int D,int N){
  107.         BigInteger plainText = encryptedText.modPow(new BigInteger(D+""), new BigInteger(N+""));
  108.         return plainText;
  109.     }

  110.    
  111.     public static String getInformation(List<BigInteger> list){
  112.         String sd = "";
  113.         Iterator<BigInteger> iterator = list.iterator();
  114.         while(iterator.hasNext()){
  115.             BigInteger next = iterator.next();
  116.             Integer integer = new Integer(next + "");
  117.             char b = (char)integer.byteValue();
  118.             sd+=b;
  119.         }
  120.         return sd;
  121.     }
  122. }

复制代码

test.java
  1. package rsa;
  2. import java.math.BigInteger;
  3. import java.util.*;
  4. import static rsa.Utils.*;

  5. public class RSA {
  6.     public static void main(String[] args) {
  7.         System.out.println("----------获取密钥和公钥RSA算法核心----------");
  8.         int[] twoPrime = getTwoPrime();
  9.         HashMap<Character, Integer> n_and_m = get_N_And_M(twoPrime);
  10.         int N = n_and_m.get('N');
  11.         int M = n_and_m.get('M');
  12.         int E = mCoprime(M);

  13.         int D = getD(M,E);
  14.         System.out.println("生成的D是\t:"+D);
  15.         System.out.println("******************公钥**************");
  16.         System.out.println("公钥\t:("+E+","+N+")");
  17.         System.out.println("私钥\t:("+D+","+N+")");

  18.         System.out.println("-----------------输入明文-----------------");
  19.         System.out.print("请输入要加密的信息:");
  20.         String str = new Scanner(System.in).nextLine();
  21.         List<Integer> information = setInformation(str);

  22.         System.out.println("转码后的信息(char转byte):"+ information.toString());

  23.         System.out.println("-------------------加密-------------------");
  24.         List<BigInteger> encryptionList = new ArrayList<BigInteger>();
  25.         Iterator<Integer> iterator = information.iterator();
  26.         while(iterator.hasNext()){
  27.             encryptionList.add(encryption(iterator.next(), E, N));//加密装入集合
  28.         }
  29.         System.out.println("公钥加密后的信息(char字符数组对应的byte数组):"+encryptionList.toString());


  30.         System.out.println("-------------------解密-------------------");
  31.         List<BigInteger>  decryptList = new ArrayList<BigInteger>();
  32.         for (int i = 0; i < encryptionList.size(); i++) {
  33.             decryptList.add(decrypt(encryptionList.get(i),D,N));//解密装入集合
  34.         }
  35.         System.out.println("私钥解密后的信息(char字符数组对应的byte数组):"+decryptList.toString());

  36.         BigInteger encryption = encryption(1234, E, N);
  37.         decrypt(encryption,D,N);

  38.         System.out.println("-------------------解密后的信息-------------------");
  39.         String s = getInformation(decryptList);
  40.         System.out.println("解密后的明文:"+s);
  41.     }

  42. }
复制代码
四、运行结果

运行截图1:


运行截图2:



本帖子中包含更多资源

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

x
回复

使用道具 举报

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

本版积分规则

教学服务系统

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

Powered by Discuz! X3.4

Copyright © 2001-2020, Tencent Cloud.

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