I'll be back!

            Focus on BPM, celebrate PegaRULES Process Commander (PRPC)
          posts - 76, comments - 161, trackbacks - 0, articles - 2
            BlogJava :: 首頁(yè) :: 新隨筆 :: 聯(lián)系 :: 聚合  :: 管理
          PGP即Pretty Good Privacy,是一個(gè)基于RSA公鑰&私鑰及AES等非對(duì)稱(chēng)加密算法的加密軟件系列,比較具有代表性加密解密客戶(hù)端已被Symantec收購(gòu),詳見(jiàn)www.pgp.com,在Symantec的網(wǎng)站上可以下載最新版客戶(hù)端軟件。

          本文講的是使用Java基于Bouncy Castle包的PGP加密解密示例,按照以下步驟即可輕松實(shí)現(xiàn):

          1. 客戶(hù)端軟件
          由于Symantec的PGP客戶(hù)端是收費(fèi)軟件,本文只需要使用其中的生成秘鑰和加密文件的功能,用該軟件有些浪費(fèi),所以我下載了Java 免費(fèi)版的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支持大量的密碼術(shù)算法,其中包括OpenPGP,引用很廣泛,Pega就是使用Bouncy Castle對(duì)郵件和客戶(hù)重要數(shù)據(jù)進(jìn)行加密解密的。
          它既可以安裝成JDK擴(kuò)展也可以放到特定java項(xiàng)目中使用。

          3. 在Oracle官網(wǎng)下載UnlimitedJCEPolicy
          http://www.oracle.com/technetwork/java/javase/downloads/jce-7-download-432124.html
          JDK默認(rèn)Policy只能支持<=128位Key,GPG的密鑰從1024-2048,所以必須擴(kuò)展該P(yáng)olicy。具體安裝方法參考文件中的ReadMe文件。

          4. 編寫(xiě)代碼
          使用Portable PGP加密文件后生成Public Key和Private Key。把試用Public Key加密的文件"Encrypted File.txt"和Private Key "Key.asc"一同放到項(xiàng)目文件夾中。
          使用以下代碼即可解密文件。(拷貝以下代碼可以直接執(zhí)行,以下代碼也是來(lái)自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");
              }
          }

          評(píng)論

          # re: 基于Java Bouncy Castle的PGP加密解密示例  回復(fù)  更多評(píng)論   

          2014-10-23 15:29 by
          你好
          調(diào)用encryptFile("D:/sftp/serverDownload/ISI1000000218_STOCK_02_20141008.PGP", "D:/sftp/serverDownload/ISI1000000218_STOCK_02_20141008.TXT","D:/sushuiyou.asc",true,true);加密沒(méi)有問(wèn)題;但調(diào)用decryptFile("D:/sftp/serverDownload/ISI1000000218_STOCK_02_20141008.PGP", "D:/sushuiyou.asc", "admin123".toCharArray(), "D:/sftp/serverDownload/ISI1000000218_STOCK_02_20141008.TXT");解密時(shí),在PGPSecretKeyRingCollection pgpSec = new PGPSecretKeyRingCollection(PGPUtil.getDecoderStream(keyIn));
          這步,報(bào)org.bouncycastle.openpgp.PGPException: org.bouncycastle.openpgp.PGPPublicKeyRing found where PGPSecretKeyRing expected錯(cuò)誤,是怎么回事啊,多謝

          # re: 基于Java Bouncy Castle的PGP加密解密示例  回復(fù)  更多評(píng)論   

          2014-12-16 18:55 by fedro
          這是因?yàn)樾枰氖撬借€,但你提供的是公鑰

          # re: 基于Java Bouncy Castle的PGP加密解密示例  回復(fù)  更多評(píng)論   

          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)
          這個(gè)錯(cuò)誤時(shí)哪里的問(wèn)題?

          # re: 基于Java Bouncy Castle的PGP加密解密示例  回復(fù)  更多評(píng)論   

          2015-03-12 09:52 by Terrence_Wang
          樓主大哥還有各位大哥幫幫忙~~我按照上面的方法解密不行,如下:
          以下這段是解密中的一段代碼,取出來(lái)的第一個(gè)object確實(shí)是markerpacket,但是進(jìn)入到else中取出的第二個(gè)object的根本沒(méi)有值。直接就報(bào)錯(cuò)了。
          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();
          }

          錯(cuò)誤信息:
          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加密解密示例  回復(fù)  更多評(píng)論   

          2015-03-12 10:00 by Terrence_Wang
          @信
          前輩幫幫忙~~想問(wèn)下一個(gè)有關(guān)java pgp解密的問(wèn)題。謝謝了。我按照上面的方法解密不行,如下:
          以下這段是解密中的一段代碼,取出來(lái)的第一個(gè)object確實(shí)是markerpacket,但是進(jìn)入到else中取出的第二個(gè)object的根本沒(méi)有值。直接就報(bào)錯(cuò)了。
          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();
          }

          錯(cuò)誤信息:
          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加密解密示例  回復(fù)  更多評(píng)論   

          2015-03-13 16:11 by hunger
          好東西,親測(cè)有用

          # re: 基于Java Bouncy Castle的PGP加密解密示例  回復(fù)  更多評(píng)論   

          2015-03-13 16:15 by hunger
          @Terrence_Wang
          你的工程沒(méi)有導(dǎo)入bcprov-jdk15on-152.jar包吧

          # re: 基于Java Bouncy Castle的PGP加密解密示例  回復(fù)  更多評(píng)論   

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

          哎呀~~~我還以為不會(huì)有人回我呢~我太雞動(dòng)了。。。小弟先謝謝了!!我導(dǎo)入了jar包了。我也很奇怪。。。該導(dǎo)入的包我都導(dǎo)入了。而且我看了一下bcprov-jdk15on-152.jar下的原碼,根本就沒(méi)有org/bouncycastle/util/io/TeeInputStream 這個(gè)類(lèi)。bcpg-jdk15on-152.jar;bcprov-jdk15on-152.jar我都換著試過(guò),都是一樣的錯(cuò)。 以下是我導(dǎo)入的包:bcpg-jdk15on-151.jar ;bcprov-jdk15on-151.jar;bouncycastle.jar;local_policy.jar;US_export_policy.jar;

          有一個(gè)地方比較奇怪,就是以下這段是解密中的一段代碼,取出來(lái)的第一個(gè)object確實(shí)是markerpacket,但是進(jìn)入到else中取出的第二個(gè)object的根本沒(méi)有值。直接就報(bào)錯(cuò)了。
          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();
          }
          大哥,救救小弟吧。。。客戶(hù)那邊要把我逼瘋了。網(wǎng)上的資源特別少。還望您給指點(diǎn)一下。這個(gè)是我的qq:925953813 。 小弟先謝過(guò)了。

          # re: 基于Java Bouncy Castle的PGP加密解密示例  回復(fù)  更多評(píng)論   

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

          # re: 基于Java Bouncy Castle的PGP加密解密示例  回復(fù)  更多評(píng)論   

          2015-03-17 11:12 by hunger
          @Terrence_Wang
          真實(shí)尷尬,其實(shí)我也是第一次用這個(gè)東西,看著樓主的提示然后到官網(wǎng)下載,如樓主所說(shuō),我就導(dǎo)入了兩個(gè)jar包bcpg-jdk15on-152.jar、bcprov-jdk15on-152.jar,還有就是到sun官網(wǎng)下載對(duì)應(yīng)jdk版本的jce更換到j(luò)dk1.7.0_71\jre\lib\security目錄下的兩個(gè)文件,后面就直接修改bcpg-jdk15on-152.jar包中的KeyBasedLargeFileProcessor進(jìn)行解密,其中也遇到幾個(gè)問(wèn)題,但是就是沒(méi)有遇到你的問(wèn)題

          # re: 基于Java Bouncy Castle的PGP加密解密示例  回復(fù)  更多評(píng)論   

          2015-03-17 11:16 by hunger
          @Terrence_Wang
          local_policy.jar;US_export_policy.jar這兩個(gè)包不是導(dǎo)入工程的,這點(diǎn)樓主可能說(shuō)的不是很清楚,是要放到你的jre目錄下的lib/security目錄下的,這個(gè)我也是在其他文章中看到的

          # re: 基于Java Bouncy Castle的PGP加密解密示例  回復(fù)  更多評(píng)論   

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

          那bouncycastle.jar 這個(gè)包也要導(dǎo)入吧? 我按照你的方法試了,但還是一樣。我一直做的都是解密。我在想會(huì)不會(huì)是我的解密.asc 的問(wèn)題。大哥,能告訴下你的qq嗎? 或者麻煩加我一下,我q:925953813 。 我只想要一下你的demo 試一下。我一定會(huì)感激你的!!!

          # re: 基于Java Bouncy Castle的PGP加密解密示例  回復(fù)  更多評(píng)論   

          2015-03-17 15:08 by hunger
          @Terrence_Wang
          我沒(méi)有使用bouncycastle.jar這個(gè)包,然后是我是安裝了gpg4win-2.2.3.exe來(lái)生成公鑰加密文件的,然后再用私鑰解密,gpg4win-2.2.3.exe安裝后有一個(gè)Kleopatra的圖形化界面,操作挺方便的,下載官網(wǎng):http://gpg4win.org/download.html。也可以使用bcpg-jdk15on-152.jar包中KeyBasedLargeFileProcessor類(lèi)中的方法來(lái)對(duì)文件加密解密,代碼加密再用代碼解密生成的解密后文件會(huì)在你的工程目錄中。我這邊不允許上qq的。

          # re: 基于Java Bouncy Castle的PGP加密解密示例  回復(fù)  更多評(píng)論   

          2015-03-17 15:32 by Terrence_Wang
          @hunger
          我不用bouncycastle.jar這個(gè)包工程是有錯(cuò)的。我也是用gpg軟件生成的公鑰和密鑰。我這怎么就不行呢。能麻煩您下班有時(shí)間的時(shí)候,能把工程發(fā)我一下嗎?我參考一下。925953813@qq.com 明天就要向客戶(hù)交差了,我就差這個(gè)地方過(guò)不去啊。。。。。 跪謝啊~~~

          # re: 基于Java Bouncy Castle的PGP加密解密示例  回復(fù)  更多評(píng)論   

          2015-03-17 15:59 by hunger
          @Terrence_Wang
          我下班早的話(huà),回去做個(gè)demo給你吧

          # re: 基于Java Bouncy Castle的PGP加密解密示例  回復(fù)  更多評(píng)論   

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

          # re: 基于Java Bouncy Castle的PGP加密解密示例[未登錄](méi)  回復(fù)  更多評(píng)論   

          2015-05-29 13:01 by 123
          @hunger
          為什么我的解密會(huì)進(jìn)這個(gè)判斷里,求解

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

          # re: 基于Java Bouncy Castle的PGP加密解密示例[未登錄](méi)  回復(fù)  更多評(píng)論   

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

          # re: 基于Java Bouncy Castle的PGP加密解密示例[未登錄](méi)  回復(fù)  更多評(píng)論   

          2015-05-29 14:16 by 123
          @hunger
          pgpe –r AAA –s –u BBB FILE1 –o FILE2 用這種方法加密的,,,應(yīng)該用哪個(gè)方法解密呀,,,求大神啊。。。。

          # re: 基于Java Bouncy Castle的PGP加密解密示例  回復(fù)  更多評(píng)論   

          2015-06-25 16:47 by dangzhenjiuhao
          你好,用JAVA生成的公鑰\私鑰跟PGP8.1的生成方式一樣嗎,如果有對(duì)應(yīng)的私鑰,能解密PGP8.1加密的文件嗎?

          # re: 基于Java Bouncy Castle的PGP加密解密示例  回復(fù)  更多評(píng)論   

          2015-07-27 15:13 by dangzhenjiuhao
          可不可以給個(gè)DEMO啊,謝謝

          # re: 基于Java Bouncy Castle的PGP加密解密示例[未登錄](méi)  回復(fù)  更多評(píng)論   

          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]);
          當(dāng)執(zhí)行到最后一句時(shí)報(bào)一下異常:
          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)

          請(qǐng)各位大神分析一下這是啥原因?謝謝!!

          # re: 基于Java Bouncy Castle的PGP加密解密示例  回復(fù)  更多評(píng)論   

          2016-03-02 10:32 by 毛小龍
          對(duì)文件進(jìn)行加密 在測(cè)試類(lèi)里面已經(jīng)跑通了 抽取出來(lái)調(diào)用就報(bào)這個(gè)錯(cuò) 各位幫忙看看是什么原因啊?????
          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)

          只有注冊(cè)用戶(hù)登錄后才能發(fā)表評(píng)論。


          網(wǎng)站導(dǎo)航:
           
          主站蜘蛛池模板: 松溪县| 玉门市| 东源县| 温州市| 阳信县| 阳原县| 通城县| 祁连县| 武乡县| 桃园县| 九台市| 白沙| 西藏| 临夏县| 伊宁县| 寻乌县| 安达市| 深水埗区| 油尖旺区| 晋城| 眉山市| 乌兰浩特市| 清远市| 徐水县| 成都市| 吕梁市| 新化县| 台东市| 富锦市| 黎川县| 香河县| 宁安市| 恩施市| 临潭县| 邢台县| 喀喇| 庄浪县| 富蕴县| 浦北县| 盐源县| 清远市|