use of org.bouncycastle.openpgp.operator.PGPDigestCalculator in project definitive-guide-jakarta-ee-security by Apress.
the class PGPKeyPairGeneratorWithRSA method main.
public static void main(String[] args) throws PGPException, NoSuchAlgorithmException, IOException {
char[] passphrase = "testpass".toCharArray();
String identity = "testidentity";
KeyPairGenerator keygen = KeyPairGenerator.getInstance("RSA");
keygen.initialize(2048);
KeyPair keyPair = keygen.generateKeyPair();
PGPDigestCalculator sha1Calc = new JcaPGPDigestCalculatorProviderBuilder().build().get(HashAlgorithmTags.SHA1);
PGPKeyPair pgpKeyPair = new JcaPGPKeyPair(PGPPublicKey.RSA_GENERAL, keyPair, new Date());
PGPSecretKey secretKey = new PGPSecretKey(PGPSignature.DEFAULT_CERTIFICATION, pgpKeyPair, identity, sha1Calc, null, null, new JcaPGPContentSignerBuilder(pgpKeyPair.getPublicKey().getAlgorithm(), HashAlgorithmTags.SHA1), new JcePBESecretKeyEncryptorBuilder(PGPEncryptedData.CAST5, sha1Calc).setProvider(new BouncyCastleProvider()).build(passphrase));
System.out.println("Public key: " + Base64.getEncoder().encodeToString(secretKey.getPublicKey().getEncoded()));
System.out.println("Private key: " + Base64.getEncoder().encodeToString(secretKey.getEncoded()));
}
use of org.bouncycastle.openpgp.operator.PGPDigestCalculator in project codebunker by gazampa.
the class PGPExporter method createPGPSecretKey.
public PGPSecretKey createPGPSecretKey() {
PGPSecretKey secretKey = null;
try {
PGPDigestCalculator sha1Calc = new JcaPGPDigestCalculatorProviderBuilder().build().get(HashAlgorithmTags.SHA256);
secretKey = new PGPSecretKey(PGPSignature.DEFAULT_CERTIFICATION, keyPair, identity, sha1Calc, null, null, new JcaPGPContentSignerBuilder(keyPair.getPublicKey().getAlgorithm(), HashAlgorithmTags.SHA256), new JcePBESecretKeyEncryptorBuilder(PGPEncryptedData.CAST5, sha1Calc).setProvider("BC").build(passPhrase));
} catch (PGPException pgpe) {
System.out.println("problem creating the secret key");
}
setSecretKey(secretKey);
return secretKey;
}
use of org.bouncycastle.openpgp.operator.PGPDigestCalculator 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.operator.PGPDigestCalculator 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.operator.PGPDigestCalculator in project pgpainless by pgpainless.
the class BcImplementationFactory method getPBESecretKeyEncryptor.
@Override
public PBESecretKeyEncryptor getPBESecretKeyEncryptor(PGPSecretKey secretKey, Passphrase passphrase) throws PGPException {
int keyEncryptionAlgorithm = secretKey.getKeyEncryptionAlgorithm();
if (secretKey.getS2K() == null) {
return getPBESecretKeyEncryptor(SymmetricKeyAlgorithm.requireFromId(keyEncryptionAlgorithm), passphrase);
}
int hashAlgorithm = secretKey.getS2K().getHashAlgorithm();
PGPDigestCalculator digestCalculator = getPGPDigestCalculator(hashAlgorithm);
long iterationCount = secretKey.getS2K().getIterationCount();
return new BcPBESecretKeyEncryptorBuilder(keyEncryptionAlgorithm, digestCalculator, (int) iterationCount).build(passphrase.getChars());
}
Aggregations