使用Java';s内置图书馆
这些要求是:使用Java';s内置图书馆,java,rsa,Java,Rsa,这些要求是: 素数p和q应至少为1024位 两个素数的差值应大于2^512(为了安全起见) 问题:我想知道的是,我指定了p和q的位长度,还指定了SecureRandom实例来随机生成它们,但我被告知差异可能不大于2^512。那么,如何指定差值,使其大于2^512?然后我想我将不再能够随机生成p和q 是BigInteger类构造函数的文档,它显示如果我想手动指定它,我必须使用bye arraybyte[],如果我这样做,那么就没有办法随机生成它 任何暗示都很好 多谢各位 以下是我对该方法的异议:
2^512
(为了安全起见)p
和q
的位长度,还指定了SecureRandom
实例来随机生成它们,但我被告知差异可能不大于2^512
。那么,如何指定差值,使其大于2^512
?然后我想我将不再能够随机生成p
和q
是BigInteger类构造函数的文档,它显示如果我想手动指定它,我必须使用bye arraybyte[]
,如果我这样做,那么就没有办法随机生成它
任何暗示都很好
多谢各位
以下是我对该方法的异议:
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位素数是相当困难的。我可能是RSA的新手,但我会尽力帮忙。如果您生成了一个p值,那么q必须大于p+2^512才能得到2^512的差值。因此,如果您可以从SecureRandom类中查看,并且可以以某种方式使用诸如next()或nextBytes()之类的方法之一来指定生成的下一个随机数,这可能会帮助我回答您的问题,谢谢。但我认为q应该更大,p应该更小,所以q减去(p);不是p减去(q);注意
pDiff=pDiff.abs()代码>。如果您希望q
大于p
,则需要添加q.compareTo(p)
检查某处。
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;
}