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

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

Introduction

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

Prototype

public BigInteger getPublicExponent() 

Source Link

Usage

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:io.apigee.trireme.crypto.algorithms.RsaKeyPairProvider.java

License:Open Source License

private PublicKey convertPublicKey(SubjectPublicKeyInfo pk) throws CryptoException, IOException {
    RSAPublicKey rsa = RSAPublicKey.getInstance(pk.parsePublicKey());

    try {/*from  ww w . j a va2  s. c om*/
        KeyFactory factory = KeyFactory.getInstance("RSA");
        RSAPublicKeySpec pubSpec = new RSAPublicKeySpec(rsa.getModulus(), rsa.getPublicExponent());
        return factory.generatePublic(pubSpec);

    } catch (GeneralSecurityException gse) {
        throw new CryptoException(gse);
    }
}

From source file:io.apigee.trireme.crypto.RSAConverter.java

License:Open Source License

public static PublicKey convertPublicKey(SubjectPublicKeyInfo pk) throws CryptoException, IOException {
    RSAPublicKey rsa = RSAPublicKey.getInstance(pk.parsePublicKey());

    try {// w ww . j a  v  a  2s . c  o  m
        KeyFactory factory = KeyFactory.getInstance("RSA");
        RSAPublicKeySpec pubSpec = new RSAPublicKeySpec(rsa.getModulus(), rsa.getPublicExponent());
        return factory.generatePublic(pubSpec);

    } catch (GeneralSecurityException gse) {
        throw new CryptoException(gse);
    }
}

From source file:net.wstech2.me.httpsclient.CertificateValidatorUtils.java

License:Apache License

/**
 * /* w  w w  .  ja v  a 2  s. co  m*/
 * Inspected and display various informations from the Certificate passed as
 * parameter. Keys are presented in HEX values and ASN1 structures dumped
 * using ASN1Dump.dumpAsString.
 * 
 * This method is intended for debug purposes only.
 * 
 * 
 * @param cert
 *            The X509CertificateStructure to be inspected.
 * 
 */
