I'll be back!

  Focus on BPM, celebrate PegaRULES Process Commander (PRPC)
posts - 76, comments - 161, trackbacks - 0, articles - 2
  BlogJava :: 首页 :: 新随笔 :: 联系 :: 聚合  :: 管理

基于Java Bouncy Castle的PGP加密解密示例

Posted on 2013-05-24 08:37 zolly 阅读(13816) 评论(23)  编辑  收藏
PGP即Pretty Good Privacy,是一个基于RSA公钥&私钥及AES等非对称加密算法的加密软件系列,比较具有代表性加密解密客户端已被Symantec收购,详见www.pgp.com,在Symantec的网站上可以下载最新版客户端软件。

本文讲的是使用Java基于Bouncy Castle包的PGP加密解密示例,按照以下步骤即可轻松实现:

1. 客户端软件
由于Symantec的PGP客户端是收费软件,本文只需要使用其中的生成秘钥和加密文件的功能,用该软件有些浪费,所以我下载了Java 免费版的Portable PGP(http://sourceforge.net/projects/ppgp/)

2. 下载Bouncy Castle包
http://www.bouncycastle.org/latest_releases.html
bcprov-jdk15on-148.jar和bcpg-jdk15on-148.jar
Bouncy Castle支持大量的密码术算法,其中包括OpenPGP,引用很广泛,Pega就是使用Bouncy Castle对邮件和客户重要数据进行加密解密的。
它既可以安装成JDK扩展也可以放到特定java项目中使用。

3. 在Oracle官网下载UnlimitedJCEPolicy
http://www.oracle.com/technetwork/java/javase/downloads/jce-7-download-432124.html
JDK默认Policy只能支持<=128位Key,GPG的密钥从1024-2048,所以必须扩展该Policy。具体安装方法参考文件中的ReadMe文件。

4. 编写代码
使用Portable PGP加密文件后生成Public Key和Private Key。把试用Public Key加密的文件"Encrypted File.txt"和Private Key "Key.asc"一同放到项目文件夹中。
使用以下代码即可解密文件。(拷贝以下代码可以直接执行,以下代码也是来自Bouncy Castle源代码包)

PGPExampleUtil.java
package com.zolly.bouncycastle;

import java.io.BufferedInputStream;
import java.io.ByteArrayOutputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.security.NoSuchProviderException;
import java.util.Iterator;

import org.bouncycastle.openpgp.PGPCompressedDataGenerator;
import org.bouncycastle.openpgp.PGPException;
import org.bouncycastle.openpgp.PGPLiteralData;
import org.bouncycastle.openpgp.PGPPrivateKey;
import org.bouncycastle.openpgp.PGPPublicKey;
import org.bouncycastle.openpgp.PGPPublicKeyRing;
import org.bouncycastle.openpgp.PGPPublicKeyRingCollection;
import org.bouncycastle.openpgp.PGPSecretKey;
import org.bouncycastle.openpgp.PGPSecretKeyRing;
import org.bouncycastle.openpgp.PGPSecretKeyRingCollection;
import org.bouncycastle.openpgp.PGPUtil;

class PGPExampleUtil
{
    static byte[] compressFile(String fileName, int algorithm) throws IOException
    {
        ByteArrayOutputStream bOut = new ByteArrayOutputStream();
        PGPCompressedDataGenerator comData = new PGPCompressedDataGenerator(algorithm);
        PGPUtil.writeFileToLiteralData(comData.open(bOut), PGPLiteralData.BINARY,
            new File(fileName));
        comData.close();
        return bOut.toByteArray();
    }

    /**
     * Search a secret key ring collection for a secret key corresponding to keyID if it
     * exists.
     * 
     * 
@param pgpSec a secret key ring collection.
     * 
@param keyID keyID we want.
     * 
@param pass passphrase to decrypt secret key with.
     * 
@return
     * 
@throws PGPException
     * 
@throws NoSuchProviderException
     
*/
    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");
    }

    static PGPPublicKey readPublicKey(String fileName) throws IOException, PGPException
    {
        InputStream keyIn = new BufferedInputStream(new FileInputStream(fileName));
        PGPPublicKey pubKey = readPublicKey(keyIn);
        keyIn.close();
        return pubKey;
    }

    /**
     * A simple routine that opens a key ring file and loads the first available key
     * suitable for encryption.
     * 
     * 
@param input
     * 
@return
     * 
@throws IOException
     * 
@throws PGPException
     
*/
    static PGPPublicKey readPublicKey(InputStream input) throws IOException, PGPException
    {
        PGPPublicKeyRingCollection pgpPub = new PGPPublicKeyRingCollection(
            PGPUtil.getDecoderStream(input));

        //
        
// we just loop through the collection till we find a key suitable for encryption, in the real
        
// world you would probably want to be a bit smarter about this.
        
//

        Iterator keyRingIter = pgpPub.getKeyRings();
        while (keyRingIter.hasNext())
        {
            PGPPublicKeyRing keyRing = (PGPPublicKeyRing)keyRingIter.next();

            Iterator keyIter = keyRing.getPublicKeys();
            while (keyIter.hasNext())
            {
                PGPPublicKey key = (PGPPublicKey)keyIter.next();

                if (key.isEncryptionKey())
                {
                    return key;
                }
            }
        }

        throw new IllegalArgumentException("Can't find encryption key in key ring.");
    }

    static PGPSecretKey readSecretKey(String fileName) throws IOException, PGPException
    {
        InputStream keyIn = new BufferedInputStream(new FileInputStream(fileName));
        PGPSecretKey secKey = readSecretKey(keyIn);
        keyIn.close();
        return secKey;
    }

    /**
     * A simple routine that opens a key ring file and loads the first available key
     * suitable for signature generation.
     * 
     * 
@param input stream to read the secret key ring collection from.
     * 
@return a secret key.
     * 
@throws IOException on a problem with using the input stream.
     * 
@throws PGPException if there is an issue parsing the input stream.
     
*/
    static PGPSecretKey readSecretKey(InputStream input) throws IOException, PGPException
    {
        PGPSecretKeyRingCollection pgpSec = new PGPSecretKeyRingCollection(
            PGPUtil.getDecoderStream(input));

        //
        
// we just loop through the collection till we find a key suitable for encryption, in the real
        
// world you would probably want to be a bit smarter about this.
        
//

        Iterator keyRingIter = pgpSec.getKeyRings();
        while (keyRingIter.hasNext())
        {
            PGPSecretKeyRing keyRing = (PGPSecretKeyRing)keyRingIter.next();

            Iterator keyIter = keyRing.getSecretKeys();
            while (keyIter.hasNext())
            {
                PGPSecretKey key = (PGPSecretKey)keyIter.next();

                if (key.isSigningKey())
                {
                    return key;
                }
            }
        }

        throw new IllegalArgumentException("Can't find signing key in key ring.");
    }
}

