Share

GnuPG Java Wrapper API

Yaniv Yemini wrote a small GnuPG Java Wrapper API.
Just had a small look over it.
So to get it your version from here
Here is just a small demo:

import javax.swing.JOptionPane;
import org.gpg.java.GnuPG;
public class Loader {
    public static void main (String args[]){
        GnuPG pgp = new GnuPG ();
        String toolChain[] = {"sign", "clearsign", "signAndEncrypt", "encrypt", "decrypt"};
        String message = JOptionPane.showInputDialog(null, 
                                "Message you want to encrypt?", 
                                "Enter your message", 
                                JOptionPane.QUESTION_MESSAGE);
        String keyID = "0x56B69D6B";
        System.out.println("using message: "+message);
        System.out.println("using key ID: "+keyID);
        for(String tool : toolChain){
            System.out.println("running: "+tool);
            if(tool.equals("sign")){
                String passPhrase = enterPassPhrase(tool);
                pgp.sign (message, passPhrase);
            }
            if(tool.equals("clearsign")){
                String passPhrase = enterPassPhrase(tool);
                pgp.clearSign (message, passPhrase);
            }
            if(tool.equals("signAndEncrypt")){
                String passPhrase = enterPassPhrase(tool);
                pgp.signAndEncrypt (message, keyID, passPhrase);
            }
            if(tool.equals("encrypt")){
                pgp.encrypt (message, keyID);
            }
            if(tool.equals("decrypt")){
                String passPhrase = enterPassPhrase(tool);
                pgp.decrypt (message, passPhrase);
            }
            System.out.println("result: " + pgp.getGpg_result() + "nn");
            System.out.println("error: " + pgp.getGpg_err() + "nn");
            System.out.println("exit: " + pgp.getGpg_exitCode() + "nn");
        }
    }
    public static String enterPassPhrase(String usage){
        return JOptionPane.showInputDialog(null, 
                                "Please enter the Passphrase of your private Key for "+usage, 
                                "Passphrase", 
                                JOptionPane.QUESTION_MESSAGE);
    }
}

Unforntunetally there is a Problem with decrypting a message. It is possible to decrypt the String with the gpg CI Version, but within Java it does not work. So maybe the error is on my site :-).

You may also like...

Leave a Reply