教学服务系统

 找回密码
 立即注册
搜索
查看: 519|回复: 2

信息计算2019级2班8号张黎曦

[复制链接]

9

主题

19

帖子

89

积分

注册会员

Rank: 2

积分
89
发表于 2022-4-22 22:36:50 | 显示全部楼层 |阅读模式
mooc截图

本帖子中包含更多资源

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

x
回复

使用道具 举报

9

主题

19

帖子

89

积分

注册会员

Rank: 2

积分
89
 楼主| 发表于 2022-4-30 10:20:35 | 显示全部楼层
mooc截图

本帖子中包含更多资源

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

x
回复

使用道具 举报

9

主题

19

帖子

89

积分

注册会员

Rank: 2

积分
89
 楼主| 发表于 2022-5-3 09:36:24 | 显示全部楼层
import java.math.BigInteger;
import java.util.*;

public class rsa {
​//判断两数是否互素
   public static int getgcd(BigInteger n1,BigInteger n2)
   {
​    BigInteger q,r;
​    do
​    {
​​    q = n1.divide(n2);
​​    r = n1.subtract(q.multiply(n2));
​​    if(r.compareTo(BigInteger.ZERO)==0)
​​    {
​​    ​break;
​​    }
​​    else
​​    {
​​​    n1 = n2;
​​​    n2 = r;
​​    }
​    }while(r.compareTo(BigInteger.ZERO)!=0);
​   
​    if(n2.compareTo(BigInteger.ONE) == 0)
​​    return 1;
​    else
​​    return 0;
   }
   //求逆元
   public static BigInteger getInverse(BigInteger u,BigInteger n)
   {
   ​BigInteger q,r,n1 = n,n2 = u,t,b1=BigInteger.ZERO,b2=BigInteger.ONE;
   ​do
   ​{
   ​​q = n1.divide(n2);
   ​​r = n1.subtract(q.multiply(n2));
   ​​if(r.compareTo(BigInteger.ZERO)!=0)
   ​​{
   ​​​n1 = n2;
   ​​    n2 = r;
   ​​    t = b2;
   ​​    b2 = b1.subtract(q.multiply(b2));
   ​​    b1 = t;
   ​​}
   ​}while(r.compareTo(BigInteger.ZERO)!=0);
   ​if(n2.compareTo(BigInteger.ONE)==0)
   ​    return (b2.add(n)).mod(n);
   ​else
   ​​return BigInteger.ZERO;
   }
   //求高幂模
   public static BigInteger getgaomimou(BigInteger a,BigInteger b,BigInteger n)
   {
   ​BigInteger c = BigInteger.ONE;
   ​while(b.compareTo(BigInteger.ZERO)!=0)
   ​{
   ​​while((b.mod(new BigInteger("2"))).compareTo(BigInteger.ZERO)==0)
   ​​{
   ​​​b = b.divide(new BigInteger("2"));
   ​​​a = a.multiply(a).mod(n);
   ​​}
   ​​if((b.mod(new BigInteger("2"))).compareTo(BigInteger.ZERO)!=0)
   ​​{
   ​​​b = b.subtract(BigInteger.ONE);
   ​​​c= c.multiply(a).mod(n);
   ​​}
   ​}
   ​return c;
   }
   //加解密
​public static void main(String[] args) {
​​BigInteger e,d;
​​//生成两个大素数
​​BigInteger p = BigInteger.probablePrime(30, new Random());
​​BigInteger q = BigInteger.probablePrime(30, new Random());
​​BigInteger n = p.multiply(q);
​​System.out.println("n="+n);
​​BigInteger mn = (p.subtract(BigInteger.ONE))
​​​​.multiply(q.subtract(BigInteger.ONE));
​​//选取正整数e,作为公开的加密密钥
​​for(e = new BigInteger("2");e.compareTo(mn)<0;e = e.add(BigInteger.ONE))
​​{
​​​int t = getgcd(e,mn);
​​​if(t == 1)
​​​​break;
​​}
​​System.out.println("加密密钥e="+e);
​​//解密密钥
​​d = getInverse(e,mn);
​​System.out.print("请输入明文:");
​​Scanner in=new Scanner(System.in);
​​BigInteger ptext=in.nextBigInteger();
​​BigInteger ctext = getgaomimou(ptext,e,n);
​​System.out.println("加密结果为:"+ctext);
​​ptext = getgaomimou(ctext,d,n);
​​System.out.println("解密结果为:"+ptext);
​}​​
}


本帖子中包含更多资源

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

x
回复

使用道具 举报

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

本版积分规则

教学服务系统

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

Powered by Discuz! X3.4

Copyright © 2001-2020, Tencent Cloud.

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