java.security.interfaces.RSAPrivateCrtKey#getPrimeExponentQ ( )源码实例Demo

下面列出了java.security.interfaces.RSAPrivateCrtKey#getPrimeExponentQ ( ) 实例代码,或者点击链接到github查看源代码,也可以在右侧发表评论。

源代码1 项目: jna-gmp   文件: ModPowVectors.java
private static void generateTestVector(int rsaKeyBits, int suffix) throws Exception {
  KeyPair pair = generateKeyPair(rsaKeyBits);
  RSAPrivateCrtKey priv = (RSAPrivateCrtKey) pair.getPrivate();

  // The core RSA private key operation is doing the modPow for the two components.
  BigInteger p = priv.getPrimeP();
  BigInteger dp = priv.getPrimeExponentP();
  BigInteger q = priv.getPrimeQ();
  BigInteger dq = priv.getPrimeExponentQ();

  byte[] random = new byte[rsaKeyBits / 8];
  SECURE_RANDOM.nextBytes(random);
  // Clear the top bit to ensure it fits.
  random[0] &= 0x7F;
  BigInteger message = new BigInteger(1, random);
  BigInteger pResult = message.modPow(dp, p);
  BigInteger qResult = message.modPow(dq, q);

  System.out.println("public static final TestVector VECTOR" + suffix + " = ");
  new TestVector(message, p, dp, pResult, q, dq, qResult).printJavaConstructorFor();
  System.out.println();
}
 
源代码2 项目: cute-proxy   文件: KeyStoreGenerator.java
private RSAPrivateCrtKeyParameters getPrivateKeyParameters(RSAPrivateCrtKey privateCrtKey) {
    return new RSAPrivateCrtKeyParameters(privateCrtKey.getModulus(),
            privateCrtKey.getPublicExponent(),
            privateCrtKey.getPrivateExponent(),
            privateCrtKey.getPrimeP(), privateCrtKey.getPrimeQ(), privateCrtKey.getPrimeExponentP(),
            privateCrtKey.getPrimeExponentQ(),
            privateCrtKey.getCrtCoefficient());
}
 
源代码3 项目: RipplePower   文件: JCERSAPrivateCrtKey.java
/**
 * construct a private key from another RSAPrivateCrtKey.
 *
 * @param key the object implementing the RSAPrivateCrtKey interface.
 */
JCERSAPrivateCrtKey(
    RSAPrivateCrtKey key)
{
    this.modulus = key.getModulus();
    this.publicExponent = key.getPublicExponent();
    this.privateExponent = key.getPrivateExponent();
    this.primeP = key.getPrimeP();
    this.primeQ = key.getPrimeQ();
    this.primeExponentP = key.getPrimeExponentP();
    this.primeExponentQ = key.getPrimeExponentQ();
    this.crtCoefficient = key.getCrtCoefficient();
}
 
源代码4 项目: RipplePower   文件: BCRSAPrivateCrtKey.java
/**
 * construct a private key from another RSAPrivateCrtKey.
 *
 * @param key the object implementing the RSAPrivateCrtKey interface.
 */
BCRSAPrivateCrtKey(
    RSAPrivateCrtKey key)
{
    this.modulus = key.getModulus();
    this.publicExponent = key.getPublicExponent();
    this.privateExponent = key.getPrivateExponent();
    this.primeP = key.getPrimeP();
    this.primeQ = key.getPrimeQ();
    this.primeExponentP = key.getPrimeExponentP();
    this.primeExponentQ = key.getPrimeExponentQ();
    this.crtCoefficient = key.getCrtCoefficient();
}
 
源代码5 项目: xipki   文件: SignerUtil.java
public static RSAKeyParameters generateRSAPrivateKeyParameter(RSAPrivateKey key) {
  Args.notNull(key, "key");
  if (key instanceof RSAPrivateCrtKey) {
    RSAPrivateCrtKey rsaKey = (RSAPrivateCrtKey) key;

    return new RSAPrivateCrtKeyParameters(rsaKey.getModulus(), rsaKey.getPublicExponent(),
        rsaKey.getPrivateExponent(), rsaKey.getPrimeP(), rsaKey.getPrimeQ(),
        rsaKey.getPrimeExponentP(), rsaKey.getPrimeExponentQ(), rsaKey.getCrtCoefficient());
  } else {
    return new RSAKeyParameters(true, key.getModulus(), key.getPrivateExponent());
  }
}
 
