本文共 2924 字,大约阅读时间需要 9 分钟。
Following are code examples for the GnuPG Java Wrapper API class.
Before you can use the class, you will need to create a private/public key pair in GPG. If you already set up the key pair, you can skip this step.
Launch the command line terminal and type the following:
gpg --gen-key
GPG now will ask you a few questions regarding the key:
After answering these questions, GPG will create and store the key in its database.
We are now ready to use the Java API class with GPG.
import java.util.*;import java.io.*;import GnuPG;// text to be signedString text = 'GnuPG Java Wrapper API';// PGP passphraseString passPhrase = 'secret passphrase';boolean result;GnuPG pgp = new GnuPG ();result = pgp.sign (text, passPhrase);if (result){ System.out.println ("Result:\n" + pgp.gpg_result + "\n\n");}else{ System.out.println ("Error signing:" + pgp.gpg_err + "\n\n");}
import java.util.*;import java.io.*;import GnuPG;// text to be clear signedString text = 'GnuPG Java Wrapper API';// PGP passphraseString passPhrase = 'secret passphrase';boolean result;GnuPG pgp = new GnuPG ();result = pgp.clearSign (text, passPhrase);if (result){ System.out.println ("Result:\n" + pgp.gpg_result + "\n\n");}else{ System.out.println ("Error clear signing:" + pgp.gpg_err + "\n\n");}
import java.util.*;import java.io.*;import GnuPG;// text to be signed and encryptedString text = 'GnuPG Java Wrapper API'; The ID of PGP key (use gpg --list-keys to get the key ID)String keyID = '8AC1'; // PGP passphraseString passPhrase = 'secret passphrase';boolean result;GnuPG pgp = new GnuPG ();result = pgp.signAndEncrypt (text, keyID, passPhrase);if (result){ System.out.println ("Result:\n" + pgp.gpg_result + "\n\n");}else{ System.out.println ("Error encrypting and signing:" + pgp.gpg_err + "\n\n");}
import java.util.*;import java.io.*;import GnuPG;// text to be encryptedString text = 'GnuPG Java Wrapper API';// The ID of PGP key (use gpg --list-keys to get the key ID)String keyID = '8AC1'; boolean result;GnuPG pgp = new GnuPG ();result = pgp.encrypt (text, keyID);if (result){ System.out.println ("Result:\n" + pgp.gpg_result + "\n\n");}else{ System.out.println ("Error encrypting:" + pgp.gpg_err + "\n\n");}
import java.util.*;import java.io.*;import GnuPG;// text to be decryptedString text = 'AB14CE281A6... 65A4F891';// PGP passphraseString passPhrase = 'secret passphrase';boolean result;GnuPG pgp = new GnuPG ();result = pgp.decrypt (text, passPhrase);if (result){ System.out.println ("Result:\n" + pgp.gpg_result + "\n\n");}else{ System.out.println ("Error decrypting:" + pgp.gpg_err + "\n\n");}
转载地址:http://iggmb.baihongyu.com/