安装Gpg4win
1、生成密钥对
C:\WINDOWS\system32>gpg --gen-key
gpg (GnuPG) 2.0.30; Copyright (C) 2015 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 #选择密钥类型(这里我们选择加密算法是RSA、数字签名算法也是RSA)
RSA keys may be between 1024 and 4096 bits long.
What keysize do you want? (2048) 2048 #设置密钥的比特数
Requested keysize is 2048 bits
Please specify how long the key should be valid.
0 = key does not expire
<n> = key expires in n days
<n>w = key expires in n weeks
<n>m = key expires in n months
<n>y = key expires in n years
Key is valid for? (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: Ron #输入姓名
Name must be at least 5 characters long #姓名至少为5个字符
Real name: ron.zheng #输入姓名
Email address: ron.zheng@tfschange.com #输入邮箱地址
Comment: tfs #输入备注
You selected this USER-ID:
"ron.zheng (tfs) <ron.zheng@tfschange.com>"
Change (N)ame, (C)omment, (E)mail or (O)kay/(Q)uit? o #选择OK
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: key 0452FE75 marked as ultimately trusted
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: 2 signed: 0 trust: 0-, 0q, 0n, 0m, 0f, 2u
pub 2048R/0452FE75 2019-03-28
Key fingerprint = 65B4 846F 7E63 A32B 34E3 A9FB C99D B8B9 0452 FE75
uid [ultimate] ron.zheng (tfs) <ron.zheng@tfschange.com>
sub 2048R/488F27D2 2019-03-28
2、查看刚才生成的公钥
C:\WINDOWS\system32>gpg --export --armor 488F27D2
-----BEGIN PGP PUBLIC KEY BLOCK-----
Version: GnuPG v2
mQENBFyceJkBCAC9m2UeEevNgTjUG+N+6AMkBiM5B75PpcT6WJoJUuxdzXIKYDuw
zuYfVUJ59maSyAZ3rD2hHlgCjV4EycjOrrr27Ke726ZccsKGg1FG1G1PEuHL6aDa
ONRdcLbFdUpv9EwQZrfPGQnATw4p83gxZ8HwXemH+wybenSbuXQQuWY5X7lmJ0sy
8Kzmz5SGL1VLByYCyJAuLGCs8rFitW/w/JpQTQZR4BoezOecAF0DZR24SaWrkBQu
vpCh8nHaVSRE7R0xi+sLKohDfgr3MLjDAopEMmUaImUaeRwyVThwvT2tq9/Qn4GI
2b3R+Tw3afs9eyWkhK+oWSfMv/0zuBWD0axRABEBAAG0KXJvbi56aGVuZyAodGZz
KSA8cm9uLnpoZW5nQHRmc2NoYW5nZS5jb20+iQE5BBMBCAAjBQJcnHiZAhsDBwsJ
CAcDAgEGFQgCCQoLBBYCAwECHgECF4AACgkQyZ24uQRS/nWFywgAmLvoaYfAy5zV
ZMM4zpbw3kf7HpIemlDUPVzJKTOM9cu62/O3eLb592jQCVcs9U0wLlkrkRGe4geC
n72M5hR/psWoLfKOAXDrT9C+a0vW991tYtPbUfnNdDfoQPn/T0Z/jsB26WkZ5X76
8jwwVB6Q8EFD2nFv2HzC4UZl/4qylp+dzEskRtp1lL54LECEyIQDWZCCO+iddile
JpxScQz/GEXzB46o1dDCraqL1Neyyp8MHmhTaxCspKZ9eGSWjXTN+8kmTdqN+6R3
ssAecvWEwqXrGBCTWg9NvmfcYWZBImzDplnuOqkP7ZeTUhSihM+uov5ALyokrLTT
QQFQ0Hs4fbkBDQRcnHiZAQgA4XpvgCB4+ElQo5hRPZcklgZwzdLElmfjDE4F5ist
AH+ndxoTWfagvThPgZh855k4/bdXBPohkuECwGESr6RrfajMozLvfeHyYSxtQmhD
fACuYuYjfAfL7OoDTmFYiNBwWNx8fzE3i2EjUJWuJpz10zb3O8ly2X+gFe5Rv4Yj
CS0nY38aGaiKypJ54nrP3pkIusFux4rMEYzVRpDx5jWFWtPWHnKZtw8LYPQOJjES
l0Z1j0ANwJjnxFf123HvgiaAZK3H1BWUcXlnDRR2w84ZUMGJTuguYd5BjqAIrfv/
8xkZwJgqOs21Idt+KBYkksDjVZ/xDg6ycv1ZUcJgZltQfQARAQABiQEfBBgBCAAJ
BQJcnHiZAhsMAAoJEMmduLkEUv51YsgH/3nfVb4Yq+hWEsLPb2v8JoMJZcyx/mS+
Yqba4SZD/9Eg6SM+bArBfG4p4jee92EJVKIzHOp6n0YPMcF8WWrIXoy+SHReHT7V
KyS6zpxUtQk3eQC4Yutmk5NNI87gzrK16uBwG110jkHh3IlglEcLxui2bASbsL6s
waePqRHmq69rUKh/za/toVbKWAnNK0hfEizFIvMdHPVx4/Hjo02EM+CBbzKD/LZg
RuAtKiEWnFH3ljnmXHxIxnV+IzUzKGkaPUrmOoDMLEtnw/vPpMbLov3bEyHROci0
FZhUNi+4EADxXbk+FwHTSLMHgpNPhUQsgLrnWFulLbCWGm98L0WqVXU=
=GFn+
-----END PGP PUBLIC KEY BLOCK-----
3、列出密钥
C:\WINDOWS\system32>gpg --list-keys
#显示公钥文件名
C:/Users/user/AppData/Roaming/gnupg/pubring.gpg
-----------------------------------------------
#显示公钥特征
pub 2048R/0452FE75 2019-03-28
#显示用户ID
uid [ultimate] ron.zheng (tfs) <ron.zheng@tfschange.com>
#显示私钥特征
sub 2048R/488F27D2 2019-03-28
4、将公钥导入到文件
公钥文件(.gnupg/pubring.gpg)以二进制形式储存,armor 参数可以将其转换为 ASCII 码显示。
gpg --armor --output "输出文件名" --export "用户ID"
gpg --armor --output C:/data/cert/test-public-key.asc --export ron.zheng
打开查看公钥内容如下:
-----BEGIN PGP PUBLIC KEY BLOCK-----
Version: GnuPG v2
mQENBFx8sxMBCADZfybJzhFVAkd59wo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私钥
export-secret-keys 参数可以导出私钥。
gpg --armor --output C:/data/cert/tfs-private-key.asc --export-secret-keys
打开可看到私钥信息如下:
-----BEGIN PGP PRIVATE KEY BLOCK-----
Version: GnuPG v2
lQO+BFx8sxMBCADZfybJzhFVAkd59woEwoEM4Dg/vdqYAQIGLnrpAONTmTWYB2ni
M+iAcGAMf7KqearffKu1cS6U8EYlG3IHHMgMm7bt1DU4Gb4ZaxScHOXkDm1b86fR
senDmYA1OlKoSVckZ63tap2Q9pj4waWWIfXt5jt+uE+fxbPkzydw0OhtjjGlAFe5
3v5CLhWTLZW2Jxeeuwhcxxt8txxxOh/U/i8E1VSM8p97uzb4cJgFqHa96zD7KWTh
XOsWvObbj0tgRqpE/RJkeZVtW3qqS39g4y3KRUxma2025ZR8d53Tw9ebcMxjjT+h
GxNEotuCeIhbQOxVNw5qmUkTbnfVjr6ZngUlABEBAAH+AwMCtWtkRUfUh5/Cs84J
/Jo0zGAKhYeNCvwJDLB2n36GVo1Filox6KTxagOVbPknc7gef2Aa03YpMsjFlwPP
SmlTAaBnHP+YXj1ka0Ai/Pbm1lfcvoaYkK46ToJfF83nhrVza/aD5TnlQDfdqze1
JWJeMBDXMT1hazPo3N4eOD4Hu9EAMgvR2Mcjy0LzDHT7mxMPqbF6GQglYgYqkBJn
LmGlM+RAm9x2hxPSS1JF1DOUbVMbXcDfUCcZjeCPvfONpiJK/ysKUzP8rATH+Wf5
Ql8y08yic6T+fAD5AFYmZaA7vHNkovipC7/YZ1xUc3dWhaFv/stzA4IbqoHwDlC7
xdZOOm2WDRot6DpNJT3s5IGbKnvaOrdipgWNqIWDB3s10ozMCSscfndWlccrvvt0
IGGD78kiN3TzQkcNPMhBzYs0X2mnbEmc7r5NvZGgim/3d8GLfyeTrxbab11aHomV
JSmYiLNwwfF77WBaz/eeaR8IrEShDEDsP0pG5kNk9ibuCpctO3EcCFMigVOUX1Z1
cHvwxSVRXU9WCykveKuxEKXcmws3DIwhbWerJMZmv4p4O7wTw/oOY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WSZl/ARmB393Wew9iT5IKTHAJt79msq5hkGh+
Te6iA1Fu7a2KSE3FbKH15v5Rn4vHQaYRewlJQJKS6Aw9zMUL9ekog2ve2M1zvXUk
QijGz1k706ij1YaclfSXFrCbQVbNB83xF2A8eISXXY67q+MpZUaawh7Co7eiQUZb
xljWK+PU69U9PeC+GINu8QHYPGHuHn2yTR3eQubMk32kUqgmOCSKXi5KdPAqiQEf
BBgBCAAJBQJcnHiZAhsMAAoJEMmduLkEUv51YsgH/3nfVb4Yq+hWEsLPb2v8JoMJ
Zcyx/mS+Yqba4SZD/9Eg6SM+bArBfG4p4jee92EJVKIzHOp6n0YPMcF8WWrIXoy+
SHReHT7VKyS6zpxUtQk3eQC4Yutmk5NNI87gzrK16uBwG110jkHh3IlglEcLxui2
bASbsL6swaePqRHmq69rUKh/za/toVbKWAnNK0hfEizFIvMdHPVx4/Hjo02EM+CB
bzKD/LZgRuAtKiEWnFH3ljnmXHxIxnV+IzUzKGkaPUrmOoDMLEtnw/vPpMbLov3b
EyHROci0FZhUNi+4EADxXbk+FwHTSLMHgpNPhUQsgLrnWFulLbCWGm98L0WqVXU=
=Jwdu
-----END PGP PRIVATE KEY BLOCK-----
6、删除密钥,在命令行输入如下命令
必须先删除私钥,然后才能删除公钥。
在命令行输入如下命令:
gpg --delete-secret-keys 标识名
gpg --delete-keys 标识名
7、PGP加解密帮助类(Java)
/**
* <p>Title: PGPUtils</p>
* <p>Description: PGP加解密帮助类</p>
* @author Ron
* @date 2019年3月4日
*/
public class PGPUtils {
/**
* 添加提供者
*/
static{
Security.addProvider(new CryptixCrypto());
Security.addProvider(new CryptixOpenPGP() );
}
/**
* 构建 LiteralMessage 对象
* @param message
* @return
* @throws MessageException
*/
private static LiteralMessage buildLiteralMessage(byte[] message) throws MessageException{
LiteralMessageBuilder lmb = null;
try {
lmb = LiteralMessageBuilder.getInstance("OpenPGP");
} catch (NoSuchAlgorithmException e) {
e.printStackTrace();
}
lmb.init(message);
LiteralMessage literal = (LiteralMessage)lmb.build();
return literal;
}
/**
* 使用多个公钥对明文加密
* @param plain 明文
* @param recipientKeys 公钥集合
* @return 加密后的明文
* @throws MessageException
*/
public static byte[] encrypt(byte[] plain,List<KeyBundle> recipientKeys) throws MessageException{
LiteralMessage literal = buildLiteralMessage(plain);
EncryptedMessageBuilder emb = null;
try {
emb = EncryptedMessageBuilder.getInstance("OpenPGP");
} catch (NoSuchAlgorithmException e) {
e.printStackTrace();
}
emb.init(literal);
//添加接受者
for(KeyBundle key : recipientKeys){
emb.addRecipient(key);
}
//压缩
emb.setAttribute("compressed", "true");
//得到加密信息
Message msg = emb.build();
PGPArmouredMessage pgpMsg = new PGPArmouredMessage(msg);
return pgpMsg.getEncoded();
}
/**
* 使用单张公钥加密
* @param plain 明文
* @param publicKey 公钥
* @return 返回加密后的密文
* @throws MessageException
*/
public static byte[] encrypt(byte[] plain,KeyBundle publicKey) throws MessageException{
List<KeyBundle> list = new ArrayList<KeyBundle>();
list.add(publicKey);
return encrypt(plain, list);
}
/**
* 使用单张公钥加密
* @param plain 明文
* @param publicKey 公钥路径
* @return 返回加密后的密文
* @throws MessageException
*/
public static byte[] encrypt(byte[] plain,String publicKeyPath) throws MessageException, IOException{
InputStream priInputStream = new FileInputStream(new File(publicKeyPath));
KeyBundle publicKeyBundle = PGPUtils.streamToKeyBundle(priInputStream);
return PGPUtils.encrypt(plain, publicKeyBundle);
}
/**
* 使用私钥和密码对明文签名
* @param plain 明文
* @param privateKey 私钥
* @param keypass 私钥密码
* @return 签名后的明文
* @throws MessageException
* @throws UnrecoverableKeyException
*/
public static byte[] sign(byte[] plain,KeyBundle privateKey,String keypass)throws MessageException,UnrecoverableKeyException{
SignedMessageBuilder smb = null;
try {
smb = SignedMessageBuilder.getInstance("OpenPGP");
} catch (NoSuchAlgorithmException e) {
e.printStackTrace();
}
LiteralMessage literal = buildLiteralMessage(plain);
smb.init(literal);
smb.addSigner(privateKey, keypass.toCharArray());
Message msg = smb.build();
PGPArmouredMessage armoured = new PGPArmouredMessage(msg);
return armoured.getEncoded();
}
/**
* 使用私钥和密码对明文签名
* @param plain 明文
* @param privateKey 私钥路径
* @param keypass 私钥密码
* @return 签名后的明文
* @throws MessageException
* @throws UnrecoverableKeyException
*/
public static byte[] sign(byte[] plain,String privateKeyPath,String keypass)throws MessageException,UnrecoverableKeyException, IOException{
InputStream priInputStream = new FileInputStream(new File(privateKeyPath));
KeyBundle privateKeyBundle = PGPUtils.streamToKeyBundle(priInputStream);
return PGPUtils.sign(plain, privateKeyBundle, keypass);
}
/**
* 使用私钥和密码解密加密后的数据
* @param encrypted PGP加密过的数据
* @param privateKey 私钥
* @param keypass 私钥密码
* @return 解密后的明文
* @throws MessageException
* @throws IOException
* @throws UnrecoverableKeyException
* @throws NotEncryptedToParameterException
*/
public static byte[] decrypt(byte[] encrypted,KeyBundle privateKey,String keypass) throws MessageException, IOException, UnrecoverableKeyException, NotEncryptedToParameterException{
MessageFactory mf = null;
try {
mf = MessageFactory.getInstance("OpenPGP");
} catch (NoSuchAlgorithmException e) {
e.printStackTrace();
}
Collection msgs = mf.generateMessages(new ByteArrayInputStream(encrypted));
//得到集合中的EncryptedMessage对象
Message message = (Message)msgs.iterator().next();
if (!(message instanceof EncryptedMessage)) {
throw new MessageException("Not a encrypted message.");
}
EncryptedMessage em = (EncryptedMessage)message;
Message msg = em.decrypt(privateKey,keypass.toCharArray());
return ((LiteralMessage)msg).getBinaryData();
}
/**
* 使用私钥和密码解密加密后的数据
* @param encrypted PGP加密过的数据
* @param privateKey 私钥路径
* @param keypass 私钥密码
* @return 解密后的明文
* @throws MessageException
* @throws IOException
* @throws UnrecoverableKeyException
* @throws NotEncryptedToParameterException
*/
public static byte[] decrypt(byte[] encrypted,String privateKeyPath,String keypass) throws MessageException, IOException, UnrecoverableKeyException, NotEncryptedToParameterException{
InputStream priInputStream = new FileInputStream(new File(privateKeyPath));
KeyBundle privateKeyBundle = PGPUtils.streamToKeyBundle(priInputStream);
return PGPUtils.decrypt(encrypted,privateKeyBundle,keypass);
}
/**
* 解密验签
* @param encrypted 密文
* @param privateKey 私钥
* @param keypass 私钥密码
* @param publicKey 公钥
* @return 返回明文
* @throws UnrecoverableKeyException
* @throws MessageException
* @throws IOException
* @throws NotEncryptedToParameterException
*/
public static byte[] decryptVerify(byte[] encrypted,KeyBundle privateKey,String keypass,KeyBundle publicKey) throws UnrecoverableKeyException, MessageException, IOException, NotEncryptedToParameterException{
return PGPUtils.verify(PGPUtils.decrypt(encrypted, privateKey, keypass), publicKey);
}
/**
* 解密验签
* @param encrypted 密文
* @param privateKey 私钥路径
* @param keypass 私钥密码
* @param publicKey 公钥路径
* @return 返回明文
* @throws UnrecoverableKeyException
* @throws MessageException
* @throws IOException
* @throws NotEncryptedToParameterException
*/
public static byte[] decryptVerify(byte[] encrypted,String privateKeyPath,String keypass,String publicKeyPath) throws UnrecoverableKeyException, MessageException, IOException, NotEncryptedToParameterException{
InputStream priInputStream = new FileInputStream(new File(privateKeyPath));
KeyBundle privateKeyBundle = PGPUtils.streamToKeyBundle(priInputStream);
InputStream pubInputStream = new FileInputStream(new File(publicKeyPath));
KeyBundle pubKeyBundle = PGPUtils.streamToKeyBundle(pubInputStream);
return PGPUtils.decryptVerify(encrypted,privateKeyBundle,keypass,pubKeyBundle);
}
/**
* 验证Message
* @param signed 验证的内容
* @param publickey 公钥
* @return 返回验证后的内容
* @throws MessageException
* @throws IOException
*/
public static byte[] verify(byte[] signed,KeyBundle publickey) throws MessageException, IOException{
MessageFactory mf = null;
try {
mf = MessageFactory.getInstance("OpenPGP");
} catch (NoSuchAlgorithmException e) {
e.printStackTrace();
}
Message msg = (Message)mf.generateMessages(new ByteArrayInputStream(signed)).iterator().next();
if (!(msg instanceof SignedMessage)) {
throw new MessageException(" Not a signed message.");
}
SignedMessage sm = (SignedMessage)msg;
if (sm.verify(publickey)) {
} else {
throw new MessageException(" Signature verify fail. ");
}
if (!(sm.getContents() instanceof LiteralMessage)){
throw new MessageException(" Not a signed message.");
}
LiteralMessage lm = (LiteralMessage)sm.getContents();
return lm.getBinaryData();
}
/**
* 流转换为PGP KeuBundle 对象
* @param inputStream Key
* @return 转换后的 KeuBundle
* @throws MessageException
* @throws IOException
*/
public static KeyBundle streamToKeyBundle(InputStream inputStream) throws MessageException, IOException {
MessageFactory messageFactory = null;
try {
messageFactory = MessageFactory.getInstance("OpenPGP");
} catch (NoSuchAlgorithmException e) {
e.printStackTrace();
}
Collection msgs = messageFactory.generateMessages(inputStream);
KeyBundleMessage keybm = (KeyBundleMessage)msgs.iterator().next();
return keybm.getKeyBundle();
}
/**
* 签名加密
* @param plain 明文
* @param privateKey 私钥
* @param keypass 私钥密码
* @param recipientKeys 公钥
* @return 返回签名加密后的数据
* @throws UnrecoverableKeyException
* @throws MessageException
*/
public static byte [] signAndEncrypt(byte[] plain,KeyBundle privateKey,String keypass,List<KeyBundle> recipientKeys) throws UnrecoverableKeyException, MessageException{
return PGPUtils.encrypt(PGPUtils.sign(plain, privateKey, keypass),recipientKeys);
}
/**
* 签名加密
* @param plain 明文
* @param privateKey 私钥
* @param keypass 私钥密码
* @param recipientKeys 公钥
* @return 返回签名加密后的数据
* @throws UnrecoverableKeyException
* @throws MessageException
*/
public static byte [] signAndEncrypt(byte[] plain,KeyBundle privateKey,String keypass,KeyBundle publicKey) throws UnrecoverableKeyException, MessageException{
return PGPUtils.encrypt(PGPUtils.sign(plain, privateKey, keypass),publicKey);
}
/**
* @author Ron
* <p>Title: signAndEncrypt</p>
* <p>Description: </p>
* @param plain 明文
* @param privateKeyPath 私钥路径
* @param keypass 私钥密码
* @param publicKeyPath 公钥
* @return 返回签名加密后的数据
* @throws UnrecoverableKeyException
* @throws MessageException
* @throws IOException
*/
public static byte [] signAndEncrypt(byte[] plain,String privateKeyPath,String keypass,String publicKeyPath) throws UnrecoverableKeyException, MessageException, IOException{
InputStream priInputStream = new FileInputStream(new File(privateKeyPath));
KeyBundle privateKeyBundle = PGPUtils.streamToKeyBundle(priInputStream);
InputStream pubInputStream = new FileInputStream(new File(publicKeyPath));
KeyBundle pubKeyBundle = PGPUtils.streamToKeyBundle(pubInputStream);
return PGPUtils.signAndEncrypt(plain,privateKeyBundle,keypass,pubKeyBundle);
}
public static void main(String[] args) throws MessageException, IOException, UnrecoverableKeyException, NotEncryptedToParameterException {
String data = "{\"platId\": \"***\",\"authorizeCode\": \"EPORT0000102\",\"msgType\": \"VLD001\",\"format\": \"json\",\"signature\": \"signature\",\"userName\": \"test\",\"password\": \"***\",\"data\": {\"entryId\": \"E20170000003131789\",\"ownerCode\": \"**********\",\"ownerName\": \"广东鱼珠木材电子商务有限公司\",\"cnsnTradeCode\": \"********\",\"consignorCname\": \"广东广物木材产业股份有限公司\",\"consignorEname\": \"En*******************\",\"agentCode\": \"**********\",\"agentName\": \"广东省电子口岸管理有限公司\",\"ciqDespCtryCode\": \"NZL\",\"ciqDespCtryCodeName\": \"新西兰\",\"despPortCode\": \"991201\",\"despPortCodeName\": \"天津出口加工区\",\"cusTradeNationCode\": \"AFG\",\"cusTradeNationCodeName\": \"阿富汗\",\"districtCode\": \"11013\",\"districtCodeName\": \"中关村国家自主创新示范区(东城园)\",\"packNo\": \"100\",\"grossWt\": \"100\",\"netWt\": \"100\",\"supvModeCdde\": \"1039\",\"supvModeCddeName\": \"市场采购\",\"decMergeListVo\": [{\"gNo\": \"1\",\"contrItem\": \"1\",\"codeTs\": \"1001110001\",\"gName\": \"种用硬粒小麦\",\"gModel\": \"0|2|冰袋|100%涤纶|无牌 款号:AC04S1238\", \"declTotal\": \"60\",\"tradeCurr\": \"美元\",\"gQty\":\"10\"},{\"gNo\": \"1\",\"contrItem\": \"1\",\"codeTs\": \"1001110001\",\"gName\": \"种用硬粒小麦\",\"gModel\": \"0|2|冰袋|100%涤纶|无牌 款号:AC04S1238\", \"declTotal\": \"60\",\"tradeCurr\": \"美元\",\"gQty\":\"10\"}]}}";
String privateKey = "D:/Work/WorkSpace/trunk_remote_dev/tfs-china-eport-api/src/main/resources/cert/test-private-key.txt";
String keypass = "tfs12345678";
String publicKey = "D:/Work/WorkSpace/trunk_remote_dev/tfs-china-eport-api/src/main/resources/cert/test-public-key.txt";
byte[] dataEn = PGPUtils.signAndEncrypt(data.getBytes(),privateKey,keypass,publicKey);
String dateByEn = new String(dataEn);
System.out.println(dateByEn);
byte[] dataDe = PGPUtils.decryptVerify(dataEn, privateKey,keypass,publicKey);
String dateByDe = new String(dataDe);
System.out.println(dateByDe);
System.out.println("--------------------------------------分割线--------------------------------------");
dataEn = PGPUtils.encrypt(data.getBytes(), publicKey);
dateByEn = new String(dataEn);
System.out.println(dateByEn);
dataDe = PGPUtils.decrypt(dataEn, privateKey, keypass);
dateByDe = new String(dataDe);
System.out.println(dateByDe);
}
}
测试:
public class PGPTest {
@Test
public void testPGP() throws MessageException, IOException, UnrecoverableKeyException, NotEncryptedToParameterException{
String orgData = "Ron:广州天孚世佳信息科技有限公司";
byte[] data = PGPUtils.encrypt(orgData.getBytes(), "公钥文件地址");
String jsonData = new String(Base64.encodeBase64(data));
//解密
byte[] baseBytes = Base64.decodeBase64(jsonData.getBytes());
//解密
byte[] jsonByte = PGPUtils.decrypt(baseBytes,"私钥文件地址","私钥密钥");
String plainData = new String(jsonByte);
System.out.println(plainData);
}
}