源代码6 项目: ripple-lib-java   文件: JCERSAPrivateCrtKey.java
/**
 * construct a private key from another RSAPrivateCrtKey.
 *
 * @param key the object implementing the RSAPrivateCrtKey interface.
 */
JCERSAPrivateCrtKey(
    RSAPrivateCrtKey key)
{
    this.modulus = key.getModulus();
    this.publicExponent = key.getPublicExponent();
    this.privateExponent = key.getPrivateExponent();
    this.primeP = key.getPrimeP();
    this.primeQ = key.getPrimeQ();
    this.primeExponentP = key.getPrimeExponentP();
    this.primeExponentQ = key.getPrimeExponentQ();
    this.crtCoefficient = key.getCrtCoefficient();
}
 
源代码7 项目: ripple-lib-java   文件: BCRSAPrivateCrtKey.java
/**
 * construct a private key from another RSAPrivateCrtKey.
 *
 * @param key the object implementing the RSAPrivateCrtKey interface.
 */
BCRSAPrivateCrtKey(
    RSAPrivateCrtKey key)
{
    this.modulus = key.getModulus();
    this.publicExponent = key.getPublicExponent();
    this.privateExponent = key.getPrivateExponent();
    this.primeP = key.getPrimeP();
    this.primeQ = key.getPrimeQ();
    this.primeExponentP = key.getPrimeExponentP();
    this.primeExponentQ = key.getPrimeExponentQ();
    this.crtCoefficient = key.getCrtCoefficient();
}
 
源代码8 项目: dragonwell8_jdk   文件: PrivateKeyEqualityTest.java
public static void main(String[] args) throws NoSuchAlgorithmException,
        NoSuchProviderException, InvalidKeySpecException {
    // Generate the first key.
    KeyPairGenerator generator
            = KeyPairGenerator.getInstance(KEYALG, PROVIDER_NAME);
    KeyPair keyPair = generator.generateKeyPair();
    RSAPrivateKey rsaPrivateKey = (RSAPrivateKey) keyPair.getPrivate();
    if (!(rsaPrivateKey instanceof RSAPrivateCrtKey)) {
        System.err.println("rsaPrivateKey class : " + rsaPrivateKey.getClass().getName());
        throw new RuntimeException("rsaPrivateKey is not a RSAPrivateCrtKey instance");
    }

    // Generate the second key.
    KeyFactory factory = KeyFactory.getInstance(KEYALG, PROVIDER_NAME);
    RSAPrivateKeySpec rsaPrivateKeySpec = new RSAPrivateKeySpec(
            rsaPrivateKey.getModulus(), rsaPrivateKey.getPrivateExponent());
    RSAPrivateKey rsaPrivateKey2 = (RSAPrivateKey) factory.generatePrivate(
            rsaPrivateKeySpec);

    // Generate the third key.
    PKCS8EncodedKeySpec encodedKeySpec = new PKCS8EncodedKeySpec(
            rsaPrivateKey.getEncoded());
    RSAPrivateKey rsaPrivateKey3 = (RSAPrivateKey) factory.generatePrivate(
            encodedKeySpec);

    // Check for equality.
    if (rsaPrivateKey.equals(rsaPrivateKey2)) {
        throw new RuntimeException("rsaPrivateKey should not equal to rsaPrivateKey2");
    }
    if (!rsaPrivateKey3.equals(rsaPrivateKey)) {
        throw new RuntimeException("rsaPrivateKey3 should equal to rsaPrivateKey");
    }
    if (rsaPrivateKey3.equals(rsaPrivateKey2)) {
        throw new RuntimeException("rsaPrivateKey3 should not equal to rsaPrivateKey2");
    }
    if (rsaPrivateKey2.equals(rsaPrivateKey3)) {
        throw new RuntimeException("rsaPrivateKey2 should not equal to rsaPrivateKey3");
    }

    // Generate the fourth key.
    RSAPrivateCrtKey rsaPrivateCrtKey =  (RSAPrivateCrtKey)rsaPrivateKey;
    RSAPrivateCrtKeySpec rsaPrivateCrtKeySpec = new RSAPrivateCrtKeySpec(
            rsaPrivateCrtKey.getModulus(),
            rsaPrivateCrtKey.getPublicExponent(),
            rsaPrivateCrtKey.getPrivateExponent(),
            rsaPrivateCrtKey.getPrimeP(),
            rsaPrivateCrtKey.getPrimeQ(),
            rsaPrivateCrtKey.getPrimeExponentP(),
            rsaPrivateCrtKey.getPrimeExponentQ(),
            rsaPrivateCrtKey.getCrtCoefficient()
        );
    RSAPrivateCrtKey rsaPrivateKey4 = (RSAPrivateCrtKey) factory.generatePrivate(
            rsaPrivateCrtKeySpec);
    if (!rsaPrivateKey.equals(rsaPrivateKey4)) {
        throw new RuntimeException("rsaPrivateKey should equal to rsaPrivateKey4");
    }
}
 
