RSA的公私钥PKCS#1和PKCS#8的格式转换以及在php,python,java中的使用

生成以及转换

#!/bin/sh
# 生成PKCS#1的公私钥
openssl genrsa -out pkcs1_private.pem 1024
openssl rsa -in pkcs1_private.pem -RSAPublicKey_out -out pkcs1_public.pem

# 由PKCS#1的私钥,生成PKCS#8的公私钥
openssl pkcs8 -topk8 -inform PEM -in pkcs1_private.pem -outform PEM -nocrypt -out from_pkcs1_private_to_pkcs8_private.pem
openssl rsa -in pkcs1_private.pem -pubout -out from_pkcs1_private_to_pkcs8_public.pem

# 由PKCS#8的私钥,生成PKCS#1的公私钥
openssl rsa -in from_pkcs1_private_to_pkcs8_private.pem -out from_pkcs8_private_to_pkcs1_private.pem
openssl rsa -in from_pkcs1_private_to_pkcs8_private.pem -RSAPublicKey_out -out from_pkcs8_private_to_pkcs1_public.pem

# 由PKCS1公钥生成PKCS#8公钥:
openssl rsa -RSAPublicKey_in -in pkcs1_public.pem -pubout -out from_pkcs1_public_to_pkcs8_public.pem

# 由PKCS8公钥生成PKCS#1公钥:
openssl rsa -pubin -in from_pkcs1_private_to_pkcs8_public.pem -RSAPublicKey_out -out from_pkcs8_public_to_pkcs1_public.pem

可以看到当拥有任意一种私钥时,就可以生成所有需要的东西。当拥有一种公钥时,只能生成另一种公钥。

使用方法

虽然使用法则是:公钥加密,私钥解密。私钥签名,公钥验签。但是还是存在某些情况需要使用私钥来进行加密,即需要四个函数:

  • 公钥加密
  • 私钥解密
  • 私钥加密
  • 公钥解密

以下给出在python,php,Java中的函数实现,以及支持公私钥的格式:

python

使用M2Crypto库

私钥:PKCS#8 PKCS#1 公钥:PKCS#8

ubuntu下安装方式:

#第一种方式:使用apt-get(以Python2版本进行测试的)
sudo apt-get install python-m2crypto, M2Crypto将安装到系统Python库中。

#第二种方式:从源码进行安装((兼容Python2,3))

sudo apt-get install build-essential python3-dev python-dev libssl-dev swig
pip install https://gitlab.com/m2crypto/m2crypto/repository/python3/archive.tar.gz

#第三种方式:pip安装(不兼容Python3)
pip install M2Crypto

mac下安装方式:

brew install swig

sudo env LDFLAGS="-L$(brew --prefix openssl)/lib" CFLAGS="-I$(brew --prefix openssl)/include"  SWIG_FEATURES="-cpperraswarn -includeall -I$(brew --prefix openssl)/include" pip install pyOpenSSL==17.1.0
 3607  sudo env LDFLAGS="-L$(brew --prefix openssl)/lib" CFLAGS="-I$(brew --prefix openssl)/include"  SWIG_FEATURES="-cpperraswarn -includeall -I$(brew --prefix openssl)/include" pip install M2Crypto
# -*- coding: UTF-8 -*-
import M2Crypto

pkcs1_public='''-----BEGIN RSA PUBLIC KEY-----
MIGJAoGBAK3m6BabZZ2qQwjmIOBOZ1q9g9OnqGapuinLs3182ew2LAQT62iLReBC
NB64TRh/tU4iIIjx5bNRpNZ8IrcP92YVNuxMrdSCqXpC5gpGFKf1CfG0SrO+TPmO
/d1zexJq/yArc7HbYMFZRfks7BjnaQGJ5rCVEVyS/y+0I5hU+t37AgMBAAE=
-----END RSA PUBLIC KEY-----'''

