I'll be back!

            Focus on BPM, celebrate PegaRULES Process Commander (PRPC)
          posts - 76, comments - 161, trackbacks - 0, articles - 2
            BlogJava :: 首頁 :: 新隨筆 :: 聯系 :: 聚合  :: 管理

          日歷

          <2016年1月>
          272829303112
          3456789
          10111213141516
          17181920212223
          24252627282930
          31123456

          公告

          PegaRULES Process Commander Pega Lead System
          Architect
          PegaRULES Process Commander Pega Senior System
          Architect
          PegaRULES Process Commander Pega System Architect
          Sun Certified Java Programmer Sun Certified Java
          Programmer
          Cognizant Certified PRPC System Architect V4.2 Cognizant Certified
          Pega System Architect
          V4.2, CoreJava
          Macromedia Certified Web Designer Macromedia Certified
          Web Designer
          Email:
          shzolly@gmail.com

          友情鏈接

          搜索

          •  

          最新評論

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

          Posted on 2013-05-24 08:37 zolly 閱讀(13946) 評論(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)

          只有注冊用戶登錄后才能發表評論。


          網站導航:
           
          主站蜘蛛池模板: 中西区| 张家界市| 徐汇区| 辽阳县| 文化| 扎鲁特旗| 昌宁县| 南郑县| 井冈山市| 绥宁县| 正镶白旗| 合阳县| 城步| 健康| 金山区| 读书| 阜康市| 宝鸡市| 西和县| 乡宁县| 建瓯市| 浮山县| 纳雍县| 湄潭县| 雅安市| 潜江市| 天柱县| 三台县| 柘荣县| 华安县| 岗巴县| 孟津县| 财经| 绍兴市| 喀喇| 桐庐县| 康定县| 务川| 巴林左旗| 慈溪市| 孙吴县|