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

时间:2014-11-17 17:18:30

标签: java rsa

要求是:

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

  2. 两个素数的差异应大于2^512(为安全起见)

  3. 问题: 我想知道的是,我指定了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");
        }
    }
    

1 个答案:

答案 0 :(得分:1)

很难随机生成两个小于2 512 的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;
}