源代码9 项目: TencentKona-8   文件: PrivateKeyEqualityTest.java
public static void main(String[] args) throws NoSuchAlgorithmException,
        NoSuchProviderException, InvalidKeySpecException {
    // Generate the first key.
    KeyPairGenerator generator
            = KeyPairGenerator.getInstance(KEYALG, PROVIDER_NAME);
    KeyPair keyPair = generator.generateKeyPair();
    RSAPrivateKey rsaPrivateKey = (RSAPrivateKey) keyPair.getPrivate();
    if (!(rsaPrivateKey instanceof RSAPrivateCrtKey)) {
        System.err.println("rsaPrivateKey class : " + rsaPrivateKey.getClass().getName());
        throw new RuntimeException("rsaPrivateKey is not a RSAPrivateCrtKey instance");
    }

    // Generate the second key.
    KeyFactory factory = KeyFactory.getInstance(KEYALG, PROVIDER_NAME);
    RSAPrivateKeySpec rsaPrivateKeySpec = new RSAPrivateKeySpec(
            rsaPrivateKey.getModulus(), rsaPrivateKey.getPrivateExponent());
    RSAPrivateKey rsaPrivateKey2 = (RSAPrivateKey) factory.generatePrivate(
            rsaPrivateKeySpec);

    // Generate the third key.
    PKCS8EncodedKeySpec encodedKeySpec = new PKCS8EncodedKeySpec(
            rsaPrivateKey.getEncoded());
    RSAPrivateKey rsaPrivateKey3 = (RSAPrivateKey) factory.generatePrivate(
            encodedKeySpec);

    // Check for equality.
    if (rsaPrivateKey.equals(rsaPrivateKey2)) {
        throw new RuntimeException("rsaPrivateKey should not equal to rsaPrivateKey2");
    }
    if (!rsaPrivateKey3.equals(rsaPrivateKey)) {
        throw new RuntimeException("rsaPrivateKey3 should equal to rsaPrivateKey");
    }
    if (rsaPrivateKey3.equals(rsaPrivateKey2)) {
        throw new RuntimeException("rsaPrivateKey3 should not equal to rsaPrivateKey2");
    }
    if (rsaPrivateKey2.equals(rsaPrivateKey3)) {
        throw new RuntimeException("rsaPrivateKey2 should not equal to rsaPrivateKey3");
    }

    // Generate the fourth key.
    RSAPrivateCrtKey rsaPrivateCrtKey =  (RSAPrivateCrtKey)rsaPrivateKey;
    RSAPrivateCrtKeySpec rsaPrivateCrtKeySpec = new RSAPrivateCrtKeySpec(
            rsaPrivateCrtKey.getModulus(),
            rsaPrivateCrtKey.getPublicExponent(),
            rsaPrivateCrtKey.getPrivateExponent(),
            rsaPrivateCrtKey.getPrimeP(),
            rsaPrivateCrtKey.getPrimeQ(),
            rsaPrivateCrtKey.getPrimeExponentP(),
            rsaPrivateCrtKey.getPrimeExponentQ(),
            rsaPrivateCrtKey.getCrtCoefficient()
        );
    RSAPrivateCrtKey rsaPrivateKey4 = (RSAPrivateCrtKey) factory.generatePrivate(
            rsaPrivateCrtKeySpec);
    if (!rsaPrivateKey.equals(rsaPrivateKey4)) {
        throw new RuntimeException("rsaPrivateKey should equal to rsaPrivateKey4");
    }
}
 
