Skip to content

Instantly share code, notes, and snippets.

@pchmn
Created May 28, 2020 12:47
Show Gist options
  • Save pchmn/70716095aab238d341a50efc4eb53d57 to your computer and use it in GitHub Desktop.
Save pchmn/70716095aab238d341a50efc4eb53d57 to your computer and use it in GitHub Desktop.
package org.bouncycastle.openpgp.examples;
import org.bouncycastle.bcpg.ArmoredOutputStream;
import org.bouncycastle.bcpg.HashAlgorithmTags;
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.PGPDigestCalculator;
import org.bouncycastle.openpgp.operator.PublicKeyDataDecryptorFactory;
import org.bouncycastle.openpgp.operator.jcajce.*;
import org.bouncycastle.util.io.Streams;
import java.io.*;
import java.security.*;
import java.util.Date;
public class EncryptWithRSA {
private static PGPPublicKey publicKey;
private static PGPPrivateKey privateKey;
public static byte[] createRsaEncryptedObject(PGPPublicKey encryptionKey, byte[] data)
throws PGPException, IOException
{
ByteArrayOutputStream bOut = new ByteArrayOutputStream();
PGPLiteralDataGenerator lData = new PGPLiteralDataGenerator();
OutputStream pOut = lData.open(bOut,
PGPLiteralData.BINARY,
PGPLiteralData.CONSOLE,
data.length,
new Date());
pOut.write(data);
pOut.close();
byte[] plainText = bOut.toByteArray();
ByteArrayOutputStream encOut = new ByteArrayOutputStream();
PGPEncryptedDataGenerator encGen = new PGPEncryptedDataGenerator(
new JcePGPDataEncryptorBuilder(
SymmetricKeyAlgorithmTags.AES_256)
.setWithIntegrityPacket(true)
.setSecureRandom(new SecureRandom())
.setProvider("BC"));
encGen.addMethod(new JcePublicKeyKeyEncryptionMethodGenerator(encryptionKey)
.setProvider("BC"));
OutputStream cOut = encGen.open(encOut, plainText.length);
cOut.write(plainText);
cOut.close();
return encOut.toByteArray();
}
public static byte[] extractRsaEncryptedObject(PGPPrivateKey privateKey, byte[] pgpEncryptedData)
throws PGPException, IOException
{
PGPObjectFactory pgpFact = new JcaPGPObjectFactory(pgpEncryptedData);
PGPEncryptedDataList encList = (PGPEncryptedDataList)pgpFact.nextObject();
// note: we can only do this because we know we match the first encrypted data object
PGPPublicKeyEncryptedData encData = (PGPPublicKeyEncryptedData)encList.get(0);
PublicKeyDataDecryptorFactory dataDecryptorFactory = new JcePublicKeyDataDecryptorFactoryBuilder()
.setProvider("BC").build(privateKey);
InputStream clear = encData.getDataStream(dataDecryptorFactory);
byte[] literalData = Streams.readAll(clear);
if (encData.verify())
{
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 generateKeyPair() throws NoSuchProviderException, NoSuchAlgorithmException, PGPException {
KeyPairGenerator kpg = KeyPairGenerator.getInstance("RSA", "BC");
kpg.initialize(1024);
KeyPair kp = kpg.generateKeyPair();
PGPDigestCalculator sha1Calc = new JcaPGPDigestCalculatorProviderBuilder().build().get(HashAlgorithmTags.SHA1);
PGPKeyPair keyPair = new JcaPGPKeyPair(PGPPublicKey.RSA_GENERAL, kp, new Date());
PGPSecretKey secretKey = new PGPSecretKey(PGPSignature.DEFAULT_CERTIFICATION, keyPair, "test", sha1Calc, null, null, new JcaPGPContentSignerBuilder(keyPair.getPublicKey().getAlgorithm(), HashAlgorithmTags.SHA1), new JcePBESecretKeyEncryptorBuilder(PGPEncryptedData.CAST5, sha1Calc).setProvider("BC").build("test".toCharArray()));
publicKey = secretKey.getPublicKey();
privateKey = secretKey.extractPrivateKey(new JcePBESecretKeyDecryptorBuilder().setProvider("BC").build("test".toCharArray()));
}
public static void main(
String[] args)
throws Exception
{
Security.addProvider(new BouncyCastleProvider());
generateKeyPair();
byte[] encryptedText = createRsaEncryptedObject(publicKey, "text to encrypt".getBytes());
System.out.println("encrypted text:" + new String(encryptedText));
byte[] decryptedText = extractRsaEncryptedObject(privateKey, encryptedText);
System.out.println("decrypted text:" + new String(decryptedText));
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment