Example usage for org.bouncycastle.jce.provider JCERSAPublicKey getPublicExponent

List of usage examples for org.bouncycastle.jce.provider JCERSAPublicKey getPublicExponent

Introduction

In this page you can find the example usage for org.bouncycastle.jce.provider JCERSAPublicKey getPublicExponent.

Prototype

public BigInteger getPublicExponent() 

Source Link

Document

return the public exponent.

Usage

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

License:Open Source License

/**
 * makes RSA public key from PEM string/*  w w w  .ja va2s .  c o  m*/
 * 
 * @param s    PEM string that contains the key
 * @return
 * @see JCERSAPublicKey
 */
public static RSAPublicKey extractPublicRSAKey(String s) {
    RSAPublicKey theKey;
    try {
        PEMReader reader = new PEMReader(new StringReader(s));
        Object o = reader.readObject();
        if (!(o instanceof JCERSAPublicKey)) {
            throw new IOException("Encryption.extractPublicRSAKey: no public key found in string '" + s + "'");
        }
        JCERSAPublicKey JCEKey = (JCERSAPublicKey) o;
        theKey = getRSAPublicKey(JCEKey.getModulus(), JCEKey.getPublicExponent());

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

    return theKey;
}

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

License:Open Source License

/**
 * copy from one format to another/*from   www. j  av a  2 s.com*/
 */
public static RSAPublicKey getRSAPublicKey(JCERSAPublicKey jpub) {
    return getRSAPublicKey(jpub.getModulus(), jpub.getPublicExponent());
}

From source file:org.xdi.oxauth.ws.rs.FederationMetadataSignatureTest.java

License:MIT License

@Test
public void test() throws JSONException, NoSuchProviderException, NoSuchAlgorithmException, IOException,
        IllegalBlockSizeException, InvalidKeyException, InvalidKeySpecException, NoSuchPaddingException,
        BadPaddingException, InvalidJwtException {
    final JSONObject jsonHeader = JwtHeader.instance().setType(JwtType.JWS)
            .setAlgorithm(SignatureAlgorithm.RS512).toJsonObject();
    final JSONObject jsonPayload = new JSONObject(TEST_METADATA);

    final KeyPair keyPair = JwtUtil.generateRsaKey();
    final JCERSAPrivateCrtKey jcersaPrivateCrtKey = (JCERSAPrivateCrtKey) keyPair.getPrivate();
    final JCERSAPublicKey jcersaPublicKey = (JCERSAPublicKey) keyPair.getPublic();

    final RSAPrivateKey privateKey = new RSAPrivateKey(jcersaPrivateCrtKey.getModulus(),
            jcersaPrivateCrtKey.getPrivateExponent());
    final RSAPublicKey publicKey = new RSAPublicKey(jcersaPublicKey.getModulus(),
            jcersaPublicKey.getPublicExponent());

    String encodedString = JwtUtil.encodeJwt(jsonHeader, jsonPayload, SignatureAlgorithm.RS512, privateKey);

    System.out.println("Encoded string: " + encodedString);
    String[] parts = encodedString.split("\\.");

    if (parts.length == 3) {
        String encodedHeader = parts[0];
        String encodedPayload = parts[1];
        String encodedSignature = parts[2];

        String header = new String(JwtUtil.base64urldecode(encodedHeader), Util.UTF8_STRING_ENCODING);
        String payload = new String(JwtUtil.base64urldecode(encodedPayload), Util.UTF8_STRING_ENCODING);
        System.out.println("Header: " + header);
        System.out.println("Payload: " + payload);

        byte[] signature = JwtUtil.base64urldecode(encodedSignature);

        final String signingInput = encodedHeader + "." + encodedPayload;

        boolean signatureVerified = JwtUtil
                .verifySignatureRS512(signingInput.getBytes(Util.UTF8_STRING_ENCODING), signature, publicKey);
        assertTrue(signatureVerified, "Invalid signature");
    }/*  ww w.  ja v  a2 s  .  co m*/
}

From source file:org.xdi.oxauth.ws.rs.TokenSignaturesHttpTest.java

License:MIT License

@Test
public void rs256() throws NoSuchProviderException, NoSuchAlgorithmException, SignatureException,
        InvalidKeyException, InvalidKeySpecException, IllegalBlockSizeException, IOException,
        NoSuchPaddingException, BadPaddingException {
    showTitle("rs256");

    // Generate RSA Key
    KeyPair keyPair = JwtUtil.generateRsaKey();
    JCERSAPrivateCrtKey jcersaPrivateCrtKey = (JCERSAPrivateCrtKey) keyPair.getPrivate();
    JCERSAPublicKey jcersaPublicKey = (JCERSAPublicKey) keyPair.getPublic();
    RSAPrivateKey privateKey = new RSAPrivateKey(jcersaPrivateCrtKey.getModulus(),
            jcersaPrivateCrtKey.getPrivateExponent());
    System.out.println("PRIVATE KEY");
    System.out.println("Modulus: " + privateKey.getModulus());
    System.out.println("Private Exponent: " + privateKey.getPrivateExponent());
    RSAPublicKey publicKey = new RSAPublicKey(jcersaPublicKey.getModulus(),
            jcersaPublicKey.getPublicExponent());
    System.out.println("PUBLIC KEY");
    System.out.println("Modulus: " + publicKey.getModulus());
    System.out.println("Public Exponent: " + publicKey.getPublicExponent());

    // Encode message
    String signingInput = "eyJhbGciOiJIUzI1NiJ9.eyJub25jZSI6ICI2Qm9HN1QwR0RUZ2wiLCAiaWRfdG9rZW4iOiB7Im1heF9hZ2UiOiA4NjQwMH0sICJzdGF0ZSI6ICJTVEFURTAiLCAicmVkaXJlY3RfdXJpIjogImh0dHBzOi8vbG9jYWxob3N0L2NhbGxiYWNrMSIsICJ1c2VyaW5mbyI6IHsiY2xhaW1zIjogeyJuYW1lIjogbnVsbH19LCAiY2xpZW50X2lkIjogIkAhMTExMSEwMDA4IUU2NTQuQjQ2MCIsICJzY29wZSI6IFsib3BlbmlkIl0sICJyZXNwb25zZV90eXBlIjogWyJjb2RlIl19";

    byte[] signature = JwtUtil.getSignatureRS256(signingInput.getBytes(), privateKey);
    String encodedSignature = JwtUtil.base64urlencode(signature);
    System.out.println("Encoded Signature: " + encodedSignature);

    // Verify signature
    boolean signatureVerified = JwtUtil.verifySignatureRS256(signingInput.getBytes(), signature, publicKey);
    assertTrue(signatureVerified, "Invalid signature");
}

From source file:org.xdi.oxauth.ws.rs.TokenSignaturesHttpTest.java

License:MIT License

@Test
public void rs384() throws NoSuchProviderException, NoSuchAlgorithmException, SignatureException,
        InvalidKeyException, InvalidKeySpecException, IllegalBlockSizeException, IOException,
        NoSuchPaddingException, BadPaddingException {
    showTitle("rs384");

    // Generate RSA Key
    KeyPair keyPair = JwtUtil.generateRsaKey();
    JCERSAPrivateCrtKey jcersaPrivateCrtKey = (JCERSAPrivateCrtKey) keyPair.getPrivate();
    JCERSAPublicKey jcersaPublicKey = (JCERSAPublicKey) keyPair.getPublic();
    RSAPrivateKey privateKey = new RSAPrivateKey(jcersaPrivateCrtKey.getModulus(),
            jcersaPrivateCrtKey.getPrivateExponent());
    System.out.println("PRIVATE KEY");
    System.out.println("Modulus: " + privateKey.getModulus());
    System.out.println("Private Exponent: " + privateKey.getPrivateExponent());
    RSAPublicKey publicKey = new RSAPublicKey(jcersaPublicKey.getModulus(),
            jcersaPublicKey.getPublicExponent());
    System.out.println("PUBLIC KEY");
    System.out.println("Modulus: " + publicKey.getModulus());
    System.out.println("Public Exponent: " + publicKey.getPublicExponent());

    // Encode message
    String signingInput = "eyJhbGciOiJIUzI1NiJ9.eyJub25jZSI6ICI2Qm9HN1QwR0RUZ2wiLCAiaWRfdG9rZW4iOiB7Im1heF9hZ2UiOiA4NjQwMH0sICJzdGF0ZSI6ICJTVEFURTAiLCAicmVkaXJlY3RfdXJpIjogImh0dHBzOi8vbG9jYWxob3N0L2NhbGxiYWNrMSIsICJ1c2VyaW5mbyI6IHsiY2xhaW1zIjogeyJuYW1lIjogbnVsbH19LCAiY2xpZW50X2lkIjogIkAhMTExMSEwMDA4IUU2NTQuQjQ2MCIsICJzY29wZSI6IFsib3BlbmlkIl0sICJyZXNwb25zZV90eXBlIjogWyJjb2RlIl19";

    byte[] signature = JwtUtil.getSignatureRS384(signingInput.getBytes(), privateKey);
    String encodedSignature = JwtUtil.base64urlencode(signature);
    System.out.println("Encoded Signature: " + encodedSignature);

    // Verify signature
    boolean signatureVerified = JwtUtil.verifySignatureRS384(signingInput.getBytes(), signature, publicKey);
    assertTrue(signatureVerified, "Invalid signature");
}

From source file:org.xdi.oxauth.ws.rs.TokenSignaturesHttpTest.java

License:MIT License

@Test
public void rs512() throws NoSuchProviderException, NoSuchAlgorithmException, SignatureException,
        InvalidKeyException, InvalidKeySpecException, IllegalBlockSizeException, IOException,
        NoSuchPaddingException, BadPaddingException {
    showTitle("rs512");

    // Generate RSA Key
    KeyPair keyPair = JwtUtil.generateRsaKey();
    JCERSAPrivateCrtKey jcersaPrivateCrtKey = (JCERSAPrivateCrtKey) keyPair.getPrivate();
    JCERSAPublicKey jcersaPublicKey = (JCERSAPublicKey) keyPair.getPublic();
    RSAPrivateKey privateKey = new RSAPrivateKey(jcersaPrivateCrtKey.getModulus(),
            jcersaPrivateCrtKey.getPrivateExponent());
    System.out.println("PRIVATE KEY");
    System.out.println("Modulus: " + privateKey.getModulus());
    System.out.println("Private Exponent: " + privateKey.getPrivateExponent());
    RSAPublicKey publicKey = new RSAPublicKey(jcersaPublicKey.getModulus(),
            jcersaPublicKey.getPublicExponent());
    System.out.println("PUBLIC KEY");
    System.out.println("Modulus: " + publicKey.getModulus());
    System.out.println("Public Exponent: " + publicKey.getPublicExponent());

    // Encode message
    String signingInput = "eyJhbGciOiJIUzI1NiJ9.eyJub25jZSI6ICI2Qm9HN1QwR0RUZ2wiLCAiaWRfdG9rZW4iOiB7Im1heF9hZ2UiOiA4NjQwMH0sICJzdGF0ZSI6ICJTVEFURTAiLCAicmVkaXJlY3RfdXJpIjogImh0dHBzOi8vbG9jYWxob3N0L2NhbGxiYWNrMSIsICJ1c2VyaW5mbyI6IHsiY2xhaW1zIjogeyJuYW1lIjogbnVsbH19LCAiY2xpZW50X2lkIjogIkAhMTExMSEwMDA4IUU2NTQuQjQ2MCIsICJzY29wZSI6IFsib3BlbmlkIl0sICJyZXNwb25zZV90eXBlIjogWyJjb2RlIl19";

    byte[] signature = JwtUtil.getSignatureRS512(signingInput.getBytes(), privateKey);
    String encodedSignature = JwtUtil.base64urlencode(signature);
    System.out.println("Encoded Signature: " + encodedSignature);

    // Verify signature
    boolean signatureVerified = JwtUtil.verifySignatureRS512(signingInput.getBytes(), signature, publicKey);
    assertTrue(signatureVerified, "Invalid signature");
}

From source file:TorJava.Common.Encryption.java

License:Open Source License

/**
 * makes RSA public key from string/*from ww  w  .j a va  2 s. c  o  m*/
 * 
 * @param s
 *            string that contais the key
 * @return
 * @see JCERSAPublicKey
 */
public static RSAPublicKeyStructure extractRSAKey(String s) {

    PEMReader reader = new PEMReader(new StringReader(s));
    JCERSAPublicKey JCEKey;
    RSAPublicKeyStructure theKey;

    try {
        Object o = reader.readObject();
        if (!(o instanceof JCERSAPublicKey))
            throw new IOException("Common.extractRSAKey: no public key found for signing key in string '" + s
                    + "' type " + o.getClass().getName());
        JCEKey = (JCERSAPublicKey) o;
        theKey = new RSAPublicKeyStructure(JCEKey.getModulus(), JCEKey.getPublicExponent());

    } catch (IOException e) {
        Logger.logDirectory(Logger.WARNING, "Common.extractRSAKey: Caught exception:" + e.getMessage());
        theKey = null;
    }

    return theKey;
}

From source file:TorJava.Common.Encryption.java

License:Open Source License

/**
 * copy from one format to another/*from   w  w  w. j  a v a2  s .  c  om*/
 */
public static RSAPublicKeyStructure getRSAPublicKeyStructureFromJCERSAPublicKey(JCERSAPublicKey jpub) {
    return new RSAPublicKeyStructure(jpub.getModulus(), jpub.getPublicExponent());
}

From source file:TorJava.Common.Encryption.java

License:Open Source License

public static RSAPrivateKeyStructure getRSAPrivateKeyStructureFromJCERSAPrivateKey(JCERSAPrivateKey key,
        JCERSAPublicKey pubkey) {
    return new RSAPrivateKeyStructure(key.getModulus(), pubkey.getPublicExponent(), key.getPrivateExponent(),
            null, null, null, null, null);
}