Example usage for org.bouncycastle.crypto.params RSAKeyParameters getExponent

List of usage examples for org.bouncycastle.crypto.params RSAKeyParameters getExponent

Introduction

In this page you can find the example usage for org.bouncycastle.crypto.params RSAKeyParameters getExponent.

Prototype

public BigInteger getExponent() 

Source Link

Usage

From source file:co.lqnt.lockbox.key.KeyFactory.java

License:Open Source License

/**
 * Create a public key from a PEM formatted public key.
 *
 * @param input The PEM data to read.//from  w  w  w  .j a v  a 2  s .c  o m
 *
 * @return The public key
 * @throws PublicKeyReadException If reading of the public key fails.
 */
public PublicKey createPublicKey(final InputStream input) throws PublicKeyReadException {
    Object pemObject;
    try {
        pemObject = this.parsePemObject(input);
    } catch (PEMException e) {
        throw new PublicKeyReadException(e);
    }

    SubjectPublicKeyInfo publicKeyInfo;
    if (pemObject instanceof SubjectPublicKeyInfo) {
        publicKeyInfo = (SubjectPublicKeyInfo) pemObject;
    } else {
        throw new PublicKeyReadException();
    }

    AsymmetricKeyParameter keyParameter;
    try {
        keyParameter = this.bcKeyParametersFactory().createPublicKeyParameters(publicKeyInfo);
    } catch (IOException e) {
        throw new PublicKeyReadException(e);
    }

    RSAKeyParameters publicKeyParameters;
    if (keyParameter instanceof RSAKeyParameters) {
        publicKeyParameters = (RSAKeyParameters) keyParameter;
    } else {
        throw new PublicKeyReadException();
    }

    return new PublicKey(publicKeyParameters.getModulus(), publicKeyParameters.getExponent());
}

From source file:com.aqnote.shared.cryptology.cert.gen.CertGenerator.java

License:Open Source License

public X509Certificate signCert(PKCS10CertificationRequest pkcs10CSR, X500Name issuer, KeyPair pKeyPair)
        throws Exception {
    SubjectPublicKeyInfo pkInfo = pkcs10CSR.getSubjectPublicKeyInfo();
    RSAKeyParameters rsa = (RSAKeyParameters) PublicKeyFactory.createKey(pkInfo);
    RSAPublicKeySpec rsaSpec = new RSAPublicKeySpec(rsa.getModulus(), rsa.getExponent());
    KeyFactory kf = KeyFactory.getInstance(ALG_RSA);
    PublicKey publicKey = kf.generatePublic(rsaSpec);

    SubjectPublicKeyInfo keyInfo = new SubjectPublicKeyInfo(ASN1Sequence.getInstance(publicKey.getEncoded()));
    X509v3CertificateBuilder certBuilder = new X509v3CertificateBuilder(issuer,
            BigInteger.valueOf(System.currentTimeMillis()),
            new Date(System.currentTimeMillis() - DateConstant.ONE_DAY),
            new Date(System.currentTimeMillis() + DateConstant.ONE_YEAR), pkcs10CSR.getSubject(), keyInfo);

    ContentSigner signer = new JcaContentSignerBuilder(ALG_SIG_SHA256_RSA).setProvider(JCE_PROVIDER)
            .build(pKeyPair.getPrivate());
    X509Certificate signedCert = new JcaX509CertificateConverter().setProvider(JCE_PROVIDER)
            .getCertificate(certBuilder.build(signer));
    signedCert.verify(pKeyPair.getPublic());

    return signedCert;
}

From source file:com.foilen.smalltools.crypt.asymmetric.RSACrypt.java

License:Open Source License

@Override
public RSAKeyDetails retrieveKeyDetails(AsymmetricKeys keyPair) {
    BigInteger modulus = null;/*from ww  w. ja v a 2  s.c  om*/
    BigInteger publicExponent = null;
    BigInteger privateExponent = null;

    try {
        // Public key
        if (keyPair.getPublicKey() != null) {
            AsymmetricKeyParameter key = keyPair.getPublicKey();

            if (!(key instanceof RSAKeyParameters)) {
                throw new SmallToolsException(
                        "The public key is not of type RSAKeyParameters. Type is " + key.getClass().getName());
            }

            RSAKeyParameters rsaKey = (RSAKeyParameters) key;
            modulus = rsaKey.getModulus();
            publicExponent = rsaKey.getExponent();
        }

        // Private key
        if (keyPair.getPrivateKey() != null) {
            AsymmetricKeyParameter key = keyPair.getPrivateKey();

            if (!(key instanceof RSAKeyParameters)) {
                throw new SmallToolsException(
                        "The private key is not of type RSAKeyParameters. Type is " + key.getClass().getName());
            }

            RSAKeyParameters rsaKey = (RSAKeyParameters) key;
            modulus = rsaKey.getModulus();
            privateExponent = rsaKey.getExponent();
        }

        return new RSAKeyDetails(modulus, publicExponent, privateExponent);

    } catch (SmallToolsException e) {
        throw e;
    } catch (Exception e) {
        throw new SmallToolsException("Could not retrieve the details", e);
    }

}