pkcs1_private='''-----BEGIN RSA PRIVATE KEY-----
MIICWwIBAAKBgQCt5ugWm2WdqkMI5iDgTmdavYPTp6hmqbopy7N9fNnsNiwEE+to
i0XgQjQeuE0Yf7VOIiCI8eWzUaTWfCK3D/dmFTbsTK3Ugql6QuYKRhSn9QnxtEqz
vkz5jv3dc3sSav8gK3Ox22DBWUX5LOwY52kBieawlRFckv8vtCOYVPrd+wIDAQAB
AoGAR7qukGSYjWflLo59kQfF6c+xyGOnOnFXsFWtO118JcpSbXwp5X1M3StxhBpQ
8oH6rre048ejD0vlyfJ5/zg+utZ0V2x5xM4DxBTyZifdujZGac9dzsfZ/CO6NS1s
HblrMTnm5EiousPH1lywmhGce7LVfMR76mVAFZTpHScHLAECQQDY+sL4hs8v578j
nhURLS5bwvih4wVbajPsN7oiswEsjCYymDO8IywL4l9Pkvfxd4d3dh6BhsYfB2W8
2ItBaWdBAkEAzSz0D2zJGC/x5DN5At1NnmxSpMfqo6e3L9vMHe7T5mepsRwPIobQ
DQrdBdCeBd30qVi1lw7NmMPKEflIxo2SOwJAbotLO+0Kr4BlPBM07nxTSwLJQ0jz
GMDB1U4K8dS6+2Qnrc0nRmmw7hkVr+fTlFzuUmmGXz03wOU26wBz6g52QQJASUVA
czG6LrUQgRoQoQE+8tBkQwxRstf2B1VK83WSnrluVB1dGktiiQXUIHt7s0SsVr2j
O6rBqUhiJUEMyDtOeQJAb5DnL8q5FbPkZg82LilKBtMyI5iGYnXGkz9adp4Kgpf0
V5dXr1QJl3rdurgz5BbZjhrxU54GTFnm/izIoaAU2g==
-----END RSA PRIVATE KEY-----'''

pkcs8_public='''-----BEGIN PUBLIC KEY-----
MIGfMA0GCSqGSIb3DQEBAQUAA4GNADCBiQKBgQCt5ugWm2WdqkMI5iDgTmdavYPT
p6hmqbopy7N9fNnsNiwEE+toi0XgQjQeuE0Yf7VOIiCI8eWzUaTWfCK3D/dmFTbs
TK3Ugql6QuYKRhSn9QnxtEqzvkz5jv3dc3sSav8gK3Ox22DBWUX5LOwY52kBieaw
lRFckv8vtCOYVPrd+wIDAQAB
-----END PUBLIC KEY-----'''

pkcs8_private='''-----BEGIN PRIVATE KEY-----
MIICdQIBADANBgkqhkiG9w0BAQEFAASCAl8wggJbAgEAAoGBAK3m6BabZZ2qQwjm
IOBOZ1q9g9OnqGapuinLs3182ew2LAQT62iLReBCNB64TRh/tU4iIIjx5bNRpNZ8
IrcP92YVNuxMrdSCqXpC5gpGFKf1CfG0SrO+TPmO/d1zexJq/yArc7HbYMFZRfks
7BjnaQGJ5rCVEVyS/y+0I5hU+t37AgMBAAECgYBHuq6QZJiNZ+Uujn2RB8Xpz7HI
Y6c6cVewVa07XXwlylJtfCnlfUzdK3GEGlDygfqut7Tjx6MPS+XJ8nn/OD661nRX
bHnEzgPEFPJmJ926NkZpz13Ox9n8I7o1LWwduWsxOebkSKi6w8fWXLCaEZx7stV8
xHvqZUAVlOkdJwcsAQJBANj6wviGzy/nvyOeFREtLlvC+KHjBVtqM+w3uiKzASyM
JjKYM7wjLAviX0+S9/F3h3d2HoGGxh8HZbzYi0FpZ0ECQQDNLPQPbMkYL/HkM3kC
3U2ebFKkx+qjp7cv28wd7tPmZ6mxHA8ihtANCt0F0J4F3fSpWLWXDs2Yw8oR+UjG
jZI7AkBui0s77QqvgGU8EzTufFNLAslDSPMYwMHVTgrx1Lr7ZCetzSdGabDuGRWv
59OUXO5SaYZfPTfA5TbrAHPqDnZBAkBJRUBzMboutRCBGhChAT7y0GRDDFGy1/YH
VUrzdZKeuW5UHV0aS2KJBdQge3uzRKxWvaM7qsGpSGIlQQzIO055AkBvkOcvyrkV
s+RmDzYuKUoG0zIjmIZidcaTP1p2ngqCl/RXl1evVAmXet26uDPkFtmOGvFTngZM
Web+LMihoBTa
-----END PRIVATE KEY-----'''


