Package org.bouncycastle.openpgp

Examples of org.bouncycastle.openpgp.PGPSecretKey


    }

    private static PGPPrivateKey findSecretKey(
            PGPSecretKeyRingCollection pgpSec, long keyID, char[] pass)
            throws PGPException, NoSuchProviderException {
        PGPSecretKey pgpSecKey = pgpSec.getSecretKey(keyID);

        if (pgpSecKey == null) {
            return null;
        }

        return pgpSecKey.extractPrivateKey(pass, "BC");
    }
View Full Code Here


    public static PGPSecretKey readSecretKey(InputStream in, long keyId) throws IOException, PGPException {
        InputStream decoderStream = null;
        try {
            decoderStream = PGPUtil.getDecoderStream(in);
            PGPSecretKeyRingCollection pgpSec = new PGPSecretKeyRingCollection(decoderStream);
            PGPSecretKey key = null;
            Iterator rIt = pgpSec.getKeyRings();
            while (key == null && rIt.hasNext()) {
                PGPSecretKeyRing kRing = (PGPSecretKeyRing) rIt.next();
                Iterator kIt = kRing.getSecretKeys();
                while (key == null && kIt.hasNext()) {
                    PGPSecretKey k = (PGPSecretKey) kIt.next();
                    if (k.isSigningKey() && (keyId < 0 || k.getKeyID() == keyId)) {
                        key = k;
                    }
                }
            }
            if (key == null) {
View Full Code Here

            Iterator rIt = pgpSec.getKeyRings();
            while (rIt.hasNext()) {
                PGPSecretKeyRing kRing = (PGPSecretKeyRing) rIt.next();
                Iterator kIt = kRing.getSecretKeys();
                while (kIt.hasNext()) {
                    PGPSecretKey k = (PGPSecretKey) kIt.next();
                    if (!k.isSigningKey()) {
                        keyIDs.add(k.getKeyID());
                    }
                }
            }
            return toLongArray(keyIDs);
        } catch (PGPException ex) {
View Full Code Here

        }

        @Override
        protected void prepareProcessor(ClearTextProcessor processor) throws Exception {
            super.prepareProcessor(processor);
            PGPSecretKey secretKey = BcUtils.readSecretKey(signatureKey);
            processor.createSignatureGenerator(secretKey, signatureKeyPassphrase);
        }
View Full Code Here

    public void setArmor(boolean armor) {
        this.armor = armor;
    }

    public void createSignatureGenerator(File keyFile, long keyId, char[] password) throws Exception {
        PGPSecretKey secretKey = BcUtils.readSecretKey(keyFile, keyId);
        createSignatureGenerator(secretKey, password);
    }
View Full Code Here

            return status;
        }

        private static PGPPrivateKey findSecretKey(PGPSecretKeyRingCollection pgpSec, long keyId, char[] password)
                        throws PGPException, NoSuchProviderException {
            PGPSecretKey pgpSecKey = pgpSec.getSecretKey(keyId);
            if (pgpSecKey == null) {
                return null;
            }
            return pgpSecKey.extractPrivateKey(password, PROVIDER_ID);
        }
View Full Code Here

    private PGPSecretKey readSecretKey(InputStream in) throws IOException, PGPException {
        in = PGPUtil.getDecoderStream(in);
        PGPSecretKeyRingCollection pgpSec = new PGPSecretKeyRingCollection(in);

        PGPSecretKey key = null;
        for (Iterator it = pgpSec.getKeyRings(); key == null && it.hasNext(); ) {
            PGPSecretKeyRing kRing = (PGPSecretKeyRing) it.next();  
           
            for (Iterator it2 = kRing.getSecretKeys(); key == null && it2.hasNext(); ) {
                PGPSecretKey k = (PGPSecretKey) it2.next();
                if ((keyId == null) && k.isSigningKey()) {
                    key = k;
                }
                if ((keyId != null) && (Long.valueOf(keyId, 16).longValue() == (k.getKeyID() & MASK))) {
                    key = k;
                }
            }
        }
       
View Full Code Here

   
        kpg.initialize(1024);
   
        KeyPair                    kp = kpg.generateKeyPair();

        PGPSecretKey    secretKey = new PGPSecretKey(PGPSignature.DEFAULT_CERTIFICATION, PublicKeyAlgorithmTags.RSA_GENERAL, kp.getPublic(), kp.getPrivate(), new Date(), "fred", SymmetricKeyAlgorithmTags.CAST5, passPhrase, null, null, new SecureRandom(), "BC");
   
        PGPPublicKey    key = secretKey.getPublicKey();

        it = key.getUserIDs();

        uid = (String)it.next();

        it = key.getSignaturesForID(uid);

        sig = (PGPSignature)it.next();

        sig.initVerify(key, "BC");

        if (!sig.verifyCertification(uid, key))
        {
            fail("failed to verify certification");
        }

        pgpPrivKey = secretKey.extractPrivateKey(passPhrase, "BC");
       
        key = PGPPublicKey.removeCertification(key, uid, sig);
       
        if (key == null)
        {
            fail("failed certification removal");
        }
       
        byte[]    keyEnc = key.getEncoded();
       
        key = PGPPublicKey.addCertification(key, uid, sig);
       
        keyEnc = key.getEncoded();

        PGPSignatureGenerator sGen = new PGPSignatureGenerator(PublicKeyAlgorithmTags.RSA_GENERAL, HashAlgorithmTags.SHA1, "BC");
       
        sGen.initSign(PGPSignature.KEY_REVOCATION, secretKey.extractPrivateKey(passPhrase, "BC"));

        sig = sGen.generateCertification(key);

        key = PGPPublicKey.addCertification(key, sig);

        keyEnc = key.getEncoded();

        PGPPublicKeyRing    tmpRing = new PGPPublicKeyRing(keyEnc);

        key = tmpRing.getPublicKey();

        Iterator            sgIt = key.getSignaturesOfType(PGPSignature.KEY_REVOCATION);

        sig = (PGPSignature)sgIt.next();

        sig.initVerify(key, "BC");

        if (!sig.verifyCertification(key))
        {
            fail("failed to verify revocation certification");
        }

        //
        // use of PGPKeyPair
        //
        PGPKeyPair    pgpKp = new PGPKeyPair(PGPPublicKey.RSA_GENERAL , kp.getPublic(), kp.getPrivate(), new Date(), "BC");
       
        PGPPublicKey k1 = pgpKp.getPublicKey();
       
        PGPPrivateKey k2 = pgpKp.getPrivateKey();
       
        k1.getEncoded();

        mixedTest(k2, k1);

        //
        // key pair generation - AES_256 encryption.
        //
        kp = kpg.generateKeyPair();

        secretKey = new PGPSecretKey(PGPSignature.DEFAULT_CERTIFICATION, PublicKeyAlgorithmTags.RSA_GENERAL, kp.getPublic(), kp.getPrivate(), new Date(), "fred", SymmetricKeyAlgorithmTags.AES_256, passPhrase, null, null, new SecureRandom(), "BC");
   
        secretKey.extractPrivateKey(passPhrase, "BC");
       
        secretKey.encode(new ByteArrayOutputStream());
       
        //
        // secret key password changing.
        //
        String  newPass = "newPass";
       
        secretKey = PGPSecretKey.copyWithNewPassword(secretKey, passPhrase, newPass.toCharArray(), secretKey.getKeyEncryptionAlgorithm(), new SecureRandom(), "BC");
       
        secretKey.extractPrivateKey(newPass.toCharArray(), "BC");
       
        secretKey.encode(new ByteArrayOutputStream());
       
        key = secretKey.getPublicKey();

        key.encode(new ByteArrayOutputStream());
       
        it = key.getUserIDs();

        uid = (String)it.next();

        it = key.getSignaturesForID(uid);

        sig = (PGPSignature)it.next();

        sig.initVerify(key, "BC");

        if (!sig.verifyCertification(uid, key))
        {
            fail("failed to verify certification");
        }

        pgpPrivKey = secretKey.extractPrivateKey(newPass.toCharArray(), "BC");
       
        //
        // signature generation
        //
        String                                data = "hello world!";
       
        bOut = new ByteArrayOutputStream();
       
        ByteArrayInputStream        testIn = new ByteArrayInputStream(data.getBytes());
       
        sGen = new PGPSignatureGenerator(PublicKeyAlgorithmTags.RSA_GENERAL, HashAlgorithmTags.SHA1, "BC");
   
        sGen.initSign(PGPSignature.BINARY_DOCUMENT, pgpPrivKey);

        PGPCompressedDataGenerator cGen = new PGPCompressedDataGenerator(
            PGPCompressedData.ZIP);

        BCPGOutputStream bcOut = new BCPGOutputStream(
            cGen.open(new UncloseableOutputStream(bOut)));

        sGen.generateOnePassVersion(false).encode(bcOut);

        PGPLiteralDataGenerator    lGen = new PGPLiteralDataGenerator();

        Date testDate = new Date((System.currentTimeMillis() / 1000) * 1000);
        OutputStream lOut = lGen.open(
            new UncloseableOutputStream(bcOut),
            PGPLiteralData.BINARY,
            "_CONSOLE",
            data.getBytes().length,
            testDate);

        while ((ch = testIn.read()) >= 0)
        {
            lOut.write(ch);
            sGen.update((byte)ch);
        }

        lOut.close();

        sGen.generate().encode(bcOut);

        bcOut.close();

        //
        // verify generated signature
        //
        pgpFact = new PGPObjectFactory(bOut.toByteArray());

        c1 = (PGPCompressedData)pgpFact.nextObject();

        pgpFact = new PGPObjectFactory(c1.getDataStream());
       
        p1 = (PGPOnePassSignatureList)pgpFact.nextObject();
       
        ops = p1.get(0);
       
        p2 = (PGPLiteralData)pgpFact.nextObject();
        if (!p2.getModificationTime().equals(testDate))
        {
            fail("Modification time not preserved: " + p2.getModificationTime() + " " + testDate);
        }

        dIn = p2.getInputStream();

        ops.initVerify(secretKey.getPublicKey(), "BC");
       
        while ((ch = dIn.read()) >= 0)
        {
            ops.update((byte)ch);
        }

        p3 = (PGPSignatureList)pgpFact.nextObject();

        if (!ops.verify(p3.get(0)))
        {
            fail("Failed generated signature check");
        }
       
        //
        // signature generation - version 3
        //
        bOut = new ByteArrayOutputStream();
       
        testIn = new ByteArrayInputStream(data.getBytes());
        PGPV3SignatureGenerator    sGenV3 = new PGPV3SignatureGenerator(PGPPublicKey.RSA_GENERAL, PGPUtil.SHA1, "BC");
   
        sGen.initSign(PGPSignature.BINARY_DOCUMENT, pgpPrivKey);

        cGen = new PGPCompressedDataGenerator(
                                                                PGPCompressedData.ZIP);

        bcOut = new BCPGOutputStream(cGen.open(bOut));

        sGen.generateOnePassVersion(false).encode(bcOut);

        lGen = new PGPLiteralDataGenerator();
        lOut = lGen.open(
            new UncloseableOutputStream(bcOut),
            PGPLiteralData.BINARY,
            "_CONSOLE",
            data.getBytes().length,
            testDate);

        while ((ch = testIn.read()) >= 0)
        {
            lOut.write(ch);
            sGen.update((byte)ch);
        }

        lOut.close();

        sGen.generate().encode(bcOut);

        bcOut.close();

        //
        // verify generated signature
        //
        pgpFact = new PGPObjectFactory(bOut.toByteArray());

        c1 = (PGPCompressedData)pgpFact.nextObject();

        pgpFact = new PGPObjectFactory(c1.getDataStream());
       
        p1 = (PGPOnePassSignatureList)pgpFact.nextObject();
       
        ops = p1.get(0);
       
        p2 = (PGPLiteralData)pgpFact.nextObject();
        if (!p2.getModificationTime().equals(testDate))
        {
            fail("Modification time not preserved");
        }

        dIn = p2.getInputStream();

        ops.initVerify(secretKey.getPublicKey(), "BC");
       
        while ((ch = dIn.read()) >= 0)
        {
            ops.update((byte)ch);
        }

        p3 = (PGPSignatureList)pgpFact.nextObject();

        if (!ops.verify(p3.get(0)))
        {
            fail("Failed v3 generated signature check");
        }
       
        //
        // extract PGP 8 private key
        //
        pgpPriv = new PGPSecretKeyRing(pgp8Key);
       
        secretKey = pgpPriv.getSecretKey();
       
        pgpPrivKey = secretKey.extractPrivateKey(pgp8Pass, "BC");

        //
        // expiry
        //
        testExpiry(expiry60and30daysSig13Key, 60, 30);
View Full Code Here

        Iterator<?> encryptedDataObjects = enc.getEncryptedDataObjects();
        PGPPrivateKey privateKey = null;
        PGPPublicKeyEncryptedData encryptedData;
        while (privateKey == null && encryptedDataObjects.hasNext()) {
            encryptedData = (PGPPublicKeyEncryptedData) encryptedDataObjects.next();
            PGPSecretKey pgpSecKey = pgpSec.getSecretKey(encryptedData.getKeyID());
            privateKey = pgpSecKey.extractPrivateKey(new JcePBESecretKeyDecryptorBuilder().setProvider("BC").build(passphrase.toCharArray()));
        }
        return privateKey;
    }
View Full Code Here

    public static PGPSecretKey findSecretKey(CamelContext context, String keychainFilename, byte[] secKeyRing, String passphrase)
        throws IOException, PGPException, NoSuchProviderException {

        InputStream keyChainInputStream = determineKeyRingInputStream(context, keychainFilename, secKeyRing, false);
        PGPSecretKey secKey = null;
        try {
            secKey = findSecretKey(keyChainInputStream, passphrase);
        } finally {
            IOHelper.close(keyChainInputStream);
        }
View Full Code Here

TOP

Related Classes of org.bouncycastle.openpgp.PGPSecretKey

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.