How to Encrypt a String/Stream With Bouncycastle Pgp Without Starting With a File

How to encrypt a string/stream with bouncycastle pgp without starting with a file

Looking at the source of PGPUtil you can see what API to call when working with streams or arrays directly:

public static void writeFileToLiteralData(OutputStream out,
char fileType, File file, byte[] buffer) throws IOException {
PGPLiteralDataGenerator lData = new PGPLiteralDataGenerator();
OutputStream pOut = lData.open(out, fileType, file.getName(),
new Date(file.lastModified()), buffer);
FileInputStream in = new FileInputStream(file);
byte[] buf = new byte[buffer.length];
int len;

while ((len = in.read(buf)) > 0) {
pOut.write(buf, 0, len);
}

lData.close();
in.close();
}

BouncyCastle in-memory PGP encryption in C#

I got it working. The code uses byte[] for input and output of both decryption and encryption - no files.

Here is the full class:

class PGP
{
public PGP() { }

/**
* A simple routine that opens a key ring file and loads the first available key suitable for
* encryption.
*
* @param in
* @return
* @m_out
* @
*/
public static PgpPublicKey ReadPublicKey(Stream inputStream)
{
inputStream = PgpUtilities.GetDecoderStream(inputStream);
PgpPublicKeyRingBundle pgpPub = new PgpPublicKeyRingBundle(inputStream);
//
// 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.
//
//
// iterate through the key rings.
//
foreach (PgpPublicKeyRing kRing in pgpPub.GetKeyRings())
{
foreach (PgpPublicKey k in kRing.GetPublicKeys())
{
if (k.IsEncryptionKey)
return k;
}
}

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

/**
* 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
*/
private static PgpPrivateKey FindSecretKey(PgpSecretKeyRingBundle pgpSec, long keyId, char[] pass)
{
PgpSecretKey pgpSecKey = pgpSec.GetSecretKey(keyId);
if (pgpSecKey == null)
return null;

return pgpSecKey.ExtractPrivateKey(pass);
}

/**
* Decrypt the byte array passed into inputData and return it as
* another byte array.
*
* @param inputData - the data to decrypt
* @param keyIn - a stream from your private keyring file
* @param passCode - the password
* @return - decrypted data as byte array
*/
public static byte[] Decrypt(byte[] inputData, Stream keyIn, string passCode)
{
byte[] error = Encoding.ASCII.GetBytes("ERROR");

Stream inputStream = new MemoryStream(inputData);
inputStream = PgpUtilities.GetDecoderStream(inputStream);
MemoryStream decoded = new MemoryStream();

try
{
PgpObjectFactory pgpF = new PgpObjectFactory(inputStream);
PgpEncryptedDataList enc;
PgpObject o = pgpF.NextPgpObject();

//
// the first object might be a PGP marker packet.
//
if (o is PgpEncryptedDataList)
enc = (PgpEncryptedDataList)o;
else
enc = (PgpEncryptedDataList)pgpF.NextPgpObject();

//
// find the secret key
//
PgpPrivateKey sKey = null;
PgpPublicKeyEncryptedData pbe = null;
PgpSecretKeyRingBundle pgpSec = new PgpSecretKeyRingBundle(
PgpUtilities.GetDecoderStream(keyIn));
foreach (PgpPublicKeyEncryptedData pked in enc.GetEncryptedDataObjects())
{
sKey = FindSecretKey(pgpSec, pked.KeyId, passCode.ToCharArray());
if (sKey != null)
{
pbe = pked;
break;
}
}
if (sKey == null)
throw new ArgumentException("secret key for message not found.");

Stream clear = pbe.GetDataStream(sKey);
PgpObjectFactory plainFact = new PgpObjectFactory(clear);
PgpObject message = plainFact.NextPgpObject();

if (message is PgpCompressedData)
{
PgpCompressedData cData = (PgpCompressedData)message;
PgpObjectFactory pgpFact = new PgpObjectFactory(cData.GetDataStream());
message = pgpFact.NextPgpObject();
}
if (message is PgpLiteralData)
{
PgpLiteralData ld = (PgpLiteralData)message;
Stream unc = ld.GetInputStream();
Streams.PipeAll(unc, decoded);
}
else if (message is 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())
MessageBox.Show(null, "Message failed integrity check.", "PGP Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
else
MessageBox.Show(null, "Message integrity check passed.", "PGP Error", MessageBoxButtons.OK, MessageBoxIcon.Information);
}
else
{
MessageBox.Show(null, "No message integrity check.", "PGP Error", MessageBoxButtons.OK, MessageBoxIcon.Information);
}

return decoded.ToArray();
}
catch (Exception e)
{
if (e.Message.StartsWith("Checksum mismatch"))
MessageBox.Show(null, "Likely invalid passcode. Possible data corruption.", "Invalid Passcode", MessageBoxButtons.OK, MessageBoxIcon.Error);
else if (e.Message.StartsWith("Object reference not"))
MessageBox.Show(null, "PGP data does not exist.", "PGP Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
else if (e.Message.StartsWith("Premature end of stream"))
MessageBox.Show(null, "Partial PGP data found.", "PGP Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
else
MessageBox.Show(null, e.Message, "PGP Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
Exception underlyingException = e.InnerException;
if (underlyingException != null)
MessageBox.Show(null, underlyingException.Message, "PGP Error", MessageBoxButtons.OK, MessageBoxIcon.Error);

return error;
}
}

/**
* Encrypt the data.
*
* @param inputData - byte array to encrypt
* @param passPhrase - the password returned by "ReadPublicKey"
* @param withIntegrityCheck - check the data for errors
* @param armor - protect the data streams
* @return - encrypted byte array
*/
public static byte[] Encrypt(byte[] inputData, PgpPublicKey passPhrase, bool withIntegrityCheck, bool armor)
{
byte[] processedData = Compress(inputData, PgpLiteralData.Console, CompressionAlgorithmTag.Uncompressed);

MemoryStream bOut = new MemoryStream();
Stream output = bOut;

if (armor)
output = new ArmoredOutputStream(output);

PgpEncryptedDataGenerator encGen = new PgpEncryptedDataGenerator(SymmetricKeyAlgorithmTag.Cast5, withIntegrityCheck, new SecureRandom());
encGen.AddMethod(passPhrase);

Stream encOut = encGen.Open(output, processedData.Length);

encOut.Write(processedData, 0, processedData.Length);
encOut.Close();

if (armor)
output.Close();

return bOut.ToArray();
}

private static byte[] Compress(byte[] clearData, string fileName, CompressionAlgorithmTag algorithm)
{
MemoryStream bOut = new MemoryStream();

PgpCompressedDataGenerator comData = new PgpCompressedDataGenerator(algorithm);
Stream cos = comData.Open(bOut); // open it with the final destination
PgpLiteralDataGenerator lData = new PgpLiteralDataGenerator();

// we want to Generate compressed data. This might be a user option later,
// in which case we would pass in bOut.
Stream pOut = lData.Open(
cos, // the compressed output stream
PgpLiteralData.Binary,
fileName, // "filename" to store
clearData.Length, // length of clear data
DateTime.UtcNow // current time
);

pOut.Write(clearData, 0, clearData.Length);
pOut.Close();

comData.Close();

return bOut.ToArray();
}
}

I have been working on PGP Encrypt and Decrypt for byte[] with Bouncy Castle API and getting Invalid Armor error message

Sometimes it's good to start with a fresh set of files. In my GitHub-Repository
https://github.com/java-crypto/Stackoverflow/tree/master/PGP_Encrypt_Decrypt_Armor_Error
you can download the secret_rsa_kleo passphrase.zip with the following contents:

Main3.java: simple test program as shown below
PGPExampleUtil.java: taken from Bouncy Castle PGP examples
secret_rsa_kleo.asc: brandnew generated private key (done with Kleopatra 3.1.11)
pub_rsa_kleo.asc: public key
bcprov-jdk15to18-165.jar + bcpg-jdk15on-165.jar: Bouncy Castle jars

I generated my keypair with the passphrase "mypassphrase" (without "" :-). The output is as follows:

Test with Java version: 11.0.6+8-b520.43 BouncyCastle Version: BC version 1.65

plaintext: 54686973206973206d7920706c61696e74657874
decrytext: 54686973206973206d7920706c61696e74657874
plaintext equals decryptedtext: true

ciphetext: 85010c03f9a05b3a12b538270107ff4c960552ca571ff4a24518189e038bd574e64504398b10fc85375e5f6b62ea3f69f686ebd20a1ef7cd0bd59823c025470d85930b89a5ee2c97683d39685c32a607f8c4ecb7a8270c4aff359f0b20a4e76599894f6d987c3d2d710e56a6354001fd4bfa54770609e917915dc51994feb49155a6b2259f3f1c449baca58e43440e6aee527f56cbbd024b463ec76dceab40ffbd940297115b93a535f00ca6c7880b449077d04e35ef1e2c35f579a4df8267be809c7ce5b82f627f1e4b45e9ae0cbd79f88d3c1621b45b6a7c527e86529480949fe9f69b31b79612a91248f2f5fad6750c46d2b4d025da9b70b18d3377938e73e4f941c969f722d2b2b21a44233cf5d24701a6363eb6e28a9b4c2431db135ff4be3423a5138f70aba971173d72df910b6a336c7f15158abcd7d40c2b491d4af7732de9b0783fc8887f9ca068d8274632a42fa876d0986208
ciphertext String: �_x000C_��[:�8'�L�R�W��E���t�E9�_x0010_��7^kb�?i����
��_x000B_���,�h=9h\2���취'_x000C_J�5�_x000B_ ��e��Om�|=-q_x000E_V�5@�K�Tw ��]�����U��%�?D����CD_x000E_j�RV˽KF>�mΫ@����[��5�_x000C_�Lj_x000B_D�w�N5�,5�y�߂g���|�/bKE�_x000C_�y��<!�[j|R~�R�������1���H����u_x000C_FҴ�%ڛp��3w��s��A�i�"Ҳ�D#<��G�6>�⊛L$1�_x0013_
��4#�_x0013_�p��q=rߑ_x000B_j3l����_x000C_+IJ�s-�x?Ȉ��h�'F2�/�vИ

Just one thing to check on yout original system: the private key is taken from this line:

String privateKeyPath = System.getProperty("user.dir")+"/keys/Private_Key.asc";

but in the encrypt/decrypt methods the variable is stsPrivateKeyPath.

Main3.java:

import org.bouncycastle.bcpg.ArmoredOutputStream;
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 java.io.*;
import java.security.NoSuchProviderException;
import java.security.SecureRandom;
import java.security.Security;
import java.util.Arrays;
import java.util.Date;
import java.util.Iterator;

public class Main3 {

//private static String publicKeyPath = System.getProperty("user.dir")+"/keys/Public_Key.asc";
private static String stsPublicKeyPath = "pub_rsa_kleo.asc";
//private static String privateKeyPath = System.getProperty("user.dir")+"/keys/Private_Key.asc";
private static String stsPrivateKeyPath = "secret_rsa_kleo.asc";
private static String passwordString = "mypassphrase";

public static void main(String[] args) throws NoSuchProviderException, IOException, PGPException {
System.out.println("https://stackoverflow.com/questions/62305625/i-have-been-working-on-pgp-encrypt-and-decrypt-for-byte-with-bouncy-castle-api");
Security.addProvider(new BouncyCastleProvider());
System.out.println("\nTest with Java version: " + Runtime.version() + " BouncyCastle Version: " + Security.getProvider("BC") + "\n");

byte[] plaintext = "This is my plaintext".getBytes("UTF-8");
byte[] ciphertext = encrypt(plaintext);
byte[] decryptedtext = decrypt(ciphertext);

System.out.println("plaintext: " + bytesToHex(plaintext));
System.out.println("decrytext: " + bytesToHex(decryptedtext));
System.out.println("plaintext equals decryptedtext: " + Arrays.equals(plaintext, decryptedtext));

System.out.println("\nciphetext: " + bytesToHex(ciphertext));
System.out.println("ciphertext String: " + new String(ciphertext, "UTF-8"));

// bcprov-jdk15to18-165.jar
// bcpg-jdk15on-165.jar
// OpenJDK 11.0.5
}

public static byte[] decrypt(byte[] encrypted)
throws IOException, PGPException, NoSuchProviderException {
Security.addProvider(new BouncyCastleProvider());
InputStream keyIn = new BufferedInputStream(new FileInputStream(stsPrivateKeyPath));
char[] password = passwordString.toCharArray();
//char[] password = "".toCharArray();
InputStream in = new ByteArrayInputStream(encrypted);
in = PGPUtil.getDecoderStream(in);
JcaPGPObjectFactory pgpF = new JcaPGPObjectFactory(in);
PGPEncryptedDataList enc;
Object o = pgpF.nextObject();
if (o instanceof PGPEncryptedDataList) {
enc = (PGPEncryptedDataList) o;
} else {
enc = (PGPEncryptedDataList) pgpF.nextObject();
}
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(), password);
}
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);
PGPCompressedData cData = (PGPCompressedData)plainFact.nextObject();
JcaPGPObjectFactory pgpFact = new JcaPGPObjectFactory(cData.getDataStream());
PGPLiteralData ld = (PGPLiteralData) pgpFact.nextObject();
InputStream unc = ld.getInputStream();
ByteArrayOutputStream out = new ByteArrayOutputStream();
int ch;
while ((ch = unc.read()) >= 0) {
out.write(ch);
}
byte[] returnBytes = out.toByteArray();
out.close();
return returnBytes;
}

public static byte[] encrypt(byte[] clearData)
throws IOException, PGPException {
Security.addProvider(new BouncyCastleProvider());
String fileName=null;
boolean withIntegrityCheck = true;
boolean armor = false; // org
if (fileName == null) {
fileName = PGPLiteralData.CONSOLE;
}
PGPPublicKey encKey = PGPExampleUtil.readPublicKey(stsPublicKeyPath);
ByteArrayOutputStream encOut = new ByteArrayOutputStream();
OutputStream out = encOut;
if (armor) {
out = new ArmoredOutputStream(out);
}
ByteArrayOutputStream bOut = new ByteArrayOutputStream();
PGPCompressedDataGenerator comData = new PGPCompressedDataGenerator(
PGPCompressedData.ZIP);
OutputStream cos = comData.open(bOut); // open it with the final
// destination
PGPLiteralDataGenerator lData = new PGPLiteralDataGenerator();
OutputStream pOut = lData.open(cos, // the compressed output stream
PGPLiteralData.BINARY, fileName, // "filename" to store
clearData.length, // length of clear data
new Date() // current time
);
pOut.write(clearData);
lData.close();
comData.close();
PGPEncryptedDataGenerator cPk = new PGPEncryptedDataGenerator(new JcePGPDataEncryptorBuilder(PGPEncryptedData.CAST5).setWithIntegrityPacket(withIntegrityCheck).setSecureRandom(new SecureRandom()).setProvider("BC"));
cPk.addMethod(new JcePublicKeyKeyEncryptionMethodGenerator(encKey).setProvider("BC"));
byte[] bytes = bOut.toByteArray();
OutputStream cOut = cPk.open(out, bytes.length);
cOut.write(bytes); // obtain the actual bytes from the compressed stream
cOut.close();
out.close();
return encOut.toByteArray();
}

private static String bytesToHex(byte[] bytes) {
StringBuffer result = new StringBuffer();
for (byte b : bytes) result.append(Integer.toString((b & 0xff) + 0x100, 16).substring(1));
return result.toString();
}
}

PGPExampleUtil.java

package PGP_Encrypt_Decrypt_Armor_Error;

import org.bouncycastle.openpgp.*;
import org.bouncycastle.openpgp.operator.jcajce.JcaKeyFingerprintCalculator;
import org.bouncycastle.openpgp.operator.jcajce.JcePBESecretKeyDecryptorBuilder;

import java.io.*;
import java.security.NoSuchProviderException;
import java.util.Iterator;

// https://github.com/bcgit/bc-java/blob/master/pg/src/main/java/org/bouncycastle/openpgp/examples/PGPExampleUtil.java
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 the private key.
* @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(new JcePBESecretKeyDecryptorBuilder().setProvider("BC").build(pass));
}

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

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

secret_rsa_kleo.asc:

-----BEGIN PGP PRIVATE KEY BLOCK-----

lQPGBF7hImoBCAC1YHtHUokERd9OqXiB0/ncVwQaqEBLdd3cxRZ0Kyd7K+OxHH5f
VCdYRWSyVANn4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1pS5cqkT/T9WquybqW73whI
LpsSdBu9MWihhedCx6KtoDpSPrtzzXjKc7PJIjBPDN4pzH/0fqvN/k2736P1MP5z
yq/nOMOTevV6YZtN9SXA6Hq5AQ0EXuEiagEIAMN3rP9PhOm0ho14SXwjXKF/vXc+
EDT+vglwg7bZUpsJMYt5TMnT6dNHlV/ek8oMUEvBqBIHWrUplG/LFyaNJAjmgE4q
FoNHy1V9H9Jw439PH1T/8Mbw/n/k3xvanWcl03QcRcvPag8oaczYO+/+cbyl0R/X
lk2lAlBomfnM2+nfUFyTKVXJoG7TSHNRGbG3jmKyVzPi3Tb/JqbW2ITUBKkd+J6h
l5EK9NExqyXVoFHUf2KWfawXzEw9Dk83fKoDQHQkknzdQ+vm4l6MQ9m40OV7U3xd
6yd49p8h1+jpYv4bWguiWiJquAosABOF5DunpIUWel82uZ/PRCuLZpbGG38AEQEA
AYkBPAQYAQgAJhYhBOtbtDyOZRSPL5sgGfmgWzoStTgnBQJe4SJqAhsMBQkDwfO2
AAoJEPmgWzoStTgnFZ0H/3y9RhcPK65ssKjclr4gwMyquaDPqwKuXJLEZZNuapj0
G7j4AKJX6bN/RYJ3Nw51iC3vv5j5Pd4l6/d5PwZN5t54SV+T+6WPCbfbvBGn+jwj
mcE584hfwndioXjE+dVVoX4dhwkfZLOz6t825UTpKp2XEoFOeVpjTN8NhdiN5xe6
UHEUyHFBCkP9g81j1nKbgXXB6kss5r59WMIZrasVNOzh8WnAVLrTkuQC+/KZZqwi
8dseaBEeCR5vSzCZWjJULSeziiOGChn+e8CNKpgEn2QuNsWoOoaPe6wQVRoY/oxV
t4wc4dVh2W0HjzMOveo3Dvw66QT1NfXx4cmaOm5HWhc=
=bgyD
-----END PGP PUBLIC KEY BLOCK-----

PartialInputStream during Bouncycastle PGP decryption

Anybody who is struggeling heres a working code:

using System;
using System.Xml;
using System.IO;
using System.Security.Cryptography;
using System.Security.Cryptography.Xml;
using System.Text;

using Org.BouncyCastle.Bcpg.OpenPgp;
using Org.BouncyCastle.Security;
using Org.BouncyCastle.Utilities.IO;
using Org.BouncyCastle.Utilities.Encoders;
using Org.BouncyCastle.Bcpg;

class Program
{
private static PgpPublicKey ReadPublicKey(Stream inputStream)
{

inputStream = PgpUtilities.GetDecoderStream(inputStream);

PgpPublicKeyRingBundle pgpPub = new PgpPublicKeyRingBundle(inputStream);

//
// 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.
//

//
// iterate through the key rings.
//

foreach (PgpPublicKeyRing kRing in pgpPub.GetKeyRings())
{
foreach (PgpPublicKey k in kRing.GetPublicKeys())
{
if (k.IsEncryptionKey)
{
return k;
}
}
}

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

private static byte[] EncryptFile(byte[] clearData, string fileName, PgpPublicKey encKey, bool withIntegrityCheck)
{

MemoryStream bOut = new MemoryStream();

PgpCompressedDataGenerator comData = new PgpCopressed

Related Topics



Leave a reply



Submit