Search in sources :

Example 1 with PGPKeyPair

use of org.bouncycastle.openpgp.PGPKeyPair in project codebunker by gazampa.

the class PGPExporter method convertToPGPKeyPair.

private PGPKeyPair convertToPGPKeyPair(KeyPair pair) {
    PGPKeyPair keyPair = null;
    try {
        keyPair = new JcaPGPKeyPair(PGPPublicKey.RSA_GENERAL, pair, new Date());
    } catch (PGPException pgpe) {
        System.out.println("problem converting jca key pair");
    }
    setPGPKeyPair(keyPair);
    return keyPair;
}
Also used : PGPException(org.bouncycastle.openpgp.PGPException) JcaPGPKeyPair(org.bouncycastle.openpgp.operator.jcajce.JcaPGPKeyPair) PGPKeyPair(org.bouncycastle.openpgp.PGPKeyPair) JcaPGPKeyPair(org.bouncycastle.openpgp.operator.jcajce.JcaPGPKeyPair) Date(java.util.Date)

Example 2 with PGPKeyPair

use of org.bouncycastle.openpgp.PGPKeyPair in project codebunker by gazampa.

the class PGPKeyGenerator method main.

public static void main(String[] args) {
    // generate a pgp key pair
    try {
        Security.addProvider(new BouncyCastleProvider());
        // specify the algorithm for a key pair using new provider version
        KeyPairGenerator kpg = KeyPairGenerator.getInstance("RSA", "BC");
        // Returns a SecureRandom object that was selected by using the algorithms/providers specified in the securerandom.strongAlgorithms Security property that java ships with
        SecureRandom random = SecureRandom.getInstanceStrong();
        // Initializes the key pair generator for a certain keysize using a default parameter set and the SecureRandom implementation of the highest-priority installed provider as the source of randomness.
        kpg.initialize(2048, random);
        // This will generate a jce new key pair
        KeyPair kp = kpg.generateKeyPair();
        // pass over to utility class that has bouncy castle logic
        String identity = "billy.1.murphy@gmail.com";
        char[] passPhrase = { 'i', 'm', 'o', 'n', 't', 'h', 'e', 'l', 'i', 's', 't' };
        PGPExporter ex = new PGPExporter(kp, identity, passPhrase);
        PGPKeyPair keyPairPGP = ex.getPGPKeyPair();
        PGPPrivateKey priv = ex.getPrivateKey();
        PGPPublicKey pub = ex.getPublicKey();
        // PGPSecretKey is like a beefed up decorated version of JCA Key pair
        PGPSecretKey secret = ex.createPGPSecretKey();
        // Returns the key in its primary encoding format,
        byte[] privBytes = keyPairPGP.getPrivateKey().getPrivateKeyDataPacket().getEncoded();
        byte[] pubBytes = pub.getEncoded();
        byte[] secretBytes = secret.getEncoded();
        // generate a base64 encoding and write each to files
        String privB64Enc = Base64.getEncoder().encodeToString(privBytes);
        String pubB64Enc = Base64.getEncoder().encodeToString(pubBytes);
        String secretB64Enc = Base64.getEncoder().encodeToString(secretBytes);
        System.out.println(Arrays.toString(secretBytes));
        System.out.println(Arrays.toString(pubBytes));
        System.out.println(Arrays.toString(privBytes));
        System.out.println(secretB64Enc);
        System.out.println(pubB64Enc);
        System.out.println(privB64Enc);
        FileOutputStream out1 = new FileOutputStream("pgp-secret-2048.asc");
        FileOutputStream out2 = new FileOutputStream("pgp-pub-2048.asc");
        FileOutputStream out3 = new FileOutputStream("pgp-secret-2048.bpg");
        FileOutputStream out4 = new FileOutputStream("pgp-pub-2048.bpg");
        ex.writeSecretArmored(out1, secret);
        ex.writeSecretStandard(out3, secret);
        ex.writePublicArmored(out2, pub);
        ex.writePublicStandard(out4, pub);
    } catch (Exception e) {
        System.out.println("The writer is unable to write " + e);
    }
}
Also used : KeyPair(java.security.KeyPair) PGPKeyPair(org.bouncycastle.openpgp.PGPKeyPair) PGPKeyPair(org.bouncycastle.openpgp.PGPKeyPair) SecureRandom(java.security.SecureRandom) PGPPublicKey(org.bouncycastle.openpgp.PGPPublicKey) KeyPairGenerator(java.security.KeyPairGenerator) SignatureException(java.security.SignatureException) IOException(java.io.IOException) FileNotFoundException(java.io.FileNotFoundException) NoSuchAlgorithmException(java.security.NoSuchAlgorithmException) InvalidKeyException(java.security.InvalidKeyException) NoSuchProviderException(java.security.NoSuchProviderException) PGPException(org.bouncycastle.openpgp.PGPException) PGPSecretKey(org.bouncycastle.openpgp.PGPSecretKey) FileOutputStream(java.io.FileOutputStream) PGPPrivateKey(org.bouncycastle.openpgp.PGPPrivateKey) BouncyCastleProvider(org.bouncycastle.jce.provider.BouncyCastleProvider)

