Example usage for java.security.spec RSAPublicKeySpec getPublicExponent

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

Introduction

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

Prototype

public BigInteger getPublicExponent() 

Source Link

Document

Returns the public exponent.

Usage

From source file:com.cliqset.magicsig.util.KeyGen.java

public static void main(String[] args) {
    try {/*  ww  w  . j  a v a 2  s  . c  om*/
        FileOutputStream fos = new FileOutputStream(fileName);
        for (int x = 0; x < numKeys; x++) {
            fos.write((x + " RSA.").getBytes("ASCII"));
            KeyPairGenerator keyPairGenerator = KeyPairGenerator.getInstance("RSA");
            keyPairGenerator.initialize(keySize);
            KeyPair keyPair = keyPairGenerator.genKeyPair();

            KeyFactory keyFactory = KeyFactory.getInstance("RSA");
            RSAPublicKeySpec publicSpec = keyFactory.getKeySpec(keyPair.getPublic(), RSAPublicKeySpec.class);
            RSAPrivateKeySpec privateSpec = keyFactory.getKeySpec(keyPair.getPrivate(),
                    RSAPrivateKeySpec.class);

            fos.write(Base64.encodeBase64URLSafe(getBytes(publicSpec.getModulus())));

            fos.write(".".getBytes("ASCII"));

            fos.write(Base64.encodeBase64URLSafe(getBytes(publicSpec.getPublicExponent())));

            fos.write(".".getBytes("ASCII"));

            fos.write(Base64.encodeBase64URLSafe(getBytes(privateSpec.getPrivateExponent())));

            fos.write("\n".getBytes("ASCII"));

            System.out.println(x);
        }
        fos.close();
    } catch (Exception e) {
        e.printStackTrace();
    }
}

From source file:org.opentravel.schemacompiler.security.GenerateEncryptionKeys.java

/**
 * Generates the public and private key files used for encrypting and decrypting passwords.
 *//*from  w  ww  . ja va 2  s . c o  m*/
public static void generateKeyFiles() throws Exception {
    File publicKeyFile = new File(System.getProperty("user.dir"),
            "/src/main/resources" + PasswordHelper.PUBLIC_KEYFILE);
    File privateKeyFile = new File(System.getProperty("user.dir"),
            "/src/main/resources" + PasswordHelper.PRIVATE_KEYFILE);
    KeyPairGenerator keyGenerator = KeyPairGenerator.getInstance(PasswordHelper.ENCRYPTION_ALGORITHM);

    keyGenerator.initialize(ENCRYPTION_KEY_SIZE);

    KeyPair keyPair = keyGenerator.generateKeyPair();
    KeyFactory fact = KeyFactory.getInstance(PasswordHelper.ENCRYPTION_ALGORITHM);
    RSAPublicKeySpec publicKeySpec = fact.getKeySpec(keyPair.getPublic(), RSAPublicKeySpec.class);
    RSAPrivateKeySpec privateKeySpec = fact.getKeySpec(keyPair.getPrivate(), RSAPrivateKeySpec.class);

    System.out
            .println("Public Key : " + publicKeySpec.getModulus() + " / " + publicKeySpec.getPublicExponent());
    System.out.println(
            "Private Key: " + privateKeySpec.getModulus() + " / " + privateKeySpec.getPrivateExponent());

    writeKeyFile(publicKeyFile, publicKeySpec.getModulus(), publicKeySpec.getPublicExponent());
    writeKeyFile(privateKeyFile, privateKeySpec.getModulus(), privateKeySpec.getPrivateExponent());
}

From source file:org.umit.icm.mobile.utils.RSACrypto.java

public static RSAKey getPublicKeyIntegers(PublicKey publicKey)
        throws NoSuchAlgorithmException, InvalidKeySpecException {
    KeyFactory keyFactory = KeyFactory.getInstance("RSA");
    RSAPublicKeySpec publicKeySpec = keyFactory.getKeySpec(publicKey, RSAPublicKeySpec.class);
    RSAKey rsaKey = RSAKey.newBuilder().setExp(publicKeySpec.getPublicExponent().toString())
            .setMod(publicKeySpec.getModulus().toString()).build();

    return rsaKey;
}

From source file:org.umit.icm.mobile.test.utils.RSACryptoTests.java

public void testPublicReadWrite() throws Throwable {
    keyPair = RSACrypto.generateKey();/*  w  w  w . j a  v  a  2s .  c o  m*/
    KeyFactory keyFactory = KeyFactory.getInstance("RSA");
    RSAPublicKeySpec publicKeySpec = keyFactory.getKeySpec(keyPair.getPublic(), RSAPublicKeySpec.class);
    RSACrypto.saveKey("rsaKey.pub", publicKeySpec.getModulus(), publicKeySpec.getPublicExponent());
    Assert.assertEquals(RSACrypto.readPublicKey("rsaKey.pub"), keyPair.getPublic());
}

From source file:com.cellngine.crypto.RSACipher.java

@Override
public byte[] getPublicKey() {
    if (this.publicKey == null) {
        return null;
    }/*from  w  ww.  j  a  v  a 2s  .  c  om*/

    final RSAPublicKeySpec spec = this.getKeySpec(this.publicKey, RSAPublicKeySpec.class);

    return this.encode(spec.getModulus(), spec.getPublicExponent());
}

From source file:cloud.google.com.windows.example.ExampleCode.java

@SuppressWarnings("unchecked")
private JSONObject jsonEncode(KeyPair keys) throws NoSuchAlgorithmException, InvalidKeySpecException {
    KeyFactory factory = KeyFactory.getInstance("RSA");

    // Get the RSA spec for key manipulation.
    RSAPublicKeySpec pubSpec = factory.getKeySpec(keys.getPublic(), RSAPublicKeySpec.class);

    // Extract required parts of the key.
    BigInteger modulus = pubSpec.getModulus();
    BigInteger exponent = pubSpec.getPublicExponent();

    // Grab an encoder for the modulus and exponent to encode using RFC 3548;
    // Java SE 7 requires an external library (Google's Guava used here)
    // Java SE 8 has a built-in Base64 class that can be used instead. Apache also has an RFC 3548
    // encoder.// www . ja  va2s  .com
    BaseEncoding stringEncoder = BaseEncoding.base64();

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

    JSONObject returnJson = new JSONObject();

    // Encode the modulus, add to returned JSON object.
    String modulusString = stringEncoder.encode(arr).replaceAll("\n", "");
    returnJson.put("modulus", modulusString);

    // Encode exponent, add to returned JSON object.
    String exponentString = stringEncoder.encode(exponent.toByteArray()).replaceAll("\n", "");
    returnJson.put("exponent", exponentString);

    return returnJson;
}

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 www.  ja  v a  2s  .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 {//from  w  w w.  j  a va  2s .  co  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;
}