Browse Source

RSA加密

master
liupopo 2 years ago
parent
commit
090eef9702
  1. 18
      yxt-supervise-gf-biz/src/main/java/com/yxt/supervise/gf/shanhai/ShHttp.java
  2. 79
      yxt-supervise-gf-biz/src/test/java/demo/tool/SecurTest.java

18
yxt-supervise-gf-biz/src/main/java/com/yxt/supervise/gf/shanhai/ShHttp.java

@ -1,5 +1,8 @@
package com.yxt.supervise.gf.shanhai;
import cn.hutool.crypto.SecureUtil;
import cn.hutool.crypto.asymmetric.KeyType;
import cn.hutool.crypto.asymmetric.RSA;
import cn.hutool.http.HttpRequest;
import cn.hutool.json.JSONUtil;
import com.sun.istack.NotNull;
@ -65,18 +68,21 @@ public class ShHttp {
private static String headerData(String date) {
String res = "jiq07RIXsX/YwIkXCK4VdhIZcnuFHd+ERekZzMdZT7yxGAyON4s/qtJeJcG9z6uJMtqSUKWTvREc4CNsyuStXacJ2bGgKCzFc" +
"UTwwBUJ7AQ2ybhedpkA7JWqJ1UjooLGUYZfoTduygH2r7LG0qRgigQGD3WdB53WLuC11kT5wAzMnFtbBDHpH78oNjGhvmWpa" +
"R6meDCI2OM0GtCWwBxOagCYt8V5DfD47VyNHYn0xFRva4GFBF6K19061h1cu68eXxfIZj+clMweGlICgeXhktq8Z1T0Xmbah" +
"FjhyTmdXuRxZBeQkLsot00XJHwQ48B6Z/VdOPpqoD95NTngRdV3+A=="; // 测试数据
// String res = "jiq07RIXsX/YwIkXCK4VdhIZcnuFHd+ERekZzMdZT7yxGAyON4s/qtJeJcG9z6uJMtqSUKWTvREc4CNsyuStXacJ2bGgKCzFc" +
// "UTwwBUJ7AQ2ybhedpkA7JWqJ1UjooLGUYZfoTduygH2r7LG0qRgigQGD3WdB53WLuC11kT5wAzMnFtbBDHpH78oNjGhvmWpa" +
// "R6meDCI2OM0GtCWwBxOagCYt8V5DfD47VyNHYn0xFRva4GFBF6K19061h1cu68eXxfIZj+clMweGlICgeXhktq8Z1T0Xmbah" +
// "FjhyTmdXuRxZBeQkLsot00XJHwQ48B6Z/VdOPpqoD95NTngRdV3+A=="; // 测试数据
String keyMd5 = SecureUtil.md5(key);
String val = key + date + keyMd5;
RSA rsa = new RSA(null, publicKey);
String res = rsa.encryptBase64(val, KeyType.PublicKey);
return res;
}
private static String headerDate() {
String res = "" + (System.currentTimeMillis() / 1000);
res = "1683516744"; // 测试数据
// res = "1683516744"; // 测试数据
return res;
}
}

79
yxt-supervise-gf-biz/src/test/java/demo/tool/SecurTest.java

