Java PGP 加密 应用邮件、银行场所

  1. 在官方网站下载JCE无限制权限策略文件
  2. 下载后解压,可以看到local_policy.jar和US_export_policy.jar以及readme.txt
  3. 如果安装了JRE,将两个jar文件放到%JRE_HOME%\lib\security目录下覆盖原来的文件
  4. 如果安装了JDK,将两个jar文件放到%JDK_HOME%\jre\lib\security目录下覆盖原来文件
适用JDK6的JCE补丁:
    http://www.oracle.com/technetwork/java/javase/downloads/jce-6-download-429243.html
适用JDK7的JCE补丁:
    http://www.oracle.com/technetwork/java/javase/downloads/jce-7-download-432124.html
适用JDK8的JCE补丁: 
    http://www.oracle.com/technetwork/java/javase/downloads/jce8-download-2133166.html`

maven项目导入以下几个Jar包,以Gradle示例:(1.5的版本和1.6的版本不一样,本版本为1.6.2 注意版本!!!)

compile group: 'org.bouncycastle', name: 'bcprov-jdk15on', version: '1.62'
compile group: 'commons-io', name: 'commons-io', version: '2.6'
compile group: 'org.bouncycastle', name: 'bcpg-jdk15on', version: '1.62'

接下来2个大类都是从源码包中拷贝而出,源码包中的方法有些是privte,有些是default,因此不能引用,我们需要自己去定义一些更好更快的使用。


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;
import org.bouncycastle.openpgp.operator.jcajce.JcaKeyFingerprintCalculator;
import org.bouncycastle.openpgp.operator.jcajce.JcePBESecretKeyDecryptorBuilder;
public class PGPExampleUtil
{
    public 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 the private key.
     * @throws PGPException
     * @throws NoSuchProviderException
     */
    public 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(new JcePBESecretKeyDecryptorBuilder().setProvider("BC").build(pass));
    }

    public 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 data stream containing the public key data
     * @return the first public key found.
     * @throws IOException
     * @throws PGPException
     */
    public static PGPPublicKey readPublicKey(InputStream input) throws IOException, PGPException
    {
        PGPPublicKeyRingCollection pgpPub = new PGPPublicKeyRingCollection(
                PGPUtil.getDecoderStream(input), new JcaKeyFingerprintCalculator());

        //
        // 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.");
    }

    public 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.
     */
    public static PGPSecretKey readSecretKey(InputStream input) throws IOException, PGPException
    {
        PGPSecretKeyRingCollection pgpSec = new PGPSecretKeyRingCollection(
                PGPUtil.getDecoderStream(input), new JcaKeyFingerprintCalculator());

        //
        // 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.");
    }
}

import java.io.*;
import java.security.NoSuchProviderException;
import java.security.SecureRandom;
import java.security.Security;
import java.util.Date;
import java.util.Iterator;

import org.bouncycastle.bcpg.ArmoredOutputStream;
import org.bouncycastle.bcpg.CompressionAlgorithmTags;
import org.bouncycastle.jce.provider.BouncyCastleProvider;
import org.bouncycastle.openpgp.*;
import org.bouncycastle.openpgp.jcajce.JcaPGPObjectFactory;
import org.bouncycastle.openpgp.operator.jcajce.JcaKeyFingerprintCalculator;
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 files.
 * 

* To encrypt a file: KeyBasedFileProcessor -e [-a|-ai] fileName publicKeyFile.
* If -a is specified the output file will be "ascii-armored". * If -i is specified the output file will be have integrity checking added. *

* To decrypt: KeyBasedFileProcessor -d fileName secretKeyFile passPhrase. *

* Note 1: this example will silently overwrite files, nor does it pay any attention to * the specification of "_CONSOLE" in the filename. It also expects that a single pass phrase * will have been used. *