源代码10 项目: openjdk-jdk8u   文件: PrivateKeyEqualityTest.java
public static void main(String[] args) throws NoSuchAlgorithmException,
        NoSuchProviderException, InvalidKeySpecException {
    // Generate the first key.
    KeyPairGenerator generator
            = KeyPairGenerator.getInstance(KEYALG, PROVIDER_NAME);
    KeyPair keyPair = generator.generateKeyPair();
    RSAPrivateKey rsaPrivateKey = (RSAPrivateKey) keyPair.getPrivate();
    if (!(rsaPrivateKey instanceof RSAPrivateCrtKey)) {
        System.err.println("rsaPrivateKey class : " + rsaPrivateKey.getClass().getName());
        throw new RuntimeException("rsaPrivateKey is not a RSAPrivateCrtKey instance");
    }

    // Generate the second key.
    KeyFactory factory = KeyFactory.getInstance(KEYALG, PROVIDER_NAME);
    RSAPrivateKeySpec rsaPrivateKeySpec = new RSAPrivateKeySpec(
            rsaPrivateKey.getModulus(), rsaPrivateKey.getPrivateExponent());
    RSAPrivateKey rsaPrivateKey2 = (RSAPrivateKey) factory.generatePrivate(
            rsaPrivateKeySpec);

    // Generate the third key.
    PKCS8EncodedKeySpec encodedKeySpec = new PKCS8EncodedKeySpec(
            rsaPrivateKey.getEncoded());
    RSAPrivateKey rsaPrivateKey3 = (RSAPrivateKey) factory.generatePrivate(
            encodedKeySpec);

    // Check for equality.
    if (rsaPrivateKey.equals(rsaPrivateKey2)) {
        throw new RuntimeException("rsaPrivateKey should not equal to rsaPrivateKey2");
    }
    if (!rsaPrivateKey3.equals(rsaPrivateKey)) {
        throw new RuntimeException("rsaPrivateKey3 should equal to rsaPrivateKey");
    }
    if (rsaPrivateKey3.equals(rsaPrivateKey2)) {
        throw new RuntimeException("rsaPrivateKey3 should not equal to rsaPrivateKey2");
    }
    if (rsaPrivateKey2.equals(rsaPrivateKey3)) {
        throw new RuntimeException("rsaPrivateKey2 should not equal to rsaPrivateKey3");
    }

    // Generate the fourth key.
    RSAPrivateCrtKey rsaPrivateCrtKey =  (RSAPrivateCrtKey)rsaPrivateKey;
    RSAPrivateCrtKeySpec rsaPrivateCrtKeySpec = new RSAPrivateCrtKeySpec(
            rsaPrivateCrtKey.getModulus(),
            rsaPrivateCrtKey.getPublicExponent(),
            rsaPrivateCrtKey.getPrivateExponent(),
            rsaPrivateCrtKey.getPrimeP(),
            rsaPrivateCrtKey.getPrimeQ(),
            rsaPrivateCrtKey.getPrimeExponentP(),
            rsaPrivateCrtKey.getPrimeExponentQ(),
            rsaPrivateCrtKey.getCrtCoefficient()
        );
    RSAPrivateCrtKey rsaPrivateKey4 = (RSAPrivateCrtKey) factory.generatePrivate(
            rsaPrivateCrtKeySpec);
    if (!rsaPrivateKey.equals(rsaPrivateKey4)) {
        throw new RuntimeException("rsaPrivateKey should equal to rsaPrivateKey4");
    }
}
 
