I want to encrypt and decrypt the Java JSON rest request using GPG but its not working. Getting Invalid header Encountered
during decrypt method.
I am using below dependency in my Spring boot 2.3 project with Java 1.8
<code> <dependency>
<groupId>org.bouncycastle</groupId>
<artifactId>bcpg-jdk18on</artifactId>
<version>1.78.1</version>
</dependency>
<dependency>
<groupId>org.bouncycastle</groupId>
<artifactId>bcprov-jdk18on</artifactId>
<version>1.78.1</version>
</dependency>
</code>
<code> <dependency>
<groupId>org.bouncycastle</groupId>
<artifactId>bcpg-jdk18on</artifactId>
<version>1.78.1</version>
</dependency>
<dependency>
<groupId>org.bouncycastle</groupId>
<artifactId>bcprov-jdk18on</artifactId>
<version>1.78.1</version>
</dependency>
</code>
<dependency>
<groupId>org.bouncycastle</groupId>
<artifactId>bcpg-jdk18on</artifactId>
<version>1.78.1</version>
</dependency>
<dependency>
<groupId>org.bouncycastle</groupId>
<artifactId>bcprov-jdk18on</artifactId>
<version>1.78.1</version>
</dependency>
Below is my encrypted data.
<code>Encrypted Data is-----BEGIN PGP MESSAGE-----
Version: BCPG v@RELEASE_NAME@
wcFMAyRgUyQUFpOMAQ//VVtzEGltqEo90Fpmk/lq0KyjGu4bSu9Rp9qhK1cyLX1d
vyMrPreLfTBzbL37KKcGWF1lqc9StlRaqCUQW/vjEjpN0Nnxhm9vYrq3+zChF2qB
yKKAb0bc1uoWitxNROEkcQeKm6oWbGJ+WwG/MIFsg90RTer89Rtg0zJ5/CoAlNcg
yHNdAzlSmT2s36I6a4ctuO+VYKT+uNIXCnsHfW5kKZkD6Ypai7guu4lKu613gLnO
g7dFA7U9RzFiXxg7YJMwulA2imlx1etqQkzxuQPNj6BPXHW8g/1brG1XAXAhW3qO
zIzQZUia4VNVOhKXIC2rQtO+wYzXL9dif2iQ3vQ/QZkCYvhAwi9ktx/XPrSmreao
h9ZOCCgS4dTTKjcz7vTaTV3+mvL2H+DrkgmoGJB/NzF9jbOGvSsO5HlSmrpxLSr7
WDGrfxaNiSIxSbuyI2DHboZWtWVDXjAV87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
=XZXh
-----END PGP MESSAGE-----
and during decryption its giving error as
<code>java.io.IOException: invalid header encountered
at org.bouncycastle.bcpg.BCPGInputStream.readPacket(Unknown Source)
at org.bouncycastle.openpgp.PGPCompressedData.<init>(Unknown Source)
</code>
<code>java.io.IOException: invalid header encountered
at org.bouncycastle.bcpg.BCPGInputStream.readPacket(Unknown Source)
at org.bouncycastle.openpgp.PGPCompressedData.<init>(Unknown Source)
</code>
java.io.IOException: invalid header encountered
at org.bouncycastle.bcpg.BCPGInputStream.readPacket(Unknown Source)
at org.bouncycastle.openpgp.PGPCompressedData.<init>(Unknown Source)
here my complete code for this task,
<code>public class EncryptionDecryptionUtils {
static {
Security.addProvider(new org.bouncycastle.jce.provider.BouncyCastleProvider());
}
public static byte[] encrypt(String plaintext, PGPPublicKey publicKey) throws IOException, PGPException {
ByteArrayOutputStream byteArrayOutputStream = new ByteArrayOutputStream();
try (OutputStream armoredOutputStream = new ArmoredOutputStream(byteArrayOutputStream)) {
PGPEncryptedDataGenerator encryptedDataGenerator = new PGPEncryptedDataGenerator(
new JcePGPDataEncryptorBuilder(PGPEncryptedData.AES_256)
.setWithIntegrityPacket(true)
.setSecureRandom(new SecureRandom())
.setProvider("BC"));
encryptedDataGenerator.addMethod(new JcePublicKeyKeyEncryptionMethodGenerator(publicKey)
.setProvider("BC"));
try (OutputStream outputStream = encryptedDataGenerator.open(armoredOutputStream, new byte[4096])) {
outputStream.write(plaintext.getBytes());
}
}
return byteArrayOutputStream.toByteArray();
}
public static String decrypt(byte[] encryptedData, InputStream privateKeyInputStream, char[] passphrase)
throws IOException, PGPException, NoSuchProviderException {
ByteArrayInputStream byteArrayInputStream = new ByteArrayInputStream(encryptedData);
try (InputStream armoredInputStream = new ArmoredInputStream(byteArrayInputStream)) {
PGPObjectFactory pgpObjectFactory = new PGPObjectFactory(armoredInputStream,
new JcaKeyFingerprintCalculator());
Object pgpObject = pgpObjectFactory.nextObject();
if (pgpObject instanceof PGPEncryptedDataList) {
PGPEncryptedDataList encryptedDataList = (PGPEncryptedDataList) pgpObject;
PGPSecretKeyRingCollection secretKeyRingCollection = new PGPSecretKeyRingCollection(
PGPUtil.getDecoderStream(privateKeyInputStream), new JcaKeyFingerprintCalculator());
PGPPublicKeyEncryptedData encryptedDataObject = null;
PGPSecretKey secretKey = null;
for (int i = 0; i < encryptedDataList.size() && encryptedDataObject == null; i++) {
encryptedDataObject = (PGPPublicKeyEncryptedData) encryptedDataList.get(i);
secretKey = secretKeyRingCollection.getSecretKey(encryptedDataObject.getKeyID());
if (secretKey != null) {
break;
}
}
if (secretKey == null) {
throw new PGPException("No secret key found for decryption.");
}
InputStream decryptedStream = encryptedDataObject.getDataStream(
new JcePublicKeyDataDecryptorFactoryBuilder().setProvider("BC")
.build(secretKey.extractPrivateKey(
new JcePBESecretKeyDecryptorBuilder().setProvider("BC").build(passphrase))));
pgpObjectFactory = new PGPObjectFactory(decryptedStream, new JcaKeyFingerprintCalculator());
pgpObject = pgpObjectFactory.nextObject();
if (pgpObject instanceof PGPCompressedData) {
PGPCompressedData compressedData = (PGPCompressedData) pgpObject;
pgpObjectFactory = new PGPObjectFactory(compressedData.getDataStream(),
new JcaKeyFingerprintCalculator());
pgpObject = pgpObjectFactory.nextObject();
}
if (pgpObject instanceof PGPLiteralData) {
PGPLiteralData literalData = (PGPLiteralData) pgpObject;
InputStream literalDataStream = literalData.getInputStream();
StringBuilder plaintext = new StringBuilder();
byte[] buffer = new byte[4096];
int bytesRead;
while ((bytesRead = literalDataStream.read(buffer)) != -1) {
plaintext.append(new String(buffer, 0, bytesRead));
}
return plaintext.toString();
} else {
throw new PGPException("Message is not a simple encrypted file - type unknown.");
}
} else {
throw new PGPException("Expected PGPEncryptedDataList, found " + pgpObject.getClass().getName());
}
}
}
private static PGPPublicKey getPublicKey(InputStream publicKeyInputStream) throws IOException, PGPException {
PGPPublicKeyRingCollection pgpPublicKeyRingCollection = new PGPPublicKeyRingCollection(
PGPUtil.getDecoderStream(publicKeyInputStream), new JcaKeyFingerprintCalculator());
Iterator<PGPPublicKeyRing> keyRingIterator = pgpPublicKeyRingCollection.getKeyRings();
while (keyRingIterator.hasNext()) {
PGPPublicKeyRing keyRing = keyRingIterator.next();
Iterator<PGPPublicKey> publicKeyIterator = keyRing.getPublicKeys();
while (publicKeyIterator.hasNext()) {
PGPPublicKey publicKey = publicKeyIterator.next();
if (publicKey.isEncryptionKey()) {
return publicKey;
}
}
}
throw new IllegalArgumentException("No encryption key found in the public key ring.");
}
public static void main(String args[]) {
try {
String publicKeyPath = "C:/Users/rest/src/main/resources/keys/dev/publicKey.asc";
String privateKeyPath = "C:/Users/rest/src/main/resources/keys/dev/privateKey.asc";
InputStream publicKeyStream = new FileInputStream(publicKeyPath);
InputStream privateKeyStream = new FileInputStream(privateKeyPath);
String testdata = "Hello man encrypt it then decrypt";
PGPPublicKey publicKey = getPublicKey(publicKeyStream);
byte[] encryptedData = encrypt(testdata, publicKey);
System.out.println("Encrypted Data is" + new String(encryptedData, StandardCharsets.UTF_8));
String decrypt = decrypt(encryptedData, privateKeyStream, "May@2024".toCharArray());
System.out.println("Decrypted Data is" + decrypt);
} catch (Exception e) {
System.out.println("Exception is " + e.getMessage());
}
}
}
</code>
<code>public class EncryptionDecryptionUtils {
static {
Security.addProvider(new org.bouncycastle.jce.provider.BouncyCastleProvider());
}
public static byte[] encrypt(String plaintext, PGPPublicKey publicKey) throws IOException, PGPException {
ByteArrayOutputStream byteArrayOutputStream = new ByteArrayOutputStream();
try (OutputStream armoredOutputStream = new ArmoredOutputStream(byteArrayOutputStream)) {
PGPEncryptedDataGenerator encryptedDataGenerator = new PGPEncryptedDataGenerator(
new JcePGPDataEncryptorBuilder(PGPEncryptedData.AES_256)
.setWithIntegrityPacket(true)
.setSecureRandom(new SecureRandom())
.setProvider("BC"));
encryptedDataGenerator.addMethod(new JcePublicKeyKeyEncryptionMethodGenerator(publicKey)
.setProvider("BC"));
try (OutputStream outputStream = encryptedDataGenerator.open(armoredOutputStream, new byte[4096])) {
outputStream.write(plaintext.getBytes());
}
}
return byteArrayOutputStream.toByteArray();
}
public static String decrypt(byte[] encryptedData, InputStream privateKeyInputStream, char[] passphrase)
throws IOException, PGPException, NoSuchProviderException {
ByteArrayInputStream byteArrayInputStream = new ByteArrayInputStream(encryptedData);
try (InputStream armoredInputStream = new ArmoredInputStream(byteArrayInputStream)) {
PGPObjectFactory pgpObjectFactory = new PGPObjectFactory(armoredInputStream,
new JcaKeyFingerprintCalculator());
Object pgpObject = pgpObjectFactory.nextObject();
if (pgpObject instanceof PGPEncryptedDataList) {
PGPEncryptedDataList encryptedDataList = (PGPEncryptedDataList) pgpObject;
PGPSecretKeyRingCollection secretKeyRingCollection = new PGPSecretKeyRingCollection(
PGPUtil.getDecoderStream(privateKeyInputStream), new JcaKeyFingerprintCalculator());
PGPPublicKeyEncryptedData encryptedDataObject = null;
PGPSecretKey secretKey = null;
for (int i = 0; i < encryptedDataList.size() && encryptedDataObject == null; i++) {
encryptedDataObject = (PGPPublicKeyEncryptedData) encryptedDataList.get(i);
secretKey = secretKeyRingCollection.getSecretKey(encryptedDataObject.getKeyID());
if (secretKey != null) {
break;
}
}
if (secretKey == null) {
throw new PGPException("No secret key found for decryption.");
}
InputStream decryptedStream = encryptedDataObject.getDataStream(
new JcePublicKeyDataDecryptorFactoryBuilder().setProvider("BC")
.build(secretKey.extractPrivateKey(
new JcePBESecretKeyDecryptorBuilder().setProvider("BC").build(passphrase))));
pgpObjectFactory = new PGPObjectFactory(decryptedStream, new JcaKeyFingerprintCalculator());
pgpObject = pgpObjectFactory.nextObject();
if (pgpObject instanceof PGPCompressedData) {
PGPCompressedData compressedData = (PGPCompressedData) pgpObject;
pgpObjectFactory = new PGPObjectFactory(compressedData.getDataStream(),
new JcaKeyFingerprintCalculator());
pgpObject = pgpObjectFactory.nextObject();
}
if (pgpObject instanceof PGPLiteralData) {
PGPLiteralData literalData = (PGPLiteralData) pgpObject;
InputStream literalDataStream = literalData.getInputStream();
StringBuilder plaintext = new StringBuilder();
byte[] buffer = new byte[4096];
int bytesRead;
while ((bytesRead = literalDataStream.read(buffer)) != -1) {
plaintext.append(new String(buffer, 0, bytesRead));
}
return plaintext.toString();
} else {
throw new PGPException("Message is not a simple encrypted file - type unknown.");
}
} else {
throw new PGPException("Expected PGPEncryptedDataList, found " + pgpObject.getClass().getName());
}
}
}
private static PGPPublicKey getPublicKey(InputStream publicKeyInputStream) throws IOException, PGPException {
PGPPublicKeyRingCollection pgpPublicKeyRingCollection = new PGPPublicKeyRingCollection(
PGPUtil.getDecoderStream(publicKeyInputStream), new JcaKeyFingerprintCalculator());
Iterator<PGPPublicKeyRing> keyRingIterator = pgpPublicKeyRingCollection.getKeyRings();
while (keyRingIterator.hasNext()) {
PGPPublicKeyRing keyRing = keyRingIterator.next();
Iterator<PGPPublicKey> publicKeyIterator = keyRing.getPublicKeys();
while (publicKeyIterator.hasNext()) {
PGPPublicKey publicKey = publicKeyIterator.next();
if (publicKey.isEncryptionKey()) {
return publicKey;
}
}
}
throw new IllegalArgumentException("No encryption key found in the public key ring.");
}
public static void main(String args[]) {
try {
String publicKeyPath = "C:/Users/rest/src/main/resources/keys/dev/publicKey.asc";
String privateKeyPath = "C:/Users/rest/src/main/resources/keys/dev/privateKey.asc";
InputStream publicKeyStream = new FileInputStream(publicKeyPath);
InputStream privateKeyStream = new FileInputStream(privateKeyPath);
String testdata = "Hello man encrypt it then decrypt";
PGPPublicKey publicKey = getPublicKey(publicKeyStream);
byte[] encryptedData = encrypt(testdata, publicKey);
System.out.println("Encrypted Data is" + new String(encryptedData, StandardCharsets.UTF_8));
String decrypt = decrypt(encryptedData, privateKeyStream, "May@2024".toCharArray());
System.out.println("Decrypted Data is" + decrypt);
} catch (Exception e) {
System.out.println("Exception is " + e.getMessage());
}
}
}
</code>
public class EncryptionDecryptionUtils {
static {
Security.addProvider(new org.bouncycastle.jce.provider.BouncyCastleProvider());
}
public static byte[] encrypt(String plaintext, PGPPublicKey publicKey) throws IOException, PGPException {
ByteArrayOutputStream byteArrayOutputStream = new ByteArrayOutputStream();
try (OutputStream armoredOutputStream = new ArmoredOutputStream(byteArrayOutputStream)) {
PGPEncryptedDataGenerator encryptedDataGenerator = new PGPEncryptedDataGenerator(
new JcePGPDataEncryptorBuilder(PGPEncryptedData.AES_256)
.setWithIntegrityPacket(true)
.setSecureRandom(new SecureRandom())
.setProvider("BC"));
encryptedDataGenerator.addMethod(new JcePublicKeyKeyEncryptionMethodGenerator(publicKey)
.setProvider("BC"));
try (OutputStream outputStream = encryptedDataGenerator.open(armoredOutputStream, new byte[4096])) {
outputStream.write(plaintext.getBytes());
}
}
return byteArrayOutputStream.toByteArray();
}
public static String decrypt(byte[] encryptedData, InputStream privateKeyInputStream, char[] passphrase)
throws IOException, PGPException, NoSuchProviderException {
ByteArrayInputStream byteArrayInputStream = new ByteArrayInputStream(encryptedData);
try (InputStream armoredInputStream = new ArmoredInputStream(byteArrayInputStream)) {
PGPObjectFactory pgpObjectFactory = new PGPObjectFactory(armoredInputStream,
new JcaKeyFingerprintCalculator());
Object pgpObject = pgpObjectFactory.nextObject();
if (pgpObject instanceof PGPEncryptedDataList) {
PGPEncryptedDataList encryptedDataList = (PGPEncryptedDataList) pgpObject;
PGPSecretKeyRingCollection secretKeyRingCollection = new PGPSecretKeyRingCollection(
PGPUtil.getDecoderStream(privateKeyInputStream), new JcaKeyFingerprintCalculator());
PGPPublicKeyEncryptedData encryptedDataObject = null;
PGPSecretKey secretKey = null;
for (int i = 0; i < encryptedDataList.size() && encryptedDataObject == null; i++) {
encryptedDataObject = (PGPPublicKeyEncryptedData) encryptedDataList.get(i);
secretKey = secretKeyRingCollection.getSecretKey(encryptedDataObject.getKeyID());
if (secretKey != null) {
break;
}
}
if (secretKey == null) {
throw new PGPException("No secret key found for decryption.");
}
InputStream decryptedStream = encryptedDataObject.getDataStream(
new JcePublicKeyDataDecryptorFactoryBuilder().setProvider("BC")
.build(secretKey.extractPrivateKey(
new JcePBESecretKeyDecryptorBuilder().setProvider("BC").build(passphrase))));
pgpObjectFactory = new PGPObjectFactory(decryptedStream, new JcaKeyFingerprintCalculator());
pgpObject = pgpObjectFactory.nextObject();
if (pgpObject instanceof PGPCompressedData) {
PGPCompressedData compressedData = (PGPCompressedData) pgpObject;
pgpObjectFactory = new PGPObjectFactory(compressedData.getDataStream(),
new JcaKeyFingerprintCalculator());
pgpObject = pgpObjectFactory.nextObject();
}
if (pgpObject instanceof PGPLiteralData) {
PGPLiteralData literalData = (PGPLiteralData) pgpObject;
InputStream literalDataStream = literalData.getInputStream();
StringBuilder plaintext = new StringBuilder();
byte[] buffer = new byte[4096];
int bytesRead;
while ((bytesRead = literalDataStream.read(buffer)) != -1) {
plaintext.append(new String(buffer, 0, bytesRead));
}
return plaintext.toString();
} else {
throw new PGPException("Message is not a simple encrypted file - type unknown.");
}
} else {
throw new PGPException("Expected PGPEncryptedDataList, found " + pgpObject.getClass().getName());
}
}
}
private static PGPPublicKey getPublicKey(InputStream publicKeyInputStream) throws IOException, PGPException {
PGPPublicKeyRingCollection pgpPublicKeyRingCollection = new PGPPublicKeyRingCollection(
PGPUtil.getDecoderStream(publicKeyInputStream), new JcaKeyFingerprintCalculator());
Iterator<PGPPublicKeyRing> keyRingIterator = pgpPublicKeyRingCollection.getKeyRings();
while (keyRingIterator.hasNext()) {
PGPPublicKeyRing keyRing = keyRingIterator.next();
Iterator<PGPPublicKey> publicKeyIterator = keyRing.getPublicKeys();
while (publicKeyIterator.hasNext()) {
PGPPublicKey publicKey = publicKeyIterator.next();
if (publicKey.isEncryptionKey()) {
return publicKey;
}
}
}
throw new IllegalArgumentException("No encryption key found in the public key ring.");
}
public static void main(String args[]) {
try {
String publicKeyPath = "C:/Users/rest/src/main/resources/keys/dev/publicKey.asc";
String privateKeyPath = "C:/Users/rest/src/main/resources/keys/dev/privateKey.asc";
InputStream publicKeyStream = new FileInputStream(publicKeyPath);
InputStream privateKeyStream = new FileInputStream(privateKeyPath);
String testdata = "Hello man encrypt it then decrypt";
PGPPublicKey publicKey = getPublicKey(publicKeyStream);
byte[] encryptedData = encrypt(testdata, publicKey);
System.out.println("Encrypted Data is" + new String(encryptedData, StandardCharsets.UTF_8));
String decrypt = decrypt(encryptedData, privateKeyStream, "May@2024".toCharArray());
System.out.println("Decrypted Data is" + decrypt);
} catch (Exception e) {
System.out.println("Exception is " + e.getMessage());
}
}
}
Can any one please let me know what can be issue, instead of JSONObject i tried with simple string but its still failing.
Any suggestion must be apprecaited.