@ -5,6 +5,7 @@ import cn.hutool.core.date.DateUtil;
import cn.hutool.core.util.CharsetUtil;
import cn.hutool.core.util.StrUtil;
import cn.hutool.crypto.SecureUtil;
import cn.hutool.crypto.asymmetric.AsymmetricCrypto;
import cn.hutool.crypto.asymmetric.KeyType;
import cn.hutool.crypto.asymmetric.RSA;
import cn.hutool.http.HttpRequest;
@ -12,13 +13,20 @@ import com.yxt.supervise.gf.api.utils.RsaUtil;
import com.yxt.supervise.gf.shanhai.ShRequester;
import com.yxt.supervise.gf.shanhai.resp.BaseResponse;
import org.junit.Test;
import sun.misc.BASE64Decoder;
import java.security.KeyFactory;
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.Date;
public class SecurTest {
// @Test
public void testDate() {
public void testDate() throws Exception {
long l = System.currentTimeMillis();
// System.out.println("" + l);
// System.out.println(DateUtil.format(new Date(),"yyyyMMddHHmmss"));
@ -39,24 +47,32 @@ public class SecurTest {
String publicKeyBase64 = Base64.encode(publicKey);
RSA rsa = new RSA(null, publicKey);
String s = rsa.encryptBase64(data1, KeyType.PublicKey);
System.out.println(s);
// String data = SecureUtil.rsa(privateKeyBase64, publicKeyBase64).encryptBase64(data1, KeyType.PublicKey);
byte[] encrypt = rsa.encrypt(StrUtil.bytes(data1, CharsetUtil.CHARSET_UTF_8), KeyType.PublicKey);
String data = StrUtil.str(encrypt, CharsetUtil.CHARSET_UTF_8);
System.out.println(data);
// byte[] encrypt = rsa.encrypt(StrUtil.bytes(data1, CharsetUtil.CHARSET_UTF_8), KeyType.PublicKey);
// String data = StrUtil.str(encrypt, CharsetUtil.CHARSET_UTF_8);
// System.out.println(data);
// AsymmetricCrypto asymmetricCrypto = new AsymmetricCrypto("RSA", null,
// getRSAPublidKeyBybase64(publicKey));
//
// String s1 = asymmetricCrypto.encryptBase64(data1, KeyType.PublicKey);
// System.out.println(s1);
// String body = HttpRequest.get("http://hf-service.tiefaos.com/api/system/apply_material/index")
// String data = "jiq07RIXsX/YwIkXCK4VdhIZcnuFHd+ERekZzMdZT7yxGAyON4s/qtJeJcG9z6uJMtqSUKWTvREc4CNsyuStXacJ2bGgKCzFcUTwwBUJ7AQ2ybhedpkA7JWqJ1UjooLGUYZfoTduygH2r7LG0qRgigQGD3WdB53WLuC11kT5wAzMnFtbBDHpH78oNjGhvmWpaR6meDCI2OM0GtCWwBxOagCYt8V5DfD47VyNHYn0xFRva4GFBF6K19061h1cu68eXxfIZj+clMweGlICgeXhktq8Z1T0XmbahFjhyTmdXuRxZBeQkLsot00XJHwQ48B6Z/VdOPpqoD95NTngRdV3+A==";
// String date = "1683516744";
String body = HttpRequest.get("http://hf.lx-obj.top/api/system/apply_material/index")
.header("data", data)
.header("data", s)
.header("date", date)
.execute()
.body();
System.out.println(body);
}
@Test
// @Test
public void testRequester() {
BaseResponse api$system$applyMaterial$index = ShRequester.getApi$system$apply_material$index(null);
System.out.println(api$system$applyMaterial$index);
@ -64,4 +80,55 @@ public class SecurTest {
// String api$system$applyMaterial$getActualOrderMaterial = ShRequester.getApi$system$apply_material$get_actual_order_material("10888");
// System.out.println(api$system$applyMaterial$getActualOrderMaterial);
}
/**
* @param
* @return
* @desc: 将字符串转换成RSAPublicKey类型
* @date 2020-6-12 11:03:05
*/
public static RSAPublicKey getRSAPublidKeyBybase64(String base64s) throws Exception {
X509EncodedKeySpec keySpec = new X509EncodedKeySpec((new BASE64Decoder()).decodeBuffer(base64s));
RSAPublicKey publicKey = null;
KeyFactory keyFactory = KeyFactory.getInstance("RSA");
try {
publicKey = (RSAPublicKey) keyFactory.generatePublic(keySpec);
} catch (InvalidKeySpecException e) {
e.printStackTrace();
}
return publicKey;
}
/**
* @param
* @return
* @desc: 将字符串转换成RSAPrivateKey类型
* @date 2020-6-12 11:03:01
*/
public static RSAPrivateKey getRSAPrivateKeyBybase64(String base64s) throws Exception {
PKCS8EncodedKeySpec keySpec = new PKCS8EncodedKeySpec((new BASE64Decoder()).decodeBuffer(base64s));
RSAPrivateKey privateKey = null;
KeyFactory keyFactory = KeyFactory.getInstance("RSA");
try {
privateKey = (RSAPrivateKey) keyFactory.generatePrivate(keySpec);
} catch (InvalidKeySpecException var4) {
var4.printStackTrace();
}
return privateKey;
}
// @Test
public void testXxx() throws Exception {
AsymmetricCrypto asymmetricCrypto = new AsymmetricCrypto("RSA", getRSAPrivateKeyBybase64("私钥解密"),
getRSAPublidKeyBybase64("公钥加密"));
String s1 = asymmetricCrypto.encryptBase64("你好呀", KeyType.PublicKey);
System.out.println(s1);
String s = asymmetricCrypto.decryptStr(s1, KeyType.PrivateKey);
System.out.println(s);
}
}

Loading…
Cancel
Save