
7 changed files with 657 additions and 2 deletions
@ -0,0 +1,308 @@ |
|||||
|
package com.yxt.supervise.gf.api.utils; |
||||
|
|
||||
|
import sun.misc.BASE64Decoder; |
||||
|
import sun.misc.BASE64Encoder; |
||||
|
|
||||
|
import javax.crypto.Cipher; |
||||
|
import java.io.ByteArrayOutputStream; |
||||
|
import java.io.IOException; |
||||
|
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.HashMap; |
||||
|
import java.util.Map; |
||||
|
|
||||
|
/** |
||||
|
* @author feikefei |
||||
|
* @create 2023-06-30-14:53 |
||||
|
*/ |
||||
|
public class RSAUtils { |
||||
|
/** |
||||
|
* 指定加密算法为RSA |
||||
|
*/ |
||||
|
private static final String ALGORITHM = "RSA"; |
||||
|
/** |
||||
|
* 加密填充方式 |
||||
|
*/ |
||||
|
public static final String ECB_PKCS1_PADDING = "RSA/ECB/PKCS1Padding";//加密填充方式
|
||||
|
/** |
||||
|
* 密钥长度,用来初始化 |
||||
|
*/ |
||||
|
private static final int KEYSIZE = 1024; |
||||
|
/** |
||||
|
* 公钥 |
||||
|
*/ |
||||
|
private static Key publicKey = null; |
||||
|
/** |
||||
|
* 私钥 |
||||
|
*/ |
||||
|
private static Key privateKey = null; |
||||
|
/** |
||||
|
* 公钥字符串 |
||||
|
*/ |
||||
|
private static String publicKeyString =""; |
||||
|
/** |
||||
|
* 私钥字符串 |
||||
|
*/ |
||||
|
private static String privateKeyString =""; |
||||
|
/** |
||||
|
* RSA最大加密明文大小 |
||||
|
*/ |
||||
|
private static final int MAX_ENCRYPT_BLOCK = 117; |
||||
|
/** |
||||
|
* RSA最大解密密文大小 |
||||
|
*/ |
||||
|
private static final int MAX_DECRYPT_BLOCK = 128; |
||||
|
|
||||
|
/** |
||||
|
* 生成密钥对 |
||||
|
* |
||||
|
* @throws Exception |
||||
|
*/ |
||||
|
public static Map<String, String> generateKeyPair() throws Exception { |
||||
|
|
||||
|
// // /** RSA算法要求有一个可信任的随机数源 */
|
||||
|
SecureRandom secureRandom = new SecureRandom(); |
||||
|
/** 为RSA算法创建一个KeyPairGenerator对象 */ |
||||
|
KeyPairGenerator keyPairGenerator = KeyPairGenerator.getInstance(ALGORITHM); |
||||
|
/** 利用上面的随机数据源初始化这个KeyPairGenerator对象 */ |
||||
|
keyPairGenerator.initialize(KEYSIZE, secureRandom); |
||||
|
// /** 生成密匙对 */
|
||||
|
KeyPair keyPair = keyPairGenerator.generateKeyPair(); |
||||
|
Map<String, String> keyMap = new HashMap<String, String>(); |
||||
|
// /** 得到公钥 */
|
||||
|
BASE64Encoder encoder = new BASE64Encoder(); |
||||
|
|
||||
|
keyMap.put("public", new String(encoder.encode(keyPair.getPublic().getEncoded()).getBytes(), "UTF-8")); |
||||
|
keyMap.put("private", new String(encoder.encode(keyPair.getPrivate().getEncoded()).getBytes(), "UTF-8")); |
||||
|
return keyMap; |
||||
|
} |
||||
|
|
||||
|
/** |
||||
|
* 生成公钥对象 |
||||
|
* |
||||
|
* @param publicKeyStr |
||||
|
* @throws Exception |
||||
|
*/ |
||||
|
public static Key setPublicKey(String publicKeyStr) throws Exception { |
||||
|
return RSAUtils.publicKey = generatePublicKey(publicKeyStr); |
||||
|
} |
||||
|
|
||||
|
/** |
||||
|
* 生成私钥对象 |
||||
|
* |
||||
|
* @param privateKeyStr |
||||
|
* @throws Exception |
||||
|
*/ |
||||
|
public static void setPrivateKey(String privateKeyStr) throws Exception { |
||||
|
RSAUtils.privateKey = generatePrivateKey(privateKeyStr); |
||||
|
} |
||||
|
|
||||
|
/** |
||||
|
* 私钥加密方法 |
||||
|
* |
||||
|
* @param privatekey |
||||
|
* @return |
||||
|
* @throws Exception |
||||
|
*/ |
||||
|
public static String encryptByPrivateKey(String source, String privatekey) throws Exception { |
||||
|
generatePrivateKey(privatekey); |
||||
|
/** 得到Cipher对象来实现对源数据的RSA加密 */ |
||||
|
Cipher cipher = Cipher.getInstance(ECB_PKCS1_PADDING); |
||||
|
cipher.init(Cipher.ENCRYPT_MODE, privateKey); |
||||
|
byte[] data = source.getBytes(); |
||||
|
/** 执行数据分组加密操作 */ |
||||
|
int inputLen = data.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(data, offSet, MAX_ENCRYPT_BLOCK); |
||||
|
} else { |
||||
|
cache = cipher.doFinal(data, offSet, inputLen - offSet); |
||||
|
} |
||||
|
out.write(cache, 0, cache.length); |
||||
|
i++; |
||||
|
offSet = i * MAX_ENCRYPT_BLOCK; |
||||
|
} |
||||
|
byte[] encryptedData = out.toByteArray(); |
||||
|
out.close(); |
||||
|
BASE64Encoder encoder = new BASE64Encoder(); |
||||
|
return encoder.encode(encryptedData); |
||||
|
} |
||||
|
|
||||
|
/** |
||||
|
* 使用公钥解密算法 |
||||
|
* |
||||
|
* @param cryptoSrc 密文 |
||||
|
* @return |
||||
|
* @throws Exception |
||||
|
*/ |
||||
|
public static String decryptByPublicKey(String cryptoSrc, String publicStr) throws Exception { |
||||
|
setPublicKey(publicStr); |
||||
|
/** 得到Cipher对象对已用公钥加密的数据进行RSA解密 */ |
||||
|
Cipher cipher = Cipher.getInstance(ECB_PKCS1_PADDING); |
||||
|
cipher.init(Cipher.DECRYPT_MODE, publicKey); |
||||
|
BASE64Decoder decoder = new BASE64Decoder(); |
||||
|
byte[] encryptedData = decoder.decodeBuffer(cryptoSrc); |
||||
|
/** 执行解密操作 */ |
||||
|
|
||||
|
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); |
||||
|
} |
||||
|
|
||||
|
|
||||
|
/** |
||||
|
* 公钥加密方法 |
||||
|
* |
||||
|
* @param pubString |
||||
|
* @return |
||||
|
* @throws Exception |
||||
|
*/ |
||||
|
public static String encryptByPublicKey(String source, String pubString) throws Exception { |
||||
|
Key key = setPublicKey(pubString); |
||||
|
/** 得到Cipher对象来实现对源数据的RSA加密 */ |
||||
|
Cipher cipher = Cipher.getInstance(ECB_PKCS1_PADDING); |
||||
|
cipher.init(Cipher.ENCRYPT_MODE, key); |
||||
|
byte[] data = source.getBytes(); |
||||
|
/** 执行分组加密操作 */ |
||||
|
int inputLen = data.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(data, offSet, MAX_ENCRYPT_BLOCK); |
||||
|
} else { |
||||
|
cache = cipher.doFinal(data, offSet, inputLen - offSet); |
||||
|
} |
||||
|
out.write(cache, 0, cache.length); |
||||
|
i++; |
||||
|
offSet = i * MAX_ENCRYPT_BLOCK; |
||||
|
} |
||||
|
byte[] encryptedData = out.toByteArray(); |
||||
|
out.close(); |
||||
|
|
||||
|
BASE64Encoder encoder = new BASE64Encoder(); |
||||
|
return encoder.encode(encryptedData); |
||||
|
} |
||||
|
|
||||
|
|
||||
|
/** |
||||
|
* 私钥解密算法 |
||||
|
* |
||||
|
* @param cryptoSrc 密文 |
||||
|
* @return |
||||
|
* @throws Exception |
||||
|
*/ |
||||
|
public static String decryptByPrivateKey(String cryptoSrc, String privatekey) throws Exception { |
||||
|
//生成私钥对象
|
||||
|
setPrivateKey(privatekey); |
||||
|
/** 得到Cipher对象对已用公钥加密的数据进行RSA解密 */ |
||||
|
Cipher cipher = Cipher.getInstance(ECB_PKCS1_PADDING); |
||||
|
cipher.init(Cipher.DECRYPT_MODE, privateKey); |
||||
|
BASE64Decoder decoder = new BASE64Decoder(); |
||||
|
byte[] encryptedData = decoder.decodeBuffer(cryptoSrc); |
||||
|
|
||||
|
/** 执行解密操作 */ |
||||
|
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); |
||||
|
} |
||||
|
|
||||
|
/** |
||||
|
* 将给定的公钥字符串转换为公钥对象 |
||||
|
* |
||||
|
* @param publicKeyStr |
||||
|
* @return |
||||
|
* @throws Exception |
||||
|
*/ |
||||
|
private static Key generatePublicKey(String publicKeyStr) throws Exception { |
||||
|
publicKeyString = publicKeyStr; |
||||
|
try { |
||||
|
BASE64Decoder base64Decoder = new BASE64Decoder(); |
||||
|
byte[] buffer = base64Decoder.decodeBuffer(publicKeyStr); |
||||
|
KeyFactory keyFactory = KeyFactory.getInstance(ALGORITHM); |
||||
|
X509EncodedKeySpec keySpec = new X509EncodedKeySpec(buffer); |
||||
|
publicKey = (RSAPublicKey) keyFactory.generatePublic(keySpec); |
||||
|
return publicKey; |
||||
|
} catch (NoSuchAlgorithmException e) { |
||||
|
throw new Exception("无此算法"); |
||||
|
} catch (InvalidKeySpecException e) { |
||||
|
throw new Exception("公钥非法"); |
||||
|
} catch (IOException e) { |
||||
|
throw new Exception("公钥数据内容读取错误"); |
||||
|
} catch (NullPointerException e) { |
||||
|
throw new Exception("公钥数据为空"); |
||||
|
} |
||||
|
} |
||||
|
|
||||
|
/** |
||||
|
* 将给定的私钥字符串转换为私钥对象 |
||||
|
* |
||||
|
* @param privateKeyStr |
||||
|
* @return |
||||
|
* @throws Exception |
||||
|
*/ |
||||
|
private static Key generatePrivateKey(String privateKeyStr) throws Exception { |
||||
|
privateKeyString = privateKeyStr; |
||||
|
try { |
||||
|
BASE64Decoder base64Decoder = new BASE64Decoder(); |
||||
|
byte[] buffer = base64Decoder.decodeBuffer(privateKeyStr); |
||||
|
KeyFactory keyFactory = KeyFactory.getInstance(ALGORITHM); |
||||
|
PKCS8EncodedKeySpec keySpec = new PKCS8EncodedKeySpec(buffer); |
||||
|
privateKey = (RSAPrivateKey) keyFactory.generatePrivate(keySpec); |
||||
|
return privateKey; |
||||
|
} catch (NoSuchAlgorithmException e) { |
||||
|
throw new Exception("无此算法"); |
||||
|
} catch (InvalidKeySpecException e) { |
||||
|
throw new Exception("私钥非法"); |
||||
|
} catch (IOException e) { |
||||
|
throw new Exception("私钥数据内容读取错误"); |
||||
|
} catch (NullPointerException e) { |
||||
|
throw new Exception("私钥数据为空"); |
||||
|
} |
||||
|
} |
||||
|
} |
@ -0,0 +1,170 @@ |
|||||
|
package com.yxt.supervise.gf.api.utils; |
||||
|
|
||||
|
|
||||
|
|
||||
|
import cn.hutool.crypto.SecureUtil; |
||||
|
import cn.hutool.crypto.asymmetric.RSA; |
||||
|
import org.apache.commons.codec.digest.DigestUtils; |
||||
|
import sun.misc.BASE64Encoder; |
||||
|
|
||||
|
import javax.crypto.Cipher; |
||||
|
import javax.crypto.NoSuchPaddingException; |
||||
|
import java.io.ByteArrayOutputStream; |
||||
|
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.Base64; |
||||
|
import java.util.HashMap; |
||||
|
import java.util.Map; |
||||
|
|
||||
|
/** |
||||
|
* @author feikefei |
||||
|
* @create 2023-06-30-8:49 |
||||
|
*/ |
||||
|
public class RsaUtil { |
||||
|
/** |
||||
|
* 指定加密算法 |
||||
|
*/ |
||||
|
private static final String KEY_ALGORITHM = "RSA"; |
||||
|
|
||||
|
/** |
||||
|
* 指定生成多少位的密钥 |
||||
|
*/ |
||||
|
private static final int KEY_BIT = 1024; |
||||
|
|
||||
|
/** *//** |
||||
|
* RSA最大加密明文大小 |
||||
|
*/ |
||||
|
private static final int MAX_ENCRYPT_BLOCK = 117; |
||||
|
|
||||
|
public static final String ECB_PKCS1_PADDING = "RSA/ECB/PKCS1Padding"; |
||||
|
|
||||
|
/** *//** |
||||
|
* RSA最大解密密文大小 |
||||
|
* 1024位密钥的要设置为128, |
||||
|
* 248位密钥的要改成256,不然会报错 |
||||
|
*/ |
||||
|
private static final int MAX_DECRYPT_BLOCK = 128; |
||||
|
|
||||
|
/** |
||||
|
* 生成密钥对 |
||||
|
* @return |
||||
|
* @throws Exception |
||||
|
*/ |
||||
|
public static Map<String,Object> genKeyPair() throws Exception { |
||||
|
KeyPairGenerator keyPairGenerator = KeyPairGenerator.getInstance(KEY_ALGORITHM); |
||||
|
keyPairGenerator.initialize(KEY_BIT); |
||||
|
KeyPair keyPair = keyPairGenerator.generateKeyPair(); |
||||
|
RSAPublicKey rsaPublicKey = (RSAPublicKey) keyPair.getPublic();//生成公钥
|
||||
|
RSAPrivateKey rsaPrivateKey = (RSAPrivateKey) keyPair.getPrivate();//生成密钥
|
||||
|
Map<String,Object> map = new HashMap<>(2); |
||||
|
map.put("publicKey",rsaPublicKey); |
||||
|
map.put("privateKey",rsaPrivateKey); |
||||
|
return map; |
||||
|
} |
||||
|
|
||||
|
/** |
||||
|
* 获取公钥 |
||||
|
* @param map |
||||
|
* @return |
||||
|
*/ |
||||
|
public static String getPublicKey(Map<String,Object> map){ |
||||
|
Key key = (Key) map.get("publicKey"); |
||||
|
String publicKey = Base64.getEncoder().encodeToString(key.getEncoded()); |
||||
|
return publicKey; |
||||
|
} |
||||
|
|
||||
|
/** |
||||
|
* 获取私钥 |
||||
|
* @param map |
||||
|
* @return |
||||
|
*/ |
||||
|
public static String getPrivate(Map<String,Object> map){ |
||||
|
Key key = (Key) map.get("privateKey"); |
||||
|
String privateKey = Base64.getEncoder().encodeToString(key.getEncoded()); |
||||
|
return privateKey; |
||||
|
} |
||||
|
|
||||
|
/** |
||||
|
* 公钥分段加密 --》公钥加密 |
||||
|
* @param data |
||||
|
* @param publicKey |
||||
|
* @return |
||||
|
* @throws Exception |
||||
|
*/ |
||||
|
public static String encrypt(String data, String publicKey) throws Exception { |
||||
|
byte[] bytes = Base64.getDecoder().decode(publicKey);//先把公钥进行解码
|
||||
|
X509EncodedKeySpec x509EncodedKeySpec = new X509EncodedKeySpec(bytes); |
||||
|
KeyFactory keyFactory = KeyFactory.getInstance(KEY_ALGORITHM); |
||||
|
PublicKey publicK = keyFactory.generatePublic(x509EncodedKeySpec); |
||||
|
Cipher cipher = Cipher.getInstance(keyFactory.getAlgorithm()); |
||||
|
cipher.init(Cipher.ENCRYPT_MODE, publicK); |
||||
|
int inputLen = data.getBytes("UTF-8").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(data.getBytes("UTF-8"), offset, MAX_ENCRYPT_BLOCK); |
||||
|
} else { |
||||
|
cache = cipher.doFinal(data.getBytes("UTF-8"), offset, inputLen - offset); |
||||
|
} |
||||
|
out.write(cache, 0, cache.length); |
||||
|
i++; |
||||
|
offset = i * MAX_ENCRYPT_BLOCK; |
||||
|
} |
||||
|
byte[] encryptedData = out.toByteArray(); |
||||
|
out.close(); |
||||
|
// 获取加密内容使用base64进行编码,并以UTF-8为标准转化成字符串
|
||||
|
// 加密后的字符串
|
||||
|
return Base64.getEncoder().encodeToString(encryptedData); |
||||
|
} |
||||
|
|
||||
|
/** |
||||
|
* 私钥分段解密 --》私钥解密 |
||||
|
* @param data |
||||
|
* @param privateKey |
||||
|
* @return |
||||
|
* @throws Exception |
||||
|
*/ |
||||
|
public static String decrypt(String data, String privateKey) throws Exception { |
||||
|
byte[] bytes = Base64.getDecoder().decode(privateKey);//先对私钥进行解码
|
||||
|
PKCS8EncodedKeySpec pkcs8EncodedKeySpec = new PKCS8EncodedKeySpec(bytes); |
||||
|
KeyFactory keyFactory = KeyFactory.getInstance(KEY_ALGORITHM); |
||||
|
PrivateKey privateK = keyFactory.generatePrivate(pkcs8EncodedKeySpec); |
||||
|
|
||||
|
Cipher cipher = Cipher.getInstance(keyFactory.getAlgorithm()); |
||||
|
cipher.init(Cipher.DECRYPT_MODE, privateK); |
||||
|
byte[] dataBytes = Base64.getDecoder().decode(data);//对要解密的数据进行解码
|
||||
|
int inputLen = dataBytes.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(dataBytes, offset, MAX_DECRYPT_BLOCK); |
||||
|
} else { |
||||
|
cache = cipher.doFinal(dataBytes, 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, "UTF-8"); |
||||
|
} |
||||
|
|
||||
|
public static String[] RandomAuthentHeader() { |
||||
|
String timeSpan = String.valueOf(System.currentTimeMillis() / 100); |
||||
|
String[] authentHeaders = new String[] { "sJI8PuGweKztQ6nLgp3dqcwljdKGFjPBD3XyUqCFsVG8rYhAIbD2AkNIKaefbjOB" + timeSpan + SecureUtil.md5("sJI8PuGweKztQ6nLgp3dqcwljdKGFjPBD3XyUqCFsVG8rYhAIbD2AkNIKaefbjOB"), timeSpan }; |
||||
|
return authentHeaders; |
||||
|
} |
||||
|
} |
@ -0,0 +1,44 @@ |
|||||
|
package com.yxt.supervise.gf.api.utils; |
||||
|
|
||||
|
import javax.crypto.Cipher; |
||||
|
import java.nio.charset.StandardCharsets; |
||||
|
import java.security.KeyFactory; |
||||
|
import java.security.PublicKey; |
||||
|
import java.security.spec.X509EncodedKeySpec; |
||||
|
import java.util.Base64; |
||||
|
|
||||
|
/** |
||||
|
* @author feikefei |
||||
|
* @create 2023-06-30-15:54 |
||||
|
*/ |
||||
|
public class ThirdPartyApiClient { |
||||
|
private static final String PUBLIC_KEY_STRING ="MIIBIjANBgkqhkiG9w0BAQEFAAOCAQ8AMIIBCgKCAQEA1Z/+Bslu203UtutmzMW/FqS9bQvaMXQHgvU+ilNR5Hm2IZZalBWPoSbCGzlCUA19C3X6T17X09OMa6aDPKBmF6yXvUI7E/nWM9qUJ5hj4zVO/9GoH03WUyCWSBQg4f+LcnLS75v8nI6moOJ5ILkHmg2KNbEdx55UWjSqBatEopnLWtMFDGZswEbKBH3e2yalK6ddh2kUrtcuQGqFYm/uViAOV+KoptwY2MCNSRLKYE4pCA2BCit7nr3EqNZRqqhKaOE44iyv45zCFrllx3nnwF3X/l+rR4G7Vc7HZwiqKF3RXl9PqmTLJkzhdpAdQX/Kr3SRRHsv/DzKjAZr/2jKrQIDAQAB"; |
||||
|
public static String jiami(String data){ |
||||
|
try { |
||||
|
// 解码RSA公钥字符串为字节数组
|
||||
|
byte[] publicKeyBytes = Base64.getDecoder().decode(PUBLIC_KEY_STRING); |
||||
|
// 根据字节数组创建RSA公钥对象
|
||||
|
X509EncodedKeySpec spec = new X509EncodedKeySpec(publicKeyBytes); |
||||
|
KeyFactory keyFactory = KeyFactory.getInstance("RSA"); |
||||
|
PublicKey publicKey = keyFactory.generatePublic(spec); |
||||
|
// 使用公钥对象对数据进行加密
|
||||
|
String encryptedData = encryptWithRSA(data, publicKey); |
||||
|
return encryptedData; |
||||
|
} catch (Exception e) { |
||||
|
e.printStackTrace(); |
||||
|
} |
||||
|
return null; |
||||
|
} |
||||
|
|
||||
|
private static String encryptWithRSA(String data, PublicKey publicKey) { |
||||
|
try { |
||||
|
Cipher cipher = Cipher.getInstance("RSA"); |
||||
|
cipher.init(Cipher.ENCRYPT_MODE, publicKey); |
||||
|
byte[] encryptedBytes = cipher.doFinal(data.getBytes(StandardCharsets.UTF_8)); |
||||
|
return Base64.getEncoder().encodeToString(encryptedBytes); |
||||
|
} catch (Exception e) { |
||||
|
e.printStackTrace(); |
||||
|
return null; |
||||
|
} |
||||
|
} |
||||
|
} |
@ -0,0 +1,125 @@ |
|||||
|
package com.yxt.supervise.gf.biz.test; |
||||
|
|
||||
|
import cn.hutool.crypto.SecureUtil; |
||||
|
import cn.hutool.http.HttpRequest; |
||||
|
import cn.hutool.http.HttpUtil; |
||||
|
import com.yxt.supervise.gf.api.utils.RSAUtils; |
||||
|
import com.yxt.supervise.gf.api.utils.RsaUtil; |
||||
|
import com.yxt.supervise.gf.api.utils.ThirdPartyApiClient; |
||||
|
import org.apache.http.Header; |
||||
|
import org.apache.http.HttpEntity; |
||||
|
import org.apache.http.HttpResponse; |
||||
|
import org.apache.http.HttpStatus; |
||||
|
import org.apache.http.client.ClientProtocolException; |
||||
|
import org.apache.http.client.methods.CloseableHttpResponse; |
||||
|
import org.apache.http.client.methods.HttpGet; |
||||
|
import org.apache.http.client.methods.HttpHead; |
||||
|
import org.apache.http.client.methods.HttpUriRequest; |
||||
|
import org.apache.http.conn.ssl.SSLConnectionSocketFactory; |
||||
|
import org.apache.http.impl.client.CloseableHttpClient; |
||||
|
import org.apache.http.impl.client.HttpClientBuilder; |
||||
|
import org.apache.http.impl.client.HttpClients; |
||||
|
import org.apache.http.impl.conn.PoolingHttpClientConnectionManager; |
||||
|
import org.apache.http.util.EntityUtils; |
||||
|
import org.springframework.web.bind.annotation.CrossOrigin; |
||||
|
import org.springframework.web.bind.annotation.RequestMapping; |
||||
|
import org.springframework.web.bind.annotation.RestController; |
||||
|
|
||||
|
import javax.net.ssl.SSLContext; |
||||
|
import javax.net.ssl.TrustManager; |
||||
|
import javax.net.ssl.X509TrustManager; |
||||
|
import java.io.BufferedReader; |
||||
|
import java.io.IOException; |
||||
|
import java.io.InputStream; |
||||
|
import java.io.InputStreamReader; |
||||
|
import java.net.HttpURLConnection; |
||||
|
import java.net.URL; |
||||
|
import java.security.Key; |
||||
|
import java.security.KeyManagementException; |
||||
|
import java.security.NoSuchAlgorithmException; |
||||
|
import java.security.PublicKey; |
||||
|
import java.security.cert.X509Certificate; |
||||
|
|
||||
|
/** |
||||
|
* @author feikefei |
||||
|
* @create 2023-06-30-8:50 |
||||
|
*/ |
||||
|
@RestController |
||||
|
@RequestMapping("test") |
||||
|
public class Test { |
||||
|
private static final String publicKey = "MIIBIjANBgkqhkiG9w0BAQEFAAOCAQ8AMIIBCgKCAQEA1Z/+Bslu203UtutmzMW/FqS9bQvaMXQHgvU+ilNR5Hm2IZZalBWPoSbCGzlCUA19C3X6T17X09OMa6aDPKBmF6yXvUI7E/nWM9qUJ5hj4zVO/9GoH03WUyCWSBQg4f+LcnLS75v8nI6moOJ5ILkHmg2KNbEdx55UWjSqBatEopnLWtMFDGZswEbKBH3e2yalK6ddh2kUrtcuQGqFYm/uViAOV+KoptwY2MCNSRLKYE4pCA2BCit7nr3EqNZRqqhKaOE44iyv45zCFrllx3nnwF3X/l+rR4G7Vc7HZwiqKF3RXl9PqmTLJkzhdpAdQX/Kr3SRRHsv/DzKjAZr/2jKrQIDAQAB"; |
||||
|
private static PoolingHttpClientConnectionManager httpClientConnectionManager; |
||||
|
@RequestMapping("/test") |
||||
|
public void test(){ |
||||
|
HttpHead reqHeader = new HttpHead(); |
||||
|
String[] autherHeader = RsaUtil.RandomAuthentHeader(); |
||||
|
String data = autherHeader[0]; |
||||
|
String date = autherHeader[1]; |
||||
|
try { |
||||
|
String encrypt = RsaUtil.encrypt(data, publicKey); |
||||
|
String s = RSAUtils.encryptByPublicKey(data, publicKey); |
||||
|
String jiami = ThirdPartyApiClient.jiami(data); |
||||
|
reqHeader.setHeader("data", jiami); |
||||
|
reqHeader.setHeader("date", date); |
||||
|
final String reqUri = "http://hf-service.tiefaos.com/api/system/apply_material/index"; |
||||
|
String tokenJson = httpGet(reqUri, reqHeader.getAllHeaders()); |
||||
|
HttpRequest header = HttpRequest.get(reqUri).header("data", jiami).header("date", date); |
||||
|
System.out.println(tokenJson); |
||||
|
System.out.println(header); |
||||
|
}catch (Exception e){ |
||||
|
e.printStackTrace(); |
||||
|
} |
||||
|
} |
||||
|
|
||||
|
public static String httpGet(String url, Header[] headers) throws Exception { |
||||
|
HttpUriRequest uriRequest = new HttpGet(url); |
||||
|
if (null != headers) |
||||
|
uriRequest.setHeaders(headers); |
||||
|
CloseableHttpClient httpClient = null; |
||||
|
try { |
||||
|
httpClient = declareHttpClientSSL(url); |
||||
|
CloseableHttpResponse httpresponse = httpClient.execute(uriRequest); |
||||
|
HttpEntity httpEntity = httpresponse.getEntity(); |
||||
|
String result = EntityUtils.toString(httpEntity, "utf-8"); |
||||
|
return result; |
||||
|
} catch (ClientProtocolException e) { |
||||
|
System.out.println(String.format("http请求失败,uri{%s},exception{%s}", new Object[] { url, e })); |
||||
|
} catch (IOException e) { |
||||
|
System.out.println(String.format("IO Exception,uri{%s},exception{%s}", new Object[] { url, e })); |
||||
|
} finally { |
||||
|
if (null != httpClient) |
||||
|
httpClient.close(); |
||||
|
} |
||||
|
return null; |
||||
|
} |
||||
|
|
||||
|
private static CloseableHttpClient declareHttpClientSSL(String url) { |
||||
|
if (url.startsWith("https://")) { |
||||
|
return sslClient(); |
||||
|
} else { |
||||
|
return HttpClientBuilder.create().setConnectionManager(httpClientConnectionManager).build(); |
||||
|
} |
||||
|
} |
||||
|
|
||||
|
private static CloseableHttpClient sslClient() { |
||||
|
try { |
||||
|
SSLContext ctx = SSLContext.getInstance("TLS"); |
||||
|
X509TrustManager tm = new X509TrustManager() { |
||||
|
public X509Certificate[] getAcceptedIssuers() { |
||||
|
return null; |
||||
|
} |
||||
|
public void checkClientTrusted(X509Certificate[] xcs, String str) { |
||||
|
} |
||||
|
public void checkServerTrusted(X509Certificate[] xcs, String str) { |
||||
|
} |
||||
|
}; |
||||
|
ctx.init(null, new TrustManager[] { tm }, null); |
||||
|
SSLConnectionSocketFactory sslConnectionSocketFactory = SSLConnectionSocketFactory.getSocketFactory(); |
||||
|
return HttpClients.custom().setSSLSocketFactory(sslConnectionSocketFactory).build(); |
||||
|
} catch (NoSuchAlgorithmException e) { |
||||
|
throw new RuntimeException(e); |
||||
|
} catch (KeyManagementException e) { |
||||
|
throw new RuntimeException(e); |
||||
|
} |
||||
|
} |
||||
|
} |
Loading…
Reference in new issue