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;
}
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);
}
}
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);
}
}
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()));
}
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;
}
}
Aggregations