Example usage for java.security.spec RSAPrivateKeySpec getPrivateExponent

List of usage examples for java.security.spec RSAPrivateKeySpec getPrivateExponent

Introduction

In this page you can find the example usage for java.security.spec RSAPrivateKeySpec getPrivateExponent.

Prototype

public BigInteger getPrivateExponent() 

Source Link

Document

Returns the private exponent.

Usage

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

public static void main(String[] args) {
    try {/*ww w  .  java  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:Main.java

public static void savePrivateKey(RSAPrivateKeySpec privateKey, Context context) {
    BigInteger privateModBI = privateKey.getModulus();
    BigInteger privateExpBI = privateKey.getPrivateExponent();

    byte[] privateModBA = privateModBI.toByteArray();// Base64.encodeInteger(pubModBI);
    // // for some
    // strange reason
    // this throws
    // NoSuchMethodError
    byte[] privateExpBA = privateExpBI.toByteArray();// Base64.encodeInteger(pubExpBI);

    try {// w  ww  .  j av  a2  s. co m
        String privateModBase64Str = Base64.encodeToString(privateModBA, Base64.NO_WRAP);
        String privateExpBase64Str = Base64.encodeToString(privateExpBA, Base64.NO_WRAP);
        savePrivateKey(privateModBase64Str, privateExpBase64Str, context);

    } catch (NoSuchMethodError e) {
        Log.e(TAG, "Base64.encode() method not available", e);
    }
}

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

/**
 * Generates the public and private key files used for encrypting and decrypting passwords.
 *///from  ww w .  ja  va2  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:Main.java

public static String getJwkPrivate(KeyPair kp) {
    try {/* w w  w  .j  ava2  s  .c  om*/
        JSONObject jk = new JSONObject();
        jk.put("kty", "RSA");
        // generate random kid 
        SecureRandom random = new SecureRandom();
        String kid = new BigInteger(130, random).toString(32);
        jk.put("kid", kid);
        jk.put("e", "AQAB");

        KeyFactory kfactory = KeyFactory.getInstance("RSA");

        RSAPrivateKeySpec privkspec = (RSAPrivateKeySpec) kfactory.getKeySpec(kp.getPrivate(),
                RSAPrivateKeySpec.class);
        RSAPublicKeySpec pubkspec = (RSAPublicKeySpec) kfactory.getKeySpec(kp.getPublic(),
                RSAPublicKeySpec.class);

        // Log.d("getJwkPrivate n",pubkspec.getPublicExponent().toString());
        // Log.d("getJwkPrivate d",privkspec.getPrivateExponent().toString());

        jk.put("n", encodeB64(pubkspec.getModulus().toByteArray()));
        jk.put("d", encodeB64(privkspec.getPrivateExponent().toByteArray()));
        JSONArray ja = new JSONArray();
        ja.put(jk);
        JSONObject jo = new JSONObject();
        jo.put("keys", ja);

        return jo.toString();

    } catch (Exception e) {
        e.printStackTrace();
    }
    return null;
}

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

public void testPrivateReadWrite() throws Throwable {
    keyPair = RSACrypto.generateKey();//from w  w  w  .  j a  v a2 s.  c o  m
    KeyFactory keyFactory = KeyFactory.getInstance("RSA");
    RSAPrivateKeySpec privateKeySpec = keyFactory.getKeySpec(keyPair.getPrivate(), RSAPrivateKeySpec.class);
    RSACrypto.saveKey("rsaKey.priv", privateKeySpec.getModulus(), privateKeySpec.getPrivateExponent());
    Assert.assertEquals(RSACrypto.readPrivateKey("rsaKey.priv"), keyPair.getPrivate());
}

From source file:com.orange.oidc.tim.service.KryptoUtils.java

public static String getJwkPrivate(KeyPair kp) {
    try {/*from w w w .jav a  2s. c  o  m*/
        JSONObject jk = new JSONObject();
        jk.put("kty", "RSA");
        // generate random kid for tim_app_key
        SecureRandom random = new SecureRandom();
        String kid = new BigInteger(130, random).toString(32);
        jk.put("kid", kid);
        jk.put("e", "AQAB");

        KeyFactory kfactory = KeyFactory.getInstance("RSA");

        RSAPrivateKeySpec privkspec = (RSAPrivateKeySpec) kfactory.getKeySpec(kp.getPrivate(),
                RSAPrivateKeySpec.class);
        RSAPublicKeySpec pubkspec = (RSAPublicKeySpec) kfactory.getKeySpec(kp.getPublic(),
                RSAPublicKeySpec.class);

        // Log.d("getJwkPrivate n",pubkspec.getPublicExponent().toString());
        // Log.d("getJwkPrivate d",privkspec.getPrivateExponent().toString());

        jk.put("n", encodeB64(pubkspec.getModulus().toByteArray()));
        jk.put("d", encodeB64(privkspec.getPrivateExponent().toByteArray()));
        JSONArray ja = new JSONArray();
        ja.put(jk);
        JSONObject jo = new JSONObject();
        jo.put("keys", ja);

        return jo.toString();

    } catch (Exception e) {
        e.printStackTrace();
    }
    return null;
}

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

@Override
public byte[] getPrivateKey() {
    if (this.privateKey == null) {
        return null;
    }/*from ww w .  j  a va  2s.  c  o m*/

    final RSAPrivateKeySpec spec = this.getKeySpec(this.privateKey, RSAPrivateKeySpec.class);

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