Example usage for java.security.interfaces RSAPrivateKey getPrivateExponent

List of usage examples for java.security.interfaces RSAPrivateKey getPrivateExponent

Introduction

In this page you can find the example usage for java.security.interfaces RSAPrivateKey getPrivateExponent.

Prototype

public BigInteger getPrivateExponent();

Source Link

Document

Returns the private exponent.

Usage

From source file:de.pawlidi.openaletheia.generator.KeyGenerator.java

public static boolean flushPrivateKeySpec(final String directory, RSAPrivateKey privateKey) {
    if (StringUtils.isBlank(directory) || !new File(directory).isDirectory() || privateKey == null) {
        return false;
    }/*from   ww  w.j a v a  2 s .c om*/
    return writeKeySpec(new File(directory, PRIVATE_KEYSPEC_FILE), privateKey.getModulus(),
            privateKey.getPrivateExponent());
}

From source file:com.github.aynu.mosir.core.standard.util.SecurityHelper.java

/**
 * RSA???/*from w w w  . j ava  2 s  . com*/
 * <dl>
 * <dt>?
 * <dd>RSA??????2048??????
 * </dl>
 * @return RSA?
 */
public static KeyPair createKeyPair() {
    try {
        final KeyPairGenerator generator = KeyPairGenerator.getInstance(ALGO_KEY);
        generator.initialize(2048);
        final KeyPair pair = generator.generateKeyPair();
        if (LOG.isDebugEnabled()) {
            final RSAPublicKey publicKey = (RSAPublicKey) pair.getPublic();
            final RSAPrivateKey privateKey = (RSAPrivateKey) pair.getPrivate();
            LOG.debug("public-modulus={}", Base64.encodeBase64String(publicKey.getModulus().toByteArray()));
            LOG.debug("public-exponent={}",
                    Base64.encodeBase64String(publicKey.getPublicExponent().toByteArray()));
            LOG.debug("private-modulus={}", Base64.encodeBase64String(privateKey.getModulus().toByteArray()));
            LOG.debug("private-exponent={}",
                    Base64.encodeBase64String(privateKey.getPrivateExponent().toByteArray()));
        }
        return pair;
    } catch (final NoSuchAlgorithmException e) {
        throw new StandardRuntimeException(e);
    }
}

From source file:com.github.aynu.yukar.framework.util.SecurityHelper.java

/**
 * RSA???/*  ww w. j a v a  2 s  .  c o  m*/
 * <dl>
 * <dt>?
 * <dd>RSA??????2048??????
 * </dl>
 * @return RSA?
 */
public static KeyPair createKeyPair() {
    try {
        final KeyPairGenerator generator = KeyPairGenerator.getInstance("RSA");
        generator.initialize(2048);
        final KeyPair pair = generator.generateKeyPair();
        if (LOG.isDebugEnabled()) {
            final RSAPublicKey publicKey = (RSAPublicKey) pair.getPublic();
            final RSAPrivateKey privateKey = (RSAPrivateKey) pair.getPrivate();
            LOG.debug("public-modulus={}", Base64.encodeBase64String(publicKey.getModulus().toByteArray()));
            LOG.debug("public-exponent={}",
                    Base64.encodeBase64String(publicKey.getPublicExponent().toByteArray()));
            LOG.debug("private-modulus={}", Base64.encodeBase64String(privateKey.getModulus().toByteArray()));
            LOG.debug("private-exponent={}",
                    Base64.encodeBase64String(privateKey.getPrivateExponent().toByteArray()));
        }
        return pair;
    } catch (final NoSuchAlgorithmException e) {
        throw new StandardRuntimeException(e);
    }
}

From source file:mitm.common.security.certificate.GenerateKeyPairs.java

private void writeKeyPair(KeyPair keyPair) throws IOException {
    System.out.println("Keypair:");

    RSAPrivateKey privateKey = (RSAPrivateKey) keyPair.getPrivate();
    RSAPublicKey publicKey = (RSAPublicKey) keyPair.getPublic();

    System.out.println("Modulus:");
    System.out.println(bigIntToString(privateKey.getModulus()));

    System.out.println();//from   w w  w  . ja va2 s .  com
    System.out.println("Private exponent:");
    System.out.println(bigIntToString(privateKey.getPrivateExponent()));

    System.out.println();
    System.out.println("Public exponent:");
    System.out.println(bigIntToString(publicKey.getPublicExponent()));

    System.out.println();
    System.out.println("Encoded public key:");
    System.out.println(bytesToHex(keyPair.getPublic().getEncoded()));

    System.out.println();
    System.out.println("Encoded private key:");
    System.out.println(bytesToHex(keyPair.getPrivate().getEncoded()));

    System.out.println();
    System.out.println("Serial number:");
    System.out.println(bigIntToString(serialNumberGenerator.generate()));
}

From source file:com.thoughtworks.go.server.util.HttpTestUtil.java

private KeyPair generateKeyPair() {
    try {//from   w ww  .ja va  2 s. com
        KeyPair seed = KeyPairGenerator.getInstance("RSA", "BC").generateKeyPair();
        RSAPrivateKey privateSeed = (RSAPrivateKey) seed.getPrivate();
        RSAPublicKey publicSeed = (RSAPublicKey) seed.getPublic();
        KeyFactory fact = KeyFactory.getInstance("RSA", "BC");
        RSAPrivateKeySpec privateKeySpec = new RSAPrivateKeySpec(privateSeed.getModulus(),
                privateSeed.getPrivateExponent());
        RSAPublicKeySpec publicKeySpec = new RSAPublicKeySpec(publicSeed.getModulus(),
                publicSeed.getPublicExponent());
        return new KeyPair(fact.generatePublic(publicKeySpec), fact.generatePrivate(privateKeySpec));
    } catch (Exception e) {
        throw new RuntimeException(e);
    }
}

From source file:org.cesecore.keys.util.KeyTools.java

/**
 * Test if a private key is extractable (could be stored).
 * /*  ww  w.java2s.  com*/
 * @param privK
 *            key to test.
 * @return true if the key is extractable.
 */
public static boolean isPrivateKeyExtractable(final PrivateKey privK) {
    if (privK instanceof RSAPrivateKey) {
        final RSAPrivateKey rsa = (RSAPrivateKey) privK;
        final BigInteger result = rsa.getPrivateExponent();
        return result != null && result.bitLength() > 0;
    }
    if (privK instanceof ECPrivateKey) {
        final ECPrivateKey ec = (ECPrivateKey) privK;
        final BigInteger result = ec.getS();
        return result != null && result.bitLength() > 0;
    }
    if (privK instanceof DHPrivateKey) {
        final DHPrivateKey dh = (DHPrivateKey) privK;
        final BigInteger result = dh.getX();
        return result != null && result.bitLength() > 0;
    }
    if (privK instanceof DSAPrivateKey) {
        final DSAPrivateKey dsa = (DSAPrivateKey) privK;
        final BigInteger result = dsa.getX();
        return result != null && result.bitLength() > 0;
    }
    return false;
}