Example usage for java.security.interfaces RSAPublicKey getPublicExponent

List of usage examples for java.security.interfaces RSAPublicKey getPublicExponent

Introduction

In this page you can find the example usage for java.security.interfaces RSAPublicKey getPublicExponent.

Prototype

public BigInteger getPublicExponent();

Source Link

Document

Returns the public exponent.

Usage

From source file:com.glaf.core.security.RSAUtils.java

public static void main(String[] args) {
    RSAPublicKey publicKey = RSAUtils.getDefaultPublicKey();
    System.out.println(new String(Hex.encodeHex(publicKey.getModulus().toByteArray())));
    System.out.println(new String(Hex.encodeHex(publicKey.getPublicExponent().toByteArray())));
    System.out.println(RSAUtils.decryptStringByJs(
            "2d7754804ecfb3c3e6fb7d12cdf439036f1d8e2ad34c5a6467bd6f1c165bb47f0fa57134b013aba49be5edf5231c2f7b611af5e974b521ea715b1a6bad6cfbf4ba8e0886c5fe1ce903d30ae0c8cd5f422860d67fa4fd3e2a8fc7872c6b052a6c8f480cfde5e147d959f3db5032767c393ff271742f66be7657290a5de218e375"));
}

From source file:Main.java

public static boolean RsaPublicKeyComparer(RSAPublicKey key1, RSAPublicKey key2) {
    return key1.getPublicExponent().compareTo(key2.getPublicExponent()) == 0
            && key1.getModulus().compareTo(key2.getModulus()) == 0;
}

From source file:org.cloudfoundry.identity.uaa.oauth.jwk.JsonWebKeyHelper.java

public static JsonWebKey fromPEMPrivateKey(String key) {
    KeyPair pair = KeyInfo.parseKeyPair(key);
    RSAPublicKey rsaKey = (RSAPublicKey) pair.getPublic();
    BigInteger modulus = rsaKey.getModulus();
    BigInteger exponent = rsaKey.getPublicExponent();
    Map<String, Object> properties = new HashMap();
    properties.put("n", base64.encodeAsString(modulus.toByteArray()));
    properties.put("e", base64.encodeAsString(exponent.toByteArray()));
    properties.put("kty", "RSA");
    properties.put("use", sig.name());
    properties.put("value", KeyInfo.pemEncodePublicKey(rsaKey));
    return new JsonWebKey(properties);
}

From source file:org.picketbox.json.key.RSAKey.java

public static RSAKey convert(RSAPublicKey publicKey) throws ProcessingException {
    BigInteger modulus = publicKey.getModulus();
    BigInteger exponent = publicKey.getPublicExponent();

    RSAKey rsaKey = new RSAKey();
    rsaKey.setMod(PicketBoxJSONUtil.b64Encode(modulus.toByteArray()));
    rsaKey.setExp(PicketBoxJSONUtil.b64Encode(exponent.toByteArray()));
    return rsaKey;
}

From source file:org.apache.whirr.util.KeyPair.java

private static byte[] encodePublicKey(RSAPublicKey key) throws IOException {
    ByteArrayOutputStream keyBlob = new ByteArrayOutputStream();
    write("ssh-rsa".getBytes(), keyBlob);
    write(key.getPublicExponent().toByteArray(), keyBlob);
    write(key.getModulus().toByteArray(), keyBlob);

    ByteArrayOutputStream out = new ByteArrayOutputStream();
    out.write("ssh-rsa ".getBytes());
    out.write(Base64.encodeBase64(keyBlob.toByteArray()));

    return out.toByteArray();
}

From source file:com.axelor.apps.account.ebics.certificate.KeyUtil.java

/**
 * Returns the digest value of a given public key.
 * //from  w w  w. j  a  v a  2 s. c  o m
 * 
 * <p>In Version H003? of the EBICS protocol the ES of the financial:
 * 
 * <p>The SHA-256 hash values of the financial institution's public keys for X002 and E002 are
 * composed by concatenating the exponent with a blank character and the modulus in hexadecimal
 * representation (using lower case letters) without leading zero (as to the hexadecimal
 * representation). The resulting string has to be converted into a byte array based on US ASCII
 * code.
 * 
 * @param publicKey the public key
 * @return the digest value
 * @throws EbicsException
 */
