java gpg_java---gpg加密

本文介绍了如何使用Java调用GPG命令行工具进行文件的加密和解密操作,包括生成密钥对、设置密钥有效期、导出公钥和私钥等步骤,并提供了Java代码示例来执行加密和解密命令。
摘要由CSDN通过智能技术生成

gpg --gen-key   //输入此命令

gpg (GnuPG) 2.0.17; Copyright (C) 2011 Free Software Foundation, Inc

This is free software: you are free to change and redistribute it.

There is NO WARRANTY, to the extent permitted by law.

Please select what kind of key you want:

(1) RSA and RSA (default)

(2) DSA and Elgamal

(3) DSA (sign only)

(4) RSA (sign only)

Your selection?1   //只有1可以用于加密,其它用于签名

RSA keys may be between 1024 and 4096 bits long.

What keysize do you want? (2048)  // 选择密码的位数,位数越大越安全,但速度慢

Requested keysize is 2048 bits

Please specify how long the key should be valid.

0 = key does not expire

  = key expires in n days

w = key expires in n weeks

m = key expires in n months

y = key expires in n years

Key is valid for? (0) 0   //根据实际情况选择密钥时限 0表示永久

Key does not expire at all

Is this correct? (y/N) y//确认

GnuPG needs to construct a user ID to identify your key.

Real name: myname   //请输入真实姓名,以后会用到

Email address: myemail@server.com  //输入邮箱,不能重复

Comment: comment  //可以为空

You selected this USER-ID:

"raolin (use for GPG Encrypt) "

Change (N)ame, (C)omment, (E)mail or (O)kay/(Q)uit?o //输入o确认

You need a Passphrase to protect your secret key.  //输入两次密码

We need to generate a lot of random bytes. It is a good idea to perform

some other action (type on the keyboard, move the mouse, utilize the

disks) during the prime generation; this gives the random number

generator a better chance to gain enough entropy.

We need to generate a lot of random bytes. It is a good idea to perform

some other action (type on the keyboard, move the mouse, utilize the

disks) during the prime generation; this gives the random number

generator a better chance to gain enough entropy.

gpg: C:/Documents and Settings/raolin/Application Data/gnupg/trustdb.gpg: trustd

b created

gpg: key 8CC6954D marked as ultimately trusted    //密钥ID

public and secret key created and signed.

gpg: checking the trustdb

gpg: 3 marginal(s) needed, 1 complete(s) needed, PGP trust model

gpg: depth: 0  valid:   1  signed:   0  trust: 0-, 0q, 0n, 0m, 0f, 1u

pub   2048R/8CC6954D 2011-08-02

Key fingerprint = 2D3F A584 6B77 59E6 E937  650E 9867 920D 8CC6 954D

uid                  raolin (use for GPG Encrypt)

sub   2048R/D55E7B91 2011-08-02

========================================

C:\Documents and Settings\admin>gpg --output revoke.asc --gen-revoke 8CC6954D  //密钥回收

sec  2048R/8CC6954D 2011-08-02 raolin (use for GPG Encrypt)

Create a revocation certificate for this key? (y/N) y

Please select the reason for the revocation:

0 = No reason specified

1 = Key has been compromised

2 = Key is superseded

3 = Key is no longer used

Q = Cancel

(Probably you want to select 1 here)

Your decision? 0

Enter an optional description; end it with an empty line:

> revoke file generation

>

Reason for revocation: No reason specified

revoke file generation

Is this okay? (y/N) y

You need a passphrase to unlock the secret key for

user: "raolin (use for GPG Encrypt) "

2048-bit RSA key, ID 8CC6954D, created 2011-08-02

ASCII armored output forced.

Revocation certificate created.

Please move it to a medium which you can hide away; if Mallory gets

access to this certificate he can use it to make your key unusable.

It is smart to print this certificate and store it away, just in case

your media become unreadable.  But have some caution:  The print system of

your machine might store the data and make it available to others!

==============================================

C:\Documents and Settings\admin>gpg -o C:\public-gpg -a --export 8CC6954D//导出密钥公钥

C:\Documents and Settings\admin>gpg -o c:\secret-key -a --export-secret-keys 8CC6954D//导出密钥私钥

C:\Documents and Settings\admin>gpg --list-sigs//列出密钥使用 gpg --list-keys

C:/Documents and Settings/admin/Application Data/gnupg/pubring.gpg

-------------------------------------------------------------------

pub   2048R/8CC6954D 2011-08-02

uid                  raolin (use for GPG Encrypt)

sig 3        8CC6954D 2011-08-02  raolin (use for GPG Encrypt)

>

sub   2048R/D55E7B91 2011-08-02

sig          8CC6954D 2011-08-02  raolin (use for GPG Encrypt)

>

//列出密钥和签字使用 gpg --list-keys

//列出并检查密钥签字 gpg --check-sigs

C:\Documents and Settings\admin>gpg --check-sigs

C:/Documents and Settings/admin/Application Data/gnupg/pubring.gpg

-------------------------------------------------------------------

pub   2048R/8CC6954D 2011-08-02

uid                  raolin (use for GPG Encrypt)

sig!3        8CC6954D 2011-08-02  raolin (use for GPG Encrypt)

>

sub   2048R/D55E7B91 2011-08-02

sig!         8CC6954D 2011-08-02  raolin (use for GPG Encrypt)

>

C:\Documents and Settings\admin>gpg -ear  myname c:/123.txt   //myname 为生成密钥时输入的用户    c:/123.txt  要对那个文件进行加密   加密码后生成的文件为c:/123.txt.asc

-e GPG加密

-a 加成ASCII

-r 指定用户加密码

C:\Documents and Settings\admin>gpg -d c:/123.txt.asc >c:/1233.txt //解密

使用JAVA 调用些命令:

package windows;

import java.io.BufferedReader;

import java.io.IOException;

import java.io.InputStreamReader;

public class CallCmdInJava {

/**

*

* @param command

* @return

* @throws IOException

*/

public static String callCmd(String command) throws IOException{

try{

Runtime r = Runtime.getRuntime();

Process p = r.exec(command);

BufferedReader br = new BufferedReader(new InputStreamReader(p

.getInputStream()));

p.getOutputStream().flush();

p.getOutputStream().close();

String message="";

StringBuffer result = new StringBuffer();

while((message = br.readLine())!= null){

result.append(message).append("\n");

}

return result.toString();

} catch (IOException e) {

return e.getMessage();

}

}

public static void main(String[] args) {

try {

System.out.println(callCmd("c:\\text.bat"));

} catch (IOException e) {

// TODO Auto-generated catch block

e.printStackTrace();

}

}

}

text.bat:

set path=%path%;C:\Program Files\GNU\GnuPG\pub #这是GPG的安装目录,然后输入要执行的cmd 命令 如下

# gpg --list-sigs

#gpg --check-sigs

gpg -ear myname c:/123.txt #加密123.txt 文件--自动生成123.txt.asc文件

#gpg -d c:/123.txt.asc >c:/1233.txt

下载次数: 65

下载次数: 43

分享到:

18e900b8666ce6f233d25ec02f95ee59.png

72dd548719f0ace4d5f9bca64e1d7715.png

2011-08-05 10:02

浏览 5120

评论

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值