Search in sources :

Example 1 with PGPDigestCalculator

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()));
}
Also used : KeyPair(java.security.KeyPair) JcaPGPKeyPair(org.bouncycastle.openpgp.operator.jcajce.JcaPGPKeyPair) PGPDigestCalculator(org.bouncycastle.openpgp.operator.PGPDigestCalculator) JcaPGPKeyPair(org.bouncycastle.openpgp.operator.jcajce.JcaPGPKeyPair) JcaPGPKeyPair(org.bouncycastle.openpgp.operator.jcajce.JcaPGPKeyPair) KeyPairGenerator(java.security.KeyPairGenerator) Date(java.util.Date) JcaPGPContentSignerBuilder(org.bouncycastle.openpgp.operator.jcajce.JcaPGPContentSignerBuilder) JcaPGPDigestCalculatorProviderBuilder(org.bouncycastle.openpgp.operator.jcajce.JcaPGPDigestCalculatorProviderBuilder) JcePBESecretKeyEncryptorBuilder(org.bouncycastle.openpgp.operator.jcajce.JcePBESecretKeyEncryptorBuilder) BouncyCastleProvider(org.bouncycastle.jce.provider.BouncyCastleProvider)

Example 2 with PGPDigestCalculator

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;
}
Also used : PGPException(org.bouncycastle.openpgp.PGPException) JcaPGPContentSignerBuilder(org.bouncycastle.openpgp.operator.jcajce.JcaPGPContentSignerBuilder) PGPSecretKey(org.bouncycastle.openpgp.PGPSecretKey) PGPDigestCalculator(org.bouncycastle.openpgp.operator.PGPDigestCalculator) JcaPGPDigestCalculatorProviderBuilder(org.bouncycastle.openpgp.operator.jcajce.JcaPGPDigestCalculatorProviderBuilder) JcePBESecretKeyEncryptorBuilder(org.bouncycastle.openpgp.operator.jcajce.JcePBESecretKeyEncryptorBuilder)

Example 3 with PGPDigestCalculator

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);
    }
}
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 PGPDigestCalculator

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

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());
}
Also used : PGPDigestCalculator(org.bouncycastle.openpgp.operator.PGPDigestCalculator) BcPBESecretKeyEncryptorBuilder(org.bouncycastle.openpgp.operator.bc.BcPBESecretKeyEncryptorBuilder)

Aggregations

PGPDigestCalculator (org.bouncycastle.openpgp.operator.PGPDigestCalculator)8 PGPSecretKey (org.bouncycastle.openpgp.PGPSecretKey)5 JcaPGPContentSignerBuilder (org.bouncycastle.openpgp.operator.jcajce.JcaPGPContentSignerBuilder)5 JcaPGPDigestCalculatorProviderBuilder (org.bouncycastle.openpgp.operator.jcajce.JcaPGPDigestCalculatorProviderBuilder)5 KeyPair (java.security.KeyPair)4 KeyPairGenerator (java.security.KeyPairGenerator)4 Date (java.util.Date)4 PGPKeyPair (org.bouncycastle.openpgp.PGPKeyPair)4 JcePBESecretKeyEncryptorBuilder (org.bouncycastle.openpgp.operator.jcajce.JcePBESecretKeyEncryptorBuilder)4 PGPPrivateKey (org.bouncycastle.openpgp.PGPPrivateKey)3 JcaPGPKeyPair (org.bouncycastle.openpgp.operator.jcajce.JcaPGPKeyPair)3 PGPKeyRingGenerator (org.bouncycastle.openpgp.PGPKeyRingGenerator)2 PGPPublicKey (org.bouncycastle.openpgp.PGPPublicKey)2 PGPSecretKeyRing (org.bouncycastle.openpgp.PGPSecretKeyRing)2 PGPSignatureSubpacketGenerator (org.bouncycastle.openpgp.PGPSignatureSubpacketGenerator)2 PBESecretKeyEncryptor (org.bouncycastle.openpgp.operator.PBESecretKeyEncryptor)2 PGPContentSignerBuilder (org.bouncycastle.openpgp.operator.PGPContentSignerBuilder)2 SecureRandom (java.security.SecureRandom)1 RSAPrivateCrtKey (java.security.interfaces.RSAPrivateCrtKey)1 ArrayList (java.util.ArrayList)1