java.security.interfaces.RSAPrivateKey#getPrivateExponent ( )源码实例Demo

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

源代码1 项目: protect   文件: KeyGeneratorCli.java
public static PaillierKeyPair convertToPaillier(final KeyPair rsaKeyPair)
		throws InvalidKeySpecException, NoSuchAlgorithmException {
	// Get keys
	final RSAPrivateKey rsaPrivateKey = (RSAPrivateKey) rsaKeyPair.getPrivate();
	final RSAPublicKey rsaPublicKey = (RSAPublicKey) rsaKeyPair.getPublic();

	// Get fields
	final BigInteger n = rsaPublicKey.getModulus(); // treat as 'n'
	final BigInteger g = rsaPublicKey.getPublicExponent(); // treat as 'g'
	final BigInteger lambda = rsaPrivateKey.getPrivateExponent(); // treat as 'lambda'

	// Convert them back to Paillier keys
	final PaillierPrivateKey privKey = new PaillierPrivateKey(lambda, n);
	final PaillierPublicKey pubKey = new PaillierPublicKey(n, g);

	// Convert to key pair
	return new PaillierKeyPair(pubKey, privKey);
}
 
源代码2 项目: protect   文件: MessageStatusCli.java
public static PaillierKeyPair convertToPaillier(final KeyPair rsaKeyPair)
		throws InvalidKeySpecException, NoSuchAlgorithmException {
	// Get keys
	final RSAPrivateKey rsaPrivateKey = (RSAPrivateKey) rsaKeyPair.getPrivate();
	final RSAPublicKey rsaPublicKey = (RSAPublicKey) rsaKeyPair.getPublic();

	// Get fields
	final BigInteger n = rsaPublicKey.getModulus(); // treat as 'n'
	final BigInteger g = rsaPublicKey.getPublicExponent(); // treat as 'g'
	final BigInteger lambda = rsaPrivateKey.getPrivateExponent(); // treat as 'lambda'

	// Convert them back to Paillier keys
	final PaillierPrivateKey privKey = new PaillierPrivateKey(lambda, n);
	final PaillierPublicKey pubKey = new PaillierPublicKey(n, g);

	// Convert to key pair
	return new PaillierKeyPair(pubKey, privKey);
}
 
源代码3 项目: Mycat2   文件: DecryptUtil.java
public static String encrypt(byte[] keyBytes, String plainText)
		throws Exception {
	PKCS8EncodedKeySpec spec = new PKCS8EncodedKeySpec(keyBytes);
	KeyFactory factory = KeyFactory.getInstance("RSA");
	PrivateKey privateKey = factory.generatePrivate(spec);
	Cipher cipher = Cipher.getInstance("RSA");
       try {
	    cipher.init(Cipher.ENCRYPT_MODE, privateKey);
       } catch (InvalidKeyException e) {
           //For IBM JDK, 原因请看解密方法中的说明
           RSAPrivateKey rsaPrivateKey = (RSAPrivateKey) privateKey;
           RSAPublicKeySpec publicKeySpec = new RSAPublicKeySpec(rsaPrivateKey.getModulus(), rsaPrivateKey.getPrivateExponent());
           Key fakePublicKey = KeyFactory.getInstance("RSA").generatePublic(publicKeySpec);
           cipher = Cipher.getInstance("RSA");
           cipher.init(Cipher.ENCRYPT_MODE, fakePublicKey);
       }

	byte[] encryptedBytes = cipher.doFinal(plainText.getBytes("UTF-8"));
	String encryptedString = Base64.byteArrayToBase64(encryptedBytes);

	return encryptedString;
}
 
