gpt4 book ai didi

java - 使用 Java 内置库的简单 RSA 加密

转载 作者:行者123 更新时间:2023-11-30 11:12:16 25 4
gpt4 key购买 nike

要求是:

  1. 质数 p 和 q 应至少为 1024 位

  2. 两个素数之差应该大于2^512(安全起见)

问题:我想知道的是我指定了pq的位长,并且还有 SecureRandom 实例来随机生成它们,但我被告知差异可能不会大于 2^512。那么我如何指定差异,使其大于 2^512?然后我想我将不再能够随机生成 pq

Here是 BigInteger 类构造函数的文档,它表明如果我想手动指定它,我必须使用 bye 数组 byte[],如果我这样做,则无法随机生成它。

任何提示都会很棒。

谢谢。

这是我遇到问题的方法:

public RSA(int bits) {
bitlen = bits;
SecureRandom random = new SecureRandom();
BigInteger p = new BigInteger(bitlen, 100, random);
BigInteger q = new BigInteger(bitlen, 100, random);
n = p.multiply(q);
BigInteger m = (p.subtract(BigInteger.ONE)).multiply(q
.subtract(BigInteger.ONE));
e = new BigInteger(Integer.toString(eValue));
while (m.gcd(e).intValue() > 1) {
e = e.add(new BigInteger("2"));
}
d = e.modInverse(m);
}

完整的源代码如下:

import java.math.BigInteger;
import java.security.SecureRandom;

public class RSA {

public static double runningTime;
private BigInteger n, d, e;
private int bitlen = 1024;
static int eValue = 65537;

/** Create an instance that can encrypt using someone provided public key. */
public RSA(BigInteger newn, BigInteger newe) {
n = newn;
e = newe;
}

/** Create an instance that can both encrypt and decrypt. */
public RSA(int bits) {
bitlen = bits;
SecureRandom random = new SecureRandom();
BigInteger p = new BigInteger(bitlen, 100, random);
BigInteger q = new BigInteger(bitlen, 100, random);
n = p.multiply(q);
BigInteger m = (p.subtract(BigInteger.ONE)).multiply(q
.subtract(BigInteger.ONE));
e = new BigInteger(Integer.toString(eValue));
while (m.gcd(e).intValue() > 1) {
e = e.add(new BigInteger("2"));
}
d = e.modInverse(m);
}

/** Encrypt the given plain-text message. */
public String encrypt(String message) {
return (new BigInteger(message.getBytes())).modPow(e, n).toString();
}

/** Encrypt the given plain-text message. */
public BigInteger encrypt(BigInteger message) {
return message.modPow(e, n);
}

/** Decrypt the given cipher-text message. */
public String decrypt(String message) {
return new String((new BigInteger(message)).modPow(d, n).toByteArray());
}

/** Decrypt the given cipher-text message. */
public BigInteger decrypt(BigInteger message) {
return message.modPow(d, n);
}

/** Generate a new public and private key set. */
public void generateKeys() {
SecureRandom random = new SecureRandom();
BigInteger p = new BigInteger(bitlen, 100, random);
BigInteger q = new BigInteger(bitlen, 100, random);
n = p.multiply(q);
BigInteger m = (p.subtract(BigInteger.ONE)).multiply(q
.subtract(BigInteger.ONE));
e = new BigInteger(Integer.toString(eValue));
while (m.gcd(e).intValue() > 1) {
e = e.add(new BigInteger("2"));
}
d = e.modInverse(m);
}

/** Return the modulus. */
public BigInteger getN() {
return n;
}

/** Return the public key. */
public BigInteger getE() {
return e;
}

/** Test program. */
public static void main(String[] args) {
runningTime = System.nanoTime();
RSA rsa = new RSA(1024);

String text1 = "RSA-Encryption Practice";
System.out.println("Plaintext: " + text1);
BigInteger plaintext = new BigInteger(text1.getBytes());

BigInteger ciphertext = rsa.encrypt(plaintext);
System.out.println("cipher-text: " + ciphertext);
plaintext = rsa.decrypt(ciphertext);

String text2 = new String(plaintext.toByteArray());
System.out.println("Plaintext: " + text2);
System.out.println("RunningTime: "
+ (runningTime = System.nanoTime() - runningTime) / 1000000
+ " ms");
}
}

最佳答案

很难随机生成两个相距小于 2512 的 1024 位素数。概率是<2-509(这是不可能的)。所以一般你不应该有问题。如果这种情况仍然发生,您可以在 checkDiff 在 while 循环中返回 false 时重试。这使它成为一种非常快速收敛的拉斯维加斯算法。

final static BigInteger targetDiff = new BigInteger("2").pow(512);

public boolean checkDiff(BigInteger p, BigInteger q){
BigInteger pDiff = p.subtract(q);
pDiff = pDiff.abs();

BigInteger diff = pDiff.subtract(targetDiff);

return diff.compareTo(BigInteger.ZERO) == 1;
}

关于java - 使用 Java 内置库的简单 RSA 加密,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/26978189/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com