Example usage for java.security KeyPair getPrivate

List of usage examples for java.security KeyPair getPrivate

Introduction

In this page you can find the example usage for java.security KeyPair getPrivate.

Prototype

public PrivateKey getPrivate() 

Source Link

Document

Returns a reference to the private key component of this key pair.

Usage

From source file:mitm.djigzo.web.pages.dkim.DKIMSettings.java

protected void onValidateForm() throws HierarchicalPropertiesException, WebServiceCheckedException {
    keyPair = StringUtils.trimToNull(keyPair);

    if (keyPair != null) {
        /*/*from   w w  w  . j a va  2s. c  o  m*/
         * Check if the keyPair is really a PEM encoded keypair 
         */
        PEMReader pem = new PEMReader(new StringReader(keyPair));

        Object o = null;

        try {
            o = pem.readObject();
        } catch (IOException e) {
            logKeyPairError("The input is not valid PEM encoded");
        }

        if (o != null) {
            if (!(o instanceof KeyPair)) {
                String clazz = o.getClass().toString();

                if (o instanceof PublicKey) {
                    clazz = "public key";
                } else if (o instanceof PrivateKey) {
                    clazz = "private key";
                }

                logKeyPairError("The input is not a valid key pair but is a " + clazz);
            } else {
                KeyPair keyPair = (KeyPair) o;

                if (keyPair.getPrivate() == null) {
                    logKeyPairError("The private key is missing");
                } else if (keyPair.getPublic() == null) {
                    logKeyPairError("The public key is missing");
                }
            }
        } else {
            logKeyPairError("The input does not contain a valid key pair");
        }
    } else {
        logKeyPairError("The input does not contain a valid key pair");
    }
}

From source file:com.formkiq.core.service.crypto.SecureTokenServiceImpl.java

@Override
public Pair<RSAPublicKey, RSAPrivateKey> initializeToken(final int keysize) throws NoSuchAlgorithmException {

    KeyPairGenerator kpg = KeyPairGenerator.getInstance("RSA");
    kpg.initialize(keysize);/*w  ww .java2 s .c o m*/

    KeyPair kp = kpg.genKeyPair();
    RSAPublicKey publicKey = (RSAPublicKey) kp.getPublic();
    RSAPrivateKey privateKey = (RSAPrivateKey) kp.getPrivate();

    return Pair.of(publicKey, privateKey);
}

From source file:com.formkiq.core.service.propertystore.PropertyStoreDatabase.java

@Override
public void storeKeyPair(final KeyPair key) {

    KeyGenerator keygen = new KeyGenerator();
    String pubkey = keygen.toString(key.getPublic());
    String privkey = keygen.toString(key.getPrivate());

    this.propertyService.save(null, PUBLIC_KEY, pubkey);
    this.propertyService.save(null, PRIVATE_KEY, privkey);
}

From source file:org.apache.usergrid.rest.management.ExternalSSOEnabledIT.java

private void generateKey() {
    KeyPair kp = RsaProvider.generateKeyPair(1024);
    publicKey = kp.getPublic();
    privateKey = kp.getPrivate();
}

From source file:de.dennishoersch.web.chat.Encryption.java

/**
 * @param keyPair// www. jav a2 s  .  c om
 * @param encrypted
 * @return decrypted text
 */
public String decryptRSA(KeyPair keyPair, String encrypted) {
    byte[] encrypted_ = Base64.decodeBase64(encrypted.getBytes());

    return decryptRSA(keyPair.getPrivate(), encrypted_);
}

From source file:com.vmware.admiral.auth.lightwave.pc.X509CertificateHelper.java

private X509Certificate generateCertificate(KeyPair keyPair, String dn, String sigAlg)
        throws OperatorCreationException, CertificateException {
    ContentSigner sigGen = new JcaContentSignerBuilder(sigAlg).build(keyPair.getPrivate());

    Date startDate = new Date(System.currentTimeMillis() - 24 * 60 * 60 * 1000);
    Date endDate = new Date(System.currentTimeMillis() + 365 * 24 * 60 * 60 * 1000);

    X509v3CertificateBuilder v3CertGen = new JcaX509v3CertificateBuilder(new X500Name("CN=" + dn),
            new BigInteger(64, new SecureRandom()), startDate, endDate, new X500Name("CN=" + dn),
            keyPair.getPublic());/*from www  .j  av a2s  . co  m*/

    X509CertificateHolder certHolder = v3CertGen.build(sigGen);
    X509Certificate x509Certificate = new JcaX509CertificateConverter().getCertificate(certHolder);

    return x509Certificate;
}

From source file:com.kuzumeji.platform.standard.SecurityServiceTest.java

@Test
public void testKeyPair() {
    // RSA???/*from  www  .  j a  v  a 2s  . com*/
    final KeyPair keyPair = testee.generateKeyPair();
    final RSAPublicKey publicKey = (RSAPublicKey) keyPair.getPublic();
    final RSAPrivateKey privateKey = (RSAPrivateKey) keyPair.getPrivate();
    LOG.debug("?->{}", dumpKeyPair(publicKey));
    LOG.debug("?->{}", dumpKeyPair(privateKey));
    // RSA???/
    testee.saveKeyPair("testee", keyPair);
    final KeyPair keyPair2 = testee.loadKeyPair("testee");
    assertThat(keyPair2.getPublic().getAlgorithm(), is(publicKey.getAlgorithm()));
    assertThat(keyPair2.getPublic().getFormat(), is(publicKey.getFormat()));
    assertThat(keyPair2.getPublic().getEncoded(), is(publicKey.getEncoded()));
    assertThat(keyPair2.getPrivate().getAlgorithm(), is(privateKey.getAlgorithm()));
    assertThat(keyPair2.getPrivate().getFormat(), is(privateKey.getFormat()));
    assertThat(keyPair2.getPrivate().getEncoded(), is(privateKey.getEncoded()));
    // ???(??)
    final File file = testee.savePublicKeyFile(publicKey);
    LOG.debug("? : {}", file.getPath());
}

From source file:com.xk72.cocoafob.LicenseGenerator.java

private void initKeys(InputStream keyInputStream) throws IOException {
    Object readKey = readKey(keyInputStream);
    if (readKey instanceof KeyPair) {
        KeyPair keyPair = (KeyPair) readKey;
        privateKey = (DSAPrivateKey) keyPair.getPrivate();
        publicKey = (DSAPublicKey) keyPair.getPublic();
    } else if (readKey instanceof DSAPublicKey) {
        publicKey = (DSAPublicKey) readKey;
    } else {//from   w  ww.j a  v  a2  s.co m
        throw new IllegalArgumentException(
                "The supplied key stream didn't contain a public or private key: " + readKey.getClass());
    }
}

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

public static String getJwkPrivate(KeyPair kp) {
    try {//from   w  w  w .j  a v a  2s  .co  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: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   www .  ja v  a 2 s .c  o m*/
    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()));
}