|

楼主 |
发表于 2022-5-11 20:07:19
|
显示全部楼层
- package test;
- import java.math.BigInteger;
- public class Prime {
- public boolean sushu(double n) {
- // 判断素数
- boolean isPrime = true;
- if (n > 2) {
- if (n % 2 == 0) {
- isPrime = false;
- } else {
- for (int i = 3; i <= (int) Math.sqrt(n); i += 2) {
- if (n % i == 0) {
- isPrime = false;
- break;
- }
- }
- }
- }
- return isPrime;
- }
- private static final BigInteger ZERO = BigInteger.ZERO;
- private static final BigInteger ONE = BigInteger.ONE;
- private static final BigInteger TWO = new BigInteger("2");
- private static final int ERR_VAL = 100;
- public BigInteger nextPrime(BigInteger start) {
- // 产生一个比给定大整数 start 大的素数,错误率低于 1/2 的 ERR_VAL 次方
- if (isEven(start))
- start = start.add(ONE);
- else
- start = start.add(TWO);
- if (start.isProbablePrime(ERR_VAL)) //判断start有没有可能是素数
- return (start);
- else
- // 采用递归方式
- return (nextPrime(start));
- }
- private static boolean isEven(BigInteger n) {
- // 测试一个大整数是否为偶数
- return (n.mod(TWO).equals(ZERO));
- }
- public BigInteger bigRandom(int numDigits) {
- // 产生一个随机大整数,各位上的数字都是随机产生的,首位不为 0
- StringBuffer s = new StringBuffer("");
- for (int i = 0; i < numDigits; i++)
- if (i == 0)
- s.append(randomDigit(false));
- else
- s.append(randomDigit(true));
- return (new BigInteger(s.toString()));
- }
- private StringBuffer randomDigit(boolean isZeroOK) {
- // 产生一个随机的数字(字符串形式的),isZeroOK 决定这个数字是否可以为 0
- int index;
- if (isZeroOK)
- index = (int) Math.floor(Math.random() * 10);
- else
- index = 1 + (int) Math.floor(Math.random() * 9);
- return (digits[index]);
- }
- private static StringBuffer[] digits = { new StringBuffer("0"),
- new StringBuffer("1"), new StringBuffer("2"),
- new StringBuffer("3"), new StringBuffer("4"),
- new StringBuffer("5"), new StringBuffer("6"),
- new StringBuffer("7"), new StringBuffer("8"), new StringBuffer("9") };
- public static void main(String[] args) {
- }
- }
- ********************************************************
- ************************************************************************************
- *******************************************************************************************************
- package test;
- import java.math.BigInteger;
- import java.util.Scanner;
- public class test0511{
- static BigInteger x, y;
- public static boolean fun(BigInteger x, BigInteger y) {
- BigInteger t;
- while (!(y.equals(new BigInteger("0")))) {
- t = x;
- x = y;
- y = t.mod(y);
- }
- if (x.equals(new BigInteger("1")))
- return false;
- else
- return true;
- }
- public static BigInteger siyao(BigInteger a, BigInteger b) {
- BigInteger temp;
- if (b.equals(new BigInteger("0"))) {
- x = new BigInteger("1");
- y = new BigInteger("0");
- return x;
- }
- siyao(b, a.mod(b));
- temp = x;
- x = y;
- y = temp.subtract((a.divide(b).multiply(y)));
- return x;
- }
- public static void main(String[] args) {
- BigInteger p = null;
- BigInteger e, d, n, t, c;
- BigInteger q = null;
- for (int i = 0; i < 2; i++) {// 生成p,q
- int numDigits;
- Prime pri = new Prime();
- numDigits = Integer.parseInt("10");
- BigInteger start = pri.bigRandom(numDigits);// 生成一个十位的大数
- BigInteger big = pri.nextPrime(start);// 求出比start大的素数
- if (i == 0)
- p = big; // 把第一次生成的数赋值给p
- else
- q = big; // 把第二次生成的数赋值给q
- }
-
- Scanner sc = new Scanner(System.in); // 键盘输入明文
- n = p.multiply(q); // 求出φ(n)
- t = p.subtract(new BigInteger("1")).multiply( // 求出φ(n)=t=(p-1)*(q-1)
- q.subtract(new BigInteger("1")));
- int numDigits;
- Prime pri = new Prime();
- numDigits = Integer.parseInt("10");
- BigInteger start = pri.bigRandom(numDigits);
- BigInteger big = pri.nextPrime(start); // 找出一个质数e
- e = big;
- // 求出私钥d
- d = siyao(e, t);
- System.out.println("Produces 2 prime numbers");
- System.out.println("p:"+p);
- System.out.println("q:"+q);
- System.out.println("n=p*q:"+n);
- System.out.println("e:"+e);
- System.out.println("φ(n)=(p-1)*(q-1):"+t);
- System.out.println("public key:"+e);
- System.out.println("privil key:"+d);
- System.out.println("give message:");
- String mess = sc.nextLine();
- BigInteger m = new BigInteger(mess.getBytes());
- System.out.println("public Large integers:" + m);
- c = m.modPow(e, n);
- System.out.println("privil large intergars:" + c);
- m = c.modPow(d, n);
- System.out.println("Decryption yields large integers in plaintext:" + c);
- String str = new String(m.toByteArray());
- System.out.println("Decryption yields the plaintext :" + str);
- }
- }
复制代码
|
本帖子中包含更多资源
您需要 登录 才可以下载或查看,没有帐号?立即注册
x
|