KeyBasedLargeFileProcessor.java
package com.zolly.bouncycastle;

import java.io.BufferedInputStream;
import java.io.BufferedOutputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.security.NoSuchProviderException;
import java.security.SecureRandom;
import java.security.Security;
import java.util.Iterator;

import org.bouncycastle.bcpg.ArmoredOutputStream;
import org.bouncycastle.jce.provider.BouncyCastleProvider;
import org.bouncycastle.openpgp.PGPCompressedData;
import org.bouncycastle.openpgp.PGPCompressedDataGenerator;
import org.bouncycastle.openpgp.PGPEncryptedData;
import org.bouncycastle.openpgp.PGPEncryptedDataGenerator;
import org.bouncycastle.openpgp.PGPEncryptedDataList;
import org.bouncycastle.openpgp.PGPException;
import org.bouncycastle.openpgp.PGPLiteralData;
import org.bouncycastle.openpgp.PGPObjectFactory;
import org.bouncycastle.openpgp.PGPOnePassSignatureList;
import org.bouncycastle.openpgp.PGPPrivateKey;
import org.bouncycastle.openpgp.PGPPublicKey;
import org.bouncycastle.openpgp.PGPPublicKeyEncryptedData;
import org.bouncycastle.openpgp.PGPSecretKeyRingCollection;
import org.bouncycastle.openpgp.PGPUtil;
import org.bouncycastle.openpgp.operator.jcajce.JcePGPDataEncryptorBuilder;
import org.bouncycastle.openpgp.operator.jcajce.JcePublicKeyDataDecryptorFactoryBuilder;
import org.bouncycastle.openpgp.operator.jcajce.JcePublicKeyKeyEncryptionMethodGenerator;
import org.bouncycastle.util.io.Streams;