public static void main(String[] args) throws NoSuchAlgorithmException,
        NoSuchProviderException, InvalidKeySpecException {
    // Generate the first key.
    KeyPairGenerator generator
            = KeyPairGenerator.getInstance(KEYALG, PROVIDER_NAME);
    KeyPair keyPair = generator.generateKeyPair();
    RSAPrivateKey rsaPrivateKey = (RSAPrivateKey) keyPair.getPrivate();
    if (!(rsaPrivateKey instanceof RSAPrivateCrtKey)) {
        System.err.println("rsaPrivateKey class : " + rsaPrivateKey.getClass().getName());
        throw new RuntimeException("rsaPrivateKey is not a RSAPrivateCrtKey instance");
    }

    // Generate the second key.
    KeyFactory factory = KeyFactory.getInstance(KEYALG, PROVIDER_NAME);
    RSAPrivateKeySpec rsaPrivateKeySpec = new RSAPrivateKeySpec(
            rsaPrivateKey.getModulus(), rsaPrivateKey.getPrivateExponent());
    RSAPrivateKey rsaPrivateKey2 = (RSAPrivateKey) factory.generatePrivate(
            rsaPrivateKeySpec);

    // Generate the third key.
    PKCS8EncodedKeySpec encodedKeySpec = new PKCS8EncodedKeySpec(
            rsaPrivateKey.getEncoded());
    RSAPrivateKey rsaPrivateKey3 = (RSAPrivateKey) factory.generatePrivate(
            encodedKeySpec);

    // Check for equality.
    if (rsaPrivateKey.equals(rsaPrivateKey2)) {
        throw new RuntimeException("rsaPrivateKey should not equal to rsaPrivateKey2");
    }
    if (!rsaPrivateKey3.equals(rsaPrivateKey)) {
        throw new RuntimeException("rsaPrivateKey3 should equal to rsaPrivateKey");
    }
    if (rsaPrivateKey3.equals(rsaPrivateKey2)) {
        throw new RuntimeException("rsaPrivateKey3 should not equal to rsaPrivateKey2");
    }
    if (rsaPrivateKey2.equals(rsaPrivateKey3)) {
        throw new RuntimeException("rsaPrivateKey2 should not equal to rsaPrivateKey3");
    }

    // Generate the fourth key.
    RSAPrivateCrtKey rsaPrivateCrtKey =  (RSAPrivateCrtKey)rsaPrivateKey;
    RSAPrivateCrtKeySpec rsaPrivateCrtKeySpec = new RSAPrivateCrtKeySpec(
            rsaPrivateCrtKey.getModulus(),
            rsaPrivateCrtKey.getPublicExponent(),
            rsaPrivateCrtKey.getPrivateExponent(),
            rsaPrivateCrtKey.getPrimeP(),
            rsaPrivateCrtKey.getPrimeQ(),
            rsaPrivateCrtKey.getPrimeExponentP(),
            rsaPrivateCrtKey.getPrimeExponentQ(),
            rsaPrivateCrtKey.getCrtCoefficient()
        );
    RSAPrivateCrtKey rsaPrivateKey4 = (RSAPrivateCrtKey) factory.generatePrivate(
            rsaPrivateCrtKeySpec);
    if (!rsaPrivateKey.equals(rsaPrivateKey4)) {
        throw new RuntimeException("rsaPrivateKey should equal to rsaPrivateKey4");
    }
}
 
