org.bouncycastle.crypto.params.RSAPrivateCrtKeyParameters Java Examples

The following examples show how to use org.bouncycastle.crypto.params.RSAPrivateCrtKeyParameters. You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example #1
Source File: SslClientCertificateImplTest.java    From hivemq-community-edition with Apache License 2.0 7 votes vote down vote up
private KeyPair createKeyPair() throws InvalidKeySpecException, NoSuchAlgorithmException {

        final RSAKeyPairGenerator gen = new RSAKeyPairGenerator();

        gen.init(new RSAKeyGenerationParameters(BigInteger.valueOf(3), new SecureRandom(), 1024, 80));
        final AsymmetricCipherKeyPair keypair = gen.generateKeyPair();

        final RSAKeyParameters publicKey = (RSAKeyParameters) keypair.getPublic();
        final RSAPrivateCrtKeyParameters privateKey = (RSAPrivateCrtKeyParameters) keypair.getPrivate();

        final PublicKey pubKey = KeyFactory.getInstance("RSA").generatePublic(
                new RSAPublicKeySpec(publicKey.getModulus(), publicKey.getExponent()));

        final PrivateKey privKey = KeyFactory.getInstance("RSA").generatePrivate(
                new RSAPrivateCrtKeySpec(publicKey.getModulus(), publicKey.getExponent(),
                        privateKey.getExponent(), privateKey.getP(), privateKey.getQ(),
                        privateKey.getDP(), privateKey.getDQ(), privateKey.getQInv()));

        return new KeyPair(pubKey, privKey);
    }
 
Example #2
Source File: BouncyCastleV1CryptoProvider.java    From paseto with MIT License 6 votes vote down vote up
@Override
public KeyPair rsaGenerate() {
	RSAKeyPairGenerator keyGen = new RSAKeyPairGenerator();
	keyGen.init(new RSAKeyGenerationParameters(E, new SecureRandom(), RSA_KEY_SIZE,
			PrimeCertaintyCalculator.getDefaultCertainty(RSA_KEY_SIZE)));
	AsymmetricCipherKeyPair pair = keyGen.generateKeyPair();

	RSAKeyParameters pub = (RSAKeyParameters) pair.getPublic();
	RSAPrivateCrtKeyParameters priv = (RSAPrivateCrtKeyParameters) pair.getPrivate();

	// As in BCRSAPrivateKey / BCRSAPublicKey
	AlgorithmIdentifier algo = new AlgorithmIdentifier(PKCSObjectIdentifiers.rsaEncryption, DERNull.INSTANCE);
	byte[] publicKey = KeyUtil.getEncodedSubjectPublicKeyInfo(algo, new RSAPublicKey(pub.getModulus(),
			pub.getExponent()));
	byte[] privateKey = KeyUtil.getEncodedPrivateKeyInfo(algo, new RSAPrivateKey(priv.getModulus(),
			priv.getPublicExponent(), priv.getExponent(), priv.getP(), priv.getQ(), priv.getDP(), priv.getDQ(),
			priv.getQInv()));

	return new KeyPair(privateKey, publicKey);
}
 
Example #3
Source File: NativeRSAVectors.java    From jna-gmp with Apache License 2.0 6 votes vote down vote up
private static void generateTestVector(int rsaKeyBits, int suffix) throws Exception {
  AsymmetricCipherKeyPair pair = generateKeyPair(rsaKeyBits);
  RSAPrivateCrtKeyParameters priv = (RSAPrivateCrtKeyParameters) pair.getPrivate();

  byte[] message = new byte[rsaKeyBits / 8];
  SECURE_RANDOM.nextBytes(message);
  // Clear the top bit to ensure it fits.
  message[0] &= 0x7F;

  RSAEngine encoder = new RSAEngine();
  encoder.init(true, pair.getPrivate());
  byte[] signed = encoder.processBlock(message, 0, message.length);

  RSAEngine decoder = new RSAEngine();
  decoder.init(false, pair.getPublic());
  byte[] decoded = decoder.processBlock(signed, 0, message.length);

  Assert.assertArrayEquals(message, decoded);

  System.out.println("public static final TestVector VECTOR" + suffix + " = ");
  new TestVector(new BigInteger(1, message), new BigInteger(1, signed),
      (RSAPrivateCrtKeyParameters) pair.getPrivate()).printJavaConstructorFor();
  System.out.println();
}
 
