parent
c7ecad94e4
commit
b2b6040984
@ -1,182 +0,0 @@
|
||||
/**
|
||||
* Copyright 2020 OPSLI 快速开发平台 https://www.opsli.com
|
||||
* <p>
|
||||
* Licensed under the Apache License, Version 2.0 (the "License"); you may not
|
||||
* use this file except in compliance with the License. You may obtain a copy of
|
||||
* the License at
|
||||
* <p>
|
||||
* http://www.apache.org/licenses/LICENSE-2.0
|
||||
* <p>
|
||||
* Unless required by applicable law or agreed to in writing, software
|
||||
* distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
|
||||
* WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
|
||||
* License for the specific language governing permissions and limitations under
|
||||
* the License.
|
||||
*/
|
||||
package org.opsli.core.utils;
|
||||
|
||||
|
||||
import cn.hutool.core.util.CharsetUtil;
|
||||
import cn.hutool.core.util.StrUtil;
|
||||
import cn.hutool.crypto.asymmetric.KeyType;
|
||||
import cn.hutool.crypto.asymmetric.RSA;
|
||||
import com.alibaba.fastjson.JSONObject;
|
||||
import org.opsli.common.exception.ServiceException;
|
||||
import org.opsli.common.utils.Props;
|
||||
import org.opsli.core.msg.CoreMsg;
|
||||
|
||||
import java.util.Collection;
|
||||
|
||||
/**
|
||||
* 非对称加密 RSA 工具类
|
||||
*
|
||||
* @author 周鹏程
|
||||
*/
|
||||
public enum EncryptAndDecryptByRsaUtil {
|
||||
|
||||
/** 默认实例 */
|
||||
INSTANCE();
|
||||
|
||||
/** RSA KEY */
|
||||
private final String rsaKey = "data";
|
||||
|
||||
/** RSA对象 */
|
||||
private final RSA rsa;
|
||||
|
||||
EncryptAndDecryptByRsaUtil(){
|
||||
this.rsa = this.createRsa();
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* 生成 RSA 对象
|
||||
* @return RSA
|
||||
*/
|
||||
public RSA createRsa(){
|
||||
return new RSA();
|
||||
}
|
||||
|
||||
/**
|
||||
* 生成 RSA 对象
|
||||
* @param publicKey 公钥
|
||||
* @param privateKey 私钥
|
||||
* @return RSA
|
||||
*/
|
||||
public RSA createRsa(String publicKey, String privateKey){
|
||||
return new RSA(privateKey, publicKey);
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* 获得公钥
|
||||
* @return String
|
||||
*/
|
||||
public String getPublicKey(){
|
||||
return rsa.getPublicKeyBase64();
|
||||
}
|
||||
|
||||
/**
|
||||
* 获得私钥
|
||||
* @return String
|
||||
*/
|
||||
public String getPrivateKey(){
|
||||
return rsa.getPrivateKeyBase64();
|
||||
}
|
||||
|
||||
/**
|
||||
* RSA 加密数据
|
||||
* @param data 数据
|
||||
* @return String
|
||||
*/
|
||||
public String encryptedData(Object data){
|
||||
return this.encryptedData(this.rsa, data);
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* RSA 加密数据
|
||||
* @param rsa RSA
|
||||
* @param data 数据
|
||||
* @return String
|
||||
*/
|
||||
public String encryptedData(RSA rsa, Object data){
|
||||
String encryptedStr;
|
||||
try {
|
||||
JSONObject jsonObject = new JSONObject();
|
||||
jsonObject.put(rsaKey, data);
|
||||
encryptedStr = rsa.encryptBase64(StrUtil.bytes(jsonObject.toString(), CharsetUtil.CHARSET_UTF_8), KeyType.PublicKey);
|
||||
}catch (Exception e){
|
||||
// 加密失败
|
||||
throw new ServiceException(CoreMsg.OTHER_EXCEPTION_RSA_EN);
|
||||
}
|
||||
return encryptedStr;
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* RSA 解密数据
|
||||
* @param data 数据
|
||||
* @return Object
|
||||
*/
|
||||
public Object decryptedDataToObj(String data){
|
||||
return this.decryptedDataToObj(this.rsa, data);
|
||||
}
|
||||
|
||||
/**
|
||||
* RSA 解密数据
|
||||
* @param rsa RSA
|
||||
* @param data 数据
|
||||
* @return Object
|
||||
*/
|
||||
public Object decryptedDataToObj(RSA rsa, String data){
|
||||
Object obj;
|
||||
String decryptedData = this.decryptedData(rsa, data);
|
||||
try{
|
||||
obj = JSONObject.parse(decryptedData);
|
||||
}catch (Exception e){
|
||||
// RSA非对称解密反射失败
|
||||
throw new ServiceException(CoreMsg.OTHER_EXCEPTION_RSA_REFLEX);
|
||||
}
|
||||
return obj;
|
||||
}
|
||||
|
||||
|
||||
|
||||
/**
|
||||
* RSA 解密数据
|
||||
* @param data
|
||||
* @return String
|
||||
*/
|
||||
public String decryptedData(String data){
|
||||
return this.decryptedData(this.rsa, data);
|
||||
}
|
||||
|
||||
|
||||
|
||||
/**
|
||||
* RSA 解密数据
|
||||
* @param rsa RSA
|
||||
* @param data 数据
|
||||
* @return String
|
||||
*/
|
||||
public String decryptedData(RSA rsa,String data){
|
||||
//解密,因为编码传值时有空格出现
|
||||
String decryptStr;
|
||||
try{
|
||||
data = data.replaceAll(" ", "+");
|
||||
String tmp = rsa.decryptStr(data, KeyType.PrivateKey);
|
||||
JSONObject jsonObject = JSONObject.parseObject(tmp);
|
||||
Object obj = jsonObject.get(rsaKey);
|
||||
if(obj instanceof Collection){
|
||||
decryptStr = jsonObject.getJSONArray(rsaKey).toJSONString();
|
||||
}else{
|
||||
decryptStr = jsonObject.getJSONObject(rsaKey).toJSONString();
|
||||
}
|
||||
}catch (Exception e){
|
||||
// 解密失败
|
||||
throw new ServiceException(CoreMsg.OTHER_EXCEPTION_RSA_DE);
|
||||
}
|
||||
return decryptStr;
|
||||
}
|
||||
|
||||
}
|
@ -1,26 +0,0 @@
|
||||
import cn.hutool.crypto.SecureUtil;
|
||||
import cn.hutool.crypto.SmUtil;
|
||||
import cn.hutool.crypto.asymmetric.SM2;
|
||||
import org.junit.Test;
|
||||
|
||||
/**
|
||||
* @author 周鹏程
|
||||
* @date 2021-01-31 5:15 下午
|
||||
**/
|
||||
public class EncryptTest {
|
||||
|
||||
@Test
|
||||
public void testEncrypt(){
|
||||
|
||||
|
||||
SM2 sm2 = SmUtil.sm2("MIICdgIBADANBgkqhkiG9w0BAQEFAASCAmAwggJcAgEAAoGBAJqTLKryt+SYq8oHZ/PeFR64Pq2A9qsKZmZFzU6gvkQ37JeCFCYMitxmF7AtlktSktB3dPdxHEppbfSdY1+i140eAqMZga7j4FmjIdnyW1OauiVgaimJLAEyE7uTAAPggrU9kdqgXbA8hIffm5tu6DKRq/t1NpYEO42S/IGK/yoxAgMBAAECgYEAiWu+klwm0LxKPdpHuK7/58e1MVst8PHWB6aW2AhgHxX46NlkQE92RGsfNCnTLDPFAkCxZCrTE/SXJJmn9yY2qoS26OV0PbTGajk96M8lDi9JSmWCNV1eywPecObSyvtPd5jaPtq2jkgNY/hHJjH6kV7UAFZuaSK7jxskfq7uR2ECQQDPfmGjPiMc65+LE9U7jC4LokyUi1yCgN6AY5MgF6fkxUVJD2mtl9BqRK7qE0OnsRb0NzID3PSfa7aA2I0Rlsj/AkEAvrXUBQ6hfuEwD1896qpSJUr7tLidby/3jYwSoewuydDT2duDc2ZCz4/U/1NpxSxWT10ZZi2ExsFZn/3PDylczwJARA3oijkcHSUu69eybVh51bkCswnOasNHtwZxv+niWEdXhTH38EbFxcUHNaDh5MNRiwH7dobm+M7EShg8lJNHEwJAclRdU97OkFr9zeliHCGZd4P5XAFlWHfgJ7p2nR4Teqe3qZ6Aspj2qqpmnd7qxOrsn02H4YqeU+0sBs9I56T7XwJAAg8wHrh/FAPY96mAya0bpv6zm/7bave17vs+8B+fhBEHHuvetfv8Xi/RkXL0rjE4LaTHefoUbZPNbIhNYiN0CQ==",
|
||||
"MIGfMA0GCSqGSIb3DQEBAQUAA4GNADCBiQKBgQCakyyq8rfkmKvKB2fz3hUeuD6tgParCmZmRc1OoL5EN+yXghQmDIrcZhewLZZLUpLQd3T3cRxKaW30nWNfoteNHgKjGYGu4+BZoyHZ8ltTmrolYGopiSwBMhO7kwAD4IK1PZHaoF2wPISH35ubbugykav7dTaWBDuNkvyBiv8qMQIDAQAB"
|
||||
|
||||
);
|
||||
|
||||
System.out.println(sm2.getPublicKeyBase64());
|
||||
System.out.println(sm2.getPrivateKeyBase64());
|
||||
|
||||
|
||||
}
|
||||
}
|
@ -1,79 +0,0 @@
|
||||
import cn.hutool.core.convert.Convert;
|
||||
import com.alibaba.fastjson.JSONObject;
|
||||
import com.google.common.collect.Maps;
|
||||
import lombok.Data;
|
||||
import org.junit.Test;
|
||||
import org.opsli.core.utils.EncryptAndDecryptByRsaUtil;
|
||||
|
||||
import java.util.ArrayList;
|
||||
import java.util.List;
|
||||
import java.util.Map;
|
||||
|
||||
/**
|
||||
* Rsa 加密认证
|
||||
*/
|
||||
public class RsaTest {
|
||||
|
||||
|
||||
/**
|
||||
* 加密数据
|
||||
*/
|
||||
@Test
|
||||
public void getPublicKey(){
|
||||
System.out.println(EncryptAndDecryptByRsaUtil.INSTANCE.getPublicKey());
|
||||
|
||||
}
|
||||
|
||||
/**
|
||||
* 加密数据
|
||||
*/
|
||||
@Test
|
||||
public void test(){
|
||||
Map<String, Object> map = Maps.newHashMap();
|
||||
map.put("test1", 123);
|
||||
map.put("test2", "aaa");
|
||||
|
||||
// Test1 t1 = new Test1();
|
||||
// t1.setId( "123");
|
||||
// t1.setName( "张三");
|
||||
// t1.setAge( 16);
|
||||
|
||||
List<Integer> list = new ArrayList<>();
|
||||
list.add(1);
|
||||
list.add(2);
|
||||
list.add(111);
|
||||
|
||||
Object parse = JSONObject.parse("{\"username\":\"demo\",\"password\":\"Aa123456\",\"captcha\":\"\",\"uuid\":\"0d3eea43edf19e4ed0e88aae8d56878046a5\"}");
|
||||
|
||||
// 加密
|
||||
String encryptedData = EncryptAndDecryptByRsaUtil.INSTANCE.encryptedData(list);
|
||||
System.out.println(encryptedData);
|
||||
|
||||
// 解密
|
||||
String decryptedData = EncryptAndDecryptByRsaUtil.INSTANCE.decryptedData(encryptedData);
|
||||
Object decryptedDataToObj = EncryptAndDecryptByRsaUtil.INSTANCE.decryptedDataToObj(encryptedData);
|
||||
System.out.println(decryptedData);
|
||||
|
||||
// 解密
|
||||
List<Integer> integers = Convert.toList(Integer.class, decryptedDataToObj);
|
||||
//Map<String, Object> stringObjectMap = Convert.toMap(String.class, Object.class, decryptedData);
|
||||
//Map<String, Object> stringObjectMap = Convert.toMap(String.class, Object.class, decryptedDataToObj);
|
||||
|
||||
System.out.println(123);
|
||||
|
||||
}
|
||||
|
||||
|
||||
|
||||
}
|
||||
|
||||
@Data
|
||||
class Test1 {
|
||||
|
||||
private String id;
|
||||
|
||||
private String name;
|
||||
|
||||
private Integer age;
|
||||
|
||||
}
|
Loading…
Reference in new issue