Example usage for org.bouncycastle.asn1.pkcs RSAPublicKey RSAPublicKey

List of usage examples for org.bouncycastle.asn1.pkcs RSAPublicKey RSAPublicKey

Introduction

In this page you can find the example usage for org.bouncycastle.asn1.pkcs RSAPublicKey RSAPublicKey.

Prototype

public RSAPublicKey(BigInteger modulus, BigInteger publicExponent) 

Source Link

Usage

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

License:Open Source License

/**
 * Get the Bouncy Castle RSA public key.
 *
 * @return The BouncyCastle RSA public key.
 *//* w ww  .j  a  v a  2 s .c  o m*/
public RSAPublicKey bcPublicKey() {
    return new RSAPublicKey(this.modulus(), this.publicExponent());
}

From source file:com.android.verity.BootKey.java

License:Apache License

public BootKey(PublicKey key) throws Exception {
    java.security.interfaces.RSAPublicKey k = (java.security.interfaces.RSAPublicKey) key;
    this.keyMaterial = new RSAPublicKey(k.getModulus(), k.getPublicExponent());
    this.algorithmIdentifier = new AlgorithmIdentifier(PKCSObjectIdentifiers.sha256WithRSAEncryption);
}

From source file:org.jcryptool.visual.jctca.Util.java

License:Open Source License

public static KeyPair asymmetricKeyPairToNormalKeyPair(AsymmetricCipherKeyPair keypair) {
    RSAKeyParameters publicKey = (RSAKeyParameters) keypair.getPublic();
    RSAPrivateCrtKeyParameters privateKey = (RSAPrivateCrtKeyParameters) keypair.getPrivate();
    @SuppressWarnings("unused")
    RSAPublicKey pkStruct = new RSAPublicKey(publicKey.getModulus(), publicKey.getExponent());
    // JCE format needed for the certificate - because
    // getEncoded() is necessary...
    PublicKey pubKey;/*from www.  jav  a2 s  . c  o  m*/
    try {
        pubKey = KeyFactory.getInstance("RSA").generatePublic(//$NON-NLS-1$
                new RSAPublicKeySpec(publicKey.getModulus(), publicKey.getExponent()));
        PrivateKey privKey = KeyFactory.getInstance("RSA").generatePrivate(//$NON-NLS-1$
                new RSAPrivateCrtKeySpec(publicKey.getModulus(), publicKey.getExponent(),
                        privateKey.getExponent(), privateKey.getP(), privateKey.getQ(), privateKey.getDP(),
                        privateKey.getDQ(), privateKey.getQInv()));

        return new KeyPair(pubKey, privKey);
    } catch (InvalidKeySpecException e) {
        LogUtil.logError(e);
    } catch (NoSuchAlgorithmException e) {
        LogUtil.logError(e);
    }
    // and this one for the KeyStore
    return null;
}

From source file:org.opcfoundation.ua.transport.security.BcCryptoProvider.java

License:Open Source License

public void encryptAsymm(PublicKey encryptingCertificate, SecurityAlgorithm algorithm, byte[] dataToEncrypt,
        byte[] output, int outputOffset) throws ServiceResultException {

    try {//from  w  w  w  .  j  a v  a 2s.c  om
        java.security.interfaces.RSAPublicKey encryptingCertificateRSA = (java.security.interfaces.RSAPublicKey) encryptingCertificate;
        RSAPublicKey publicKey = new RSAPublicKey(encryptingCertificateRSA.getModulus(),
                encryptingCertificateRSA.getPublicExponent());
        AsymmetricBlockCipher cipher = getAsymmetricCipher(algorithm, publicKey);

        int len = 0;
        int inputBlockSize = cipher.getInputBlockSize();
        int outputBlockSize = cipher.getOutputBlockSize();
        logger.debug("Encrypt: inputBlockSize={}, outputBlockSize={}, dataToEncrypt.length={}", inputBlockSize,
                outputBlockSize, dataToEncrypt.length);
        for (int i = 0; i < dataToEncrypt.length; i += inputBlockSize) {
            int size = Math.min(dataToEncrypt.length - i, inputBlockSize);
            byte[] tmp = cipher.processBlock(dataToEncrypt, i, size);
            System.arraycopy(tmp, 0, output, outputOffset + len, tmp.length);
            len += tmp.length;
        }

    } catch (InvalidCipherTextException e) {
        throw new ServiceResultException(StatusCodes.Bad_InternalError, e);
    }

}

From source file:org.opcfoundation.ua.transport.security.BcCryptoProvider.java

License:Open Source License

@Override
public boolean verifyAsymm(PublicKey signingCertificate, SecurityAlgorithm algorithm, byte[] dataToVerify,
        byte[] signature) throws ServiceResultException {
    if (algorithm == null)
        return true;
    if (signingCertificate == null || dataToVerify == null || signature == null)
        throw new IllegalArgumentException("null arg");

    java.security.interfaces.RSAPublicKey signingCertificateRSA = (java.security.interfaces.RSAPublicKey) signingCertificate;
    RSAPublicKey publicKey = new RSAPublicKey(signingCertificateRSA.getModulus(),
            signingCertificateRSA.getPublicExponent());
    Signer signer = getAsymmetricSigner(false, algorithm, publicKey);
    signer.update(dataToVerify, 0, dataToVerify.length);
    return signer.verifySignature(signature);

}

From source file:org.xipki.commons.security.pkcs12.P12KeypairGenerator.java

License:Open Source License

private KeyPairWithSubjectPublicKeyInfo genRSAKeypair(final int keysize, final BigInteger publicExponent,
        final SecureRandom random) throws Exception {
    KeyPair kp = KeyUtil.generateRSAKeypair(keysize, publicExponent, random);
    java.security.interfaces.RSAPublicKey rsaPubKey = (java.security.interfaces.RSAPublicKey) kp.getPublic();

    SubjectPublicKeyInfo spki = new SubjectPublicKeyInfo(
            new AlgorithmIdentifier(PKCSObjectIdentifiers.rsaEncryption, DERNull.INSTANCE),
            new RSAPublicKey(rsaPubKey.getModulus(), rsaPubKey.getPublicExponent()));
    return new KeyPairWithSubjectPublicKeyInfo(kp, spki);
}

From source file:org.xipki.pki.scep.util.ScepUtil.java

License:Open Source License

public static SubjectPublicKeyInfo createSubjectPublicKeyInfo(final PublicKey publicKey) throws IOException {
    ParamUtil.requireNonNull("publicKey", publicKey);
    if (publicKey instanceof java.security.interfaces.RSAPublicKey) {
        java.security.interfaces.RSAPublicKey rsaPubKey = (java.security.interfaces.RSAPublicKey) publicKey;
        return new SubjectPublicKeyInfo(
                new AlgorithmIdentifier(PKCSObjectIdentifiers.rsaEncryption, DERNull.INSTANCE),
                new RSAPublicKey(rsaPubKey.getModulus(), rsaPubKey.getPublicExponent()));
    } else {//  www .j  a va2 s .c  om
        throw new IllegalArgumentException("unsupported public key " + publicKey);
    }
}