Java:使用Bouncy Castle进行PGP加密 [英] Java: PGP Encryption using Bouncy Castle

查看:347
本文介绍了Java:使用Bouncy Castle进行PGP加密的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试使用PGP进行加密,并且我的crypto方法成功地加密了输入字符串,但是当我尝试解密输入字符串以验证加密是否正确完成时,不会解密该字符串.

I am trying to achieve encryption using PGP and my encrypt method successfully encrypts the input string but when I try to decrypt it to verify if the encryption is properly done, the string doesn't get decrypted.

我尝试了2种方法:

第一种方法使用 FileOutputStream 写入加密的字符串&第二种方法使用 ByteArrayOutputStream . FileOutputStream 创建一个文件,我可以使用Kleopatra对其进行解密.但是我的要求是只获取一个加密的字符串(不写在文件中).因此,当我尝试解密加密的字符串(使用 ByteArrayOutputStream 后收到)时,它不起作用.我尝试复制字符串并通过Kleopatra中的tools >> clipboard对其进行解密,但是禁用了crypto/verify选项.我尝试将字符串手动写入文件&通过FileWriter类,但是解密失败,并显示以下错误:文件包含证书&无法解密或验证.

1st approach uses FileOutputStream to write encrypted string & 2nd approach uses ByteArrayOutputStream. FileOutputStream creates a file and I am able to decrypt it using Kleopatra. However my requirement is to just get an encrypted string (not written in a file). So when I try to decrypt the encrypted string (received after using ByteArrayOutputStream) its not working. I tried copying the string and decrypting it through tools>>clipboard in Kleopatra, but the decrypt/verify option is disabled. I tried writing the string on a file manually & through FileWriter class, but decryption fails with the error that File contains certificate & cannot be decrypted or verified.

我假设只有OutputStream直接创建的文件才能成功解密.但是我必须真正检查加密的字符串.

I assume only files created directly by OutputStream gets decrypted successfully. But I have to really check the encrypted string.

任何帮助将不胜感激.

推荐答案

以下完整示例摘自《 David Cook& Jon Eaves的Java密码学:工具和技术》一书的源代码.包含所有示例的完整源代码可在此处找到: https://www.bouncycastle.org/java-crypto-tools-src.zip

The following full example is taken from the source code of the book "Java Cryptography: Tools and Techniques by David Hook & Jon Eaves". The complete source code with all examples is available here: https://www.bouncycastle.org/java-crypto-tools-src.zip

示例显示了使用El Gamal或Elliptic Curves创建私钥/公钥以及使用AES-256进行加密的过程.在ecExample方法中,我添加了两行以将加密的字符串保存到文件"pgp-encrypted-string.dat",然后重新加载数据以解密文件并显示解密的字符串.

The examples are showing a private-/public key creation with El Gamal or Elliptic Curves and encryption with AES-256. In the ecExample-method I added two lines to save the encrypted string to the file "pgp-encrypted-string.dat" and then reload the data to decypt the file and show the decrypted string.

import org.bouncycastle.bcpg.SymmetricKeyAlgorithmTags;
import org.bouncycastle.jce.provider.BouncyCastleProvider;
import org.bouncycastle.openpgp.*;
import org.bouncycastle.openpgp.jcajce.JcaPGPObjectFactory;
import org.bouncycastle.openpgp.operator.PublicKeyDataDecryptorFactory;
import org.bouncycastle.openpgp.operator.jcajce.JcaPGPKeyPair;
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.Strings;
import org.bouncycastle.util.io.Streams;

import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.nio.file.Files;
import java.nio.file.Paths;
import java.security.KeyPair;
import java.security.KeyPairGenerator;
import java.security.SecureRandom;
import java.security.Security;
import java.security.spec.ECGenParameterSpec;
import java.util.Date;

