Example usage for java.security.interfaces RSAPublicKey getModulus

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

Introduction

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

Prototype

public BigInteger getModulus();

Source Link

Document

Returns the modulus.

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

private static byte[] getPKBytes(PublicKey pk) {
    if (pk instanceof RSAPublicKey) {
        RSAPublicKey k = (RSAPublicKey) pk;
        return k.getModulus().toByteArray();
    } else if (pk instanceof DSAPublicKey) {
        DSAPublicKey k = (DSAPublicKey) pk;
        return k.getY().toByteArray();
    }//from   www . ja  v a  2s  .c  o m
    return null;
}

From source file:Main.java

public static BigInteger computeUnblindSignature(BigInteger r, BigInteger bs)
        throws UnsupportedEncodingException {
    RSAPublicKey pubKey = (RSAPublicKey) getPublicKey(public_key);
    BigInteger n = pubKey.getModulus();

    BigInteger s = r.modInverse(n).multiply(bs).mod(n);

    return s;// w  w  w  .  j av  a 2  s .co m
}

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.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);
        }//from   w  w  w .  jav  a2 s .  c om
    }
    return new VerificationKeyResponse(result);
}

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:VerifyDescriptors.java

private static boolean verifySignature(String digest, String signature, String signingKey) throws Exception {
    byte[] signatureBytes = Base64.decodeBase64(signature.substring(0 + "-----BEGIN SIGNATURE-----\n".length(),
            signature.length() - "-----END SIGNATURE-----\n".length()).replaceAll("\n", ""));
    RSAPublicKey rsaSigningKey = (RSAPublicKey) new PEMReader(new StringReader(signingKey)).readObject();
    RSAKeyParameters rsakp = new RSAKeyParameters(false, rsaSigningKey.getModulus(),
            rsaSigningKey.getPublicExponent());
    PKCS1Encoding pe = new PKCS1Encoding(new RSAEngine());
    pe.init(false, rsakp);/*from w w  w  .j  a  v a  2s  . c  o m*/
    byte[] decryptedSignatureDigest = pe.processBlock(signatureBytes, 0, signatureBytes.length);
    String decryptedSignatureDigestString = Hex.encodeHexString(decryptedSignatureDigest);
    return decryptedSignatureDigestString.equalsIgnoreCase(digest);
}

From source file:tor.TorCrypto.java

public static byte[] publicKeyToASN1(RSAPublicKey pk) throws IOException {
    byte[] modulus = pk.getModulus().toByteArray();
    byte[] pubexp = pk.getPublicExponent().toByteArray();

    ByteBuffer inner = ByteBuffer.allocate(1024);
    inner.put((byte) 2); // Integer
    inner.put((byte) 0x81); // one byte size
    inner.put((byte) modulus.length); // one byte size
    inner.put(modulus);/*from  w w  w  .  j a v  a 2 s .  c  om*/

    inner.put((byte) 2); // Integer
    //inner.put((byte)0x81); // one byte size
    inner.put((byte) pubexp.length); // one byte size
    inner.put(pubexp);
    inner.flip();

    ByteBuffer outer = ByteBuffer.allocate(1024);
    outer.put((byte) 0x30); // SEQUENCE
    outer.put((byte) 0x81); // one byte size
    outer.put((byte) inner.limit()); // one byte size
    outer.put(inner);
    outer.flip();

    byte asn[] = new byte[outer.limit()];
    outer.get(asn);
    return asn;
}