Example 3 with PGPKeyPair

use of org.bouncycastle.openpgp.PGPKeyPair in project commons by craftercms.

the class PGPUtils method createKeyPair.

/**
 * Creates a private/public PGP key pair.
 * @param length length in bytes for the keys
 * @param identity name used for the keys
 * @param password passphrase used for the private key
 * @param privateKeyStream stream to receive the encoded private key
 * @param publicKeyStream stream to receive the encoded public key
 * @throws NoSuchProviderException if there is an error with the security provider
 * @throws NoSuchAlgorithmException is there is an error with the security provider
 * @throws PGPException if there is an error creating the keys
 * @throws IOException if there is an error writing to the streams
 */
public static void createKeyPair(int length, String identity, char[] password, OutputStream privateKeyStream, OutputStream publicKeyStream) throws Exception {
    KeyPairGenerator keyPairGenerator = KeyPairGenerator.getInstance(ALGORITHM, PROVIDER);
    SecureRandom random = SecureRandom.getInstanceStrong();
    keyPairGenerator.initialize(length, random);
    KeyPair keyPair = keyPairGenerator.generateKeyPair();
    PGPPublicKey publicKey = new JcaPGPKeyConverter().getPGPPublicKey(PGPPublicKey.RSA_GENERAL, keyPair.getPublic(), new Date());
    RSAPrivateCrtKey privateCrtKey = (RSAPrivateCrtKey) keyPair.getPrivate();
    RSASecretBCPGKey secretBCPGKey = new RSASecretBCPGKey(privateCrtKey.getPrivateExponent(), privateCrtKey.getPrimeP(), privateCrtKey.getPrimeQ());
    PGPPrivateKey privateKey = new PGPPrivateKey(publicKey.getKeyID(), publicKey.getPublicKeyPacket(), secretBCPGKey);
    PGPKeyPair pgpKeyPair = new PGPKeyPair(publicKey, privateKey);
    PGPDigestCalculator calculator = new JcaPGPDigestCalculatorProviderBuilder().build().get(HashAlgorithmTags.SHA1);
    PGPSecretKey secretKey = new PGPSecretKey(PGPSignature.DEFAULT_CERTIFICATION, pgpKeyPair, identity, calculator, null, null, new JcaPGPContentSignerBuilder(pgpKeyPair.getPublicKey().getAlgorithm(), HashAlgorithmTags.SHA1), new JcePBESecretKeyEncryptorBuilder(PGPEncryptedData.CAST5, calculator).setProvider(PROVIDER).build(password));
    try (ArmoredOutputStream privateArm = new ArmoredOutputStream(privateKeyStream);
        ArmoredOutputStream publicArm = new ArmoredOutputStream(publicKeyStream)) {
        secretKey.encode(privateArm);
        secretKey.getPublicKey().encode(publicArm);
    }
}
Also used : KeyPair(java.security.KeyPair) PGPKeyPair(org.bouncycastle.openpgp.PGPKeyPair) RSAPrivateCrtKey(java.security.interfaces.RSAPrivateCrtKey) PGPKeyPair(org.bouncycastle.openpgp.PGPKeyPair) PGPDigestCalculator(org.bouncycastle.openpgp.operator.PGPDigestCalculator) SecureRandom(java.security.SecureRandom) PGPPublicKey(org.bouncycastle.openpgp.PGPPublicKey) ArmoredOutputStream(org.bouncycastle.bcpg.ArmoredOutputStream) KeyPairGenerator(java.security.KeyPairGenerator) Date(java.util.Date) JcaPGPKeyConverter(org.bouncycastle.openpgp.operator.jcajce.JcaPGPKeyConverter) RSASecretBCPGKey(org.bouncycastle.bcpg.RSASecretBCPGKey) JcaPGPContentSignerBuilder(org.bouncycastle.openpgp.operator.jcajce.JcaPGPContentSignerBuilder) PGPSecretKey(org.bouncycastle.openpgp.PGPSecretKey) PGPPrivateKey(org.bouncycastle.openpgp.PGPPrivateKey) JcaPGPDigestCalculatorProviderBuilder(org.bouncycastle.openpgp.operator.jcajce.JcaPGPDigestCalculatorProviderBuilder) JcePBESecretKeyEncryptorBuilder(org.bouncycastle.openpgp.operator.jcajce.JcePBESecretKeyEncryptorBuilder)

