GnuPG Java Wrapper API - Sample code
内容导读
互联网集市收集整理的这篇技术教程文章主要介绍了GnuPG Java Wrapper API - Sample code,小编现在分享给大家,供广大互联网技能从业者学习和参考。文章包含2939字,纯文字阅读大概需要5分钟。
内容图文
![GnuPG Java Wrapper API - Sample code](/upload/InfoBanner/zyjiaocheng/848/62347d67f8cb4054a8f09457c14ca88f.jpg)
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");
}
内容总结
以上是互联网集市为您收集整理的GnuPG Java Wrapper API - Sample code全部内容,希望文章能够帮你解决GnuPG Java Wrapper API - Sample code所遇到的程序开发问题。 如果觉得互联网集市技术教程内容还不错,欢迎将互联网集市网站推荐给程序员好友。
内容备注
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 gblab@vip.qq.com 举报,一经查实,本站将立刻删除。
内容手机端
扫描二维码推送至手机访问。