Last active
December 1, 2021 14:41
-
-
Save dwelch2344/6986219 to your computer and use it in GitHub Desktop.
A simple example of decrypting a symmetrically encrypted file (via GPG)
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?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> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
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