RSA鉴权工具类
java语言版本鉴权工具类
package com.msxf.bot.basic.utils;
import org.springframework.security.crypto.codec.Base64;
import com.alibaba.fastjson.JSON;
import java.security.*;
import java.security.interfaces.RSAPrivateKey;
import java.security.interfaces.RSAPublicKey;
import java.security.spec.PKCS8EncodedKeySpec;
import java.security.spec.X509EncodedKeySpec;
import java.util.HashMap;
import java.util.Map;
import javax.crypto.Cipher;
/**
* RSA加解密工具类
* @time 2020-07-23
*/
public class RSAUtil {
/**
* 加密算法RSA
*/
public static final String KEY_ALGORITHM = "RSA";
/**
* 签名算法
*/
public static final String SIGNATURE_ALGORITHM = "SHA256withRSA";
/**
* 获取公钥的key
*/
private static final String PUBLIC_KEY = "RSAPublicKey";
/**
* 获取私钥的key
*/
private static final String PRIVATE_KEY = "RSAPrivateKey";
/**
* <p>
* 生成密钥对(公钥和私钥)
* </p>
*
* @return
* @throws Exception
*/
public static Map<String, Object> genKeyPair() {
try {
KeyPairGenerator keyPairGen = KeyPairGenerator.getInstance(KEY_ALGORITHM);
keyPairGen.initialize(2048);
KeyPair keyPair = keyPairGen.generateKeyPair();
RSAPublicKey publicKey = (RSAPublicKey) keyPair.getPublic();
RSAPrivateKey privateKey = (RSAPrivateKey) keyPair.getPrivate();
Map<String, Object> keyMap = new HashMap<String, Object>(2);
keyMap.put(PUBLIC_KEY, publicKey);
keyMap.put(PRIVATE_KEY, privateKey);
return keyMap;
} catch (Exception e) {
e.printStackTrace();
}
return null;
}
/**
* <p>
* 获取私钥
* </p>
*
* @param keyMap 密钥对
* @return
* @throws Exception
*/
public static String getPrivateKey(Map<String, Object> keyMap)
{
try {
Key key = (Key) keyMap.get(PRIVATE_KEY);
return new String(Base64.encode(key.getEncoded()));
} catch (Exception e) {
e.printStackTrace();
}
return null;
}
/**
* <p>
* 获取公钥
* </p>
*
* @param keyMap 密钥对
* @return
* @throws Exception
*/
public static String getPublicKey(Map<String, Object> keyMap)
{
try {
Key key = (Key) keyMap.get(PUBLIC_KEY);
return new String(Base64.encode(key.getEncoded()));
} catch (Exception e) {
e.printStackTrace();
}
return null;
}
/**
* <p>
* 用私钥对信息生成数字签名
* </p>
*
* @param data 已加密数据
* @param privateKey 私钥(BASE64编码)
*
* @return
* @throws Exception
*/
public static String sign(byte[] data, String privateKey) {
try {
byte[] keyBytes = Base64.decode(privateKey.getBytes());
PKCS8EncodedKeySpec pkcs8KeySpec = new PKCS8EncodedKeySpec(keyBytes);
KeyFactory keyFactory = KeyFactory.getInstance(KEY_ALGORITHM);
PrivateKey privateK = keyFactory.generatePrivate(pkcs8KeySpec);
Signature signature = Signature.getInstance(SIGNATURE_ALGORITHM);
signature.initSign(privateK);
signature.update(data);
return new String(Base64.encode(signature.sign()));
} catch (Exception e) {
e.printStackTrace();
}
return null;
}
/**
* <p>
* 校验数字签名
* </p>
*
* @param data 已加密数据
* @param publicKey 公钥(BASE64编码)
* @param sign 数字签名
*
* @return
* @throws Exception
*
*/
public static boolean verify(byte[] data, String publicKey, String sign)
throws Exception {
byte[] keyBytes = Base64.decode(publicKey.getBytes());
X509EncodedKeySpec keySpec = new X509EncodedKeySpec(keyBytes);
KeyFactory keyFactory = KeyFactory.getInstance(KEY_ALGORITHM);
PublicKey publicK = keyFactory.generatePublic(keySpec);
Signature signature = Signature.getInstance(SIGNATURE_ALGORITHM);
signature.initVerify(publicK);
signature.update(data);
return signature.verify(Base64.decode(sign.getBytes()));
}
public static void main(String[] args) throws Exception {
Map<String, Object> key = genKeyPair();
String privateKey = getPrivateKey(key);
String publicKey = getPublicKey(key);
System.out.println("privateKey = " + privateKey);
System.out.println("publicKey = " + publicKey);
String timestamp = "1582011346394";
System.out.println("timestamp = " + timestamp);
String appId = "4570c30e6eff413a9c3a973f33512111";
System.out.println("appId = " + appId);
String sign = sign((appId + timestamp).getBytes(),privateKey);
System.out.println("sign = " + sign);
System.out.println("鉴权结果:" + verify((appId + timestamp).getBytes(),publicKey,sign));
}
}
php语言版本鉴权工具类
/**
* @main 签名
* @param $content
* @return string
* User: sync
* Date: 2020/6/12
* Time: 3:20 下午
*/
public function getRsaSign($content)
{
$rsaKey = "-----BEGIN RSA PRIVATE KEY-----\n" . $this->privateKey . "\n-----END RSA PRIVATE KEY-----";
$key = openssl_pkey_get_private($rsaKey);
openssl_sign($content, $signature, $key, "SHA256");
openssl_free_key($key);
$sign = base64_encode($signature);
return $sign;
}