Example usage for org.bouncycastle.openpgp.operator.jcajce JcaPGPKeyConverter getPrivateKey

List of usage examples for org.bouncycastle.openpgp.operator.jcajce JcaPGPKeyConverter getPrivateKey

Introduction

In this page you can find the example usage for org.bouncycastle.openpgp.operator.jcajce JcaPGPKeyConverter getPrivateKey.

Prototype

public PrivateKey getPrivateKey(PGPPrivateKey privKey) throws PGPException 

Source Link

Usage

From source file:com.goodvikings.cryptim.api.KeyRing.java

License:BEER-WARE LICENSE

private byte[] ASN1EncodeKeys() throws IOException, PGPException {
    JcaPGPKeyConverter converter = new JcaPGPKeyConverter();

    PrivateKey priv = converter.getPrivateKey(kp.getPrivateKey());
    PublicKey pub = converter.getPublicKey(kp.getPublicKey());

    ASN1EncodableVector pubSeq = new ASN1EncodableVector();

    for (String jid : keys.keySet()) {
        pubSeq.add(new DERSequence(new ASN1Encodable[] { new DERUTF8String(jid),
                new DERUTF8String(nicks.get(jid)), new DERUTCTime(keys.get(jid).getCreationTime()),
                new DEROctetString(converter.getPublicKey(keys.get(jid)).getEncoded()) }));
    }//from   w  ww. j  ava  2 s  .c  o m

    DERSequence seq = new DERSequence(new ASN1Encodable[] {
            new DERSequence(new ASN1Encodable[] { new DERUTCTime(kp.getPublicKey().getCreationTime()),
                    new DEROctetString(pub.getEncoded()) }),
            new DEROctetString(priv.getEncoded()), new DERSequence(pubSeq) });

    return seq.getEncoded();
}

From source file:com.goodvikings.cryptim.api.KeyRing.java

License:BEER-WARE LICENSE

private boolean testKeyRing() throws BadPaddingException, IllegalBlockSizeException, InvalidKeyException,
        NoSuchAlgorithmException, NoSuchPaddingException, NoSuchProviderException, PGPException {
    JcaPGPKeyConverter conv = new JcaPGPKeyConverter();

    PublicKey pub = conv.getPublicKey(kp.getPublicKey());
    PrivateKey priv = conv.getPrivateKey(kp.getPrivateKey());

    String testData = "The quick brown fox jumps over the lazy dog.";

    Cipher enc = Cipher.getInstance(pub.getAlgorithm(), PROVIDER);
    enc.init(Cipher.ENCRYPT_MODE, pub);
    byte[] ciphertext = enc.doFinal(testData.getBytes());

    Cipher dec = Cipher.getInstance(pub.getAlgorithm(), PROVIDER);
    dec.init(Cipher.DECRYPT_MODE, priv);
    return new String(dec.doFinal(ciphertext)).equals(testData);
}

From source file:org.sufficientlysecure.keychain.pgp.CanonicalizedSecretKey.java

License:Open Source License

public RSAPrivateCrtKey getCrtSecretKey() throws PgpGeneralException {
    if (mPrivateKeyState == PRIVATE_KEY_STATE_LOCKED) {
        throw new PgpGeneralException("Cannot get secret key attributes while key is locked.");
    }/*  w  w  w  . j a  v a2  s . c  om*/

    if (mPrivateKeyState == PRIVATE_KEY_STATE_DIVERT_TO_CARD) {
        throw new PgpGeneralException("Cannot get secret key attributes of divert-to-card key.");
    }

    JcaPGPKeyConverter keyConverter = new JcaPGPKeyConverter();
    PrivateKey retVal;
    try {
        retVal = keyConverter.getPrivateKey(mPrivateKey);
    } catch (PGPException e) {
        throw new PgpGeneralException("Error converting private key!", e);
    }

    return (RSAPrivateCrtKey) retVal;
}