Example 4 with PGPKeyPair

use of org.bouncycastle.openpgp.PGPKeyPair in project OpenSearch by opensearch-project.

the class InstallPluginCommandTests method newSecretKey.

public PGPSecretKey newSecretKey() throws NoSuchAlgorithmException, NoSuchProviderException, PGPException {
    final KeyPairGenerator kpg = KeyPairGenerator.getInstance("RSA");
    kpg.initialize(2048);
    final KeyPair pair = kpg.generateKeyPair();
    final PGPDigestCalculator sha1Calc = new JcaPGPDigestCalculatorProviderBuilder().build().get(HashAlgorithmTags.SHA1);
    final PGPKeyPair pkp = new JcaPGPKeyPair(PGPPublicKey.RSA_GENERAL, pair, new Date());
    return new PGPSecretKey(PGPSignature.DEFAULT_CERTIFICATION, pkp, "example@example.com", sha1Calc, null, null, new JcaPGPContentSignerBuilder(pkp.getPublicKey().getAlgorithm(), HashAlgorithmTags.SHA256), new JcePBESecretKeyEncryptorBuilder(PGPEncryptedData.AES_192, sha1Calc).setProvider(new BouncyCastleFipsProvider()).build("passphrase".toCharArray()));
}
Also used : KeyPair(java.security.KeyPair) PGPKeyPair(org.bouncycastle.openpgp.PGPKeyPair) JcaPGPKeyPair(org.bouncycastle.openpgp.operator.jcajce.JcaPGPKeyPair) JcaPGPContentSignerBuilder(org.bouncycastle.openpgp.operator.jcajce.JcaPGPContentSignerBuilder) BouncyCastleFipsProvider(org.bouncycastle.jcajce.provider.BouncyCastleFipsProvider) PGPDigestCalculator(org.bouncycastle.openpgp.operator.PGPDigestCalculator) PGPKeyPair(org.bouncycastle.openpgp.PGPKeyPair) JcaPGPKeyPair(org.bouncycastle.openpgp.operator.jcajce.JcaPGPKeyPair) PGPSecretKey(org.bouncycastle.openpgp.PGPSecretKey) JcaPGPKeyPair(org.bouncycastle.openpgp.operator.jcajce.JcaPGPKeyPair) KeyPairGenerator(java.security.KeyPairGenerator) JcaPGPDigestCalculatorProviderBuilder(org.bouncycastle.openpgp.operator.jcajce.JcaPGPDigestCalculatorProviderBuilder) Date(java.util.Date) JcePBESecretKeyEncryptorBuilder(org.bouncycastle.openpgp.operator.jcajce.JcePBESecretKeyEncryptorBuilder)

Example 5 with PGPKeyPair

use of org.bouncycastle.openpgp.PGPKeyPair in project nomulus by google.

the class GetKeyringSecretCommand method run.