def private_encrypt(data,private_key):
	bio = M2Crypto.BIO.MemoryBuffer(private_key)
	rsa_pri = M2Crypto.RSA.load_key_bio(bio)
	ctxt_pri = rsa_pri.private_encrypt(data, M2Crypto.RSA.pkcs1_padding) 
	ctxt64_pri = ctxt_pri.encode('base64')
	return ctxt64_pri

def public_decrypt(msg,public_key):
	bio = M2Crypto.BIO.MemoryBuffer(public_key)
	rsa_pub = M2Crypto.RSA.load_pub_key_bio(bio)
	ctxt_pri = msg.decode("base64")
	output = rsa_pub.public_decrypt(ctxt_pri, M2Crypto.RSA.pkcs1_padding)
	return output

def public_encrypt(data,public_key):
	bio = M2Crypto.BIO.MemoryBuffer(public_key)
	rsa_pub = M2Crypto.RSA.load_pub_key_bio(bio)
	ctxt_pri = rsa_pub.public_encrypt(data, M2Crypto.RSA.pkcs1_padding) 
	ctxt64_pri = ctxt_pri.encode('base64')
	return ctxt64_pri

def private_decrypt(msg,private_key):
	bio = M2Crypto.BIO.MemoryBuffer(private_key)
	rsa_pri = M2Crypto.RSA.load_key_bio(bio)
	ctxt_pri = msg.decode("base64")
	output = rsa_pri.private_decrypt(ctxt_pri, M2Crypto.RSA.pkcs1_padding)
	return output

print private_decrypt(public_encrypt("PKCS#8公钥加密,PKCS#8私钥解密",pkcs8_public),pkcs8_private)
print private_decrypt(public_encrypt("PKCS#8公钥加密,PKCS#1私钥解密",pkcs8_public),pkcs1_private)
print public_decrypt(private_encrypt("PKCS#8私钥加密,PKCS#8公钥解密",pkcs8_private),pkcs8_public)
print public_decrypt(private_encrypt("PKCS#1私钥加密,PKCS#8公钥解密",pkcs1_private),pkcs8_public)


#print private_decrypt(public_encrypt("PKCS#1公钥加密,PKCS#8私钥解密",pkcs1_public),pkcs8_private)
#print private_decrypt(public_encrypt("PKCS#1公钥加密,PKCS#1私钥解密",pkcs1_public),pkcs1_private)
#print public_decrypt(private_encrypt("PKCS#8私钥加密,PKCS#1公钥解密",pkcs8_private),pkcs1_public)
#print public_decrypt(private_encrypt("PKCS#1私钥加密,PKCS#1公钥解密",pkcs1_private),pkcs1_public)

php

私钥:PKCS#8 PKCS#1 公钥:PKCS#8

<?php

# php version: 7.1.23

$pkcs1_public="-----BEGIN RSA PUBLIC KEY-----
MIGJAoGBAK3m6BabZZ2qQwjmIOBOZ1q9g9OnqGapuinLs3182ew2LAQT62iLReBC
NB64TRh/tU4iIIjx5bNRpNZ8IrcP92YVNuxMrdSCqXpC5gpGFKf1CfG0SrO+TPmO
/d1zexJq/yArc7HbYMFZRfks7BjnaQGJ5rCVEVyS/y+0I5hU+t37AgMBAAE=
-----END RSA PUBLIC KEY-----";

