基于RSA算法的Java示例代码,展示了如何进行公钥加密、私钥解密、私钥签名和公钥验签。
非对称加密公私钥使用学习请查看:非堆成加密公私钥使用-****博客
代码实现
package com.chengxuyuan.demo;
import javax.crypto.Cipher;
import java.security.*;
import java.util.Base64;
public class RSADemo {
public static void main (String[] args) throws Exception {
// 生成RSA密钥对
KeyPair keyPair = generateKeyPair ();
PublicKey publicKey = keyPair.getPublic ();
PrivateKey privateKey = keyPair.getPrivate ();
String originalText = "I am a RSA demo!";
// 公钥加密
byte[] encryptedText = encrypt (originalText, publicKey);
System.out.println ("公钥加密内容: " + Base64.getEncoder ().encodeToString (encryptedText));
// 私钥解密
String decryptedText = decrypt (encryptedText, privateKey);
System.out.println ("私钥解密内容: " + decryptedText);
// 私钥签名
byte[] signature = sign (originalText, privateKey);
System.out.println ("私钥签名内容: " + Base64.getEncoder ().encodeToString (signature));
// 公钥验签
boolean isVerified = verify (originalText, signature, publicKey);
System.out.println ("验签 : " + isVerified);
}
/**
* 生成RSA密钥对
*
* @return
* @throws NoSuchAlgorithmException
*/
public static KeyPair generateKeyPair () throws NoSuchAlgorithmException {
KeyPairGenerator keyPairGenerator = KeyPairGenerator.getInstance ("RSA");
keyPairGenerator.initialize (2048);
return keyPairGenerator.generateKeyPair ();
}
/**
* 公钥加密
*
* @param plainText 文本
* @param publicKey 公钥
* @return
* @throws Exception
*/
public static byte[] encrypt (String plainText, PublicKey publicKey) throws Exception {
Cipher cipher = Cipher.getInstance ("RSA");
cipher.init (Cipher.ENCRYPT_MODE, publicKey);
return cipher.doFinal (plainText.getBytes ());
}
/**
* 私钥解密
*
* @param encryptedText 加密文本
* @param privateKey 私钥
* @return
* @throws Exception
*/
public static String decrypt (byte[] encryptedText, PrivateKey privateKey) throws Exception {
Cipher cipher = Cipher.getInstance ("RSA");
cipher.init (Cipher.DECRYPT_MODE, privateKey);
byte[] decryptedBytes = cipher.doFinal (encryptedText);
return new String (decryptedBytes);
}
/**
* 私钥签名
*
* @param plainText 文本
* @param privateKey 私钥
* @return
* @throws Exception
*/
public static byte[] sign (String plainText, PrivateKey privateKey) throws Exception {
Signature privateSignature = Signature.getInstance ("SHA256withRSA");
privateSignature.initSign (privateKey);
privateSignature.update (plainText.getBytes ());
return privateSignature.sign ();
}
/**
* 公钥验签
*
* @param plainText 文本
* @param signature 签名
* @param publicKey 公钥
* @return
* @throws Exception
*/
public static boolean verify (String plainText, byte[] signature, PublicKey publicKey) throws Exception {
Signature publicSignature = Signature.getInstance ("SHA256withRSA");
publicSignature.initVerify (publicKey);
publicSignature.update (plainText.getBytes ());
return publicSignature.verify (signature);
}
}
运行结果
公钥加密内容: alFHnM0a2eiOvIhyqmByOrMIP/DblmhRnjS6gwSSz/7RN4YsJntwf/JUIHXZxGmCvH2eBHZAx8folkGIZma2MCqkMFPWYW0DkgutDWL/esikRILttHDtAohtfHaNqksR0AKedGVpq8av1bZOxFr1XYfeSNfILYgPbi/UuI4EVC/xFcfwHnEaxKgvPQHV/rbTjAHyz9phKcYpCy/r0EBwJcfEEC4ov7XbnVRmkDSRDzXYi3bwXn5WNKj97ZsN90J5WPRmLZPcD6KA4fFv/FklI9wB0BEWnFRPcfG9Q9w8PBc1Kmb6GGaSJ329ZxI6TQ4fpKuggpN5ejIACDNPk31AAw==
私钥解密内容: I am a RSA demo!
私钥签名内容: etp/Cf3tzAZqVOAqr5GMrOVnvR18hCMWNjGuTz41nqyu4iU5JxHZEW/8Fh0K4AW07qEQveRxNVhiXAKOvqai03HAT//ePUnOqJ7YU4qkLer/WSuHCgJkcB0c7w1egNsAr06m+mI9UsU6Q7TM+EWrctiJH6cSm0zfNHkVTq7fq2viGwxsmULto04dEVVP1PyiDL0PsPSTOrZ5rycoK3WoiyG1SLf127cgRjrnb1tt1h+7E3stcYSnGQdWrcpwVXWhNV3etzQaAKwkNyHkhPTXgY9v1S1YR4WLCGVfCRfPwJt91qF3C3T49B7s7ofQavbSWnIrzfVoHkjIF7ZT3c+Nig==
验签 : true