public class PGPEncryptionExampleForSO
{
     /**
     * Create an encrypted data blob using an AES-256 session key and the
     * passed in public key.
     *
     * @param encryptionKey the public key to use.
     * @param data the data to be encrypted.
     * @return a PGP binary encoded version of the encrypted data.
     */
    public static byte[] createEncryptedData(
        PGPPublicKey encryptionKey,
        byte[] data)
        throws PGPException, IOException
    {
        PGPEncryptedDataGenerator encGen = new PGPEncryptedDataGenerator(
            new JcePGPDataEncryptorBuilder(SymmetricKeyAlgorithmTags.AES_256)
                .setWithIntegrityPacket(true)
                .setSecureRandom(new SecureRandom()).setProvider("BC"));
        encGen.addMethod(
            new JcePublicKeyKeyEncryptionMethodGenerator(encryptionKey)
                .setProvider("BC"));
        ByteArrayOutputStream encOut = new ByteArrayOutputStream();
        // create an indefinite length encrypted stream
        OutputStream cOut = encGen.open(encOut, new byte[4096]);
        // write out the literal data
        PGPLiteralDataGenerator lData = new PGPLiteralDataGenerator();
        OutputStream pOut = lData.open(
            cOut, PGPLiteralData.BINARY,
            PGPLiteralData.CONSOLE, data.length, new Date());
        pOut.write(data);
        pOut.close();
        // finish the encryption
        cOut.close();
        return encOut.toByteArray();
    }

    /**
     * Extract the plain text data from the passed in encoding of PGP
     * encrypted data. The routine assumes the passed in private key
     * is the one that matches the first encrypted data object in the
     * encoding.
     *
     * @param privateKey the private key to decrypt the session key with.
     * @param pgpEncryptedData the encoding of the PGP encrypted data.
     * @return a byte array containing the decrypted data.
     */
    public static byte[] extractPlainTextData(
        PGPPrivateKey privateKey,
        byte[] pgpEncryptedData)
        throws PGPException, IOException
    {
        PGPObjectFactory pgpFact = new JcaPGPObjectFactory(pgpEncryptedData);
        PGPEncryptedDataList encList = (PGPEncryptedDataList)pgpFact.nextObject();
        // find the matching public key encrypted data packet.
        PGPPublicKeyEncryptedData encData = null;
        for (PGPEncryptedData pgpEnc: encList)
        {
            PGPPublicKeyEncryptedData pkEnc
                = (PGPPublicKeyEncryptedData)pgpEnc;
            if (pkEnc.getKeyID() == privateKey.getKeyID())
            {
                encData = pkEnc;
                break;
            }
        }
        if (encData == null)
        {
            throw new IllegalStateException("matching encrypted data not found");
        }
        // build decryptor factory
        PublicKeyDataDecryptorFactory dataDecryptorFactory =
            new JcePublicKeyDataDecryptorFactoryBuilder()
                .setProvider("BC")
                .build(privateKey);
        InputStream clear = encData.getDataStream(dataDecryptorFactory);
        byte[] literalData = Streams.readAll(clear);
        clear.close();
        // check data decrypts okay
        if (encData.verify())
        {
            // parse out literal data
            PGPObjectFactory litFact = new JcaPGPObjectFactory(literalData);
            PGPLiteralData litData = (PGPLiteralData)litFact.nextObject();
            byte[] data = Streams.readAll(litData.getInputStream());
            return data;
        }
        throw new IllegalStateException("modification check failed");
    }

    private static void elgamalExample()
        throws Exception
    {
        byte[] msg = Strings.toByteArray("Hello, world!");
        KeyPairGenerator kpGen = KeyPairGenerator.getInstance("DH", "BC");
        kpGen.initialize(2048);
        KeyPair kp = kpGen.generateKeyPair();
        PGPKeyPair elgKp = new JcaPGPKeyPair(
            PGPPublicKey.ELGAMAL_ENCRYPT, kp, new Date());
        byte[] encData = createEncryptedData(elgKp.getPublicKey(), msg);
        byte[] decData = extractPlainTextData(elgKp.getPrivateKey(), encData);
        System.out.println("elgamal encryption msg length: " + msg.length + " enc.length: " + encData.length + " dec.length: " + decData.length);
        System.out.println(Strings.fromByteArray(decData));
    }

