GnuPG Java Wrapper API - Sample code

转自

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 signed
String		text = 'GnuPG Java Wrapper API';
// PGP passphrase
String		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 signed
String		text = 'GnuPG Java Wrapper API';
// PGP passphrase
String		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 encrypted
String		text = 'GnuPG Java Wrapper API';
 The ID of PGP key (use gpg --list-keys to get the key ID)
String		keyID = '8AC1';	
// PGP passphrase
String		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 encrypted
String		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 decrypted
String		text = 'AB14CE281A6... 65A4F891';
// PGP passphrase
String		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");
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值