$pkcs1_private="-----BEGIN RSA PRIVATE KEY-----
MIICWwIBAAKBgQCt5ugWm2WdqkMI5iDgTmdavYPTp6hmqbopy7N9fNnsNiwEE+to
i0XgQjQeuE0Yf7VOIiCI8eWzUaTWfCK3D/dmFTbsTK3Ugql6QuYKRhSn9QnxtEqz
vkz5jv3dc3sSav8gK3Ox22DBWUX5LOwY52kBieawlRFckv8vtCOYVPrd+wIDAQAB
AoGAR7qukGSYjWflLo59kQfF6c+xyGOnOnFXsFWtO118JcpSbXwp5X1M3StxhBpQ
8oH6rre048ejD0vlyfJ5/zg+utZ0V2x5xM4DxBTyZifdujZGac9dzsfZ/CO6NS1s
HblrMTnm5EiousPH1lywmhGce7LVfMR76mVAFZTpHScHLAECQQDY+sL4hs8v578j
nhURLS5bwvih4wVbajPsN7oiswEsjCYymDO8IywL4l9Pkvfxd4d3dh6BhsYfB2W8
2ItBaWdBAkEAzSz0D2zJGC/x5DN5At1NnmxSpMfqo6e3L9vMHe7T5mepsRwPIobQ
DQrdBdCeBd30qVi1lw7NmMPKEflIxo2SOwJAbotLO+0Kr4BlPBM07nxTSwLJQ0jz
GMDB1U4K8dS6+2Qnrc0nRmmw7hkVr+fTlFzuUmmGXz03wOU26wBz6g52QQJASUVA
czG6LrUQgRoQoQE+8tBkQwxRstf2B1VK83WSnrluVB1dGktiiQXUIHt7s0SsVr2j
O6rBqUhiJUEMyDtOeQJAb5DnL8q5FbPkZg82LilKBtMyI5iGYnXGkz9adp4Kgpf0
V5dXr1QJl3rdurgz5BbZjhrxU54GTFnm/izIoaAU2g==
-----END RSA PRIVATE KEY-----";

$pkcs8_public="-----BEGIN PUBLIC KEY-----
MIGfMA0GCSqGSIb3DQEBAQUAA4GNADCBiQKBgQCt5ugWm2WdqkMI5iDgTmdavYPT
p6hmqbopy7N9fNnsNiwEE+toi0XgQjQeuE0Yf7VOIiCI8eWzUaTWfCK3D/dmFTbs
TK3Ugql6QuYKRhSn9QnxtEqzvkz5jv3dc3sSav8gK3Ox22DBWUX5LOwY52kBieaw
lRFckv8vtCOYVPrd+wIDAQAB
-----END PUBLIC KEY-----";

$pkcs8_private="-----BEGIN PRIVATE KEY-----
MIICdQIBADANBgkqhkiG9w0BAQEFAASCAl8wggJbAgEAAoGBAK3m6BabZZ2qQwjm
IOBOZ1q9g9OnqGapuinLs3182ew2LAQT62iLReBCNB64TRh/tU4iIIjx5bNRpNZ8
IrcP92YVNuxMrdSCqXpC5gpGFKf1CfG0SrO+TPmO/d1zexJq/yArc7HbYMFZRfks
7BjnaQGJ5rCVEVyS/y+0I5hU+t37AgMBAAECgYBHuq6QZJiNZ+Uujn2RB8Xpz7HI
Y6c6cVewVa07XXwlylJtfCnlfUzdK3GEGlDygfqut7Tjx6MPS+XJ8nn/OD661nRX
bHnEzgPEFPJmJ926NkZpz13Ox9n8I7o1LWwduWsxOebkSKi6w8fWXLCaEZx7stV8
xHvqZUAVlOkdJwcsAQJBANj6wviGzy/nvyOeFREtLlvC+KHjBVtqM+w3uiKzASyM
JjKYM7wjLAviX0+S9/F3h3d2HoGGxh8HZbzYi0FpZ0ECQQDNLPQPbMkYL/HkM3kC
3U2ebFKkx+qjp7cv28wd7tPmZ6mxHA8ihtANCt0F0J4F3fSpWLWXDs2Yw8oR+UjG
jZI7AkBui0s77QqvgGU8EzTufFNLAslDSPMYwMHVTgrx1Lr7ZCetzSdGabDuGRWv
59OUXO5SaYZfPTfA5TbrAHPqDnZBAkBJRUBzMboutRCBGhChAT7y0GRDDFGy1/YH
VUrzdZKeuW5UHV0aS2KJBdQge3uzRKxWvaM7qsGpSGIlQQzIO055AkBvkOcvyrkV
s+RmDzYuKUoG0zIjmIZidcaTP1p2ngqCl/RXl1evVAmXet26uDPkFtmOGvFTngZM
Web+LMihoBTa
-----END PRIVATE KEY-----";