源代码4 项目: dble   文件: DecryptUtil.java
private static String encrypt(byte[] keyBytes, String plainText)
        throws Exception {
    PKCS8EncodedKeySpec spec = new PKCS8EncodedKeySpec(keyBytes);
    KeyFactory factory = KeyFactory.getInstance("RSA");
    PrivateKey privateKey = factory.generatePrivate(spec);
    Cipher cipher = Cipher.getInstance("RSA");
    try {
        cipher.init(Cipher.ENCRYPT_MODE, privateKey);
    } catch (InvalidKeyException e) {
        //For IBM JDK
        RSAPrivateKey rsaPrivateKey = (RSAPrivateKey) privateKey;
        RSAPublicKeySpec publicKeySpec = new RSAPublicKeySpec(rsaPrivateKey.getModulus(), rsaPrivateKey.getPrivateExponent());
        Key fakePublicKey = KeyFactory.getInstance("RSA").generatePublic(publicKeySpec);
        cipher = Cipher.getInstance("RSA");
        cipher.init(Cipher.ENCRYPT_MODE, fakePublicKey);
    }

    byte[] encryptedBytes = cipher.doFinal(plainText.getBytes("UTF-8"));
    return Base64.byteArrayToBase64(encryptedBytes);
}
 
源代码5 项目: heisenberg   文件: KeyPairGen.java
public static String encrypt(byte[] keyBytes, String plainText) throws Exception {
    PKCS8EncodedKeySpec spec = new PKCS8EncodedKeySpec(keyBytes);
    KeyFactory factory = KeyFactory.getInstance("RSA");
    PrivateKey privateKey = factory.generatePrivate(spec);
    Cipher cipher = Cipher.getInstance("RSA");
    try {
        cipher.init(1, privateKey);
    } catch (InvalidKeyException e) {
        RSAPrivateKey rsaPrivateKey = (RSAPrivateKey) privateKey;
        RSAPublicKeySpec publicKeySpec = new RSAPublicKeySpec(rsaPrivateKey.getModulus(),
            rsaPrivateKey.getPrivateExponent());
        Key fakePublicKey = KeyFactory.getInstance("RSA").generatePublic(publicKeySpec);
        cipher = Cipher.getInstance("RSA");
        cipher.init(1, fakePublicKey);
    }

    byte[] encryptedBytes = cipher.doFinal(plainText.getBytes());
    return Base64.encodeBase64String(encryptedBytes);

}
 
源代码6 项目: swim   文件: RsaPrivateKeyDef.java
public static RsaPrivateKeyDef from(RSAPrivateKey key) {
  if (key instanceof RSAMultiPrimePrivateCrtKey) {
    return from((RSAMultiPrimePrivateCrtKey) key);
  } else if (key instanceof RSAPrivateCrtKey) {
    return from((RSAPrivateCrtKey) key);
  } else {
    return new RsaPrivateKeyDef(key.getModulus(), key.getPrivateExponent(), key);
  }
}
 
源代码7 项目: protect   文件: KeyGeneratorCli.java
public static PaillierPrivateKey convertToPaillierPrivateKey(final RSAPrivateKey rsaPrivateKey)
		throws InvalidKeySpecException, NoSuchAlgorithmException {

	// Get fields
	final BigInteger n = rsaPrivateKey.getModulus(); // treat as 'n'
	final BigInteger lambda = rsaPrivateKey.getPrivateExponent(); // treat as 'lambda'

	// Convert them back to Paillier private key
	return new PaillierPrivateKey(lambda, n);
}
 
源代码8 项目: protect   文件: MessageStatusCli.java
public static PaillierPrivateKey convertToPaillierPrivateKey(final RSAPrivateKey rsaPrivateKey)
		throws InvalidKeySpecException, NoSuchAlgorithmException {

	// Get fields
	final BigInteger n = rsaPrivateKey.getModulus(); // treat as 'n'
	final BigInteger lambda = rsaPrivateKey.getPrivateExponent(); // treat as 'lambda'

	// Convert them back to Paillier private key
	return new PaillierPrivateKey(lambda, n);
}
 
源代码9 项目: protect   文件: Pem.java
public static PaillierPrivateKey convertToPaillierPrivateKey(final RSAPrivateKey rsaPrivateKey)
		throws InvalidKeySpecException, NoSuchAlgorithmException {

	// Get fields
	final BigInteger n = rsaPrivateKey.getModulus(); // treat as 'n'
	final BigInteger lambda = rsaPrivateKey.getPrivateExponent(); // treat as 'lambda'

	// Convert them back to Paillier private key
	return new PaillierPrivateKey(lambda, n);
}
 