/**
 * A simple utility class that encrypts/decrypts public key based
 * encryption large files.
 
*/
public class KeyBasedLargeFileProcessor
{
    public static void decryptFile(
        String inputFileName,
        String keyFileName,
        char[] passwd,
        String defaultFileName)
        throws IOException, NoSuchProviderException
    {
        InputStream in = new BufferedInputStream(new FileInputStream(inputFileName));
        InputStream keyIn = new BufferedInputStream(new FileInputStream(keyFileName));
        decryptFile(in, keyIn, passwd, defaultFileName);
        keyIn.close();
        in.close();
    }
    
    /**
     * decrypt the passed in message stream
     
*/
    public static void decryptFile(
        InputStream in,
        InputStream keyIn,
        char[]      passwd,
        String      defaultFileName)
        throws IOException, NoSuchProviderException
    {    
        in = PGPUtil.getDecoderStream(in);
        
        try
        {
            PGPObjectFactory        pgpF = new PGPObjectFactory(in);
            PGPEncryptedDataList    enc;

            Object                  o = pgpF.nextObject();
            //
            
// the first object might be a PGP marker packet.
            
//
            if (o instanceof PGPEncryptedDataList)
            {
                enc = (PGPEncryptedDataList)o;
            }
            else
            {
                enc = (PGPEncryptedDataList)pgpF.nextObject();
            }
            
            //
            
// find the secret key
            
//
            Iterator                    it = enc.getEncryptedDataObjects();
            PGPPrivateKey               sKey = null;
            PGPPublicKeyEncryptedData   pbe = null;
            PGPSecretKeyRingCollection  pgpSec = new PGPSecretKeyRingCollection(
                PGPUtil.getDecoderStream(keyIn));                                                                 
            
            while (sKey == null && it.hasNext())
            {
                pbe = (PGPPublicKeyEncryptedData)it.next();
                
                sKey = PGPExampleUtil.findSecretKey(pgpSec, pbe.getKeyID(), passwd);
            }
            
            if (sKey == null)
            {
                throw new IllegalArgumentException("secret key for message not found.");
            }
            
            InputStream         clear = pbe.getDataStream(new JcePublicKeyDataDecryptorFactoryBuilder().setProvider("BC").build(sKey));
            
            PGPObjectFactory    plainFact = new PGPObjectFactory(clear);
            
            PGPCompressedData   cData = (PGPCompressedData)plainFact.nextObject();
    
            InputStream         compressedStream = new BufferedInputStream(cData.getDataStream());
            PGPObjectFactory    pgpFact = new PGPObjectFactory(compressedStream);
            
            Object              message = pgpFact.nextObject();
            
            if (message instanceof PGPLiteralData)
            {
                PGPLiteralData ld = (PGPLiteralData)message;

                String outFileName = ld.getFileName();
                if (outFileName.length() == 0)
                {
                    outFileName = defaultFileName;
                }

                InputStream unc = ld.getInputStream();
                OutputStream fOut =  new BufferedOutputStream(new FileOutputStream(outFileName));

                Streams.pipeAll(unc, fOut);

                fOut.close();
            }
            else if (message instanceof PGPOnePassSignatureList)
            {
                throw new PGPException("encrypted message contains a signed message - not literal data.");
            }
            else
            {
                throw new PGPException("message is not a simple encrypted file - type unknown.");
            }

            if (pbe.isIntegrityProtected())
            {
                if (!pbe.verify())
                {
                    System.err.println("message failed integrity check");
                }
                else
                {
                    System.err.println("message integrity check passed");
                }
            }
            else
            {
                System.err.println("no message integrity check");
            }
        }
        catch (PGPException e)
        {
            System.err.println(e);
            if (e.getUnderlyingException() != null)
            {
                e.getUnderlyingException().printStackTrace();
            }
        }
    }

