Java PGP
From Foochal
Contents |
[edit]
How to use PGP or GPG from Java
This article describes how to use PGP encryption and decryption from your java programs
[edit]
Quick tutorial on using Gnu PGP, or PGP from command line
[edit]
Generating your PGP keys
Generate your keys in custom directory (default is USER_HOME/.gnupg).
$ GPG_DIR=gpgdir $ mkdir $GPG_DIR $ gpg --gen-key --homedir $GPG_DIR
Use the following inputs:
- kind of key
- RSA (sign only)
- Key is valid for?
- (0)
- Is this correct? (y/N)
- y
- Real name
- Foochal
- Email address
- foochal@foochal.org
- Comment
- Change (N)ame, (C)omment, (E)mail or (O)kay/(Q)uit?
- O
- You need a Passphrase to protect your secret key.
- change this -> my secret passphrase <- change this
[edit]
Encrypting a file
GPG=/usr/bin/gpg PASSPHRASE_FILE=passphrase.txt # gpg home directory, no trailing slash GPG_DIR=gpgdir ORIGINAL_FILE=secret.txt ENCRYPTED_FILE=secret.pgp DECRYPTED_FILE=secret.decrypted.txt RECIPIENT_ID="Foochal <foochal@foochal.org>" rm -f $ENCRYPTED_FILE $DECRYPTED_FILE #encryption $GPG --trust-model always --homedir $GPG_DIR --encrypt --batch --recipient "$RECIPIENT_ID" --output $ENCRYPTED_FILE $ORIGINAL_FILE
[edit]
Decrypting a file
#decryption cat $PASSPHRASE_FILE | $GPG --trust-model always --homedir $GPG_DIR --decrypt --batch --passphrase-fd 0 --output $DECRYPTED_FILE $ENCRYPTED_FILE # verify that encryption and decryption were successful diff $ORIGINAL_FILE $DECRYPTED_FILE

