Example usage for org.bouncycastle.openpgp PGPEncryptedDataGenerator S2K_SHA1

List of usage examples for org.bouncycastle.openpgp PGPEncryptedDataGenerator S2K_SHA1

Introduction

In this page you can find the example usage for org.bouncycastle.openpgp PGPEncryptedDataGenerator S2K_SHA1.

Prototype

int S2K_SHA1

To view the source code for org.bouncycastle.openpgp PGPEncryptedDataGenerator S2K_SHA1.

Click Source Link

Document

Specifier for SHA-1 S2K PBE generator.

Usage

From source file:org.brownsocks.payments.gateways.enets.pgp.BCPGPProvider.java

@Override
public String signAndEncrypt(String message) throws IOException {

    try {/*from   w ww.ja v  a  2s  .  c  om*/
        /* Final < Armored < Crypted < Clear PGP */
        ByteArrayOutputStream out = new ByteArrayOutputStream();
        ArmoredOutputStream armoredOutput = new ArmoredOutputStream(out);
        PGPEncryptedDataGenerator crypter = new PGPEncryptedDataGenerator(PGPEncryptedDataGenerator.S2K_SHA1,
                new SecureRandom(), _provider);
        crypter.addMethod(getRemotePublicKey());
        BCPGOutputStream pgpOut = new BCPGOutputStream(crypter.open(armoredOutput, new byte[512]));

        /* Prepare for signing */
        PGPSignatureGenerator signer = new PGPSignatureGenerator(getSigningPublicKey().getAlgorithm(),
                PGPUtil.SHA1, _provider);
        signer.initSign(PGPSignature.BINARY_DOCUMENT, getSigningPrivateKey());

        /* Output the standard header */
        signer.generateOnePassVersion(false).encode(pgpOut);

        /* Output the literal data */
        PGPLiteralDataGenerator literalDataGenerator = new PGPLiteralDataGenerator(true);
        literalDataGenerator.open(pgpOut, 'b', "bar", message.getBytes().length, new Date())
                .write(message.getBytes());

        /* Calculate signature and output it */
        signer.update(message.getBytes());
        signer.generate().encode(pgpOut);

        pgpOut.close();
        armoredOutput.close();
        out.close();

        byte[] result = out.toByteArray();

        // brain dead UMAPI adds an extra base64 encoding on top of the ASCII armored string. Go figure.
        return new String(Base64.encode(result));

    } catch (PGPException pgpException) {
        throw new IOException("PGP subsystem problem.", pgpException);

    } catch (NoSuchAlgorithmException noSuchAlgorithmException) {
        throw new IOException("Missing algorithm. Are you running a compatible JVM/Bouncycastle version?",
                noSuchAlgorithmException);

    } catch (SignatureException signatureException) {
        throw new IOException("PGP subsystem problem.", signatureException);

    } catch (NoSuchProviderException noSuchProviderException) {
        throw new IOException("Missing provider. Are you running a compatible JVM/Bouncycastle version?",
                noSuchProviderException);

    }

}