    private static void ecExample()
        throws Exception
    {
        byte[] msg = Strings.toByteArray("Hello, world!");
        KeyPairGenerator kpGen = KeyPairGenerator.getInstance("EC", "BC");
        kpGen.initialize(new ECGenParameterSpec("P-256"));
        KeyPair kp = kpGen.generateKeyPair();
        PGPKeyPair ecdhKp = new JcaPGPKeyPair(PGPPublicKey.ECDH, kp, new Date());
        byte[] encData = createEncryptedData(ecdhKp.getPublicKey(), msg);
        // save encrypted string
        Files.write(Paths.get("pgp-encrypted-string.dat"), encData);
        // load encrypted string
        byte[] encDataLoad = Files.readAllBytes(Paths.get("pgp-encrypted-string.dat"));
        byte[] decData = extractPlainTextData(ecdhKp.getPrivateKey(), encDataLoad);
        System.out.println("ec encryption msg length: " + msg.length + " enc.length: " + encData.length + " dec.length: " + decData.length);
        System.out.println(Strings.fromByteArray(decData));
    }

    public static void main(String[] args)
        throws Exception
    {
        Security.addProvider(new BouncyCastleProvider());
        // you need the two files bcpg-jdk15on-165.jar and bcprov-jdk15to18-165.jar to run the example
        System.out.println("Example from Java Cryptography: Tools and Techniques by David Hook & Jon Eaves");
        System.out.println("get source files: https://www.bouncycastle.org/java-crypto-tools-src.zip");
        elgamalExample();
        ecExample();
    }
}

这是简短的输出:

Example from Java Cryptography: Tools and Techniques by David Hook & Jon Eaves
get source files: https://www.bouncycastle.org/java-crypto-tools-src.zip
elgamal encryption msg length: 13 enc.length: 601 dec.length: 13
Hello, world!
ec encryption msg length: 13 enc.length: 200 dec.length: 13
Hello, world!

已添加:据我所知,您正在使用类似

Added: As I now understand you're having a String like

This string needs an encryption

,您想使用rsa pgp公钥对其进行加密:

and you want to encrypt it with a rsa pgp public key:

-----BEGIN PGP PUBLIC KEY BLOCK-----
Version: BCPG v1.65

mI0EXr/nDgEEAKhB6ufAB954aBIlNjPCsryzUVLu0qkC/1RtnFHf+J6IVegV8Wi7
28V074inQcw6o6FTLtFTaLRP4+3eXNATdjGSjrvcP7k+nu50vydugHv43fPuCiZ7
6gbbMTE9gPiLPA2pS+SmQJnr9hOrD5rzwYP1yNNIsRJ9qmU5NeZyu+szABEBAAG0
DHRlc3RpZGVudGl0eYicBBABAgAGBQJev+cOAAoJEPBDuyqTbz/gY0YD/R+gDkfe
qPgNuk6iI2wLSGEeZRXr6Ru1cyG73CRvz7BjCpwWx039AdQzP9gkeo6MEj8Z0c73
obqEP8NtvvOcwC7+/QiGLTR2mgCsNhk54+iCGsvNbkpkr/rRoYZGyvb+rxui0A61
DCB1w5hdnyMg2OglFNrkaPfpNjMsTebfF5eS
=h1+m
-----END PGP PUBLIC KEY BLOCK-----

并获取加密的字符串

-----BEGIN PGP MESSAGE-----
Version: BCPG v1.65

hIwD8EO7KpNvP+ABA/9JkOE9PDyS/kr/lZ1Uz+NCSe1JiNcKCXjbsUbvP8CT7Tf1
cKlgzIz1mQjdpkBtVpVhEnEjmUzFy2UCRKr4b4Wx7/1UL+370CICW5HgMoi5TgTg
MYRy5I9Uba/+JxcusjWB1JJHP4ofULziXRKLWAoSPLlglZDzSmV88hNo19rl39JZ
AbMhIS2edM9hHICefL/Yaiq90hGjKMRReVopu2tPUjNLGYP7QABAvWb3WQJMZoYT
HEsyjHxeyYQylAdYB7pWQA0++Z803iclvM3skN8FBt64ebDkqfxgbhs=
=je0r
-----END PGP MESSAGE-----

