java 中使用RSA非对称性加密解密

需要引入的jar包:bcprov-jdk15on-161.jar

下载地址:https://www.bouncycastle.org/latest_releases.html

 //公钥加密
public static String encrypt(String content, PublicKey publicKey) {
try{
Security.addProvider(new org.bouncycastle.jce.provider.BouncyCastleProvider());
Cipher cipher = Cipher.getInstance("RSA/NONE/OAEPWITHSHA256AndMGF1Padding","BC");
// Cipher cipher = Cipher.getInstance("RSA/ECB/PKCS1Padding");//java默认"RSA"="RSA/ECB/PKCS1Padding"
cipher.init(Cipher.ENCRYPT_MODE, publicKey);
byte[] output = cipher.doFinal(content.getBytes());
BASE64Encoder encoder = new BASE64Encoder();
return encoder.encode(output);
}catch (Exception e){
e.printStackTrace();
}
return null;
}
    //私钥解密
public static String decrypt(String content, PrivateKey privateKey) {
try {
// Cipher cipher = Cipher.getInstance("RSA/ECB/PKCS1Padding");
Security.addProvider(new org.bouncycastle.jce.provider.BouncyCastleProvider());
Cipher cipher = Cipher.getInstance("RSA/NONE/OAEPWITHSHA256AndMGF1Padding","BC");
cipher.init(Cipher.DECRYPT_MODE, privateKey);
BASE64Decoder decoder = new BASE64Decoder();
byte[] decodeBuffer = decoder.decodeBuffer(content);
byte [] b = cipher.doFinal(decodeBuffer);
return new String(b);
} catch (Exception e){
e.printStackTrace();
return null;
}
}

测试

private static String data2 ="g7ZcUMRAIAsVDwrAFi5F4uia6KhW3gCbyfKLDxLWiBTbHuJpfPA3iSLz7RYs9/6tMO6Vq8kG4nJs9+OMyK0psK/iCLA8PsEVRczsNNJ9OS10eZ/MbKoCpRRCC89aHf59JQy757g1wquq5yCXbnJRPd7lQYobJnxp1ZeBWB9NwruISt075/6sS8Kram2IXFLP5LypFNWRCPB9HVKz3HFlLqRH0lWfIbPO1VDYsK6ooRvRbr4MnRAACs+p92VeAg6NRcqWvP4o7f/wY3DcBYpXLVfxSQjuRG0t3t61Agc81COPaelk1f2SShtmsX8MyAZWdZpTqnTwIVRiRIXjl8PHXw==";

public static void main(String[] args) throws NoSuchAlgorithmException, InvalidKeySpecException, NoSuchPaddingException, IllegalBlockSizeException, BadPaddingException, InvalidKeyException, IOException {
try
{
String ss = "TKG-Jqs1g_O_4D37fFkOv9NGBqtPXTo-zc7b1VFf-OY";
PublicKey publicKey4 = getPublicKey(yourPublicKey);
String encrypt = encrypt(ss, publicKey4);
System.out.println(encrypt); PrivateKey privateKey4 = getPrivateKey(your_PRIVATE_KEY);
String decrypt = decrypt(data2, privateKey4);
System.out.println(decrypt);
}
catch (Exception e)
{
e.printStackTrace();
}
}
    /**
* String转公钥PublicKey
* @param key
* @return
* @throws Exception
*/
public static PublicKey getPublicKey(String key) throws Exception {
byte[] keyBytes;
keyBytes = (new BASE64Decoder()).decodeBuffer(key);
X509EncodedKeySpec keySpec = new X509EncodedKeySpec(keyBytes);
KeyFactory keyFactory = KeyFactory.getInstance("RSA");
PublicKey publicKey = keyFactory.generatePublic(keySpec);
return publicKey;
} /**
* String转私钥PrivateKey
* @param key
* @return
* @throws Exception
*/
public static PrivateKey getPrivateKey(String key) throws Exception {
byte[] keyBytes;
keyBytes = (new BASE64Decoder()).decodeBuffer(key);
PKCS8EncodedKeySpec keySpec = new PKCS8EncodedKeySpec(keyBytes);
KeyFactory keyFactory = KeyFactory.getInstance("RSA");
PrivateKey privateKey = keyFactory.generatePrivate(keySpec);
return privateKey;
}
上一篇:RPC之——HTTP协议栈


下一篇:ASP.NET MVC5+EF6+EasyUI 后台管理系统(92)-打印EasyUI 的datagrid表格