Example usage for java.security.spec RSAPublicKeySpec getModulus

List of usage examples for java.security.spec RSAPublicKeySpec getModulus

Introduction

In this page you can find the example usage for java.security.spec RSAPublicKeySpec getModulus.

Prototype

public BigInteger getModulus() 

Source Link

Document

Returns the modulus.

Usage

From source file:com.poscoict.license.service.BoardService.java

public Map<String, Object> passwordPop(HttpSession session) throws Exception {
    logger.info("get passwordPopForm");
    Map<String, Object> map = new HashMap<String, Object>();

    KeyPairGenerator generator = KeyPairGenerator.getInstance("RSA");
    generator.initialize(2048);//from   w  ww.j av a2  s . co  m

    KeyPair keyPair = generator.genKeyPair();
    KeyFactory keyFactory = KeyFactory.getInstance("RSA");

    PublicKey publicKey = keyPair.getPublic();
    PrivateKey privateKey = keyPair.getPrivate();

    // ? ? ??  ? .
    session.setAttribute("__rsaPrivateKey__", privateKey);

    //  ?  JavaScript RSA ?? .
    RSAPublicKeySpec publicSpec = (RSAPublicKeySpec) keyFactory.getKeySpec(publicKey, RSAPublicKeySpec.class);

    map.put("publicKeyModulus", publicSpec.getModulus().toString(16));
    map.put("publicKeyExponent", publicSpec.getPublicExponent().toString(16));
    logger.info("return passwordPopForm");
    return map;
}

From source file:org.dasein.cloud.google.compute.server.ServerSupport.java

private JSONObject jsonEncode(KeyPair keys) throws InternalException {
    JSONObject returnJson = new JSONObject();
    try {//ww  w  .ja  v  a 2 s .  c o  m
        KeyFactory factory = KeyFactory.getInstance("RSA");

        RSAPublicKeySpec pubSpec = factory.getKeySpec(keys.getPublic(), RSAPublicKeySpec.class);

        BigInteger modulus = pubSpec.getModulus();
        BigInteger exponent = pubSpec.getPublicExponent();

        BaseEncoding stringEncoder = BaseEncoding.base64();

        // Strip out the leading 0 byte in the modulus.
        byte[] arr = Arrays.copyOfRange(modulus.toByteArray(), 1, modulus.toByteArray().length);

        returnJson.put("modulus", stringEncoder.encode(arr).replaceAll("\n", ""));
        returnJson.put("exponent", stringEncoder.encode(exponent.toByteArray()).replaceAll("\n", ""));
    } catch (Exception e) {
        throw new InternalException(e);
    }

    return returnJson;
}