    public static void encryptFile(
        String          outputFileName,
        String          inputFileName,
        String          encKeyFileName,
        boolean         armor,
        boolean         withIntegrityCheck)
        throws IOException, NoSuchProviderException, PGPException
    {
        OutputStream out = new BufferedOutputStream(new FileOutputStream(outputFileName));
        PGPPublicKey encKey = PGPExampleUtil.readPublicKey(encKeyFileName);
        encryptFile(out, inputFileName, encKey, armor, withIntegrityCheck);
        out.close();
    }

    public static void encryptFile(
        OutputStream    out,
        String          fileName,
        PGPPublicKey    encKey,
        boolean         armor,
        boolean         withIntegrityCheck)
        throws IOException, NoSuchProviderException
    {    
        if (armor)
        {
            out = new ArmoredOutputStream(out);
        }
        
        try
        {    
            PGPEncryptedDataGenerator   cPk = new PGPEncryptedDataGenerator(new JcePGPDataEncryptorBuilder(PGPEncryptedData.CAST5).setWithIntegrityPacket(withIntegrityCheck).setSecureRandom(new SecureRandom()).setProvider("BC"));
                
            cPk.addMethod(new JcePublicKeyKeyEncryptionMethodGenerator(encKey).setProvider("BC"));
            
            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]);
            
            comData.close();
            
            cOut.close();

            if (armor)
            {
                out.close();
            }
        }
        catch (PGPException e)
        {
            System.err.println(e);
            if (e.getUnderlyingException() != null)
            {
                e.getUnderlyingException().printStackTrace();
            }
        }
    }

    public static void main(
        String[] args)
        throws Exception
    {
        Security.addProvider(new BouncyCastleProvider());
        
        decryptFile("Encypted File.txt.pgp", "Key.asc", "123456789".toCharArray(), "Encypted File.txt");
    }
}

评论

# re: 基于Java Bouncy Castle的PGP加密解密示例  回复  更多评论   

2014-10-23 15:29 by
你好
调用encryptFile("D:/sftp/serverDownload/ISI1000000218_STOCK_02_20141008.PGP", "D:/sftp/serverDownload/ISI1000000218_STOCK_02_20141008.TXT","D:/sushuiyou.asc",true,true);加密没有问题;但调用decryptFile("D:/sftp/serverDownload/ISI1000000218_STOCK_02_20141008.PGP", "D:/sushuiyou.asc", "admin123".toCharArray(), "D:/sftp/serverDownload/ISI1000000218_STOCK_02_20141008.TXT");解密时,在PGPSecretKeyRingCollection pgpSec = new PGPSecretKeyRingCollection(PGPUtil.getDecoderStream(keyIn));
这步,报org.bouncycastle.openpgp.PGPException: org.bouncycastle.openpgp.PGPPublicKeyRing found where PGPSecretKeyRing expected错误,是怎么回事啊,多谢

# re: 基于Java Bouncy Castle的PGP加密解密示例  回复  更多评论   

2014-12-16 18:55 by fedro
这是因为需要的是私钥,但你提供的是公钥

# re: 基于Java Bouncy Castle的PGP加密解密示例  回复  更多评论   

