教学服务系统

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

信息计算2019级2班11号谢春晖

[复制链接]

9

主题

22

帖子

111

积分

注册会员

Rank: 2

积分
111
发表于 2022-4-22 15:07:50 | 显示全部楼层 |阅读模式
4月19日和4月22日慕课学习截图:

本帖子中包含更多资源

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

x
回复

使用道具 举报

9

主题

22

帖子

111

积分

注册会员

Rank: 2

积分
111
 楼主| 发表于 2022-4-29 21:02:40 | 显示全部楼层
4月29日慕课截图:

本帖子中包含更多资源

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

x
回复

使用道具 举报

9

主题

22

帖子

111

积分

注册会员

Rank: 2

积分
111
 楼主| 发表于 2022-5-3 11:53:54 | 显示全部楼层
5月3日慕课截图:

本帖子中包含更多资源

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

x
回复

使用道具 举报

9

主题

22

帖子

111

积分

注册会员

Rank: 2

积分
111
 楼主| 发表于 2022-5-3 11:55:33 | 显示全部楼层
作业:
  1. import java.security.*;
  2. import java.security.interfaces.*;
  3. import java.io.*;
  4. import java.math.*;
  5. import java.util.Scanner;
  6. public class rsa{
  7. public rsa() {
  8. }
  9. public static void generateKey() {
  10. try {
  11. KeyPairGenerator kpg = KeyPairGenerator.getInstance("RSA");
  12. kpg.initialize(1024);
  13. KeyPair kp = kpg.genKeyPair();
  14. PublicKey pbkey = kp.getPublic();
  15. PrivateKey prkey = kp.getPrivate();
  16. // 保存公钥
  17. FileOutputStream f1 = new FileOutputStream("pubkey.dat");
  18. ObjectOutputStream b1 = new ObjectOutputStream(f1);
  19. b1.writeObject(pbkey);
  20. // 保存私钥
  21. FileOutputStream f2 = new FileOutputStream("privatekey.dat");
  22. ObjectOutputStream b2 = new ObjectOutputStream(f2);
  23. b2.writeObject(prkey);
  24. } catch (Exception e) {
  25. }}
  26. public static void encrypt() throws Exception {
  27. Scanner scan = new Scanner(System.in);
  28. System.out.println("请输入一个字符串:");
  29. //System.out.println("您输入的字符串是:" + scan.next());
  30. String s=scan.next();
  31. System.out.println("你输入的是:"+s);
  32. // 获取公钥及参数e,n
  33. FileInputStream f = new FileInputStream("pubkey.dat");
  34. ObjectInputStream b = new ObjectInputStream(f);
  35. RSAPublicKey pbk = (RSAPublicKey) b.readObject();
  36. BigInteger e = pbk.getPublicExponent();
  37. BigInteger n = pbk.getModulus();
  38. System.out.println("e= " + e);
  39. System.out.println("n= " + n);
  40. // 获取明文m
  41. byte ptext[] = s.getBytes("UTF-8");
  42. BigInteger m = new BigInteger(ptext);
  43. // 计算密文c
  44. BigInteger c = m.modPow(e, n);
  45. System.out.println("c= " + c);
  46. // 保存密文
  47. String cs = c.toString();
  48. BufferedWriter out =
  49. new BufferedWriter(
  50. new OutputStreamWriter(new FileOutputStream("encrypt.dat")));
  51. out.write(cs, 0, cs.length());
  52. out.close(); }
  53. public static void decrypt() throws Exception { // 读取密文
  54. BufferedReader in =
  55. new BufferedReader(  new InputStreamReader(new FileInputStream("encrypt.dat")));
  56. String ctext = in.readLine();
  57. BigInteger c = new BigInteger(ctext);
  58. // 读取私钥
  59. FileInputStream f = new FileInputStream("privatekey.dat");
  60. ObjectInputStream b = new ObjectInputStream(f);
  61. RSAPrivateKey prk = (RSAPrivateKey) b.readObject();
  62. BigInteger d = prk.getPrivateExponent();
  63. // 获取私钥参数及解密
  64. BigInteger n = prk.getModulus();
  65. System.out.println("d= " + d);
  66. System.out.println("n= " + n);
  67. BigInteger m = c.modPow(d, n);
  68. // 显示解密结果
  69. System.out.println("m= " + m);
  70. byte[] mt = m.toByteArray();
  71. System.out.println("解密出来的文本是:");
  72. for (int i = 0; i < mt.length; i++) {
  73. System.out.print((char) mt[i]);
  74. }
  75. }
  76. public static void main(String args[]) {
  77. try {
  78. generateKey();
  79. encrypt();
  80. decrypt();
  81. } catch (Exception e) {
  82. System.out.println(e.toString());}
  83. }}
复制代码

本帖子中包含更多资源

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

x
回复

使用道具 举报

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

本版积分规则

教学服务系统

GMT+8, 2025-4-30 14:35 , Processed in 0.014680 second(s), 19 queries .

Powered by Discuz! X3.4

Copyright © 2001-2020, Tencent Cloud.

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