openssl_public_encrypt("PKCS#8公钥加密,PKCS#8私钥解密\n",$a,$pkcs8_public);
openssl_private_decrypt($a, $b,$pkcs8_private);
echo $b;

openssl_public_encrypt("PKCS#8公钥加密,PKCS#1私钥解密\n",$a,$pkcs8_public);
openssl_private_decrypt($a, $b,$pkcs1_private);
echo $b;

openssl_private_encrypt("PKCS#8私钥加密,PKCS#8公钥解密\n", $a,$pkcs8_private);
openssl_public_decrypt($a, $b, $pkcs8_public);
echo $b;

openssl_private_encrypt("PKCS#1私钥加密,PKCS#8公钥解密\n", $a,$pkcs1_private);
openssl_public_decrypt($a, $b, $pkcs8_public);
echo $b;

# 测试PKCS#1公钥无法使用

openssl_public_encrypt("PKCS#1公钥加密,PKCS#8私钥解密\n",$a,$pkcs1_public);
openssl_private_decrypt($a, $c,$pkcs8_private);
echo $c;

openssl_public_encrypt("PKCS#1公钥加密,PKCS#1私钥解密\n",$a,$pkcs1_public);
openssl_private_decrypt($a, $c,$pkcs1_private);
echo $c;

openssl_private_encrypt("PKCS#8私钥加密,PKCS#1公钥解密\n", $a,$pkcs8_private);
openssl_public_decrypt($a, $c, $pkcs1_public);
echo $c;

openssl_private_encrypt("PKCS#1私钥加密,PKCS#1公钥解密\n", $a,$pkcs1_private);
openssl_public_decrypt($a, $c, $pkcs1_public);
echo $c;
?>

java

去掉标记的头 私钥:PKCS#8 公钥:PKCS#8

import org.apache.commons.codec.binary.Base64;
import javax.crypto.Cipher;
import java.security.KeyFactory;
import java.security.interfaces.RSAPrivateKey;
import java.security.interfaces.RSAPublicKey;
import java.security.spec.PKCS8EncodedKeySpec;
import java.security.spec.X509EncodedKeySpec;