2014-12-26 15:02 by bu 李
Exception in thread "main" java.lang.ClassCastException: org.bouncycastle.openpgp.PGPLiteralData cannot be cast to org.bouncycastle.openpgp.PGPCompressedData
at com.bypay.balance.KeyBasedLargeFileProcessor.decryptFile(KeyBasedLargeFileProcessor.java:131)
at com.bypay.balance.KeyBasedLargeFileProcessor.decryptFile(KeyBasedLargeFileProcessor.java:52)
at com.bypay.balance.KeyBasedLargeFileProcessor.main(KeyBasedLargeFileProcessor.java:258)
这个错误时哪里的问题?

# re: 基于Java Bouncy Castle的PGP加密解密示例  回复  更多评论   

2015-03-12 09:52 by Terrence_Wang
楼主大哥还有各位大哥帮帮忙~~我按照上面的方法解密不行,如下:
以下这段是解密中的一段代码,取出来的第一个object确实是markerpacket,但是进入到else中取出的第二个object的根本没有值。直接就报错了。
Object o = pgpF.nextObject();
//
// the first object might be a PGP marker packet.
//
if (o instanceof PGPEncryptedDataList)
{
enc = (PGPEncryptedDataList)o;
}
else
{
enc = (PGPEncryptedDataList)pgpF.nextObject();
}

错误信息:
Exception in thread "main" java.lang.NoClassDefFoundError: org/bouncycastle/util/io/TeeInputStream
at org.bouncycastle.openpgp.PGPEncryptedDataList.<init>(Unknown Source)
at org.bouncycastle.openpgp.PGPObjectFactory.nextObject(Unknown Source)
at org.bouncycastle.openpgp.examples.KeyBasedFileProcessor.decryptFile(KeyBasedFileProcessor.java:99)
at org.bouncycastle.openpgp.examples.KeyBasedFileProcessor.decryptFile(KeyBasedFileProcessor.java:67)
at org.bouncycastle.openpgp.examples.KeyBasedFileProcessor.main(KeyBasedFileProcessor.java:259)
Caused by: java.lang.ClassNotFoundException: org.bouncycastle.util.io.TeeInputStream
at java.net.URLClassLoader$1.run(URLClassLoader.java:202)
at java.security.AccessController.doPrivileged(Native Method)
at java.net.URLClassLoader.findClass(URLClassLoader.java:190)
at java.lang.ClassLoader.loadClass(ClassLoader.java:306)
at sun.misc.Launcher$AppClassLoader.loadClass(Launcher.java:301)
at java.lang.ClassLoader.loadClass(ClassLoader.java:247)
... 5 more
求各位大侠帮帮忙。。救救我啊。。谢谢了。

# re: 基于Java Bouncy Castle的PGP加密解密示例  回复  更多评论   

2015-03-12 10:00 by Terrence_Wang
@信
前辈帮帮忙~~想问下一个有关java pgp解密的问题。谢谢了。我按照上面的方法解密不行,如下:
以下这段是解密中的一段代码,取出来的第一个object确实是markerpacket,但是进入到else中取出的第二个object的根本没有值。直接就报错了。
Object o = pgpF.nextObject();
//
// the first object might be a PGP marker packet.
//
if (o instanceof PGPEncryptedDataList)
{
enc = (PGPEncryptedDataList)o;
}
else
{
enc = (PGPEncryptedDataList)pgpF.nextObject();
}

