Examples of PGPPublicKeyRing


Examples of org.bouncycastle.openpgp.PGPPublicKeyRing

        PublicKey      pubKey = null;

        //
        // Read the public key
        //
        PGPPublicKeyRing        pgpPub = new PGPPublicKeyRing(testPubKey, new BcKeyFingerprintCalculator());

        pubKey = pgpPub.getPublicKey().getKey("BC");

        Iterator    it = pgpPub.getPublicKey().getUserIDs();
       
        String    uid = (String)it.next();

        it = pgpPub.getPublicKey().getSignaturesForID(uid);
       
        PGPSignature    sig = (PGPSignature)it.next();
       
        sig.init(new BcPGPContentVerifierBuilderProvider(), pgpPub.getPublicKey());
       
        if (!sig.verifyCertification(uid, pgpPub.getPublicKey()))
        {
            fail("failed to verify certification");
        }
       
        //
        // write a public key
        //
        ByteArrayOutputStream    bOut = new ByteArrayOutputStream();
        BCPGOutputStream         pOut = new BCPGOutputStream(bOut);
       
        pgpPub.encode(pOut);

        if (!areEqual(bOut.toByteArray(), testPubKey))   
        {
            fail("public key rewrite failed");
        }
       
        //
        // Read the public key
        //
        PGPPublicKeyRing     pgpPubV3 = new PGPPublicKeyRing(testPubKeyV3, new BcKeyFingerprintCalculator());
        PublicKey            pubKeyV3 = pgpPub.getPublicKey().getKey("BC");

        //
        // write a V3 public key
        //
        bOut = new ByteArrayOutputStream();
        pOut = new BCPGOutputStream(bOut);
       
        pgpPubV3.encode(pOut);

        //
        // Read a v3 private key
        //
        char[]                  passP = "FIXCITY_QA".toCharArray();

        if (!noIDEA())
        {
            PGPSecretKeyRing        pgpPriv = new PGPSecretKeyRing(testPrivKeyV3, new BcKeyFingerprintCalculator());
            PGPPrivateKey           pgpPrivKey = pgpPriv.getSecretKey().extractPrivateKey(new BcPBESecretKeyDecryptorBuilder(new BcPGPDigestCalculatorProvider()).build(passP));

            //
            // write a v3 private key
            //
            bOut = new ByteArrayOutputStream();
            pOut = new BCPGOutputStream(bOut);

            pgpPriv.encode(pOut);

            if (!areEqual(bOut.toByteArray(), testPrivKeyV3))
            {
                fail("private key V3 rewrite failed");
            }
        }

        //
        // Read the private key
        //
        PGPSecretKeyRing pgpPriv = new PGPSecretKeyRing(testPrivKey, new BcKeyFingerprintCalculator());
        PGPPrivateKey pgpPrivKey = pgpPriv.getSecretKey().extractPrivateKey(new BcPBESecretKeyDecryptorBuilder(new BcPGPDigestCalculatorProvider()).build(pass));
       
        //
        // write a private key
        //
        bOut = new ByteArrayOutputStream();
        pOut = new BCPGOutputStream(bOut);
       
        pgpPriv.encode(pOut);

        if (!areEqual(bOut.toByteArray(), testPrivKey))   
        {
            fail("private key rewrite failed");
        }
       

        //
        // test encryption
        //
        Cipher c = Cipher.getInstance("RSA", "BC");

        c.init(Cipher.ENCRYPT_MODE, pubKey);
       
        byte[]  in = "hello world".getBytes();

        byte[]  out = c.doFinal(in);
       
        c.init(Cipher.DECRYPT_MODE, pgpPrivKey.getKey());
       
        out = c.doFinal(out);
       
        if (!areEqual(in, out))
        {
            fail("decryption failed.");
        }

        //
        // test signature message
        //
        PGPObjectFactory           pgpFact = new PGPObjectFactory(sig1, new BcKeyFingerprintCalculator());

        PGPCompressedData          c1 = (PGPCompressedData)pgpFact.nextObject();

        pgpFact = new PGPObjectFactory(c1.getDataStream(), new BcKeyFingerprintCalculator());
       
        PGPOnePassSignatureList    p1 = (PGPOnePassSignatureList)pgpFact.nextObject();
       
        PGPOnePassSignature        ops = p1.get(0);
       
        PGPLiteralData             p2 = (PGPLiteralData)pgpFact.nextObject();

        InputStream                dIn = p2.getInputStream();
        int                        ch;

        ops.init(new BcPGPContentVerifierBuilderProvider(), pgpPub.getPublicKey(ops.getKeyID()));
       
        while ((ch = dIn.read()) >= 0)
        {
            ops.update((byte)ch);
        }

        PGPSignatureList                        p3 = (PGPSignatureList)pgpFact.nextObject();

        if (!ops.verify(p3.get(0)))
        {
            fail("Failed signature check");
        }
       
        //
        // encrypted message - read subkey
        //
        pgpPriv = new PGPSecretKeyRing(subKey, new BcKeyFingerprintCalculator());

        //
        // encrypted message
        //
        byte[]    text = { (byte)'h', (byte)'e', (byte)'l', (byte)'l', (byte)'o', (byte)' ', (byte)'w', (byte)'o', (byte)'r', (byte)'l', (byte)'d', (byte)'!', (byte)'\n' };
       
        PGPObjectFactory pgpF = new PGPObjectFactory(enc1, new BcKeyFingerprintCalculator());

        PGPEncryptedDataList            encList = (PGPEncryptedDataList)pgpF.nextObject();
   
        PGPPublicKeyEncryptedData    encP = (PGPPublicKeyEncryptedData)encList.get(0);
       
        pgpPrivKey = pgpPriv.getSecretKey(encP.getKeyID()).extractPrivateKey(new BcPBESecretKeyDecryptorBuilder(new BcPGPDigestCalculatorProvider()).build(pass));

        InputStream clear = encP.getDataStream(new BcPublicKeyDataDecryptorFactory(pgpPrivKey));
                
        pgpFact = new PGPObjectFactory(clear, new BcKeyFingerprintCalculator());

        c1 = (PGPCompressedData)pgpFact.nextObject();

        pgpFact = new PGPObjectFactory(c1.getDataStream(), new BcKeyFingerprintCalculator());
       
        PGPLiteralData    ld = (PGPLiteralData)pgpFact.nextObject();
   
        bOut = new ByteArrayOutputStream();
       
        if (!ld.getFileName().equals("test.txt"))
        {
            throw new RuntimeException("wrong filename in packet");
        }

        InputStream    inLd = ld.getDataStream();
       
        while ((ch = inLd.read()) >= 0)
        {
            bOut.write(ch);
        }

        if (!areEqual(bOut.toByteArray(), text))
        {
            fail("wrong plain text in decrypted packet");
        }

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

        cOut.write(shortText);

        cOut.close();

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

        encList = (PGPEncryptedDataList)pgpF.nextObject();
   
        encP = (PGPPublicKeyEncryptedData)encList.get(0);
       
        pgpPrivKey = pgpPriv.getSecretKey(encP.getKeyID()).extractPrivateKey(new BcPBESecretKeyDecryptorBuilder(new BcPGPDigestCalculatorProvider()).build(pass));

        PublicKeyDataDecryptorFactory dataDecryptorFactory = new BcPublicKeyDataDecryptorFactory(pgpPrivKey);

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

        clear = encP.getDataStream(dataDecryptorFactory);
       
        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(new BcPGPDataEncryptorBuilder(SymmetricKeyAlgorithmTags.CAST5).setSecureRandom(new SecureRandom()));
        puK = pgpPriv.getSecretKey(encP.getKeyID()).getPublicKey();
       
        cPk.addMethod(new BcPublicKeyKeyEncryptionMethodGenerator(puK));

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

        cOut.write(text);

        cOut.close();

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

        encList = (PGPEncryptedDataList)pgpF.nextObject();
   
        encP = (PGPPublicKeyEncryptedData)encList.get(0);
       
        pgpPrivKey = pgpPriv.getSecretKey(encP.getKeyID()).extractPrivateKey(new BcPBESecretKeyDecryptorBuilder(new BcPGPDigestCalculatorProvider()).build(pass));

        clear = encP.getDataStream(new BcPublicKeyDataDecryptorFactory(pgpPrivKey));
       
        bOut.reset();
       
        while ((ch = clear.read()) >= 0)
        {
            bOut.write(ch);
        }

        out = bOut.toByteArray();

        if (!areEqual(out, text))
        {
            fail("wrong plain text in generated packet");
        }
       
        //
        // read public key with sub key.
        //
        pgpF = new PGPObjectFactory(subPubKey, new BcKeyFingerprintCalculator());
        Object    o;
       
        while ((o = pgpFact.nextObject()) != null)
        {
            // System.out.println(o);
        }

        //
        // key pair generation - CAST5 encryption
        //
        char[]                    passPhrase = "hello".toCharArray();
       
        KeyPairGenerator    kpg = KeyPairGenerator.getInstance("RSA", "BC");
   
        kpg.initialize(1024);
   
        KeyPair                    kp = kpg.generateKeyPair();

        PGPSecretKey    secretKey = new PGPSecretKey(PGPSignature.DEFAULT_CERTIFICATION, PublicKeyAlgorithmTags.RSA_GENERAL, kp.getPublic(), kp.getPrivate(), new Date(), "fred", null, null, new JcaPGPContentSignerBuilder(PublicKeyAlgorithmTags.RSA_GENERAL, HashAlgorithmTags.SHA1).setProvider("BC"), new BcPBESecretKeyEncryptorBuilder(SymmetricKeyAlgorithmTags.CAST5).build(passPhrase));
   
        PGPPublicKey    key = secretKey.getPublicKey();

        it = key.getUserIDs();

        uid = (String)it.next();

        it = key.getSignaturesForID(uid);

        sig = (PGPSignature)it.next();

        sig.init(new BcPGPContentVerifierBuilderProvider(), key);

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

        pgpPrivKey = secretKey.extractPrivateKey(new BcPBESecretKeyDecryptorBuilder(new BcPGPDigestCalculatorProvider()).build(passPhrase));
       
        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(new BcPGPContentSignerBuilder(PublicKeyAlgorithmTags.RSA_GENERAL, HashAlgorithmTags.SHA1));
       
        sGen.init(PGPSignature.KEY_REVOCATION, secretKey.extractPrivateKey(new BcPBESecretKeyDecryptorBuilder(new BcPGPDigestCalculatorProvider()).build(passPhrase)));

        sig = sGen.generateCertification(key);

        key = PGPPublicKey.addCertification(key, sig);

        keyEnc = key.getEncoded();

        PGPPublicKeyRing    tmpRing = new PGPPublicKeyRing(keyEnc, new BcKeyFingerprintCalculator());

        key = tmpRing.getPublicKey();

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

        sig = (PGPSignature)sgIt.next();
