Maven依赖
< dependency>
< groupId> cn. hutool< / groupId>
< artifactId> hutool- all< / artifactId>
< version> 5.8 .31 < / version>
< / dependency>
RSA 非对称加密工具类
import cn. hutool. core. codec. Base64 ;
import cn. hutool. core. util. StrUtil ;
import javax. crypto. Cipher ;
import java. io. ByteArrayOutputStream ;
import java. math. BigInteger ;
import java. nio. charset. StandardCharsets ;
import java. security. * ;
import java. security. interfaces. RSAPrivateKey ;
import java. security. interfaces. RSAPublicKey ;
import java. security. spec. * ;
import java. util. HashMap ;
import java. util. Map ;
public class RsaKit {
private static final int MAX_ENCRYPT_BLOCK = 117 ;
private static final int MAX_DECRYPT_BLOCK = 128 ;
private static final String KEY_ALGORITHM = "RSA" ;
public static Map < String , String > getKeys ( ) throws Exception {
KeyPairGenerator keyPairGen = KeyPairGenerator . getInstance ( KEY_ALGORITHM ) ;
keyPairGen. initialize ( 1024 ) ;
KeyPair keyPair = keyPairGen. generateKeyPair ( ) ;
RSAPublicKey publicKey = ( RSAPublicKey ) keyPair. getPublic ( ) ;
RSAPrivateKey privateKey = ( RSAPrivateKey ) keyPair. getPrivate ( ) ;
String publicKeyStr = getPublicKeyStr ( publicKey) ;
String privateKeyStr = getPrivateKeyStr ( privateKey) ;
Map < String , String > map = new HashMap < String , String > ( 2 ) ;
map. put ( "publicKey" , publicKeyStr) ;
map. put ( "privateKey" , privateKeyStr) ;
System . out. println ( "公钥\r\n" + publicKeyStr) ;
System . out. println ( "私钥\r\n" + privateKeyStr) ;
return map;
}
public static RSAPublicKey getPublicKey ( String modulus, String exponent) {
try {
BigInteger b1 = new BigInteger ( modulus) ;
BigInteger b2 = new BigInteger ( exponent) ;
KeyFactory keyFactory = KeyFactory . getInstance ( KEY_ALGORITHM ) ;
RSAPublicKeySpec keySpec = new RSAPublicKeySpec ( b1, b2) ;
return ( RSAPublicKey ) keyFactory. generatePublic ( keySpec) ;
} catch ( Exception e) {
e. printStackTrace ( ) ;
return null ;
}
}
public static RSAPrivateKey getPrivateKey ( String modulus, String exponent) {
try {
BigInteger b1 = new BigInteger ( modulus) ;
BigInteger b2 = new BigInteger ( exponent) ;
KeyFactory keyFactory = KeyFactory . getInstance ( KEY_ALGORITHM ) ;
RSAPrivateKeySpec keySpec = new RSAPrivateKeySpec ( b1, b2) ;
return ( RSAPrivateKey ) keyFactory. generatePrivate ( keySpec) ;
} catch ( Exception e) {
e. printStackTrace ( ) ;
return null ;
}
}
public static String encryptByPublicKey ( String data, String publicKey) throws Exception {
return encryptByPublicKey ( data, publicKey, "RSA/ECB/PKCS1Padding" ) ;
}
public static String encryptByPublicKeyByWx ( String data, String publicKey) throws Exception {
return encryptByPublicKey ( data, publicKey, "RSA/ECB/OAEPWITHSHA-1ANDMGF1PADDING" ) ;
}
public static String encryptByPublicKey ( String data, String publicKey, String fillMode) throws Exception {
byte [ ] dataByte = data. getBytes ( StandardCharsets . UTF_8 ) ;
byte [ ] keyBytes = Base64 . decode ( publicKey) ;
X509EncodedKeySpec x509KeySpec = new X509EncodedKeySpec ( keyBytes) ;
KeyFactory keyFactory = KeyFactory . getInstance ( KEY_ALGORITHM ) ;
Key key = keyFactory. generatePublic ( x509KeySpec) ;
Cipher cipher = Cipher . getInstance ( fillMode) ;
cipher. init ( Cipher . ENCRYPT_MODE , key) ;
int inputLen = dataByte. length;
ByteArrayOutputStream out = new ByteArrayOutputStream ( ) ;
int offSet = 0 ;
byte [ ] cache;
int i = 0 ;
while ( inputLen - offSet > 0 ) {
if ( inputLen - offSet > MAX_ENCRYPT_BLOCK ) {
cache = cipher. doFinal ( dataByte, offSet, MAX_ENCRYPT_BLOCK ) ;
} else {
cache = cipher. doFinal ( dataByte, offSet, inputLen - offSet) ;
}
out. write ( cache, 0 , cache. length) ;
i++ ;
offSet = i * MAX_ENCRYPT_BLOCK ;
}
byte [ ] encryptedData = out. toByteArray ( ) ;
out. close ( ) ;
return StrUtil . str ( Base64 . encode ( encryptedData) ) ;
}
public static String encryptByPrivateKey ( String data, String privateKey) throws Exception {
PKCS8EncodedKeySpec priPkcs8 = new PKCS8EncodedKeySpec ( Base64 . decode ( privateKey) ) ;
KeyFactory keyFactory = KeyFactory . getInstance ( KEY_ALGORITHM ) ;
PrivateKey priKey = keyFactory. generatePrivate ( priPkcs8) ;
java. security. Signature signature = java. security. Signature. getInstance ( "SHA256WithRSA" ) ;
signature. initSign ( priKey) ;
signature. update ( data. getBytes ( StandardCharsets . UTF_8 ) ) ;
byte [ ] signed = signature. sign ( ) ;
return StrUtil . str ( Base64 . encode ( signed) ) ;
}
public static String encryptByPrivateKey ( String data, PrivateKey privateKey) throws Exception {
java. security. Signature signature = java. security. Signature. getInstance ( "SHA256WithRSA" ) ;
signature. initSign ( privateKey) ;
signature. update ( data. getBytes ( StandardCharsets . UTF_8 ) ) ;
byte [ ] signed = signature. sign ( ) ;
return StrUtil . str ( Base64 . encode ( signed) ) ;
}
public static boolean checkByPublicKey ( String data, String sign, String publicKey) throws Exception {
KeyFactory keyFactory = KeyFactory . getInstance ( KEY_ALGORITHM ) ;
byte [ ] encodedKey = Base64 . decode ( publicKey) ;
PublicKey pubKey = keyFactory. generatePublic ( new X509EncodedKeySpec ( encodedKey) ) ;
java. security. Signature signature = java. security. Signature. getInstance ( "SHA256WithRSA" ) ;
signature. initVerify ( pubKey) ;
signature. update ( data. getBytes ( StandardCharsets . UTF_8 ) ) ;
return signature. verify ( Base64 . decode ( sign. getBytes ( StandardCharsets . UTF_8 ) ) ) ;
}
public static boolean checkByPublicKey ( String data, String sign, PublicKey publicKey) throws Exception {
java. security. Signature signature = java. security. Signature. getInstance ( "SHA256WithRSA" ) ;
signature. initVerify ( publicKey) ;
signature. update ( data. getBytes ( StandardCharsets . UTF_8 ) ) ;
return signature. verify ( Base64 . decode ( sign. getBytes ( StandardCharsets . UTF_8 ) ) ) ;
}
public static String decryptByPrivateKey ( String data, String privateKey) throws Exception {
return decryptByPrivateKey ( data, privateKey, "RSA/ECB/PKCS1Padding" ) ;
}
public static String decryptByPrivateKeyByWx ( String data, String privateKey) throws Exception {
return decryptByPrivateKey ( data, privateKey, "RSA/ECB/OAEPWITHSHA-1ANDMGF1PADDING" ) ;
}
public static String decryptByPrivateKey ( String data, String privateKey, String fillMode) throws Exception {
byte [ ] encryptedData = Base64 . decode ( data) ;
byte [ ] keyBytes = Base64 . decode ( privateKey) ;
PKCS8EncodedKeySpec pkcs8KeySpec = new PKCS8EncodedKeySpec ( keyBytes) ;
KeyFactory keyFactory = KeyFactory . getInstance ( KEY_ALGORITHM ) ;
Key key = keyFactory. generatePrivate ( pkcs8KeySpec) ;
Cipher cipher = Cipher . getInstance ( fillMode) ;
cipher. init ( Cipher . DECRYPT_MODE , key) ;
int inputLen = encryptedData. length;
ByteArrayOutputStream out = new ByteArrayOutputStream ( ) ;
int offSet = 0 ;
byte [ ] cache;
int i = 0 ;
while ( inputLen - offSet > 0 ) {
if ( inputLen - offSet > MAX_DECRYPT_BLOCK ) {
cache = cipher. doFinal ( encryptedData, offSet, MAX_DECRYPT_BLOCK ) ;
} else {
cache = cipher. doFinal ( encryptedData, offSet, inputLen - offSet) ;
}
out. write ( cache, 0 , cache. length) ;
i++ ;
offSet = i * MAX_DECRYPT_BLOCK ;
}
byte [ ] decryptedData = out. toByteArray ( ) ;
out. close ( ) ;
return new String ( decryptedData) ;
}
public static PublicKey loadPublicKey ( String publicKeyStr) throws Exception {
try {
byte [ ] buffer = Base64 . decode ( publicKeyStr) ;
KeyFactory keyFactory = KeyFactory . getInstance ( KEY_ALGORITHM ) ;
X509EncodedKeySpec keySpec = new X509EncodedKeySpec ( buffer) ;
return keyFactory. generatePublic ( keySpec) ;
} catch ( NoSuchAlgorithmException e) {
throw new Exception ( "无此算法" ) ;
} catch ( InvalidKeySpecException e) {
throw new Exception ( "公钥非法" ) ;
} catch ( NullPointerException e) {
throw new Exception ( "公钥数据为空" ) ;
}
}
public static PrivateKey loadPrivateKey ( String privateKeyStr) throws Exception {
try {
byte [ ] buffer = Base64 . decode ( privateKeyStr) ;
PKCS8EncodedKeySpec keySpec = new PKCS8EncodedKeySpec ( buffer) ;
KeyFactory keyFactory = KeyFactory . getInstance ( KEY_ALGORITHM ) ;
return keyFactory. generatePrivate ( keySpec) ;
} catch ( NoSuchAlgorithmException e) {
throw new Exception ( "无此算法" ) ;
} catch ( InvalidKeySpecException e) {
throw new Exception ( "私钥非法" ) ;
} catch ( NullPointerException e) {
throw new Exception ( "私钥数据为空" ) ;
}
}
public static String getPrivateKeyStr ( PrivateKey privateKey) {
return Base64 . encode ( privateKey. getEncoded ( ) ) ;
}
public static String getPublicKeyStr ( PublicKey publicKey) {
return Base64 . encode ( publicKey. getEncoded ( ) ) ;
}
public static void main ( String [ ] args) throws Exception {
Map < String , String > keys = getKeys ( ) ;
String publicKey = keys. get ( "publicKey" ) ;
String privateKey = keys. get ( "privateKey" ) ;
String content = "我是Javen,I am Javen" ;
String encrypt = encryptByPublicKey ( content, publicKey) ;
String decrypt = decryptByPrivateKey ( encrypt, privateKey) ;
System . out. println ( "加密之后:" + encrypt) ;
System . out. println ( "解密之后:" + decrypt) ;
System . out. println ( "======华丽的分割线=========" ) ;
content = "我是Javen,I am Javen" ;
encrypt = encryptByPublicKeyByWx ( content, publicKey) ;
decrypt = decryptByPrivateKeyByWx ( encrypt, privateKey) ;
System . out. println ( "加密之后:" + encrypt) ;
System . out. println ( "解密之后:" + decrypt) ;
String sign = encryptByPrivateKey ( content, privateKey) ;
System . out. println ( "加密之后:" + sign) ;
System . out. println ( checkByPublicKey ( content, sign, publicKey) ) ;
}
}
效果图