Utility to encrypt and decrypt anything (bytes, file, streams) with pgp encryption
The best way is to look at the test class PgpEncryptionTest
. But I have shared some quick snippets below if you are in a hurry
For a detailed explanation of how the utility works go here
First instantiate a PgpEncryptionUtil
object like so:
PgpEncryptionUtil pgpEncryptionUtil = PgpEncryptionUtil.builder()
.armor(true)
.compressionAlgorithm(CompressionAlgorithmTags.ZIP)
.symmetricKeyAlgorithm(SymmetricKeyAlgorithmTags.AES_128)
.withIntegrityCheck(true)
.build();
To encrypt bytes call:
byte[] encryptedBytes = pgpEncryptionUtil.encrypt(bytesToEncrypt, publicKey)
To encrypt inputstream call:
InputStream encryptedIn = pgpEncryptionUtil.encrypt(<inputstreamToEncrypt>, lengthOfInputstreamToEncrypt, publicKey);
To encrypt file use:
pgpEncryptionUtil.encrypt(fileOutputstream, fileInputstream, fileLength, publicKey))
First instantiate a PgpDecryptionUtil
object like so:
PgpDecryptionUtil pgpDecryptionUtil = new PgpDecryptionUtil(privateKey, passKey);
To decrypt bytes call:
byte[] decryptedBytes = pgpDecryptionUtil.decrypt(encryptedBytes);
To decrypt files / inputstreams call:
pgpDecryptionUtil.decrypt(encryptedIn, fileOutputstream);