源代码12 项目: openjdk-jdk9   文件: PrivateKeyEqualityTest.java
public static void main(String[] args) throws NoSuchAlgorithmException,
        NoSuchProviderException, InvalidKeySpecException {
    // Generate the first key.
    KeyPairGenerator generator
            = KeyPairGenerator.getInstance(KEYALG, PROVIDER_NAME);
    KeyPair keyPair = generator.generateKeyPair();
    RSAPrivateKey rsaPrivateKey = (RSAPrivateKey) keyPair.getPrivate();
    if (!(rsaPrivateKey instanceof RSAPrivateCrtKey)) {
        System.err.println("rsaPrivateKey class : " + rsaPrivateKey.getClass().getName());
        throw new RuntimeException("rsaPrivateKey is not a RSAPrivateCrtKey instance");
    }

    // Generate the second key.
    KeyFactory factory = KeyFactory.getInstance(KEYALG, PROVIDER_NAME);
    RSAPrivateKeySpec rsaPrivateKeySpec = new RSAPrivateKeySpec(
            rsaPrivateKey.getModulus(), rsaPrivateKey.getPrivateExponent());
    RSAPrivateKey rsaPrivateKey2 = (RSAPrivateKey) factory.generatePrivate(
            rsaPrivateKeySpec);

    // Generate the third key.
    PKCS8EncodedKeySpec encodedKeySpec = new PKCS8EncodedKeySpec(
            rsaPrivateKey.getEncoded());
    RSAPrivateKey rsaPrivateKey3 = (RSAPrivateKey) factory.generatePrivate(
            encodedKeySpec);

    // Check for equality.
    if (rsaPrivateKey.equals(rsaPrivateKey2)) {
        throw new RuntimeException("rsaPrivateKey should not equal to rsaPrivateKey2");
    }
    if (!rsaPrivateKey3.equals(rsaPrivateKey)) {
        throw new RuntimeException("rsaPrivateKey3 should equal to rsaPrivateKey");
    }
    if (rsaPrivateKey3.equals(rsaPrivateKey2)) {
        throw new RuntimeException("rsaPrivateKey3 should not equal to rsaPrivateKey2");
    }
    if (rsaPrivateKey2.equals(rsaPrivateKey3)) {
        throw new RuntimeException("rsaPrivateKey2 should not equal to rsaPrivateKey3");
    }

    // Generate the fourth key.
    RSAPrivateCrtKey rsaPrivateCrtKey =  (RSAPrivateCrtKey)rsaPrivateKey;
    RSAPrivateCrtKeySpec rsaPrivateCrtKeySpec = new RSAPrivateCrtKeySpec(
            rsaPrivateCrtKey.getModulus(),
            rsaPrivateCrtKey.getPublicExponent(),
            rsaPrivateCrtKey.getPrivateExponent(),
            rsaPrivateCrtKey.getPrimeP(),
            rsaPrivateCrtKey.getPrimeQ(),
            rsaPrivateCrtKey.getPrimeExponentP(),
            rsaPrivateCrtKey.getPrimeExponentQ(),
            rsaPrivateCrtKey.getCrtCoefficient()
        );
    RSAPrivateCrtKey rsaPrivateKey4 = (RSAPrivateCrtKey) factory.generatePrivate(
            rsaPrivateCrtKeySpec);
    if (!rsaPrivateKey.equals(rsaPrivateKey4)) {
        throw new RuntimeException("rsaPrivateKey should equal to rsaPrivateKey4");
    }
}
 
源代码13 项目: jdk8u-jdk   文件: PrivateKeyEqualityTest.java
public static void main(String[] args) throws NoSuchAlgorithmException,
        NoSuchProviderException, InvalidKeySpecException {
    // Generate the first key.
    KeyPairGenerator generator
            = KeyPairGenerator.getInstance(KEYALG, PROVIDER_NAME);
    KeyPair keyPair = generator.generateKeyPair();
    RSAPrivateKey rsaPrivateKey = (RSAPrivateKey) keyPair.getPrivate();
    if (!(rsaPrivateKey instanceof RSAPrivateCrtKey)) {
        System.err.println("rsaPrivateKey class : " + rsaPrivateKey.getClass().getName());
        throw new RuntimeException("rsaPrivateKey is not a RSAPrivateCrtKey instance");
    }

    // Generate the second key.
    KeyFactory factory = KeyFactory.getInstance(KEYALG, PROVIDER_NAME);
    RSAPrivateKeySpec rsaPrivateKeySpec = new RSAPrivateKeySpec(
            rsaPrivateKey.getModulus(), rsaPrivateKey.getPrivateExponent());
    RSAPrivateKey rsaPrivateKey2 = (RSAPrivateKey) factory.generatePrivate(
            rsaPrivateKeySpec);

    // Generate the third key.
    PKCS8EncodedKeySpec encodedKeySpec = new PKCS8EncodedKeySpec(
            rsaPrivateKey.getEncoded());
    RSAPrivateKey rsaPrivateKey3 = (RSAPrivateKey) factory.generatePrivate(
            encodedKeySpec);

    // Check for equality.
    if (rsaPrivateKey.equals(rsaPrivateKey2)) {
        throw new RuntimeException("rsaPrivateKey should not equal to rsaPrivateKey2");
    }
    if (!rsaPrivateKey3.equals(rsaPrivateKey)) {
        throw new RuntimeException("rsaPrivateKey3 should equal to rsaPrivateKey");
    }
    if (rsaPrivateKey3.equals(rsaPrivateKey2)) {
        throw new RuntimeException("rsaPrivateKey3 should not equal to rsaPrivateKey2");
    }
    if (rsaPrivateKey2.equals(rsaPrivateKey3)) {
        throw new RuntimeException("rsaPrivateKey2 should not equal to rsaPrivateKey3");
    }

    // Generate the fourth key.
    RSAPrivateCrtKey rsaPrivateCrtKey =  (RSAPrivateCrtKey)rsaPrivateKey;
    RSAPrivateCrtKeySpec rsaPrivateCrtKeySpec = new RSAPrivateCrtKeySpec(
            rsaPrivateCrtKey.getModulus(),
            rsaPrivateCrtKey.getPublicExponent(),
            rsaPrivateCrtKey.getPrivateExponent(),
            rsaPrivateCrtKey.getPrimeP(),
            rsaPrivateCrtKey.getPrimeQ(),
            rsaPrivateCrtKey.getPrimeExponentP(),
            rsaPrivateCrtKey.getPrimeExponentQ(),
            rsaPrivateCrtKey.getCrtCoefficient()
        );
    RSAPrivateCrtKey rsaPrivateKey4 = (RSAPrivateCrtKey) factory.generatePrivate(
            rsaPrivateCrtKeySpec);
    if (!rsaPrivateKey.equals(rsaPrivateKey4)) {
        throw new RuntimeException("rsaPrivateKey should equal to rsaPrivateKey4");
    }
}
 