* Note 2: if an empty file name has been specified in the literal data object contained in the * encrypted packet a file with the name filename.out will be generated in the current working directory. */ public class KeyBasedFileProcessor { 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 { JcaPGPObjectFactory pgpF = new JcaPGPObjectFactory(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), new JcaKeyFingerprintCalculator()); 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)); JcaPGPObjectFactory plainFact = new JcaPGPObjectFactory(clear); Object message = plainFact.nextObject(); System.out.print(message); if (message instanceof PGPCompressedData) { PGPCompressedData cData = (PGPCompressedData)message; JcaPGPObjectFactory pgpFact = new JcaPGPObjectFactory(cData.getDataStream()); 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 { byte[] bytes = PGPExampleUtil.compressFile(fileName, CompressionAlgorithmTags.ZIP); PGPEncryptedDataGenerator encGen = new PGPEncryptedDataGenerator( new JcePGPDataEncryptorBuilder(PGPEncryptedData.CAST5).setWithIntegrityPacket(withIntegrityCheck).setSecureRandom(new SecureRandom()).setProvider("BC")); encGen.addMethod(new JcePublicKeyKeyEncryptionMethodGenerator(encKey).setProvider("BC")); OutputStream cOut = encGen.open(out, bytes.length); cOut.write(bytes); // System.out.print(bytes.hashCode()); 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()); if (args.length == 0) { System.err.println("usage: KeyBasedFileProcessor -e|-d [-a|ai] file [secretKeyFile passPhrase|pubKeyFile]"); return; } if (args[0].equals("-e")) { if (args[1].equals("-a") || args[1].equals("-ai") || args[1].equals("-ia")) { encryptFile(args[2] + ".asc", args[2], args[3], true, (args[1].indexOf('i') > 0)); } else if (args[1].equals("-i")) { encryptFile(args[2] + ".bpg", args[2], args[3], false, true); } else { encryptFile(args[1] + ".bpg", args[1], args[2], false, false); } } else if (args[0].equals("-d")) { decryptFile(args[1], args[2], args[3].toCharArray(), new File(args[1]).getName() + ".out"); } else { System.err.println("usage: KeyBasedFileProcessor -d|-e [-a|ai] file [secretKeyFile passPhrase|pubKeyFile]"); } } /** * * @param bytes 加密数据Byte数组 * @param encKeyFileName Public Key FileName * @param armor 是否加壳 * @param withIntegrityCheck 是否完整性校验 * @return 加密后的报文 * @throws IOException * @throws NoSuchProviderException * @throws PGPException */ public static String encryptFile( byte[] bytes, String encKeyFileName, boolean armor, boolean withIntegrityCheck) throws IOException, PGPException { PGPPublicKey encKey = PGPExampleUtil.readPublicKey(encKeyFileName); String result = encryptFile( bytes, encKey, armor, withIntegrityCheck); return result; } public static String encryptFile( byte[] bytes, PGPPublicKey encKey, boolean armor, boolean withIntegrityCheck) throws IOException { ByteArrayOutputStream encOut = new ByteArrayOutputStream(); OutputStream out = encOut; if (armor) { out = new ArmoredOutputStream(out); } try { PGPEncryptedDataGenerator encGen = new PGPEncryptedDataGenerator( new JcePGPDataEncryptorBuilder(PGPEncryptedData.CAST5).setWithIntegrityPacket(withIntegrityCheck).setSecureRandom(new SecureRandom()).setProvider("BC")); encGen.addMethod(new JcePublicKeyKeyEncryptionMethodGenerator(encKey).setProvider("BC")); OutputStream cOut = encGen.open(out, bytes.length); cOut.write(bytes); cOut.close(); if (armor) { out.close(); } } catch (PGPException e) { System.err.println(e); if (e.getUnderlyingException() != null) { e.getUnderlyingException().printStackTrace(); } } return encOut.toString(); } }

加密示例方法:

public static void main(String[] args){
        try{
            String data = "{\"header\":{\"msgId\":\"20190724163800101\",\"orgId\":\"OICBDF\",\"timeStamp\":\"2019-07-24T16:38:58.765\"},\"txnInfo\":{\"ccyPair\":\"CNHHKD\",\"dealtSide\":\"BUY\",\"txnAmount\":\"100.00\",\"txnCcy\":\"CNH\",\"tenor\":\"SPOT\",\"clientTxnsId\":\"cleint-0000101\"}}";
            //读写文件操作
//            KeyBasedFileProcessor.encryptFile("C:\\Users\\admin\\Desktop\\加密后输出的文档.txt","C:\\Users\\admin\\Desktop\\待加密文档.txt","C:\\Users\\admin\\Documents\\公钥文件.asc",true,true);

            ByteArrayOutputStream encOut = new ByteArrayOutputStream();

            OutputStream out = encOut;
            if (true) {
                out = new ArmoredOutputStream(out);
            }

            ByteArrayOutputStream bOut = new ByteArrayOutputStream();

            PGPCompressedDataGenerator comData = new PGPCompressedDataGenerator(
                    PGPCompressedDataGenerator.ZIP);
            OutputStream cos = comData.open(bOut); // open it with the final

            PGPLiteralDataGenerator lData = new PGPLiteralDataGenerator();

            OutputStream pOut = lData.open(cos, PGPLiteralData.BINARY,
                    PGPLiteralData.CONSOLE, data.getBytes().length, // length of clear
                    // data
                    new Date() // current time
            );
            pOut.write(data.getBytes());

            lData.close();
            comData.close();

            PGPEncryptedDataGenerator encGen = new PGPEncryptedDataGenerator(
                    new JcePGPDataEncryptorBuilder(PGPEncryptedData.CAST5).setWithIntegrityPacket(true).setSecureRandom(new SecureRandom()).setProvider("BC"));
            InputStream keyIn = new BufferedInputStream(new FileInputStream("C:\\Users\\admin\\Desktop\\DBSSG_EN_PUBLIC.asc"));
            PGPPublicKey encKey = PGPExampleUtil.readPublicKey(keyIn);
            encGen.addMethod(new JcePublicKeyKeyEncryptionMethodGenerator(encKey).setProvider("BC"));

            byte[] bytes = bOut.toByteArray();

            OutputStream cOut = encGen.open(out, bytes.length);

            cOut.write(bytes); // obtain the actual bytes from the compressed stream

            cOut.close();

            out.close();



//            String result = KeyBasedFileProcessor.encryptFile(encOut.toString().getBytes(),"C:\\Users\\admin\\Documents\\publicKey.asc",true,true);
//            KeyBasedFileProcessor.decryptFile("C:\\Users\\admin\\Documents\\encrypted_test.pgp", "C:\\Users\\admin\\Documents\\privateKey.asc", "123456".toCharArray(), "C:\\Users\\admin\\Documents\\dec_2.txt");
//            InputStream keyIn = new BufferedInputStream(new FileInputStream("C:\\\\Users\\\\admin\\\\Documents\\\\publicKey.asc"));
//            PGPPublicKey key = SignAndEncrypt.readPublicKey(keyIn);
//
//
//            InputStream dataIn = new BufferedInputStream(new FileInputStream("C:\\\\Users\\\\admin\\\\Documents\\\\encrypted_test.pgp"));
//            byte[] byt = new byte[dataIn.available()];
//
//
//            String result = SignAndEncrypt.encryptByteArray(byt, key, true, true);
//            System.out.print(result);
//            keyIn.close();
            System.out.print(data.hashCode());
            System.out.print(encOut.toString());
        }catch (Exception e){
            e.printStackTrace();
        }


    }

import java.io.*;
import java.util.Date;

import org.bouncycastle.bcpg.BCPGOutputStream;
import org.bouncycastle.bcpg.PacketTags;
import org.bouncycastle.openpgp.*;
import org.bouncycastle.util.Arrays;
import org.bouncycastle.util.Strings;

/**
 * Generator for producing literal data packets.
 * 

* A PGPLiteralData is used by invoking one of the open functions to create an OutputStream that raw * data can be supplied to for encoding:

*
    *
  • If the length of the data to be written is known in advance, use * {@link #open(OutputStream, char, String, long, Date)} to create a packet containing a single * literal data object.
  • *
  • If the length of the data is unknown, use * {@link #open(OutputStream, char, String, Date, byte[])} to create a packet consisting of a series * of literal data objects (partials).
  • *
*

* A PGPLiteralDataGenerator is usually used to wrap the OutputStream * {@link PGPEncryptedDataGenerator#open(OutputStream, byte[]) obtained} from a * {@link PGPEncryptedDataGenerator} or a {@link PGPCompressedDataGenerator}. *

* Once literal data has been written to the constructed OutputStream, writing of the object stream * is completed by closing the OutputStream obtained from the open() method, or * equivalently invoking {@link #close()} on this generator. *

*/ public class PGPLiteralDataGenerator implements StreamGenerator { /** Format tag for binary literal data */ public static final char BINARY = PGPLiteralData.BINARY; /** Format tag for textual literal data */ public static final char TEXT = PGPLiteralData.TEXT; /** Format tag for UTF-8 encoded textual literal data */ public static final char UTF8 = PGPLiteralData.UTF8; /** * The special name indicating a "for your eyes only" packet. */ // TODO: Not used? public static final String CONSOLE = PGPLiteralData.CONSOLE; /** * The special time for a modification time of "now" or * the present time. */ public static final Date NOW = PGPLiteralData.NOW; private BCPGOutputStream pkOut; private boolean oldFormat = false; /** * Constructs a generator for literal data objects. */ public PGPLiteralDataGenerator() { } /** * Constructs a generator for literal data objects, specifying to use new or old (PGP 2.6.x * compatible) format. *

* This can be used for compatibility with PGP 2.6.x. *

* @param oldFormat true to use PGP 2.6.x compatible format. */ public PGPLiteralDataGenerator( boolean oldFormat) { this.oldFormat = oldFormat; } private void writeHeader( OutputStream out, char format, byte[] encName, long modificationTime) throws IOException { out.write(format); out.write((byte)encName.length); for (int i = 0; i != encName.length; i++) { out.write(encName[i]); } long modDate = modificationTime / 1000; out.write((byte)(modDate >> 24)); out.write((byte)(modDate >> 16)); out.write((byte)(modDate >> 8)); out.write((byte)(modDate)); } /** * Open a literal data packet, returning a stream to store the data inside the packet. *

* The stream created can be closed off by either calling close() on the stream or close() on * the generator. Closing the returned stream does not close off the OutputStream parameter out. * * @param out the underlying output stream to write the literal data packet to. * @param format the format of the literal data that will be written to the output stream (one * of {@link #BINARY}, {@link #TEXT} or {@link #UTF8}). * @param name the name of the "file" to encode in the literal data object. * @param length the length of the data that will be written. * @param modificationTime the time of last modification we want stored. */ public OutputStream open( OutputStream out, char format, String name, long length, Date modificationTime) throws IOException { if (pkOut != null) { throw new IllegalStateException("generator already in open state"); } byte[] encName = Strings.toUTF8ByteArray(name); pkOut = new BCPGOutputStream(out, PacketTags.LITERAL_DATA, length + 2 + encName.length + 4, oldFormat); writeHeader(pkOut, format, encName, modificationTime.getTime()); return new WrappedGeneratorStream(pkOut, this); } /** * Open a literal data packet, returning a stream to store the data inside the packet as an * indefinite-length stream. The stream is written out as a series of partial packets with a * chunk size determined by the size of the passed in buffer. *

* The stream created can be closed off by either calling close() on the stream or close() on * the generator. Closing the returned stream does not close off the OutputStream parameter out. * *

* Note: if the buffer is not a power of 2 in length only the largest power of 2 bytes * worth of the buffer will be used. * * @param out the underlying output stream to write the literal data packet to. * @param format the format of the literal data that will be written to the output stream (one * of {@link #BINARY}, {@link #TEXT} or {@link #UTF8}). * @param name the name of the "file" to encode in the literal data object. * @param modificationTime the time of last modification we want stored (will be stored to * second level precision). * @param buffer a buffer to use to buffer and write partial packets. The returned stream takes * ownership of the buffer. * * @return the output stream to write data to. * @throws IOException if an error occurs writing stream header information to the provider * output stream. * @throws IllegalStateException if this generator already has an open OutputStream. */ public OutputStream open( OutputStream out, char format, String name, Date modificationTime, byte[] buffer) throws IOException { if (pkOut != null) { throw new IllegalStateException("generator already in open state"); } pkOut = new BCPGOutputStream(out, PacketTags.LITERAL_DATA, buffer); byte[] encName = Strings.toUTF8ByteArray(name); writeHeader(pkOut, format, encName, modificationTime.getTime()); return new WrappedGeneratorStream(pkOut, this); } /** * Open a literal data packet for the passed in File object, returning an output stream for * saving the file contents. *

* This method configures the generator to store the file contents in a single literal data * packet, taking the filename and modification time from the file, but does not store the * actual file data. *

* The stream created can be closed off by either calling close() on the stream or close() on * the generator. Closing the returned stream does not close off the OutputStream parameter out. *

* @param out the underlying output stream to write the literal data packet to. * @param format the format of the literal data that will be written to the output stream (one * of {@link #BINARY}, {@link #TEXT} or {@link #UTF8}). * @param file the file to determine the length and filename from. * @return the output stream to write data to. * @throws IOException if an error occurs writing stream header information to the provider * output stream. * @throws IllegalStateException if this generator already has an open OutputStream. */ public OutputStream open( OutputStream out, char format, File file) throws IOException { return open(out, format, file.getName(), file.length(), new Date(file.lastModified())); } /** * Close the literal data packet - this is equivalent to calling close on the stream * returned by the open() method. * * @throws IOException */ public void close() throws IOException { if (pkOut != null) { pkOut.finish(); pkOut.flush(); pkOut = null; } } public static void pipeFileContents(String data, OutputStream pOut, int bufferSize) throws IOException { byte[] buf = new byte[bufferSize]; InputStream in = new ByteArrayInputStream(data.getBytes()); // FileInputStream in = new FileInputStream(file); try { int len; while ((len = in.read(buf)) > 0) { pOut.write(buf, 0, len); } pOut.close(); } finally { Arrays.fill(buf, (byte)0); try { in.close(); } catch (IOException ignored) { // ignore... } } } }

接下来可以下载一个用java写的windows窗口程序去做测试(我写的main方法中对一个string消息加密,也提供了文本加密,该软件可以对文本和文件加密解密操作,您可以尽管测试),您如果有问题可以随时联系我!
Java PGP 加密 应用邮件、银行场所_第1张图片

你可能感兴趣的:(JAVA)