源代码10 项目: ambari-logsearch   文件: SslConfigurer.java
private X509Certificate createCert(KeyPair keyPair, String signatureAlgoritm, String domainName)
  throws NoSuchAlgorithmException, InvalidKeyException, SignatureException, OperatorCreationException, CertificateException, IOException {
  
  RSAPublicKey rsaPublicKey = (RSAPublicKey) keyPair.getPublic();
  RSAPrivateKey rsaPrivateKey = (RSAPrivateKey) keyPair.getPrivate();
  
  AlgorithmIdentifier sigAlgId = new DefaultSignatureAlgorithmIdentifierFinder().find(signatureAlgoritm);
  AlgorithmIdentifier digAlgId = new DefaultDigestAlgorithmIdentifierFinder().find(sigAlgId);
  BcContentSignerBuilder sigGen = new BcRSAContentSignerBuilder(sigAlgId, digAlgId);
  
  ASN1InputStream publicKeyStream = new ASN1InputStream(rsaPublicKey.getEncoded());
  SubjectPublicKeyInfo pubKey = SubjectPublicKeyInfo.getInstance(publicKeyStream.readObject());
  publicKeyStream.close();
  
  X509v3CertificateBuilder v3CertBuilder = new X509v3CertificateBuilder(
      new X500Name("CN=" + domainName + ", OU=None, O=None L=None, C=None"),
      BigInteger.valueOf(Math.abs(new SecureRandom().nextInt())),
      new Date(System.currentTimeMillis() - 1000L * 60 * 60 * 24 * 30),
      new Date(System.currentTimeMillis() + (1000L * 60 * 60 * 24 * 365*10)),
      new X500Name("CN=" + domainName + ", OU=None, O=None L=None, C=None"),
      pubKey);
  
  RSAKeyParameters keyParams = new RSAKeyParameters(true, rsaPrivateKey.getPrivateExponent(), rsaPrivateKey.getModulus());
  ContentSigner contentSigner = sigGen.build(keyParams);
  
  X509CertificateHolder certificateHolder = v3CertBuilder.build(contentSigner);
  
  JcaX509CertificateConverter certConverter = new JcaX509CertificateConverter().setProvider("BC");
  return certConverter.getCertificate(certificateHolder);
}
 
源代码11 项目: gocd   文件: HttpTestUtil.java
private KeyPair generateKeyPair() {
    try {
        KeyPair seed = KeyPairGenerator.getInstance("RSA", "BC").generateKeyPair();
        RSAPrivateKey privateSeed = (RSAPrivateKey) seed.getPrivate();
        RSAPublicKey publicSeed = (RSAPublicKey) seed.getPublic();
        KeyFactory fact = KeyFactory.getInstance("RSA", "BC");
        RSAPrivateKeySpec privateKeySpec = new RSAPrivateKeySpec(privateSeed.getModulus(), privateSeed.getPrivateExponent());
        RSAPublicKeySpec publicKeySpec = new RSAPublicKeySpec(publicSeed.getModulus(), publicSeed.getPublicExponent());
        return new KeyPair(fact.generatePublic(publicKeySpec), fact.generatePrivate(privateKeySpec));
    } catch (Exception e) {
        throw new RuntimeException(e);
    }
}
 
源代码12 项目: 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");
    }
}
 
源代码13 项目: 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");
    }
}
 
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");
    }
}
 
源代码15 项目: 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");
    }
}
 
源代码16 项目: ripple-lib-java   文件: JCERSAPrivateKey.java
JCERSAPrivateKey(
    RSAPrivateKey key)
{
    this.modulus = key.getModulus();
    this.privateExponent = key.getPrivateExponent();
}
 
源代码17 项目: ripple-lib-java   文件: BCRSAPrivateKey.java
BCRSAPrivateKey(
    RSAPrivateKey key)
{
    this.modulus = key.getModulus();
    this.privateExponent = key.getPrivateExponent();
}
 
源代码18 项目: 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");
    }
}
 
源代码19 项目: RipplePower   文件: JCERSAPrivateKey.java
JCERSAPrivateKey(
    RSAPrivateKey key)
{
    this.modulus = key.getModulus();
    this.privateExponent = key.getPrivateExponent();
}
 
源代码20 项目: RipplePower   文件: BCRSAPrivateKey.java
BCRSAPrivateKey(
    RSAPrivateKey key)
{
    this.modulus = key.getModulus();
    this.privateExponent = key.getPrivateExponent();
}