public static void dumpCertificateInfo(org.bouncycastle.asn1.x509.Certificate cert) {
    boolean valid = false;
    TBSCertificate tbs = cert.getTBSCertificate();
    RSAEngine engine = new RSAEngine();
    SHA1Digest digest = new SHA1Digest();

    GenericSigner signer = new GenericSigner((engine), digest);
    RSAPublicKey signingKey;
    try {
        signingKey = RSAPublicKey.getInstance(cert.getSubjectPublicKeyInfo().parsePublicKey());

        HttpsConnectionUtils.logDebug("Public Key:[[" + cert.getSubjectPublicKeyInfo().parsePublicKey() + "]]");

        RSAKeyParameters keySpec = new RSAKeyParameters(false, signingKey.getModulus(),
                signingKey.getPublicExponent());
        signer.init(false, keySpec);
        HttpsConnectionUtils.logDebug("TBS DER object:[[" + tbs.getEncoded("DER") + "]]");

        signer.update(tbs.getEncoded(), 0, tbs.getEncoded().length);

        valid = signer.verifySignature(cert.getSignature().getBytes());

        HttpsConnectionUtils.logDebug("signer.verifySignature:[[" + valid + "]]");

        SHA1Digest d2 = new SHA1Digest();
        d2.update(tbs.getEncoded("DER"), 0, tbs.getEncoded("DER").length);
        byte[] hash = new byte[d2.getDigestSize()];
        d2.doFinal(hash, 0);
        HttpsConnectionUtils.logDebug("tbs.getDEREncoded() HASH:[[" + new String(Hex.encode(hash)) + "]]");
        DEROctetString asn1Hash = new DEROctetString(hash);
        HttpsConnectionUtils.logDebug(
                "ASN1 DEROctetString hash:[[" + new String(Hex.encode(asn1Hash.getEncoded("DER"))) + "]]");

        d2 = new SHA1Digest();
        d2.update(cert.getEncoded(), 0, cert.getEncoded().length);
        hash = new byte[d2.getDigestSize()];
        d2.doFinal(hash, 0);
        HttpsConnectionUtils.logDebug("cert.getEncoded() HASH:[[" + new String(Hex.encode(hash)) + "]]");

        byte[] signature = cert.getSignature().getBytes();
        HttpsConnectionUtils
                .logDebug("cert.getSignature().getBytes():[[" + new String(Hex.encode(signature)) + "]]");

        PKCS1Encoding engine2 = new PKCS1Encoding(new RSAEngine());
        engine2.init(false, keySpec);
        byte[] decryptedHash = engine2.processBlock(signature, 0, signature.length);
        HttpsConnectionUtils.logDebug("decryptedHash:[[" + new String(Hex.encode(decryptedHash)) + "]]");

        ASN1Object o = ASN1Primitive.fromByteArray(decryptedHash);
        HttpsConnectionUtils.logDebug(
                "decryptedHash.getDEREncoded():[[" + new String(Hex.encode(o.getEncoded("DER"))) + "]]");

        HttpsConnectionUtils.logDebug(
                "ASN1Dump.dumpAsString(decryptedHash,true):[[" + ASN1Dump.dumpAsString(o, true) + "]]");

        HttpsConnectionUtils.logDebug("engine.getInputBlockSize():[[" + engine2.getInputBlockSize() + "]]");

        HttpsConnectionUtils.logDebug("engine.getOutputBlockSize():[[" + engine2.getOutputBlockSize() + "]]");

        ASN1Sequence asn1SignSeq = (ASN1Sequence) ASN1Sequence.fromByteArray(decryptedHash);
        HttpsConnectionUtils
                .logDebug("Signature ASN1 Sequence:[[" + ASN1Dump.dumpAsString(asn1SignSeq, true) + "]]");

        AlgorithmIdentifier algorithm = AlgorithmIdentifier.getInstance(asn1SignSeq.getObjectAt(0));
        HttpsConnectionUtils.logDebug("AlgorithmIdentifier:[[" + ASN1Dump.dumpAsString(algorithm, true) + "]]");

        DEROctetString signedHash = (DEROctetString) DEROctetString.getInstance(asn1SignSeq.getObjectAt(1));
        HttpsConnectionUtils.logDebug("signedHash:[[" + ASN1Dump.dumpAsString(signedHash, true) + "]]");

    } catch (Exception e) {
        e.printStackTrace();
    }

}

From source file:org.ebayopensource.fido.uaf.crypto.KeyCodec.java

License:Apache License

public static PublicKey getRSAPublicKey(byte[] encodedPubKey)
        throws IOException, NoSuchAlgorithmException, InvalidKeySpecException {
    RSAPublicKey pubKey8 = RSAPublicKey.getInstance(encodedPubKey);
    SubjectPublicKeyInfo info = SubjectPublicKeyInfoFactory.createSubjectPublicKeyInfo(
            new RSAKeyParameters(false, pubKey8.getModulus(), pubKey8.getPublicExponent()));
    X509EncodedKeySpec spec = new X509EncodedKeySpec(info.getEncoded());
    KeyFactory keyFactory = KeyFactory.getInstance("RSA");
    return keyFactory.generatePublic(spec);
}

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 ww  w  .  j  a  va 2  s  . 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.opcfoundation.ua.transport.security.BcCryptoProvider.java

License:Open Source License

private AsymmetricBlockCipher getAsymmetricCipher(SecurityAlgorithm algorithm, RSAPublicKey publicKey)
        throws ServiceResultException {
    CipherParameters params = new RSAKeyParameters(false, publicKey.getModulus(),
            publicKey.getPublicExponent());
    // logger.info("Cipher: \nmodulus={}, \npublicExponent={}\n",
    // publicKey.getModulus(), publicKey.getPublicExponent());
    return getAsymmetricCipher(true, algorithm, params);
}

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

License:Open Source License

private Signer getAsymmetricSigner(boolean forSigning, SecurityAlgorithm algorithm, RSAPublicKey publicKey)
        throws ServiceResultException {

    CipherParameters params = new RSAKeyParameters(false, publicKey.getModulus(),
            publicKey.getPublicExponent());
    return getAsymmetricSigner(forSigning, algorithm, params);

}

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);
}