View Full Code Here

Examples of org.bouncycastle.openpgp.PGPPublicKeyRing

        byte[]        encodedRing,
        int           masterDays,
        int           subKeyDays)
        throws Exception
    {           
        PGPPublicKeyRing pubRing = new PGPPublicKeyRing(encodedRing, new BcKeyFingerprintCalculator());
        PGPPublicKey k = pubRing.getPublicKey();
       
        if (k.getValidDays() != masterDays)
        {
            fail("mismatch on master valid days.");
        }
       
        Iterator it = pubRing.getPublicKeys();
       
        it.next();
       
        k = (PGPPublicKey)it.next();
       
View Full Code Here

Examples of org.bouncycastle.openpgp.PGPPublicKeyRing

       
        Iterator    rIt = pubRings.getKeyRings();
       
        while (rIt.hasNext())
        {
            PGPPublicKeyRing                    pgpPub = (PGPPublicKeyRing)rIt.next();
           
            count++;
           
            int    keyCount = 0;
           
            byte[]    bytes = pgpPub.getEncoded();
           
            pgpPub = new PGPPublicKeyRing(bytes, new BcKeyFingerprintCalculator());
           
            Iterator    it = pgpPub.getPublicKeys();
            while (it.hasNext())
            {
                keyCount++;

                PGPPublicKey pubK = (PGPPublicKey)it.next();
View Full Code Here

Examples of org.bouncycastle.openpgp.PGPPublicKeyRing

       
        Iterator    rIt = pubRings.getKeyRings();
       
        while (rIt.hasNext())
        {
            PGPPublicKeyRing                    pgpPub = (PGPPublicKeyRing)rIt.next();
           
            count++;
           
            int    keyCount = 0;
           
            byte[]    bytes = pgpPub.getEncoded();
           
            pgpPub = new PGPPublicKeyRing(bytes, new BcKeyFingerprintCalculator());
           
            Iterator    it = pgpPub.getPublicKeys();
            while (it.hasNext())
            {
                keyCount++;

                it.next();
View Full Code Here

Examples of org.bouncycastle.openpgp.PGPPublicKeyRing

        PGPPublicKeyRingCollection  pubRings = new PGPPublicKeyRingCollection(pub6);
        Iterator                    rIt = pubRings.getKeyRings();

        while (rIt.hasNext())
        {
            PGPPublicKeyRing    pgpPub = (PGPPublicKeyRing)rIt.next();
            Iterator            it = pgpPub.getPublicKeys();
            while (it.hasNext())
            {
                PGPPublicKey    k = (PGPPublicKey)it.next();

                if (k.getKeyID() == 0x5ce086b5b5a18ff4L)
View Full Code Here

Examples of org.bouncycastle.openpgp.PGPPublicKeyRing

    }

    public void test7()
        throws Exception
    {
        PGPPublicKeyRing    pgpPub = new PGPPublicKeyRing(pub7, new BcKeyFingerprintCalculator());
        Iterator            it = pgpPub.getPublicKeys();
        PGPPublicKey        masterKey = null;

        while (it.hasNext())
        {
            PGPPublicKey    k = (PGPPublicKey)it.next();
View Full Code Here

Examples of org.bouncycastle.openpgp.PGPPublicKeyRing

       
        Iterator    rIt = pubRings.getKeyRings();
       
        while (rIt.hasNext())
        {
            PGPPublicKeyRing          pgpPub = (PGPPublicKeyRing)rIt.next();
           
            count++;
           
            int    keyCount = 0;
           
            byte[]    bytes = pgpPub.getEncoded();
           
            pgpPub = new PGPPublicKeyRing(bytes, new BcKeyFingerprintCalculator());
           
            Iterator    it = pgpPub.getPublicKeys();
            while (it.hasNext())
            {
                keyCount++;

                it.next();
View Full Code Here

Examples of org.bouncycastle.openpgp.PGPPublicKeyRing

            {
                fail("seconds wrong on secret key ring");
            }
        }
       
        PGPPublicKeyRing    publicRing = new PGPPublicKeyRing(pub10, new BcKeyFingerprintCalculator());
        Iterator            publicKeys = publicRing.getPublicKeys();
       
        while (publicKeys.hasNext())
        {
            PGPPublicKey pubKey = (PGPPublicKey)publicKeys.next();
View Full Code Here

Examples of org.bouncycastle.openpgp.PGPPublicKeyRing

   
        PGPSecretKeyRing       keyRing = keyRingGen.generateSecretKeyRing();
       
        keyRing.getSecretKey().extractPrivateKey(new BcPBESecretKeyDecryptorBuilder(new BcPGPDigestCalculatorProvider()).build(passPhrase));
       
        PGPPublicKeyRing        pubRing = keyRingGen.generatePublicKeyRing();
       
        PGPPublicKey            vKey = null;
        PGPPublicKey            sKey = null;
       
        Iterator                    it = pubRing.getPublicKeys();
        while (it.hasNext())
        {
            PGPPublicKey    pk = (PGPPublicKey)it.next();
            if (pk.isMasterKey())
            {
View Full Code Here

Examples of org.bouncycastle.openpgp.PGPPublicKeyRing

        PGPKeyPair        rsaKeyPair2 = new PGPKeyPair(PGPPublicKey.RSA_GENERAL, rsaKp, new Date());

        PGPKeyRingGenerator    keyRingGen = new PGPKeyRingGenerator(PGPSignature.POSITIVE_CERTIFICATION, rsaKeyPair1,
                "test", PGPEncryptedData.AES_256, passPhrase, null, null, new SecureRandom(), "BC");
        PGPSecretKeyRing       secRing1 = keyRingGen.generateSecretKeyRing();
        PGPPublicKeyRing       pubRing1 = keyRingGen.generatePublicKeyRing();
        keyRingGen = new PGPKeyRingGenerator(PGPSignature.POSITIVE_CERTIFICATION, rsaKeyPair2,
                "test", PGPEncryptedData.AES_256, passPhrase, null, null, new SecureRandom(), "BC");
        PGPSecretKeyRing       secRing2 = keyRingGen.generateSecretKeyRing();
        PGPPublicKeyRing       pubRing2 = keyRingGen.generatePublicKeyRing();

        try
        {
            PGPPublicKeyRing.insertPublicKey(pubRing1, pubRing2.getPublicKey());
            fail("adding second master key (public) should throw an IllegalArgumentException");
        }
        catch (IllegalArgumentException e)
        {
            if (!e.getMessage().equals("cannot add a master key to a ring that already has one"))
View Full Code Here
TOP
Copyright © 2018 www.massapi.com. 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.