源代码14 项目: wycheproof   文件: RsaKeyTest.java
private void checkPrivateCrtKey(RSAPrivateCrtKey key, int expectedKeySize) throws Exception {
  BigInteger p = key.getPrimeP();
  BigInteger q = key.getPrimeQ();
  BigInteger n = key.getModulus();
  BigInteger e = key.getPublicExponent();
  BigInteger d = key.getPrivateExponent();
  BigInteger dp = key.getPrimeExponentP();
  BigInteger dq = key.getPrimeExponentQ();
  BigInteger crtCoeff = key.getCrtCoefficient();

  // Simple test that (n,d,e) is a valid RSA key.
  assertEquals(n, p.multiply(q));
  assertEquals(expectedKeySize, n.bitLength());
  int certainty = 80;
  assertTrue(p.isProbablePrime(certainty));
  assertTrue(q.isProbablePrime(certainty));
  // Very simple checks for weak random number generators.
  RandomUtil.checkPrime(p);
  RandomUtil.checkPrime(q);
  // TODO(bleichen): Keys that are very imbalanced can be broken with elliptic curve factoring.
  //   Add other checks. E.g. for the size of dp and dq
  assertTrue(p.bitLength() > 256);
  assertTrue(q.bitLength() > 256);
  BigInteger p1 = p.subtract(BigInteger.ONE);
  BigInteger q1 = q.subtract(BigInteger.ONE);
  BigInteger phi = p1.multiply(q1);
  BigInteger order = phi.divide(p1.gcd(q1)); // maximal order of elements
  // RFC 8017 Section 3.2 specifies that d is a positive integer smaller than n satisfying
  //    e * d == 1 (mod lcm(p-1, q-1)).
  // FIPS-PUB 186-4 specifies that d is the smallest positive integer satisfying
  // the equation above and further specifies that key with d < 2^(n.bitlenght()/2) are not
  // allowed. The second condition is very unlikely to hold if keys are chosen at random.
  // Hence seeing a small d indicates with high probability a faulty key generation, such
  // as switching e and d, or selecting the primes p and q incorretly.
  // Such keys can likely be broken easily. I.e. since lcm(p - 1, q - 1) divides d * e - 1,
  // it follows that (p - 1) * (q - 1) divides (d * e - 1) * gcd(p - 1, q - 1).
  // Hence if d * e - 1 is small then p - 1 and q - 1 must have a large common factor g.
  assertEquals(1, d.compareTo(BigInteger.ONE));
  assertEquals(-1, d.compareTo(n)); // This is the requirement of RFC 8017
  // The following would be the stricter requirement of FIPS-PUB 186-4.
  // assertEquals(-1, d.compareTo(order));
  assertTrue(d.bitLength() > expectedKeySize / 2);
  assertEquals(BigInteger.ONE, d.multiply(e).mod(order));
  assertEquals(d.mod(p1), dp.mod(p1));
  assertEquals(d.mod(q1), dq.mod(q1));
  assertEquals(q.multiply(crtCoeff).mod(p), BigInteger.ONE);
  // Checks that p - 1 and q - 1 do not have a large common factor g. Since large common
  // factors are very unlikely to occur at random one has to assume that such an event is caused
  // by a faulty generation and that g is in fact known. Coppersmith showed how to factor an RSA
  // modulus if about 1/4 of the low order bits of a factor is known.
  assertTrue(p1.gcd(q1).bitLength() < expectedKeySize / 4);
}
 
