博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
GnuPG Java Wrapper API - Sample code
阅读量:2440 次
发布时间:2019-05-10

本文共 2924 字,大约阅读时间需要 9 分钟。

Following are code examples for the GnuPG Java Wrapper API class.

Before you Begin: Create PGP Key Pair

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:

  • The kind and size of the key.
  • For how long the key should be valid.
  • Your name and email
  • Comment for the key.
  • Passphrase (password) for 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.

Signing with PGP

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");}

ClearSigning with PGP

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");}

Sign and Encrypt with PGP

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");}

Encrypting with PGP

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");}

Decrypting with PGP

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/

你可能感兴趣的文章
c/cplusplus通用makefile
查看>>
JavaScript-密码强度
查看>>
【SSH】1366-InCorrect string value:'\xE9\x99\x88\xE6\x96\xB0...'for column 'name' at row 1
查看>>
SpringCloud前身之微服务
查看>>
纵览全局——SSH
查看>>
Mybatis-略识之无
查看>>
[Vue warn]: Property or method "name" is not defined on the instance but referenced during render
查看>>
ts:json串转换成数组
查看>>
String、StringBuffer和StringBuilder的区别
查看>>
java——职责链模式
查看>>
java_选择类排序——简单选择排序
查看>>
java_中介者模式
查看>>
多线程——背景了解
查看>>
power designer使Comment与Name相同.txt
查看>>
IE下的图片空隙间距BUG和解决办法
查看>>
[pb]从excel导入数据到datawindow
查看>>
CSS Padding in Outlook 2007 and 2010
查看>>
有关内存的思考题
查看>>
LNMP一键安装
查看>>
几个分析函数的比较
查看>>