public class RSAUtils extends Object
RSA公钥/私钥/签名工具包
字符串格式的密钥在未在特殊说明情况下都为BASE64编码格式
由于非对称加密速度极其缓慢,一般文件不使用它来加密而是使用对称加密,
非对称加密算法可以用来对对称加密的密钥加密,这样保证密钥的安全也就保证了数据的安全
| 限定符和类型 | 类和说明 |
|---|---|
static class |
RSAUtils.cerIssuer
颁发者名称
|
static class |
RSAUtils.cerSubject
主体名
|
| 限定符和类型 | 字段和说明 |
|---|---|
static String |
KEY_ALGORITHM
加密算法RSA
|
static String |
SIGNATURE_ALGORITHM
签名算法
|
static String |
SIGNATURE_ALGORITHM_SHA1WithRSA |
| 构造器和说明 |
|---|
RSAUtils() |
| 限定符和类型 | 方法和说明 |
|---|---|
static byte[] |
decryptByPrivateKey(byte[] encryptedData,
String privateKey)
私钥解密
|
static byte[] |
decryptByPublicKey(byte[] encryptedData,
String publicKey)
公钥解密
|
static String |
decryptStringByPrivateKey(String encryptedData,
String privateKey)
私钥解密
|
static byte[] |
encryptByPrivateKey(byte[] data,
String privateKey)
私钥加密
|
static byte[] |
encryptByPublicKey(byte[] data,
String publicKey)
公钥加密
|
static String |
encryptStringByPublicKey(byte[] data,
String publicKey)
公钥加密
|
static void |
generateCer(File flie_cert,
String[] info,
PrivateKey keyPrivate,
PublicKey keyPublic)
生成 cer
String[] info = {"huahua_user","hnu","university","china","hunan","changsha","111111","11111111","1"};
|
static void |
generatePfx(String strPassword)
生成 pfx
|
static Map<String,Object> |
genKeyPair()
生成密钥对(公钥和私钥)
|
static Map<String,Object> |
genKeyPair(String algorithm)
生成密钥对(公钥和私钥)
|
static Map<String,Object> |
GetCertformCer(File strCer)
cer 读取
|
static Map<String,Object> |
GetCertformPfx(File strPfx,
String strPassword)
pfx 读取
|
static String |
getPrivateKey(Map<String,Object> keyMap)
获取私钥
|
static PrivateKey |
getPrivateKey(String key)
String to PrivateKey
|
static PrivateKey |
getPrivateKey(String key,
String algorithm)
String to PrivateKey
|
static String |
getPublicKey(Map<String,Object> keyMap)
获取公钥
|
static PublicKey |
getPublicKey(String key)
String to PublicKey
|
static PublicKey |
getPublicKey(String key,
String algorithm)
String to PublicKey
|
static void |
keyWireFile(File file,
String keystore)
将密钥对写入到文件
|
static void |
main(String[] args) |
static String |
sign(byte[] data,
String privateKey)
用私钥对信息生成数字签名
|
static String |
sign(byte[] data,
String privateKey,
String algorithm)
用私钥对信息生成数字签名
|
static boolean |
verify(byte[] data,
String publicKey,
String sign)
校验数字签名
|
static boolean |
verify(byte[] data,
String publicKey,
String sign,
String algorithm) |
static boolean |
verify(String str,
File CERT,
String sign)
验签
|
static boolean |
verify(String str,
File CERT,
String sign,
String algorithm)
验签
|
static boolean |
verifyCertificate(Date date,
Certificate certificate)
验证证书是否过期或无效
|
public static Map<String,Object> genKeyPair() throws Exception
生成密钥对(公钥和私钥)
Exceptionpublic static Map<String,Object> genKeyPair(String algorithm) throws Exception
生成密钥对(公钥和私钥)
Exceptionpublic static String sign(byte[] data, String privateKey, String algorithm) throws Exception
用私钥对信息生成数字签名
data - 已加密数据privateKey - 私钥(BASE64编码)Exceptionpublic static String sign(byte[] data, String privateKey) throws Exception
用私钥对信息生成数字签名
data - 已加密数据privateKey - 私钥(BASE64编码)Exceptionpublic static boolean verify(byte[] data,
String publicKey,
String sign)
throws Exception
校验数字签名
data - 已加密数据publicKey - 公钥(BASE64编码)sign - 数字签名Exceptionpublic static boolean verify(byte[] data,
String publicKey,
String sign,
String algorithm)
throws Exception
data - 已加密数据publicKey - 公钥(BASE64编码)sign - 数字签名algorithm - Exceptionpublic static boolean verify(String str, File CERT, String sign) throws Exception
Exceptionpublic static boolean verify(String str, File CERT, String sign, String algorithm) throws Exception
Exceptionpublic static byte[] decryptByPrivateKey(byte[] encryptedData,
String privateKey)
throws Exception
私钥解密
encryptedData - 已加密数据privateKey - 私钥(BASE64编码)Exceptionpublic static byte[] decryptByPublicKey(byte[] encryptedData,
String publicKey)
throws Exception
公钥解密
encryptedData - 已加密数据publicKey - 公钥(BASE64编码)Exceptionpublic static byte[] encryptByPublicKey(byte[] data,
String publicKey)
throws Exception
公钥加密
data - 源数据publicKey - 公钥(BASE64编码)Exceptionpublic static byte[] encryptByPrivateKey(byte[] data,
String privateKey)
throws Exception
私钥加密
data - 源数据privateKey - 私钥(BASE64编码)Exceptionpublic static String getPrivateKey(Map<String,Object> keyMap) throws Exception
获取私钥
keyMap - 密钥对Exceptionpublic static String getPublicKey(Map<String,Object> keyMap) throws Exception
获取公钥
keyMap - 密钥对Exceptionpublic static String encryptStringByPublicKey(byte[] data, String publicKey) throws Exception
公钥加密
data - 源数据publicKey - 公钥(BASE64编码)Exceptionpublic static String decryptStringByPrivateKey(String encryptedData, String privateKey) throws Exception
私钥解密
encryptedData - 已加密数据privateKey - 私钥(BASE64编码)Exceptionpublic static void keyWireFile(File file, String keystore) throws IOException
file - keystore - IOExceptionpublic static void generateCer(File flie_cert, String[] info, PrivateKey keyPrivate, PublicKey keyPublic) throws InvalidKeyException, NoSuchProviderException, SecurityException, SignatureException, IOException, CertificateEncodingException
String[] info = {"huahua_user","hnu","university","china","hunan","changsha","111111","11111111","1"};
keyPair_root - keyPair_user - info - keyPrivate - keyPublic - InvalidKeyExceptionNoSuchProviderExceptionSecurityExceptionSignatureExceptionIOExceptionCertificateEncodingExceptionpublic static void generatePfx(String strPassword) throws KeyStoreException, NoSuchAlgorithmException, SignatureException, InvalidKeyException, IOException, CertificateException
public static Map<String,Object> GetCertformPfx(File strPfx, String strPassword)
strPfx - strPassword - public static PublicKey getPublicKey(String key, String algorithm) throws Exception
key - algorithm - Exceptionpublic static PublicKey getPublicKey(String key) throws Exception
key - Exceptionpublic static PrivateKey getPrivateKey(String key, String algorithm) throws Exception
key - algorithm - Exceptionpublic static PrivateKey getPrivateKey(String key) throws Exception
key - Exceptionpublic static boolean verifyCertificate(Date date, Certificate certificate)
date - certificate - Copyright © 2020. All rights reserved.