public class Main {
    public static String pkcs1_public = "" +
            "MIGJAoGBAK3m6BabZZ2qQwjmIOBOZ1q9g9OnqGapuinLs3182ew2LAQT62iLReBC\n" +
            "NB64TRh/tU4iIIjx5bNRpNZ8IrcP92YVNuxMrdSCqXpC5gpGFKf1CfG0SrO+TPmO\n" +
            "/d1zexJq/yArc7HbYMFZRfks7BjnaQGJ5rCVEVyS/y+0I5hU+t37AgMBAAE=\n";
    public static String pkcs1_private = "" +
            "MIICWwIBAAKBgQCt5ugWm2WdqkMI5iDgTmdavYPTp6hmqbopy7N9fNnsNiwEE+to\n" +
            "i0XgQjQeuE0Yf7VOIiCI8eWzUaTWfCK3D/dmFTbsTK3Ugql6QuYKRhSn9QnxtEqz\n" +
            "vkz5jv3dc3sSav8gK3Ox22DBWUX5LOwY52kBieawlRFckv8vtCOYVPrd+wIDAQAB\n" +
            "AoGAR7qukGSYjWflLo59kQfF6c+xyGOnOnFXsFWtO118JcpSbXwp5X1M3StxhBpQ\n" +
            "8oH6rre048ejD0vlyfJ5/zg+utZ0V2x5xM4DxBTyZifdujZGac9dzsfZ/CO6NS1s\n" +
            "HblrMTnm5EiousPH1lywmhGce7LVfMR76mVAFZTpHScHLAECQQDY+sL4hs8v578j\n" +
            "nhURLS5bwvih4wVbajPsN7oiswEsjCYymDO8IywL4l9Pkvfxd4d3dh6BhsYfB2W8\n" +
            "2ItBaWdBAkEAzSz0D2zJGC/x5DN5At1NnmxSpMfqo6e3L9vMHe7T5mepsRwPIobQ\n" +
            "DQrdBdCeBd30qVi1lw7NmMPKEflIxo2SOwJAbotLO+0Kr4BlPBM07nxTSwLJQ0jz\n" +
            "GMDB1U4K8dS6+2Qnrc0nRmmw7hkVr+fTlFzuUmmGXz03wOU26wBz6g52QQJASUVA\n" +
            "czG6LrUQgRoQoQE+8tBkQwxRstf2B1VK83WSnrluVB1dGktiiQXUIHt7s0SsVr2j\n" +
            "O6rBqUhiJUEMyDtOeQJAb5DnL8q5FbPkZg82LilKBtMyI5iGYnXGkz9adp4Kgpf0\n" +
            "V5dXr1QJl3rdurgz5BbZjhrxU54GTFnm/izIoaAU2g==\n";
    public static String pkcs8_public = "" +
            "MIGfMA0GCSqGSIb3DQEBAQUAA4GNADCBiQKBgQCt5ugWm2WdqkMI5iDgTmdavYPT\n" +
            "p6hmqbopy7N9fNnsNiwEE+toi0XgQjQeuE0Yf7VOIiCI8eWzUaTWfCK3D/dmFTbs\n" +
            "TK3Ugql6QuYKRhSn9QnxtEqzvkz5jv3dc3sSav8gK3Ox22DBWUX5LOwY52kBieaw\n" +
            "lRFckv8vtCOYVPrd+wIDAQAB\n";
    public static String pkcs8_private = "" +
            "MIICdQIBADANBgkqhkiG9w0BAQEFAASCAl8wggJbAgEAAoGBAK3m6BabZZ2qQwjm\n" +
            "IOBOZ1q9g9OnqGapuinLs3182ew2LAQT62iLReBCNB64TRh/tU4iIIjx5bNRpNZ8\n" +
            "IrcP92YVNuxMrdSCqXpC5gpGFKf1CfG0SrO+TPmO/d1zexJq/yArc7HbYMFZRfks\n" +
            "7BjnaQGJ5rCVEVyS/y+0I5hU+t37AgMBAAECgYBHuq6QZJiNZ+Uujn2RB8Xpz7HI\n" +
            "Y6c6cVewVa07XXwlylJtfCnlfUzdK3GEGlDygfqut7Tjx6MPS+XJ8nn/OD661nRX\n" +
            "bHnEzgPEFPJmJ926NkZpz13Ox9n8I7o1LWwduWsxOebkSKi6w8fWXLCaEZx7stV8\n" +
            "xHvqZUAVlOkdJwcsAQJBANj6wviGzy/nvyOeFREtLlvC+KHjBVtqM+w3uiKzASyM\n" +
            "JjKYM7wjLAviX0+S9/F3h3d2HoGGxh8HZbzYi0FpZ0ECQQDNLPQPbMkYL/HkM3kC\n" +
            "3U2ebFKkx+qjp7cv28wd7tPmZ6mxHA8ihtANCt0F0J4F3fSpWLWXDs2Yw8oR+UjG\n" +
            "jZI7AkBui0s77QqvgGU8EzTufFNLAslDSPMYwMHVTgrx1Lr7ZCetzSdGabDuGRWv\n" +
            "59OUXO5SaYZfPTfA5TbrAHPqDnZBAkBJRUBzMboutRCBGhChAT7y0GRDDFGy1/YH\n" +
            "VUrzdZKeuW5UHV0aS2KJBdQge3uzRKxWvaM7qsGpSGIlQQzIO055AkBvkOcvyrkV\n" +
            "s+RmDzYuKUoG0zIjmIZidcaTP1p2ngqCl/RXl1evVAmXet26uDPkFtmOGvFTngZM\n" +
            "Web+LMihoBTa\n";