From source file:com.foilen.smalltools.crypt.bouncycastle.asymmetric.RSACrypt.java

License:Open Source License

@Override
public RSAKeyDetails retrieveKeyDetails(AsymmetricKeys keyPair) {

    RSAKeyDetails rsaKeyDetails = new RSAKeyDetails();

    try {/* ww w .  ja  v  a2s. c om*/
        // Public key
        if (keyPair.getPublicKey() != null) {
            AsymmetricKeyParameter key = keyPair.getPublicKey();

            if (!(key instanceof RSAKeyParameters)) {
                throw new SmallToolsException(
                        "The public key is not of type RSAKeyParameters. Type is " + key.getClass().getName());
            }

            RSAKeyParameters rsaKey = (RSAKeyParameters) key;
            rsaKeyDetails.setModulus(rsaKey.getModulus());
            rsaKeyDetails.setPublicExponent(rsaKey.getExponent());
        }

        // Private key
        if (keyPair.getPrivateKey() != null) {
            AsymmetricKeyParameter key = keyPair.getPrivateKey();
            if (!(key instanceof RSAKeyParameters)) {
                throw new SmallToolsException(
                        "The private key is not of type RSAKeyParameters. Type is " + key.getClass().getName());
            }

            RSAKeyParameters rsaKeyParameters = (RSAKeyParameters) key;
            rsaKeyDetails.setModulus(rsaKeyParameters.getModulus());
            rsaKeyDetails.setPrivateExponent(rsaKeyParameters.getExponent());

            // CRT parameters
            if (key instanceof RSAPrivateCrtKeyParameters) {
                RSAPrivateCrtKeyParameters rsaPrivateCrtKeyParameters = (RSAPrivateCrtKeyParameters) key;
                rsaKeyDetails.setCrt(true);
                rsaKeyDetails.setPrimeP(rsaPrivateCrtKeyParameters.getP());
                rsaKeyDetails.setPrimeQ(rsaPrivateCrtKeyParameters.getQ());
                rsaKeyDetails.setPrimeExponentP(rsaPrivateCrtKeyParameters.getDP());
                rsaKeyDetails.setPrimeExponentQ(rsaPrivateCrtKeyParameters.getDQ());
                rsaKeyDetails.setCrtCoefficient(rsaPrivateCrtKeyParameters.getQInv());
            }
        }

        return rsaKeyDetails;

    } catch (SmallToolsException e) {
        throw e;
    } catch (Exception e) {
        throw new SmallToolsException("Could not retrieve the details", e);
    }

}

From source file:com.foilen.smalltools.crypt.bouncycastle.cert.RSATools.java

License:Open Source License

/**
 * Create a {@link Key} from the public {@link AsymmetricKeys}.
 *
 * @param asymmetricKeys//from  ww w.ja  va2  s.c  o m
 *            the asymmetric keys
 * @return the Java key
 */
public static PublicKey createPublicKey(AsymmetricKeys asymmetricKeys) {
    try {
        KeyFactory keyFactory = KeyFactory.getInstance("RSA");
        RSAKeyParameters publicKeyParameters = (RSAKeyParameters) asymmetricKeys.getPublicKey();
        RSAPublicKeySpec keySpec = new RSAPublicKeySpec(publicKeyParameters.getModulus(),
                publicKeyParameters.getExponent());
        return keyFactory.generatePublic(keySpec);
    } catch (Exception e) {
        throw new SmallToolsException("Problem generating the key", e);
    }
}

From source file:com.geoxp.oss.CryptoHelperTest.java

License:Apache License

