Package org.bouncycastle.openpgp

Examples of org.bouncycastle.openpgp.PGPEncryptedDataGenerator$PBEMethod


            out = new ArmoredOutputStream(out);
        }
       
        try
        {   
            PGPEncryptedDataGenerator   cPk = new PGPEncryptedDataGenerator(PGPEncryptedData.CAST5, withIntegrityCheck, new SecureRandom(), "BC");
               
            cPk.addMethod(encKey);
           
            OutputStream                cOut = cPk.open(out, new byte[1 << 16]);
           
            PGPCompressedDataGenerator  comData = new PGPCompressedDataGenerator(
                                                                    PGPCompressedData.ZIP);
                                                                   
            PGPUtil.writeFileToLiteralData(comData.open(cOut), PGPLiteralData.BINARY, new File(fileName), new byte[1 << 16]);
View Full Code Here


           
            //
            // encrypt
            //
            ByteArrayOutputStream        cbOut = new ByteArrayOutputStream();
            PGPEncryptedDataGenerator    cPk = new PGPEncryptedDataGenerator(SymmetricKeyAlgorithmTags.TRIPLE_DES, new SecureRandom(), "BC");           
            PGPPublicKey                        puK = sKey.getSecretKey(pgpKeyID).getPublicKey();
           
            cPk.addMethod(puK);
           
            OutputStream    cOut = cPk.open(new UncloseableOutputStream(cbOut), bOut.toByteArray().length);

            cOut.write(text);

            cOut.close();

            pgpF = new PGPObjectFactory(cbOut.toByteArray());

            encList = (PGPEncryptedDataList)pgpF.nextObject();
       
            encP = (PGPPublicKeyEncryptedData)encList.get(0);
           
            pgpPrivKey = sKey.getSecretKey(pgpKeyID).extractPrivateKey(pass, "BC");

            clear = encP.getDataStream(pgpPrivKey, "BC");
           
            bOut.reset();
           
            while ((ch = clear.read()) >= 0)
            {
                bOut.write(ch);
            }

            out = bOut.toByteArray();

            if (!areEqual(out, text))
            {
                fail("wrong plain text in generated packet");
            }
           
            //
            // use of PGPKeyPair
            //
            BigInteger g = new BigInteger("153d5d6172adb43045b68ae8e1de1070b6137005686d29d3d73a7749199681ee5b212c9b96bfdcfa5b20cd5e3fd2044895d609cf9b410b7a0f12ca1cb9a428cc", 16);
            BigInteger p = new BigInteger("9494fec095f3b85ee286542b3836fc81a5dd0a0349b4c239dd38744d488cf8e31db8bcb7d33b41abb9e5a33cca9144b1cef332c94bf0573bf047a3aca98cdf3b", 16);

            KeyPairGenerator       kpg = KeyPairGenerator.getInstance("ElGamal", "BC");
           
            ElGamalParameterSpec   elParams = new ElGamalParameterSpec(p, g);
           
            kpg.initialize(elParams);
           
            KeyPair kp = kpg.generateKeyPair();
           
            PGPKeyPair    pgpKp = new PGPKeyPair(PGPPublicKey.ELGAMAL_GENERAL , kp.getPublic(), kp.getPrivate(), new Date(), "BC");
           
            PGPPublicKey k1 = pgpKp.getPublicKey();
           
            PGPPrivateKey k2 = pgpKp.getPrivateKey();



            // Test bug with ElGamal P size != 0 mod 8 (don't use these sizes at home!)
            SecureRandom random = new SecureRandom();
            for (int pSize = 257; pSize < 264; ++pSize)
            {
                // Generate some parameters of the given size
                AlgorithmParameterGenerator a = AlgorithmParameterGenerator.getInstance("ElGamal", "BC");
                a.init(pSize, new SecureRandom());
                AlgorithmParameters params = a.generateParameters();

                DHParameterSpec elP = (DHParameterSpec)params.getParameterSpec(DHParameterSpec.class);
                KeyPairGenerator keyGen = KeyPairGenerator.getInstance("ElGamal", "BC");

                keyGen.initialize(elP);


                // Run a short encrypt/decrypt test with random key for the given parameters
                kp = keyGen.generateKeyPair();

                PGPKeyPair elGamalKeyPair = new PGPKeyPair(
                    PublicKeyAlgorithmTags.ELGAMAL_GENERAL, kp, new Date(), "BC");

                cPk = new PGPEncryptedDataGenerator(SymmetricKeyAlgorithmTags.CAST5, random, "BC");

                puK = elGamalKeyPair.getPublicKey();

                cPk.addMethod(puK);

                cbOut = new ByteArrayOutputStream();

                cOut = cPk.open(cbOut, text.length);

                cOut.write(text);

                cOut.close();
View Full Code Here

        pOut.write(clearData);

        lData.close();
        comData.close();

        PGPEncryptedDataGenerator   cPk = new PGPEncryptedDataGenerator(algorithm, new SecureRandom(), "BC");

        cPk.addMethod(passPhrase);

        byte[]              bytes = bOut.toByteArray();

        OutputStream    cOut = cPk.open(out, bytes.length);

        cOut.write(bytes)// obtain the actual bytes from the compressed stream

        cOut.close();
       
View Full Code Here

        PGPObjectFactory f = new PGPObjectFactory(bytes);
        checkLiteralData((PGPLiteralData)f.nextObject(), text);

        ByteArrayOutputStream bcOut = new ByteArrayOutputStream();

        PGPEncryptedDataGenerator encGen = new PGPEncryptedDataGenerator(SymmetricKeyAlgorithmTags.AES_128, true, new SecureRandom(), "BC");

        encGen.addMethod(pgpPubKey);

        encGen.addMethod("password".toCharArray());

        OutputStream cOut = encGen.open(bcOut, bytes.length);

        cOut.write(bytes);

        cOut.close();
View Full Code Here

        // encrypt - short message
        //
        byte[]    shortText = { (byte)'h', (byte)'e', (byte)'l', (byte)'l', (byte)'o' };
   
        ByteArrayOutputStream        cbOut = new ByteArrayOutputStream();
        PGPEncryptedDataGenerator    cPk = new PGPEncryptedDataGenerator(SymmetricKeyAlgorithmTags.CAST5, new SecureRandom(), "BC");           
        PGPPublicKey                 puK = pgpPriv.getSecretKey(encP.getKeyID()).getPublicKey();
       
        cPk.addMethod(puK);
       
        OutputStream    cOut = cPk.open(new UncloseableOutputStream(cbOut), shortText.length);

        cOut.write(shortText);

        cOut.close();

        pgpF = new PGPObjectFactory(cbOut.toByteArray());

        encList = (PGPEncryptedDataList)pgpF.nextObject();
   
        encP = (PGPPublicKeyEncryptedData)encList.get(0);
       
        pgpPrivKey = pgpPriv.getSecretKey(encP.getKeyID()).extractPrivateKey(pass, "BC");

        if (encP.getSymmetricAlgorithm(pgpPrivKey, "BC") != SymmetricKeyAlgorithmTags.CAST5)
        {
            fail("symmetric algorithm mismatch");
        }

        clear = encP.getDataStream(pgpPrivKey, "BC");
       
        bOut.reset();
       
        while ((ch = clear.read()) >= 0)
        {
            bOut.write(ch);
        }

        out = bOut.toByteArray();

        if (!areEqual(out, shortText))
        {
            fail("wrong plain text in generated short text packet");
        }
       
        //
        // encrypt
        //
        cbOut = new ByteArrayOutputStream();
        cPk = new PGPEncryptedDataGenerator(SymmetricKeyAlgorithmTags.CAST5, new SecureRandom(), "BC");           
        puK = pgpPriv.getSecretKey(encP.getKeyID()).getPublicKey();
       
        cPk.addMethod(puK);

        cOut = cPk.open(new UncloseableOutputStream(cbOut), text.length);

        cOut.write(text);

        cOut.close();
View Full Code Here

        pOut.write(clearData);

        lData.close();
        comData.close();

        PGPEncryptedDataGenerator cPk = new PGPEncryptedDataGenerator(
                PGPEncryptedData.CAST5, withIntegrityCheck, new SecureRandom(),
                "BC");

        cPk.addMethod(encKey);

        byte[] bytes = bOut.toByteArray();

        OutputStream cOut = cPk.open(out, bytes.length);

        cOut.write(bytes); // obtain the actual bytes from the compressed stream

        cOut.close();
View Full Code Here

            OutputStream pOut = lData.open(
                            cos, PGPLiteralData.BINARY, PGPLiteralData.CONSOLE, clearText.length, new Date());
            pOut.write(clearText);
            lData.close();
            comData.close();
            PGPEncryptedDataGenerator cPk = new PGPEncryptedDataGenerator(SymmetricKeyAlgorithmMap.convert(algorithm),
                            new SecureRandom(), "BC");
            cPk.addMethod(passPhrase);
            byte[] bytes = bOut.toByteArray();
            OutputStream cOut = cPk.open(encOut, bytes.length);
            cOut.write(bytes); // obtain the actual bytes from the compressed stream
            cOut.close();
            return encOut.toByteArray();
        } catch (NoSuchProviderException ex) {
            throw new CryptoException(ex.getMessage(), ex);
View Full Code Here

        onePassSignature = v4signer.generateOnePassVersion(false);
    }


    public void createEncryptedDataGenerator(PGPPublicKey publicKey, boolean withIntegrityCheck) throws Exception {
        PGPEncryptedDataGenerator cPk = new PGPEncryptedDataGenerator(PGPEncryptedData.CAST5, withIntegrityCheck,
                        new SecureRandom(), "BC");
        cPk.addMethod(publicKey);
        encryptor = cPk;
    }
View Full Code Here

        if (armored) {
            outputStream = new ArmoredOutputStream(outputStream);
        }

        PGPEncryptedDataGenerator encGen = new PGPEncryptedDataGenerator(new JcePGPDataEncryptorBuilder(SymmetricKeyAlgorithmTags.CAST5).
                                                                             setWithIntegrityPacket(integrity).
                                                                             setSecureRandom(new SecureRandom()).
                                                                             setProvider("BC"));
        encGen.addMethod(new JcePublicKeyKeyEncryptionMethodGenerator(key));
        OutputStream encOut = encGen.open(outputStream, new byte[BUFFER_SIZE]);

        PGPCompressedDataGenerator comData = new PGPCompressedDataGenerator(CompressionAlgorithmTags.ZIP);
        OutputStream comOut = new BufferedOutputStream(comData.open(encOut));

        PGPSignatureGenerator sigGen = createSignatureGenerator(exchange, comOut);
View Full Code Here

        if (armored) {
            outputStream = new ArmoredOutputStream(outputStream);
        }

        PGPEncryptedDataGenerator encGen = new PGPEncryptedDataGenerator(PGPEncryptedData.CAST5, integrity, new SecureRandom(), "BC");
        encGen.addMethod(key);

        OutputStream encOut = encGen.open(outputStream, compressedData.length);
        try {
            encOut.write(compressedData);
        } finally {
            IOHelper.close(encOut);
            IOHelper.close(outputStream);
View Full Code Here

TOP

Related Classes of org.bouncycastle.openpgp.PGPEncryptedDataGenerator$PBEMethod

Copyright © 2018 www.massapicom. All rights reserved.
All source code are property of their respective owners. Java is a trademark of Sun Microsystems, Inc and owned by ORACLE Inc. Contact coftware#gmail.com.