源代码15 项目: jdk8u_jdk   文件: PrivateKeyEqualityTest.java
public static void main(String[] args) throws NoSuchAlgorithmException,
        NoSuchProviderException, InvalidKeySpecException {
    // Generate the first key.
    KeyPairGenerator generator
            = KeyPairGenerator.getInstance(KEYALG, PROVIDER_NAME);
    KeyPair keyPair = generator.generateKeyPair();
    RSAPrivateKey rsaPrivateKey = (RSAPrivateKey) keyPair.getPrivate();
    if (!(rsaPrivateKey instanceof RSAPrivateCrtKey)) {
        System.err.println("rsaPrivateKey class : " + rsaPrivateKey.getClass().getName());
        throw new RuntimeException("rsaPrivateKey is not a RSAPrivateCrtKey instance");
    }

    // Generate the second key.
    KeyFactory factory = KeyFactory.getInstance(KEYALG, PROVIDER_NAME);
    RSAPrivateKeySpec rsaPrivateKeySpec = new RSAPrivateKeySpec(
            rsaPrivateKey.getModulus(), rsaPrivateKey.getPrivateExponent());
    RSAPrivateKey rsaPrivateKey2 = (RSAPrivateKey) factory.generatePrivate(
            rsaPrivateKeySpec);

    // Generate the third key.
    PKCS8EncodedKeySpec encodedKeySpec = new PKCS8EncodedKeySpec(
            rsaPrivateKey.getEncoded());
    RSAPrivateKey rsaPrivateKey3 = (RSAPrivateKey) factory.generatePrivate(
            encodedKeySpec);

    // Check for equality.
    if (rsaPrivateKey.equals(rsaPrivateKey2)) {
        throw new RuntimeException("rsaPrivateKey should not equal to rsaPrivateKey2");
    }
    if (!rsaPrivateKey3.equals(rsaPrivateKey)) {
        throw new RuntimeException("rsaPrivateKey3 should equal to rsaPrivateKey");
    }
    if (rsaPrivateKey3.equals(rsaPrivateKey2)) {
        throw new RuntimeException("rsaPrivateKey3 should not equal to rsaPrivateKey2");
    }
    if (rsaPrivateKey2.equals(rsaPrivateKey3)) {
        throw new RuntimeException("rsaPrivateKey2 should not equal to rsaPrivateKey3");
    }

    // Generate the fourth key.
    RSAPrivateCrtKey rsaPrivateCrtKey =  (RSAPrivateCrtKey)rsaPrivateKey;
    RSAPrivateCrtKeySpec rsaPrivateCrtKeySpec = new RSAPrivateCrtKeySpec(
            rsaPrivateCrtKey.getModulus(),
            rsaPrivateCrtKey.getPublicExponent(),
            rsaPrivateCrtKey.getPrivateExponent(),
            rsaPrivateCrtKey.getPrimeP(),
            rsaPrivateCrtKey.getPrimeQ(),
            rsaPrivateCrtKey.getPrimeExponentP(),
            rsaPrivateCrtKey.getPrimeExponentQ(),
            rsaPrivateCrtKey.getCrtCoefficient()
        );
    RSAPrivateCrtKey rsaPrivateKey4 = (RSAPrivateCrtKey) factory.generatePrivate(
            rsaPrivateCrtKeySpec);
    if (!rsaPrivateKey.equals(rsaPrivateKey4)) {
        throw new RuntimeException("rsaPrivateKey should equal to rsaPrivateKey4");
    }
}