Skip to content

Instantly share code, notes, and snippets.

@dwelch2344
Last active December 1, 2021 14:41
Show Gist options
  • Save dwelch2344/6986219 to your computer and use it in GitHub Desktop.
Save dwelch2344/6986219 to your computer and use it in GitHub Desktop.
A simple example of decrypting a symmetrically encrypted file (via GPG)
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>co.ntier.security</groupId>
<artifactId>gpg-example</artifactId>
<version>1.0-SNAPSHOT</version>
<properties>
<org.bouncycastle.version>1.46</org.bouncycastle.version>
</properties>
<dependencies>
<dependency>
<groupId>org.bouncycastle</groupId>
<artifactId>bcmail-jdk15</artifactId>
<version>${org.bouncycastle.version}</version>
</dependency>
<dependency>
<groupId>org.bouncycastle</groupId>
<artifactId>bcpg-jdk15</artifactId>
<version>${org.bouncycastle.version}</version>
</dependency>
<dependency>
<groupId>org.bouncycastle</groupId>
<artifactId>bcprov-jdk15</artifactId>
<version>${org.bouncycastle.version}</version>
</dependency>
<dependency>
<groupId>commons-io</groupId>
<artifactId>commons-io</artifactId>
<version>2.4</version>
</dependency>
</dependencies>
</project>
import org.apache.commons.io.FileUtils;
import org.bouncycastle.bcpg.SymmetricKeyAlgorithmTags;
import org.bouncycastle.openpgp.*;
import org.bouncycastle.openpgp.examples.ByteArrayHandler;
import org.bouncycastle.openssl.PEMReader;
import org.bouncycastle.util.io.Streams;
import java.io.*;
import java.security.NoSuchProviderException;
import java.security.Security;
public class Symmetric {
private static final String FOLDER = "/Users/dave/Desktop/encrypted/";
private static final String PASS = "abc123";
public static void main(String[] args) throws IOException, PGPException, NoSuchProviderException {
Security.addProvider(new org.bouncycastle.jce.provider.BouncyCastleProvider());
File encryptedFile = new File(FOLDER + "symmetric.txt.gpg");
byte[] encryptedByteArray = FileUtils.readFileToByteArray(encryptedFile);
byte[] decryptedByteArray = ByteArrayHandler.decrypt(encryptedByteArray, PASS.toCharArray());
String decryptedString = new String(decryptedByteArray);
System.out.println(decryptedString);
System.out.println();
byte[] encryptedAgain = ByteArrayHandler.encrypt(decryptedByteArray, PASS.toCharArray(), "foobar.txt", SymmetricKeyAlgorithmTags.AES_256, true);
String encryptedAgainString = new String(encryptedAgain);
System.out.println(encryptedAgainString);
byte[] decryptedAgainByteArray = ByteArrayHandler.decrypt(encryptedAgain, PASS.toCharArray());
String decrypteAgaindString = new String(decryptedAgainByteArray);
System.out.println(decrypteAgaindString);
}
}
@jiayaoO3O
Copy link

Should a normal gpg encryption and decryption use the public key, private key, password three elements? Why you can only use the password to encrypt and decrypt

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment