import javax.crypto.BadPaddingException;
import javax.crypto.Cipher;
import javax.crypto.IllegalBlockSizeException;
import javax.crypto.NoSuchPaddingException;
import java.security.*;
import java.security.interfaces.RSAPrivateKey;
import java.security.interfaces.RSAPublicKey;
import java.security.spec.InvalidKeySpecException;
import java.security.spec.PKCS8EncodedKeySpec;
import java.security.spec.X509EncodedKeySpec;
import java.util.Base64;
import java.util.HashMap;
import java.util.Map;
/**
* RSA工具类
* 生成公钥、私钥、签名、验签
*/
public class RSATools {
// 键算法
private static final String KEY_ALGORITHM = "RSA";
// 签名算法
private static final String SIGNATURE_ALGORITHM = "MD5withRSA";
// 默认长度
private static final int DEFAULT_KEY_SIZE = 1024;
/**
* 生成秘钥对,默认长度1024
*/
public static Map<String, String> generateKeyPair() {
return generateKeyPair(DEFAULT_KEY_SIZE);
}
/**
* 生成秘钥对,可指定长度[512-2048]范围
*
* @param keySize
* @return
*/
public static Map<String, String> generateKeyPair(int keySize) {
try {
// 创建密钥对生成器
KeyPairGenerator keyPairGenerator = KeyPairGenerator.getInstance(KEY_ALGORITHM);
// 初始化密钥对生
RSA生成密钥、签名、验签
最新推荐文章于 2024-06-24 16:00:48 发布
![](https://img-home.csdnimg.cn/images/20240711042549.png)