错误信息:
Exception in thread "main" java.lang.NoClassDefFoundError: org/bouncycastle/util/io/TeeInputStream
at org.bouncycastle.openpgp.PGPEncryptedDataList.<init>(Unknown Source)
at org.bouncycastle.openpgp.PGPObjectFactory.nextObject(Unknown Source)
at org.bouncycastle.openpgp.examples.KeyBasedFileProcessor.decryptFile(KeyBasedFileProcessor.java:99)
at org.bouncycastle.openpgp.examples.KeyBasedFileProcessor.decryptFile(KeyBasedFileProcessor.java:67)
at org.bouncycastle.openpgp.examples.KeyBasedFileProcessor.main(KeyBasedFileProcessor.java:259)
Caused by: java.lang.ClassNotFoundException: org.bouncycastle.util.io.TeeInputStream
at java.net.URLClassLoader$1.run(URLClassLoader.java:202)
at java.security.AccessController.doPrivileged(Native Method)
at java.net.URLClassLoader.findClass(URLClassLoader.java:190)
at java.lang.ClassLoader.loadClass(ClassLoader.java:306)
at sun.misc.Launcher$AppClassLoader.loadClass(Launcher.java:301)
at java.lang.ClassLoader.loadClass(ClassLoader.java:247)
... 5 more
求各位大侠帮帮忙。。救救我啊。。谢谢了。

# re: 基于Java Bouncy Castle的PGP加密解密示例  回复  更多评论   

2015-03-13 16:11 by hunger
好东西,亲测有用

# re: 基于Java Bouncy Castle的PGP加密解密示例  回复  更多评论   

2015-03-13 16:15 by hunger
@Terrence_Wang
你的工程没有导入bcprov-jdk15on-152.jar包吧

# re: 基于Java Bouncy Castle的PGP加密解密示例  回复  更多评论   

2015-03-16 08:46 by Terrence_Wang
@hunger

哎呀~~~我还以为不会有人回我呢~我太鸡动了。。。小弟先谢谢了!!我导入了jar包了。我也很奇怪。。。该导入的包我都导入了。而且我看了一下bcprov-jdk15on-152.jar下的原码,根本就没有org/bouncycastle/util/io/TeeInputStream 这个类。bcpg-jdk15on-152.jar;bcprov-jdk15on-152.jar我都换着试过,都是一样的错。 以下是我导入的包:bcpg-jdk15on-151.jar ;bcprov-jdk15on-151.jar;bouncycastle.jar;local_policy.jar;US_export_policy.jar;

有一个地方比较奇怪,就是以下这段是解密中的一段代码,取出来的第一个object确实是markerpacket,但是进入到else中取出的第二个object的根本没有值。直接就报错了。
Object o = pgpF.nextObject();
//
// the first object might be a PGP marker packet.
//
if (o instanceof PGPEncryptedDataList)
{
enc = (PGPEncryptedDataList)o;
}
else
{
enc = (PGPEncryptedDataList)pgpF.nextObject();
}
大哥,救救小弟吧。。。客户那边要把我逼疯了。网上的资源特别少。还望您给指点一下。这个是我的qq:925953813 。 小弟先谢过了。

# re: 基于Java Bouncy Castle的PGP加密解密示例  回复  更多评论   

2015-03-17 09:00 by Terrence_Wang
@hunger
大哥 帮帮忙看看呗。

# re: 基于Java Bouncy Castle的PGP加密解密示例  回复  更多评论   

2015-03-17 11:12 by hunger
@Terrence_Wang
真实尴尬,其实我也是第一次用这个东西,看着楼主的提示然后到官网下载,如楼主所说,我就导入了两个jar包bcpg-jdk15on-152.jar、bcprov-jdk15on-152.jar,还有就是到sun官网下载对应jdk版本的jce更换到jdk1.7.0_71\jre\lib\security目录下的两个文件,后面就直接修改bcpg-jdk15on-152.jar包中的KeyBasedLargeFileProcessor进行解密,其中也遇到几个问题,但是就是没有遇到你的问题

# re: 基于Java Bouncy Castle的PGP加密解密示例  回复  更多评论   

2015-03-17 11:16 by hunger
@Terrence_Wang
local_policy.jar;US_export_policy.jar这两个包不是导入工程的,这点楼主可能说的不是很清楚,是要放到你的jre目录下的lib/security目录下的,这个我也是在其他文章中看到的

