Example usage for org.bouncycastle.asn1.x509 RSAPublicKeyStructure getPublicExponent

List of usage examples for org.bouncycastle.asn1.x509 RSAPublicKeyStructure getPublicExponent

Introduction

In this page you can find the example usage for org.bouncycastle.asn1.x509 RSAPublicKeyStructure getPublicExponent.

Prototype

public BigInteger getPublicExponent() 

Source Link

Usage

From source file:cf.monteux.silvertunnel.netlib.layer.tor.util.Encryption.java

License:Open Source License

/**
 * makes RSA public key from bin byte array.
 *
 * @param b byte array that contains the key
 * @return//www .  java2s . com
 * @see JCERSAPublicKey
 */
public static RSAPublicKey extractBinaryRSAKey(final byte[] b) {
    RSAPublicKey theKey;

    try {
        final ASN1InputStream ais = new ASN1InputStream(b);
        final Object asnObject = ais.readObject();
        final ASN1Sequence sequence = (ASN1Sequence) asnObject;
        final RSAPublicKeyStructure tempKey = new RSAPublicKeyStructure(sequence);
        theKey = getRSAPublicKey(tempKey.getModulus(), tempKey.getPublicExponent());
        ais.close();
    } catch (final IOException e) {
        logger.warn("Caught exception:" + e.getMessage());
        theKey = null;
    }

    return theKey;
}

From source file:com.rovemonteux.silvertunnel.netlib.layer.tor.util.Encryption.java

License:Open Source License

/**
 * makes RSA public key from bin byte array.
 *
 * @param b byte array that contains the key
 * @return//from   w  w w. j  a v a  2s.c  om
 * @see JCERSAPublicKey
 */
public static RSAPublicKey extractBinaryRSAKey(final byte[] b) {
    RSAPublicKey theKey;

    try {
        final ASN1InputStream ais = new ASN1InputStream(b);
        final Object asnObject = ais.readObject();
        final ASN1Sequence sequence = (ASN1Sequence) asnObject;
        final RSAPublicKeyStructure tempKey = new RSAPublicKeyStructure(sequence);
        theKey = getRSAPublicKey(tempKey.getModulus(), tempKey.getPublicExponent());
        ais.close();
    } catch (final IOException e) {
        LOG.warn("Caught exception:" + e.getMessage());
        theKey = null;
    }

    return theKey;
}

From source file:ezbake.crypto.RSAKeyCrypto.java

License:Apache License

private static PublicKey fixPubKey(byte[] orig) throws InvalidKeySpecException, NoSuchAlgorithmException {
    try {/*from   ww w.j a va2  s.  c  o  m*/
        ASN1InputStream in = new ASN1InputStream(orig);

        ASN1Primitive obj = in.readObject();
        RSAPublicKeyStructure keyStruct = RSAPublicKeyStructure.getInstance(obj);
        RSAPublicKeySpec keySpec = new RSAPublicKeySpec(keyStruct.getModulus(), keyStruct.getPublicExponent());
        KeyFactory keyFactory = KeyFactory.getInstance("RSA");
        return keyFactory.generatePublic(keySpec);
    } catch (IOException e) {
        throw new InvalidKeySpecException("Unable to load public key with stopgap ASN1 method", e);
    }
}

From source file:org.jclouds.crypto.pem.PKCS1EncodedPublicKeySpec.java

License:Apache License

/**
 * Decode PKCS#1 encoded public key into RSAPublicKeySpec.
 * <p>//from  w w  w. j  a v a 2  s. c  o  m
 * Keys here can be in two different formats. They can have the algorithm
 * encoded, or they can have only the modulus and the public exponent.
 * <p>
 * The latter is not a valid PEM encoded file, but it is a valid DER encoded
 * RSA key, so this method should also support it.
 * 
 * @param keyBytes
 *           Encoded PKCS#1 rsa key.
 */
private void decode(final byte[] keyBytes) throws IOException {
    RSAPublicKeyStructure pks = null;
    ASN1Sequence seq = (ASN1Sequence) ASN1Object.fromByteArray(keyBytes);
    try {
        // Try to parse the public key normally. If the algorithm is not
        // present in the encoded key, an IllegalArgumentException will be
        // raised.
        SubjectPublicKeyInfo info = new SubjectPublicKeyInfo(seq);
        pks = new RSAPublicKeyStructure((ASN1Sequence) info.getPublicKey());
    } catch (IllegalArgumentException ex) {
        // If the algorithm is not found in the encoded key, try to extract
        // just the modulus and the public exponent to build the public key.
        pks = new RSAPublicKeyStructure(seq);
    }
    keySpec = new RSAPublicKeySpec(pks.getModulus(), pks.getPublicExponent());
}

From source file:org.jruby.ext.openssl.x509store.BouncyCastleASN1FormatHandler.java

License:LGPL

private RSAPublicKey readRSAPublicKey(BufferedReader in, String endMarker) throws IOException {
    ByteArrayInputStream bAIS = new ByteArrayInputStream(readBytes(in, endMarker));
    ASN1InputStream ais = new ASN1InputStream(bAIS);
    Object asnObject = ais.readObject();
    ASN1Sequence sequence = (ASN1Sequence) asnObject;
    RSAPublicKeyStructure rsaPubStructure = new RSAPublicKeyStructure(sequence);
    RSAPublicKeySpec keySpec = new RSAPublicKeySpec(rsaPubStructure.getModulus(),
            rsaPubStructure.getPublicExponent());

    try {/*from   ww  w  . j av a  2 s .com*/
        KeyFactory keyFact = KeyFactory.getInstance("RSA");
        return (RSAPublicKey) keyFact.generatePublic(keySpec);
    } catch (NoSuchAlgorithmException e) {
        // ignore
    } catch (InvalidKeySpecException e) {
        // ignore
    }

    return null;
}

