Example usage for java.security.spec RSAPrivateKeySpec getModulus

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

Introduction

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

Prototype

public BigInteger getModulus() 

Source Link

Document

Returns the modulus.

Usage

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 {/*from w w  w  . ja v  a  2  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:com.cellngine.crypto.RSACipher.java

@Override
public byte[] getPrivateKey() {
    if (this.privateKey == null) {
        return null;
    }//from  www .  ja v a2  s .  c om

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

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

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

/**
 * Generates the public and private key files used for encrypting and decrypting passwords.
 *//*from   www  .j av  a 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.test.utils.RSACryptoTests.java

public void testPrivateReadWrite() throws Throwable {
    keyPair = RSACrypto.generateKey();//from   w ww  . jav  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());
}