    public static void main(String[] args) throws Exception {
        System.out.println(private_decrypt(public_encrypt("PKCS#8公钥加密,PKCS#8私钥解密", pkcs8_public), pkcs8_private));
        System.out.println(public_decrypt(private_encrypt("PKCS#8私钥加密,PKCS#8公钥解密", pkcs8_private), pkcs8_public));
        //System.out.println(private_decrypt(public_encrypt("PKCS#8公钥加密,PKCS#1私钥解密",pkcs8_public),pkcs1_private));
        //System.out.println(private_decrypt(public_encrypt("PKCS#1公钥加密,PKCS#8私钥解密",pkcs1_public),pkcs8_private));
        //System.out.println(private_decrypt(public_encrypt("PKCS#1公钥加密,PKCS#1私钥解密",pkcs1_public),pkcs1_private));
        //System.out.println(public_decrypt(private_encrypt("PKCS#1私钥加密,PKCS#8公钥解密",pkcs1_private),pkcs8_public));
        //System.out.println(public_decrypt(private_encrypt("PKCS#8私钥加密,PKCS#1公钥解密",pkcs8_private),pkcs1_public));
        //System.out.println(public_decrypt(private_encrypt("PKCS#1私钥加密,PKCS#1公钥解密",pkcs1_private),pkcs1_public));
    }

    public static String public_encrypt(String str, String publicKey) throws Exception {
        byte[] decoded = Base64.decodeBase64(publicKey);
        RSAPublicKey pubKey = (RSAPublicKey) KeyFactory.getInstance("RSA").generatePublic(new X509EncodedKeySpec(decoded));
        Cipher cipher = Cipher.getInstance("RSA");
        cipher.init(Cipher.ENCRYPT_MODE, pubKey);
        String outStr = Base64.encodeBase64String(cipher.doFinal(str.getBytes("UTF-8")));
        return outStr;
    }

    public static String private_encrypt(String str, String privateKey) throws Exception {
        byte[] decoded = Base64.decodeBase64(privateKey);
        RSAPrivateKey priKey = (RSAPrivateKey) KeyFactory.getInstance("RSA").generatePrivate(new PKCS8EncodedKeySpec(decoded));
        Cipher cipher = Cipher.getInstance("RSA");
        cipher.init(Cipher.ENCRYPT_MODE, priKey);
        String outStr = Base64.encodeBase64String(cipher.doFinal(str.getBytes("UTF-8")));
        return outStr;
    }

    public static String private_decrypt(String str, String privateKey) throws Exception {
        byte[] inputByte = Base64.decodeBase64(str.getBytes("UTF-8"));
        byte[] decoded = Base64.decodeBase64(privateKey);
        RSAPrivateKey priKey = (RSAPrivateKey) KeyFactory.getInstance("RSA").generatePrivate(new PKCS8EncodedKeySpec(decoded));
        Cipher cipher = Cipher.getInstance("RSA");
        cipher.init(Cipher.DECRYPT_MODE, priKey);
        String outStr = new String(cipher.doFinal(inputByte));
        return outStr;
    }

    public static String public_decrypt(String str, String publicKey) throws Exception {
        byte[] inputByte = Base64.decodeBase64(str.getBytes("UTF-8"));
        byte[] decoded = Base64.decodeBase64(publicKey);
        RSAPublicKey pubKey = (RSAPublicKey) KeyFactory.getInstance("RSA").generatePublic(new X509EncodedKeySpec(decoded));
        Cipher cipher = Cipher.getInstance("RSA");
        cipher.init(Cipher.DECRYPT_MODE, pubKey);
        String outStr = new String(cipher.doFinal(inputByte));
        return outStr;
    }
}