# re: 基于Java Bouncy Castle的PGP加密解密示例  回复  更多评论   

2015-03-17 14:18 by Terrence_Wang
@hunger

那bouncycastle.jar 这个包也要导入吧? 我按照你的方法试了,但还是一样。我一直做的都是解密。我在想会不会是我的解密.asc 的问题。大哥,能告诉下你的qq吗? 或者麻烦加我一下,我q:925953813 。 我只想要一下你的demo 试一下。我一定会感激你的!!!

# re: 基于Java Bouncy Castle的PGP加密解密示例  回复  更多评论   

2015-03-17 15:08 by hunger
@Terrence_Wang
我没有使用bouncycastle.jar这个包,然后是我是安装了gpg4win-2.2.3.exe来生成公钥加密文件的,然后再用私钥解密,gpg4win-2.2.3.exe安装后有一个Kleopatra的图形化界面,操作挺方便的,下载官网:http://gpg4win.org/download.html。也可以使用bcpg-jdk15on-152.jar包中KeyBasedLargeFileProcessor类中的方法来对文件加密解密,代码加密再用代码解密生成的解密后文件会在你的工程目录中。我这边不允许上qq的。

# re: 基于Java Bouncy Castle的PGP加密解密示例  回复  更多评论   

2015-03-17 15:32 by Terrence_Wang
@hunger
我不用bouncycastle.jar这个包工程是有错的。我也是用gpg软件生成的公钥和密钥。我这怎么就不行呢。能麻烦您下班有时间的时候,能把工程发我一下吗?我参考一下。925953813@qq.com 明天就要向客户交差了,我就差这个地方过不去啊。。。。。 跪谢啊~~~

# re: 基于Java Bouncy Castle的PGP加密解密示例  回复  更多评论   

2015-03-17 15:59 by hunger
@Terrence_Wang
我下班早的话,回去做个demo给你吧

# re: 基于Java Bouncy Castle的PGP加密解密示例  回复  更多评论   

2015-03-17 18:20 by Terrence_Wang
@hunger
太谢谢了~~~~~

# re: 基于Java Bouncy Castle的PGP加密解密示例[未登录]  回复  更多评论   

2015-05-29 13:01 by 123
@hunger
为什么我的解密会进这个判断里,求解

else if (message instanceof PGPOnePassSignatureList)
{
throw new PGPException("encrypted message contains a signed message - not literal data.");
}

# re: 基于Java Bouncy Castle的PGP加密解密示例[未登录]  回复  更多评论   

2015-05-29 13:09 by 123
有人么,,急。。。。。

# re: 基于Java Bouncy Castle的PGP加密解密示例[未登录]  回复  更多评论   

2015-05-29 14:16 by 123
@hunger
pgpe –r AAA –s –u BBB FILE1 –o FILE2 用这种方法加密的,,,应该用哪个方法解密呀,,,求大神啊。。。。

# re: 基于Java Bouncy Castle的PGP加密解密示例  回复  更多评论   

2015-06-25 16:47 by dangzhenjiuhao
你好,用JAVA生成的公钥\私钥跟PGP8.1的生成方式一样吗,如果有对应的私钥,能解密PGP8.1加密的文件吗?

# re: 基于Java Bouncy Castle的PGP加密解密示例  回复  更多评论   

2015-07-27 15:13 by dangzhenjiuhao
可不可以给个DEMO啊,谢谢

# re: 基于Java Bouncy Castle的PGP加密解密示例[未登录]  回复  更多评论   

