public class RSAUtil extends Object
| 限定符和类型 | 字段和说明 |
|---|---|
static String |
CHARSET |
static String |
RSA_ALGORITHM |
static String |
SIGNATURE_ALGORITHM |
| 构造器和说明 |
|---|
RSAUtil() |
| 限定符和类型 | 方法和说明 |
|---|---|
static String |
base64(Key key)
密钥生成base64字符
|
static KeyPair |
create() |
static KeyPair |
create(int size)
创建密钥对
返回密钥对后可以getPrivate()获取私钥或getPublic()获取公钥
获取公钥私钥后可以通过base64(key)生成String用来交换密钥
|
static RSAPrivateKey |
createPrivateKey(File file)
从文件中提取私钥
|
static RSAPrivateKey |
createPrivateKey(InputStream is)
从输入流中提取私钥
|
static RSAPrivateKey |
createPrivateKey(String base64)
创建私钥
|
static RSAPublicKey |
createPublicKey(File file)
从文件中提取公钥
|
static RSAPublicKey |
createPublicKey(InputStream is)
从输入流中提取公钥
|
static RSAPublicKey |
createPublicKey(String base64)
创建公钥
|
static String |
decrypt(String data,
PrivateKey key)
私钥解密
|
static String |
decrypt(String data,
RSAPublicKey key)
公钥解密
|
static String |
decrypt(String data,
String key) |
static String |
encrypt(String data,
PublicKey key)
公钥加密
|
static String |
encrypt(String data,
RSAPrivateKey key)
私钥加密(加密应该用公钥)
|
static String |
encrypt(String data,
String key) |
static String |
sign(byte[] data,
PrivateKey key)
用私钥对信息生成数字签名
|
static String |
sign(String data,
PrivateKey key) |
static String |
sign(String data,
String key) |
static boolean |
verify(byte[] data,
PublicKey key,
String sign)
校验数字签名
|
static boolean |
verify(byte[] data,
String key,
String sign) |
static boolean |
verify(String data,
PublicKey key,
String sign)
校验数字签名
|
static boolean |
verify(String data,
String key,
String sign)
校验数字签名
|
public static KeyPair create(int size)
size - 位数public static KeyPair create()
public static RSAPublicKey createPublicKey(String base64) throws NoSuchAlgorithmException, InvalidKeySpecException
base64 - 密钥字符串(经过base64编码)NoSuchAlgorithmException - NoSuchAlgorithmExceptionInvalidKeySpecException - InvalidKeySpecExceptionpublic static RSAPrivateKey createPrivateKey(String base64) throws NoSuchAlgorithmException, InvalidKeySpecException
base64 - 密钥字符串(经过base64编码)NoSuchAlgorithmException - NoSuchAlgorithmExceptionInvalidKeySpecException - InvalidKeySpecExceptionpublic static String encrypt(String data, PublicKey key) throws Exception
data - datakey - RSA 公钥Exception - 异常 Exceptionpublic static String decrypt(String data, PrivateKey key) throws Exception
data - datakey - RSA 私钥Exception - 异常 Exceptionpublic static String encrypt(String data, RSAPrivateKey key) throws Exception
data - datakey - RSA 私钥Exception - 异常 Exceptionpublic static String decrypt(String data, RSAPublicKey key) throws Exception
data - datakey - RSA 公钥Exception - 异常 Exceptionpublic static String sign(byte[] data, PrivateKey key) throws Exception
data - 已加密数据key - 私钥(BASE64编码)Exception - 异常 Exceptionpublic static String sign(String data, PrivateKey key) throws Exception
Exceptionpublic static boolean verify(byte[] data,
PublicKey key,
String sign)
throws Exception
data - 已加密数据key - 公钥(BASE64编码)sign - 数字签名Exception - 异常 Exceptionpublic static boolean verify(byte[] data,
String key,
String sign)
throws Exception
Exceptionpublic static boolean verify(String data, String key, String sign) throws Exception
data - 已加密数据key - 公钥(BASE64编码)sign - 数字签名Exception - 异常 Exceptionpublic static boolean verify(String data, PublicKey key, String sign) throws Exception
data - 已加密数据key - 公钥sign - 数字签名Exception - 异常 Exceptionpublic static RSAPrivateKey createPrivateKey(File file) throws Exception
file - 密钥文件Exceptionpublic static RSAPublicKey createPublicKey(File file) throws Exception
file - fileExceptionpublic static RSAPublicKey createPublicKey(InputStream is) throws Exception
is - 输入流Exceptionpublic static RSAPrivateKey createPrivateKey(InputStream is) throws Exception
is - 输入流ExceptionCopyright © 2022. All rights reserved.