@Test
public void testSSHSignatureBlobSign_RSA() throws Exception {
    RSAKeyPairGenerator rsakpg = new RSAKeyPairGenerator();
    RSAKeyGenerationParameters params = new RSAKeyGenerationParameters(new BigInteger("35"), new SecureRandom(),
            2048, 8);/* www  .java2s .  c om*/
    rsakpg.init(params);

    AsymmetricCipherKeyPair kp = rsakpg.generateKeyPair();

    RSAPrivateCrtKeyParameters privParams = (RSAPrivateCrtKeyParameters) kp.getPrivate();
    RSAKeyParameters pubParams = (RSAKeyParameters) kp.getPublic();

    KeySpec ks = new RSAPrivateKeySpec(privParams.getModulus(), privParams.getExponent());
    PrivateKey priv = KeyFactory.getInstance("RSA").generatePrivate(ks);

    ks = new RSAPublicKeySpec(pubParams.getModulus(), pubParams.getExponent());
    PublicKey pub = KeyFactory.getInstance("RSA").generatePublic(ks);

    byte[] data = PLAINTEXT.getBytes();
    byte[] sig = CryptoHelper.sshSignatureBlobSign(data, priv);

    Assert.assertTrue(CryptoHelper.sshSignatureBlobVerify(data, sig, pub));
}

From source file:com.raphfrk.craftproxyclient.net.protocol.p16x.P16xProtocol.java

License:Open Source License

public byte[] encodeRSAPublicKey(RSAKeyParameters key) {
    if (((RSAKeyParameters) key).isPrivate()) {
        return null;
    }/*from ww w  . j a v a 2  s  .  c o  m*/

    RSAKeyParameters rsaKey = (RSAKeyParameters) key;

    ASN1EncodableVector encodable = new ASN1EncodableVector();
    encodable.add(new ASN1Integer(rsaKey.getModulus()));
    encodable.add(new ASN1Integer(rsaKey.getExponent()));

    return KeyUtil.getEncodedSubjectPublicKeyInfo(
            new AlgorithmIdentifier(PKCSObjectIdentifiers.rsaEncryption, DERNull.INSTANCE),
            new DERSequence(encodable));
}

From source file:com.sun.midp.crypto.BouncyCastleRSAPrivateKey.java

License:Open Source License

public BouncyCastleRSAPrivateKey(RSAKeyParameters keyParam) {
    rsaPrivateKey = new RSAPrivateKey(BigIntegers.asUnsignedByteArray(keyParam.getModulus()),
            BigIntegers.asUnsignedByteArray(keyParam.getExponent()));
}

From source file:de.r2soft.empires.framework.security.CertificateUtil.java

License:Open Source License

/**
 * Generate a secure public key./*from w ww  .  jav  a 2s . c  o m*/
 * 
 * @param pub
 * @return
 * @throws InvalidKeySpecException
 * @throws NoSuchAlgorithmException
 */
private PublicKey generatePublicKey(AsymmetricKeyParameter pub)
        throws InvalidKeySpecException, NoSuchAlgorithmException {
    RSAKeyParameters publicKeyParameters = (RSAKeyParameters) pub;

    // Create RSA Key
    RSAPublicKeySpec rsa = new RSAPublicKeySpec(publicKeyParameters.getModulus(),
            publicKeyParameters.getExponent());
    return KeyFactory.getInstance("RSA").generatePublic(rsa);
}

From source file:de.r2soft.empires.framework.security.CertificateUtil.java

License:Open Source License

/**
 * Generate an RSA private key/*from w w  w .  j a  v a2  s.  c o  m*/
 * 
 * @param cipherParameters
 * @param cipherParameters2
 * @return
 * @throws InvalidKeySpecException
 * @throws NoSuchAlgorithmException
 */
private PrivateKey generatePrivateKey(CipherParameters cipherParameters, CipherParameters cipherParameters2)
        throws InvalidKeySpecException, NoSuchAlgorithmException {

    RSAPrivateCrtKeyParameters privateKeyParameters = (RSAPrivateCrtKeyParameters) cipherParameters;
    RSAKeyParameters publicKeyParameters = (RSAKeyParameters) cipherParameters2;

    RSAPrivateCrtKeySpec rsaPrivateKeySpec = new RSAPrivateCrtKeySpec(publicKeyParameters.getModulus(),
            publicKeyParameters.getExponent(), privateKeyParameters.getExponent(), privateKeyParameters.getP(),
            privateKeyParameters.getQ(), privateKeyParameters.getDP(), privateKeyParameters.getDQ(),
            privateKeyParameters.getQInv());

    return KeyFactory.getInstance("RSA").generatePrivate(rsaPrivateKeySpec);
}