@Override
public void run() throws Exception {
    OutputStream out = outputPath != null ? new FileOutputStream(outputPath.toFile()) : System.out;
    Security.addProvider(new BouncyCastleProvider());
    switch(keyringKeyName) {
        case BRDA_RECEIVER_PUBLIC_KEY:
            out.write(KeySerializer.serializePublicKey(keyring.getBrdaReceiverKey()));
            break;
        case BRDA_SIGNING_KEY_PAIR:
            out.write(KeySerializer.serializeKeyPair(keyring.getBrdaSigningKey()));
            break;
        case BRDA_SIGNING_PUBLIC_KEY:
            out.write(KeySerializer.serializePublicKey(keyring.getBrdaSigningKey().getPublicKey()));
            break;
        case ICANN_REPORTING_PASSWORD:
            out.write(KeySerializer.serializeString(keyring.getIcannReportingPassword()));
            break;
        case SAFE_BROWSING_API_KEY:
            out.write(KeySerializer.serializeString(keyring.getSafeBrowsingAPIKey()));
            break;
        case JSON_CREDENTIAL:
            out.write(KeySerializer.serializeString(keyring.getJsonCredential()));
            break;
        case MARKSDB_DNL_LOGIN_AND_PASSWORD:
            out.write(KeySerializer.serializeString(keyring.getMarksdbDnlLoginAndPassword()));
            break;
        case MARKSDB_LORDN_PASSWORD:
            out.write(KeySerializer.serializeString(keyring.getMarksdbLordnPassword()));
            break;
        case MARKSDB_SMDRL_LOGIN_AND_PASSWORD:
            out.write(KeySerializer.serializeString(keyring.getMarksdbSmdrlLoginAndPassword()));
            break;
        case RDE_RECEIVER_PUBLIC_KEY:
            out.write(KeySerializer.serializePublicKey(keyring.getRdeReceiverKey()));
            break;
        case RDE_SIGNING_KEY_PAIR:
            out.write(KeySerializer.serializeKeyPair(keyring.getRdeSigningKey()));
            break;
        case RDE_SIGNING_PUBLIC_KEY:
            out.write(KeySerializer.serializePublicKey(keyring.getRdeSigningKey().getPublicKey()));
            break;
        case RDE_SSH_CLIENT_PRIVATE_KEY:
            out.write(KeySerializer.serializeString(keyring.getRdeSshClientPrivateKey()));
            break;
        case RDE_SSH_CLIENT_PUBLIC_KEY:
            out.write(KeySerializer.serializeString(keyring.getRdeSshClientPublicKey()));
            break;
        case RDE_STAGING_KEY_PAIR:
            // Note that we're saving a key pair rather than just the private key because we can't
            // serialize a private key on its own. See {@link KeySerializer}.
            out.write(KeySerializer.serializeKeyPair(new PGPKeyPair(keyring.getRdeStagingEncryptionKey(), keyring.getRdeStagingDecryptionKey())));
            break;
        case RDE_STAGING_PUBLIC_KEY:
            out.write(KeySerializer.serializePublicKey(keyring.getRdeStagingEncryptionKey()));
            break;
    }
}
Also used : OutputStream(java.io.OutputStream) FileOutputStream(java.io.FileOutputStream) FileOutputStream(java.io.FileOutputStream) PGPKeyPair(org.bouncycastle.openpgp.PGPKeyPair) BouncyCastleProvider(org.bouncycastle.jce.provider.BouncyCastleProvider)

Aggregations

PGPKeyPair (org.bouncycastle.openpgp.PGPKeyPair)36 Test (org.junit.jupiter.api.Test)17 PGPPublicKey (org.bouncycastle.openpgp.PGPPublicKey)10 InputStream (java.io.InputStream)9 OutputStream (java.io.OutputStream)9 FakeKeyringModule (google.registry.testing.FakeKeyringModule)8 Date (java.util.Date)8 ByteArrayInputStream (java.io.ByteArrayInputStream)7 KeyPair (java.security.KeyPair)7 KeyPairGenerator (java.security.KeyPairGenerator)7 PGPPrivateKey (org.bouncycastle.openpgp.PGPPrivateKey)7 ByteArrayOutputStream (java.io.ByteArrayOutputStream)6 PGPException (org.bouncycastle.openpgp.PGPException)6 PGPSecretKey (org.bouncycastle.openpgp.PGPSecretKey)5 JcaPGPKeyPair (org.bouncycastle.openpgp.operator.jcajce.JcaPGPKeyPair)5 IOException (java.io.IOException)4 PGPDigestCalculator (org.bouncycastle.openpgp.operator.PGPDigestCalculator)4 ParameterizedTest (org.junit.jupiter.params.ParameterizedTest)4 FileOutputStream (java.io.FileOutputStream)3 JcaPGPContentSignerBuilder (org.bouncycastle.openpgp.operator.jcajce.JcaPGPContentSignerBuilder)3