Example #4
Source File: CryptographicUtilities.java    From openemm with GNU Affero General Public License v3.0 5 votes vote down vote up
public static PrivateKey getPrivateKeyFromKeyPair(AsymmetricCipherKeyPair keyPair) throws Exception {
	Security.addProvider(new BouncyCastleProvider());

	RSAPrivateCrtKeyParameters privateKey = (RSAPrivateCrtKeyParameters)keyPair.getPrivate();
	RSAKeyParameters publicKey = (RSAKeyParameters)keyPair.getPublic();
	return KeyFactory.getInstance("RSA").generatePrivate(
			new RSAPrivateCrtKeySpec(privateKey.getModulus(), publicKey.getExponent(), privateKey.getExponent(), privateKey.getP(), privateKey.getQ(), privateKey.getDP(), privateKey.getDQ(),
					privateKey.getQInv()));
}
 
Example #5
Source File: KeyStoreGenerator.java    From cute-proxy with BSD 2-Clause "Simplified" License 5 votes vote down vote up
private RSAPrivateCrtKeyParameters getPrivateKeyParameters(RSAPrivateCrtKey privateCrtKey) {
    return new RSAPrivateCrtKeyParameters(privateCrtKey.getModulus(),
            privateCrtKey.getPublicExponent(),
            privateCrtKey.getPrivateExponent(),
            privateCrtKey.getPrimeP(), privateCrtKey.getPrimeQ(), privateCrtKey.getPrimeExponentP(),
            privateCrtKey.getPrimeExponentQ(),
            privateCrtKey.getCrtCoefficient());
}
 
Example #6
Source File: KeyStoreGenerator.java    From cute-proxy with BSD 2-Clause "Simplified" License 5 votes vote down vote up
private static byte[] signData(ASN1ObjectIdentifier sigOID, byte[] data,
                               RSAPrivateCrtKeyParameters privateKeyParameters,
                               SecureRandom secureRandom) throws Exception {
    PrivateKey caPrivateKey = KeyFactory.getInstance("RSA").generatePrivate(getKeySpec(privateKeyParameters));
    Signature signature = Signature.getInstance(sigOID.getId());
    signature.initSign(caPrivateKey, secureRandom);
    signature.update(data);
    return signature.sign();
}
 
Example #7
Source File: SignerUtil.java    From xipki with Apache License 2.0 5 votes vote down vote up
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());
  }
}
 
Example #8
Source File: KeyStoreGenerator.java    From cute-proxy with BSD 2-Clause "Simplified" License 4 votes vote down vote up
private static RSAPrivateCrtKeySpec getKeySpec(RSAPrivateCrtKeyParameters privateKeyParameters) {
    return new RSAPrivateCrtKeySpec(privateKeyParameters.getModulus(),
            privateKeyParameters.getPublicExponent(), privateKeyParameters.getExponent(),
            privateKeyParameters.getP(), privateKeyParameters.getQ(),
            privateKeyParameters.getDP(), privateKeyParameters.getDQ(), privateKeyParameters.getQInv());
}
 
Example #9
Source File: NativeRSAVectors.java    From jna-gmp with Apache License 2.0 4 votes vote down vote up
public TestVector(String message, String signed, String p, String dP, String q, String dQ,
    String qInv, String privateExponent, String modulus, String publicExponent) {
  this(decode(message), decode(signed),
      new RSAPrivateCrtKeyParameters(decode(modulus), decode(publicExponent),
          decode(privateExponent), decode(p), decode(q), decode(dP), decode(dQ), decode(qInv)));
}
 
Example #10
Source File: NativeRSAVectors.java    From jna-gmp with Apache License 2.0 4 votes vote down vote up
public TestVector(BigInteger message, BigInteger signed, RSAPrivateCrtKeyParameters key) {
  this.message = message;
  this.signed = signed;
  this.key = key;
}
 
Example #11
Source File: NativeRSAVectors.java    From jna-gmp with Apache License 2.0 4 votes vote down vote up
public RSAPrivateCrtKeyParameters getPrivateKey() {
  return key;
}