public static byte[] getKeyDigest(RSAPublicKey publicKey) throws AxelorException {
    String modulus;
    String exponent;
    String hash;
    byte[] digest;

    exponent = Hex.encodeHexString(publicKey.getPublicExponent().toByteArray());
    modulus = Hex.encodeHexString(removeFirstByte(publicKey.getModulus().toByteArray()));
    hash = exponent + " " + modulus;

    if (hash.charAt(0) == '0') {
        hash = hash.substring(1);
    }

    try {
        digest = MessageDigest.getInstance("SHA-256", "BC").digest(hash.getBytes("US-ASCII"));
    } catch (GeneralSecurityException e) {
        throw new AxelorException(e.getMessage(), IException.CONFIGURATION_ERROR);
    } catch (UnsupportedEncodingException e) {
        throw new AxelorException(e.getMessage(), IException.CONFIGURATION_ERROR);
    }

    return new String(Hex.encodeHex(digest, false)).getBytes();
}

From source file:org.kopi.ebics.certificate.KeyUtil.java

/**
 * Returns the digest value of a given public key.
 * /*from www  .j av  a 2  s.  com*/
 * 
 * <p>In Version H003? of the EBICS protocol the ES of the financial:
 * 
 * <p>The SHA-256 hash values of the financial institution's public keys for X002 and E002 are
 * composed by concatenating the exponent with a blank character and the modulus in hexadecimal
 * representation (using lower case letters) without leading zero (as to the hexadecimal
 * representation). The resulting string has to be converted into a byte array based on US ASCII
 * code.
 * 
 * @param publicKey the public key
 * @return the digest value
 * @throws EbicsException
 */
public static byte[] getKeyDigest(RSAPublicKey publicKey) throws EbicsException {
    String modulus;
    String exponent;
    String hash;
    byte[] digest;

    exponent = Hex.encodeHexString(publicKey.getPublicExponent().toByteArray());
    modulus = Hex.encodeHexString(removeFirstByte(publicKey.getModulus().toByteArray()));
    hash = exponent + " " + modulus;

    if (hash.charAt(0) == '0') {
        hash = hash.substring(1);
    }

    try {
        digest = MessageDigest.getInstance("SHA-256", "BC").digest(hash.getBytes("US-ASCII"));
    } catch (GeneralSecurityException e) {
        throw new EbicsException(e.getMessage());
    } catch (UnsupportedEncodingException e) {
        throw new EbicsException(e.getMessage());
    }

    return new String(Hex.encodeHex(digest, false)).getBytes();
}

From source file:org.xdi.oxauth.cert.fingerprint.FingerprintHelper.java

private static byte[] getDerEncoding(RSAPublicKey key) throws IOException {
    ByteArrayOutputStream buffer = new ByteArrayOutputStream();
    DataOutputStream dataOutput = new DataOutputStream(buffer);
    writeDataWithLength("ssh-rsa".getBytes(), dataOutput);
    writeDataWithLength(key.getPublicExponent().toByteArray(), dataOutput);
    writeDataWithLength(key.getModulus().toByteArray(), dataOutput);

    return buffer.toByteArray();
}

From source file:org.excalibur.core.util.SecurityUtils2.java

public static String getFingerPrint(RSAPublicKey key) throws IOException {
    try (ByteArrayOutputStream buf = new ByteArrayOutputStream()) {
        write(key.getPublicExponent().toByteArray(), buf);
        write(key.getModulus().toByteArray(), buf);

        return getFingerPrint(buf.toByteArray());
    }/*from  www. ja v  a2 s. com*/
}

From source file:org.cloudfoundry.identity.uaa.oauth.TokenKeyEndpoint.java

public static VerificationKeyResponse getVerificationKeyResponse(KeyInfo key) {
    Map<String, Object> result = new HashMap<>();
    result.put("alg", key.getSigner().algorithm());
    result.put("value", key.getVerifierKey());
    //new values per OpenID and JWK spec
    result.put("use", sig.name());
    result.put("kid", key.getKeyId());
    result.put("kty", key.getType());

    if (key.isAssymetricKey() && "RSA".equals(key.getType())) {

        RSAPublicKey rsaKey = key.getRsaPublicKey();
        if (rsaKey != null) {
            String n = Base64Utils.encodeToUrlSafeString(rsaKey.getModulus().toByteArray());
            String e = Base64Utils.encodeToUrlSafeString(rsaKey.getPublicExponent().toByteArray());
            result.put("n", n);
            result.put("e", e);
        }/*www .  ja v  a 2s .  c  o  m*/
    }
    return new VerificationKeyResponse(result);
}