2016-01-10 12:14 by zhao
PGPEncryptedDataGenerator cPk = new PGPEncryptedDataGenerator(new JcePGPDataEncryptorBuilder(PGPEncryptedData.CAST5).setWithIntegrityPacket(withIntegrityCheck).setSecureRandom(new SecureRandom()).setProvider("BC"));
cPk.addMethod(new JcePublicKeyKeyEncryptionMethodGenerator(encKey).setProvider("BC"));
OutputStream cOut = cPk.open(out, new byte[1 << 16]);
当执行到最后一句时报一下异常:
org.bouncycastle.openpgp.PGPException: cannot create cipher: No such provider: BC
java.security.NoSuchProviderException: No such provider: BC
at javax.crypto.Cipher.getInstance(Cipher.java:588)
at org.bouncycastle.jcajce.util.NamedJcaJceHelper.createCipher(Unknown Source)

请各位大神分析一下这是啥原因?谢谢!!

# re: 基于Java Bouncy Castle的PGP加密解密示例  回复  更多评论   

2016-03-02 10:32 by 毛小龙
对文件进行加密 在测试类里面已经跑通了 抽取出来调用就报这个错 各位帮忙看看是什么原因啊?????
org.bouncycastle.openpgp.PGPException: cannot create cipher: No such provider: BC
java.security.NoSuchProviderException: No such provider: BC
at javax.crypto.Cipher.getInstance(Cipher.java:577)
at org.bouncycastle.jcajce.NamedJcaJceHelper.createCipher(Unknown Source)
at org.bouncycastle.openpgp.operator.jcajce.OperatorHelper.createCipher(Unknown Source)
at org.bouncycastle.openpgp.operator.jcajce.OperatorHelper.createPublicKeyCipher(Unknown Source)
at org.bouncycastle.openpgp.operator.jcajce.JcePublicKeyKeyEncryptionMethodGenerator.encryptSessionInfo(Unknown Source)
at org.bouncycastle.openpgp.operator.PublicKeyKeyEncryptionMethodGenerator.generate(Unknown Source)
at org.bouncycastle.openpgp.PGPEncryptedDataGenerator.open(Unknown Source)
at org.bouncycastle.openpgp.PGPEncryptedDataGenerator.open(Unknown Source)
at cn.chinacloudapp.rosebeautyapp.manager.pgp.KeyBasedLargeFileProcessor.encryptFile(KeyBasedLargeFileProcessor.java:211)
at cn.chinacloudapp.rosebeautyapp.manager.pgp.KeyBasedLargeFileProcessor.encryptFile(KeyBasedLargeFileProcessor.java:188)
at cn.chinacloudapp.rosebeautyapp.manager.util.SftpUpLoad.sftpList(SftpUpLoad.java:44)
at cn.chinacloudapp.rosebeautyapp.manager.util.CSVUtils.exportCsv(CSVUtils.java:84)
at cn.chinacloudapp.rosebeautyapp.manager.dao.GiftDao.startExportRecipientsList(GiftDao.java:364)
at cn.chinacloudapp.rosebeautyapp.manager.service.GiftService.startExportRecipientsList(GiftService.java:698)
at cn.chinacloudapp.rosebeautyapp.app.controller.PushController.startExportRecipientsList(PushController.java:57)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:57)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
at java.lang.reflect.Method.invoke(Method.java:606)
at org.springframework.scheduling.support.ScheduledMethodRunnable.run(ScheduledMethodRunnable.java:65)
at org.springframework.scheduling.support.DelegatingErrorHandlingRunnable.run(DelegatingErrorHandlingRunnable.java:54)
at org.springframework.scheduling.concurrent.ReschedulingRunnable.run(ReschedulingRunnable.java:81)
at java.util.concurrent.Executors$RunnableAdapter.call(Executors.java:471)
at java.util.concurrent.FutureTask.run(FutureTask.java:262)
at java.util.concurrent.ScheduledThreadPoolExecutor$ScheduledFutureTask.access$201(ScheduledThreadPoolExecutor.java:178)
at java.util.concurrent.ScheduledThreadPoolExecutor$ScheduledFutureTask.run(ScheduledThreadPoolExecutor.java:292)
at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1145)
at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:615)
at java.lang.Thread.run(Thread.java:744)

只有注册用户登录后才能发表评论。


网站导航: