-
-
Save bvn13/edef743be2d76fe927f7a159c3f1184b to your computer and use it in GitHub Desktop.
A simple example of decrypting a symmetrically encrypted file (via GPG)
This file contains hidden or 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 hidden or 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