From source file:org.opensc.pkcs15.asn1.attr.RSAPublicKeyChoice.java

License:Apache License

public RSAPublicKeyChoice(SubjectPublicKeyInfo spki) {
    this.spki = spki;

    try {//from w  ww . j  ava  2 s  .  c om
        RSAPublicKeyStructure pubKey = new RSAPublicKeyStructure((ASN1Sequence) spki.getPublicKey());

        this.raw = new RSAPublicKeyStructure(pubKey.getModulus(), pubKey.getPublicExponent());
    } catch (IOException e) {
        throw new IllegalArgumentException("invalid info structure in RSA public key");
    }
}

From source file:org.silvertunnel.netlib.layer.tor.util.Encryption.java

License:Open Source License

/**
 * checks signature of PKCS1-padded SHA1 hash of the input
 * //  ww w .j ava  2 s  . c o  m
 * Hint: A different implementation of this method can be found in the svn history revision<=229. 
 * 
 * @param signature
 *            signature to check
 * @param signingKey
 *            public key from signing
 * @param input
 *            byte array, signature is made over
 * 
 * @return true, if the signature is correct
 * 
 */
public static boolean verifySignature(byte[] signature, RSAPublicKeyStructure signingKey, byte[] input) {
    byte[] hash = getDigest(input);

    try {
        RSAKeyParameters myRSAKeyParameters = new RSAKeyParameters(false, signingKey.getModulus(),
                signingKey.getPublicExponent());

        PKCS1Encoding pkcsAlg = new PKCS1Encoding(new RSAEngine());
        pkcsAlg.init(false, myRSAKeyParameters);

        byte[] decryptedSignature = pkcsAlg.processBlock(signature, 0, signature.length);

        return Encoding.arraysEqual(hash, decryptedSignature);

    } catch (Exception e) {
        log.log(Level.WARNING, "unexpected", e);
        return false;
    }
}

From source file:org.silvertunnel.netlib.layer.tor.util.Encryption.java

License:Open Source License

/**
 * checks row signature//from   w  w  w  .  jav a  2  s.  co  m
 * 
 * @param signature
 *            signature to check
 * @param signingKey
 *            public key from signing
 * @param input
 *            byte array, signature is made over
 * 
 * @return true, if the signature is correct
 * 
 */
public static boolean verifySignatureXXXX(byte[] signature, RSAPublicKeyStructure signingKey, byte[] input) {

    byte[] hash = getDigest(input);
    try {
        Signature sig = Signature.getInstance("SHA1withRSA");
        KeyFactory keyFactory = KeyFactory.getInstance("RSA");
        RSAPublicKeySpec keySpec = new RSAPublicKeySpec(signingKey.getModulus(),
                signingKey.getPublicExponent());
        PublicKey pubKey = keyFactory.generatePublic(keySpec);
        sig.initVerify(pubKey);
        sig.update(input);
        log.info("");
        log.info(" HERE -> " + sig.verify(signature));

        RSAKeyParameters myRSAKeyParameters = new RSAKeyParameters(false, signingKey.getModulus(),
                signingKey.getPublicExponent());
        RSAEngine rsaAlg = new RSAEngine();
        rsaAlg.init(false, myRSAKeyParameters);
        byte[] decryptedSignature = rsaAlg.processBlock(signature, 0, signature.length);
        log.info(" inpu = " + Encoding.toHexString(input));
        log.info(" hash = " + Encoding.toHexString(hash));
        log.info("");
        log.info(" sign = " + Encoding.toHexString(signature));
        log.info(" decr = " + Encoding.toHexString(decryptedSignature));

        return Encoding.arraysEqual(hash, decryptedSignature);

    } catch (Exception e) {
        log.log(Level.WARNING, "unexpected", e);
        return false;
    }
}

From source file:org.silvertunnel.netlib.layer.tor.util.Encryption.java

License:Open Source License

/**
 * makes RSA public key from bin byte array
 * // w w  w .  j a v a2s.  c o m
 * @param s
 *            string that contais the key
 * @return
 * @see JCERSAPublicKey
 */
public static RSAPublicKey extractBinaryRSAKey(byte[] b) {
    RSAPublicKey theKey;

    try {
        ASN1InputStream ais = new ASN1InputStream(b);
        Object asnObject = ais.readObject();
        ASN1Sequence sequence = (ASN1Sequence) asnObject;
        RSAPublicKeyStructure tempKey = new RSAPublicKeyStructure(sequence);
        theKey = getRSAPublicKey(tempKey.getModulus(), tempKey.getPublicExponent());

    } catch (IOException e) {
        log.warning("Caught exception:" + e.getMessage());
        theKey = null;
    }

    return theKey;
}

From source file:TorJava.Common.Encryption.java

License:Open Source License

/**
 * checks signature of PKCS1-padded SHA1 hash of the input
 * // w w w . j  a  va  2s .  co m
 * @param signature
 *            signature to check
 * @param signingKey
 *            public key from signing
 * @param input
 *            byte array, signature is made over
 * 
 * @return true, if the signature is correct
 * 
 */
public static boolean verifySignature(byte[] signature, RSAPublicKeyStructure signingKey, byte[] input) {

    byte[] hash = getHash(input);

    try {
        RSAKeyParameters myRSAKeyParameters = new RSAKeyParameters(false, signingKey.getModulus(),
                signingKey.getPublicExponent());

        PKCS1Encoding pkcs_alg = new PKCS1Encoding(new RSAEngine());
        pkcs_alg.init(false, myRSAKeyParameters);

        byte[] decrypted_signature = pkcs_alg.processBlock(signature, 0, signature.length);

        return Encoding.arraysEqual(hash, decrypted_signature);

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

    return false;

}