现在,您想在线使用Kleoptatra解密此消息(例如 https://sela.io/pgp-en/),或者在Java中使用RSA pgp私钥和密码123456:

Now you'd like to decrypt this message with Kleoptatra, online (e.g. https://sela.io/pgp-en/) or in Java with the RSA pgp private key and the password 123456:

-----BEGIN PGP PRIVATE KEY BLOCK-----
Version: BCPG v1.65

lQH+BF6/5w4BBACoQernwAfeeGgSJTYzwrK8s1FS7tKpAv9UbZxR3/ieiFXoFfFo
u9vFdO+Ip0HMOqOhUy7RU2i0T+Pt3lzQE3Yxko673D+5Pp7udL8nboB7+N3z7gom
e+oG2zExPYD4izwNqUvkpkCZ6/YTqw+a88GD9cjTSLESfaplOTXmcrvrMwARAQAB
/gMDAhhcE1oF/u8YYExKGLgriK5JpUUSsMFU0AOHP9/zZQr09437V0f/F4J87+9s
G30lDRikGwynEGRnAvIVwqq2F+iarKGGHCZCRgbyufXS7VK6wE/43lR0kSwA2VIM
ll/KbQKP1cSZv0rqtJ1tGL7cDHFEwq10gM4Bn75HOKyBzE9oERRKz37noAECsAZn
xuXGlEB5noqTT00RxsHjBA5Os04CtEz9N+OMrg47IR7AzSQUe90lG2F6W71dhJ6V
jQaf7D6JFU3dOWPW1eBb5FQhgYF92CFRizJ42lDCiTfl2FQU49MlwLd2ofNneuPo
aVuPoYUNKwbasyx4fo2vh6rrMyxmncCizMExvh6GIVgYd7EK9s6Gxq/duuOvly4O
ZAyIY2MOon0bDXxAYR2q/wdQLamnP7rAR4uMu24m/iOuBj6wwTR8v8hhsFFTp/4u
tebwWzLnPyyBYStnTF5IZ9ZJeVl5S3zdzNcrP9g8yXtItAx0ZXN0aWRlbnRpdHmI
nAQQAQIABgUCXr/nDgAKCRDwQ7sqk28/4GNGA/0foA5H3qj4DbpOoiNsC0hhHmUV
6+kbtXMhu9wkb8+wYwqcFsdN/QHUMz/YJHqOjBI/GdHO96G6hD/Dbb7znMAu/v0I
hi00dpoArDYZOePoghrLzW5KZK/60aGGRsr2/q8botAOtQwgdcOYXZ8jINjoJRTa
5Gj36TYzLE3m3xeXkg==
=y/tQ
-----END PGP PRIVATE KEY BLOCK-----

并获取解密的字符串:

This string needs an encryption

幸运的是,要在Java中进行加密/解密,BouncyCastle Github-Repo中提供了示例文件: https://github.com/bcgit/bc-java/blob/master/pg/src/main/java/org/bouncycastle/openpgp/examples/.您可能需要使用RSA(RSAKeyPairGenerator.java)或ElGamal创建新的PGP密钥对(DSAElGamalKeyRingGenerator.java).使用生成的密钥,您可以使用KeyBasedFileProcessor.java和必要的PGPExampleUtil.java进行加密或解密.

To encrypt/decrypt in Java fortunately there are sample files available in the BouncyCastle Github-Repo: https://github.com/bcgit/bc-java/blob/master/pg/src/main/java/org/bouncycastle/openpgp/examples/. You may need to create a new PGP-keypair using RSA (RSAKeyPairGenerator.java) or ElGamal (DSAElGamalKeyRingGenerator.java). With the generated keys you can encrypt or decrypt using KeyBasedFileProcessor.java and neccessary PGPExampleUtil.java.

我以"-a testidentity 123456"作为参数创建了RSA密钥文件,使用"-e -ai plaintext.txt rsa_pub.asc"进行了加密,并使用了"-d plaintext.txt.asc rsa_secret"进行了解密..asc 123456".

I created the RSA key files with "-a testidentity 123456" as arguments, the encryption is done with "-e -ai plaintext.txt rsa_pub.asc" and the decryption goes with "-d plaintext.txt.asc rsa_secret.asc 123456".

这篇关于Java:使用Bouncy Castle进行PGP加密的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