提交 4310116f 编写于 作者: rushui.chen's avatar rushui.chen

20191021 加解密逻辑处理

上级 856efd9b
流水线 #16076 已失败 于阶段
in 0 second
......@@ -79,12 +79,6 @@
<artifactId>spring-cloud-starter-stream-rabbit</artifactId>
</dependency>
<dependency>
<groupId>com.pica.cloud.base.doctor</groupId>
<artifactId>pica-cloud-doctor-client</artifactId>
<version>1.1.0.1-SNAPSHOT</version>
</dependency>
<dependency>
<groupId>com.pica.cloud.foundation</groupId>
<artifactId>pica-cloud-utils</artifactId>
......
......@@ -75,6 +75,10 @@ public class Test {
System.out.println(encryptEntity1Json);
}
......
......@@ -77,7 +77,7 @@ public class RegisterController extends AccountBaseController {
@ApiOperation("OCIN项目")
@PostMapping(value = "/ocin")
public PicaResponse register(@RequestBody OCINRequest ocinRequest) {
PicaResponse picaResponse = registerService.ocinRegister(ocinRequest);
return picaResponse;
registerService.ocinRegister(ocinRequest);
return PicaResponse.toResponse();
}
}
......@@ -78,5 +78,5 @@ public interface DoctorMapper {
* @param mobile
* @return
*/
Long selectDoctorIdByMobile(String mobile);
//Long selectDoctorIdByMobile(String mobile);
}
\ No newline at end of file
......@@ -22,7 +22,7 @@ public interface RegisterService {
*
* @param request
*/
PicaResponse ocinRegister(OCINRequest request);
void ocinRegister(OCINRequest request);
}
package com.pica.cloud.account.account.server.service.impl;
import com.pica.cloud.account.account.common.req.OCINRequest;
import com.pica.cloud.account.account.server.entity.*;
import com.pica.cloud.account.account.server.enums.AccountAgreementEnum;
import com.pica.cloud.account.account.server.enums.AccountExceptionEnum;
......@@ -10,15 +11,11 @@ import com.pica.cloud.account.account.server.log.AccountLogUtils;
import com.pica.cloud.account.account.server.mapper.*;
import com.pica.cloud.account.account.server.queue.QueueProducer;
import com.pica.cloud.account.account.server.req.BaseRequest;
import com.pica.cloud.account.account.common.req.OCINRequest;
import com.pica.cloud.account.account.server.service.RegisterService;
import com.pica.cloud.account.account.server.util.AESUtil;
import com.pica.cloud.account.account.server.util.AccountUtils;
import com.pica.cloud.account.account.server.util.ExecutorServiceUtils;
import com.pica.cloud.account.account.server.util.TokenUtils;
import com.pica.cloud.base.doctor.doctor.client.DoctorServiceClient;
import com.pica.cloud.base.doctor.doctor.common.req.PrefectInfoReq;
import com.pica.cloud.foundation.entity.PicaResponse;
import org.apache.commons.lang3.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
......@@ -63,9 +60,6 @@ public class RegisterServiceImpl implements RegisterService {
@Autowired
private AgreementLogEntityMapper agreementLogEntityMapper;
@Autowired
private DoctorServiceClient doctorServiceClient;
@Autowired
private DoctorMapper doctorMapper;
......@@ -166,7 +160,7 @@ public class RegisterServiceImpl implements RegisterService {
* @param request
*/
@Override
public PicaResponse ocinRegister(OCINRequest request) {
public void ocinRegister(OCINRequest request) {
String mobile = request.getMobile();
AccountInfoEntity accountInfoEntity = accountInfoDetailMapper.selectByMobile(mobile);
if (accountInfoEntity == null) {
......@@ -203,15 +197,14 @@ public class RegisterServiceImpl implements RegisterService {
accountMapper.insertSelective(account);
Long userId = accountUtils.getUserIdByAcctId(productType, acctId);
processAgreement(userId);
PrefectInfoReq prefectInfoReq = new PrefectInfoReq();
/*PrefectInfoReq prefectInfoReq = new PrefectInfoReq();
prefectInfoReq.setDoctorId(userId);
prefectInfoReq.setHospitalId(request.getHospitalId().longValue());
prefectInfoReq.setName(request.getName());
doctorServiceClient.prefectInfo(prefectInfoReq);
return PicaResponse.toResponse();
*/
} else {
Long id = doctorMapper.selectDoctorIdByMobile(mobile);
return PicaResponse.toResponse(id, AccountExceptionEnum.PICA_ALREADY_REGISTER.getCode(), AccountExceptionEnum.PICA_ALREADY_REGISTER.getCode());
throw new AccountException(AccountExceptionEnum.PICA_ALREADY_REGISTER.getCode(), AccountExceptionEnum.PICA_ALREADY_REGISTER.getCode());
}
}
......
package com.pica.cloud.account.account.server.util;
import com.pica.cloud.foundation.utils.utils.EncryptCreateUtil;
import org.apache.commons.codec.binary.Base64;
import org.apache.commons.lang.StringUtils;
import sun.misc.BASE64Decoder;
import sun.misc.BASE64Encoder;
import javax.crypto.Cipher;
import javax.crypto.KeyGenerator;
import javax.crypto.spec.SecretKeySpec;
import java.security.Key;
import java.math.BigInteger;
/**
* AES加解密工具类
* Created on 2019/10/21 13:20
* author:crs
* Description:AESUtil
*/
public class AESUtil {
//密钥 (需要前端和后端保持一致)
private static final String KEY = "abcdefgabcdefg12";
//算法
private static final String ALGORITHMSTR = "AES/ECB/PKCS5Padding";
/**
* 指定加密算法为AES
* aes解密
* @param encrypt 内容
* @return
* @throws Exception
public static String aesDecrypt(String encrypt) {
try {
return aesDecrypt(encrypt, KEY);
} catch (Exception e) {
e.printStackTrace();
return "";
}
}
*/
private static final String ALGORITHM = "AES";
/**
* UUID随机密钥:必须长度为16
*
* aes加密
* @param content
* @return
* @throws Exception
public static String aesEncrypt(String content) {
try {
return aesEncrypt(content, KEY);
} catch (Exception e) {
e.printStackTrace();
return "";
}
}
*/
/**
* 将byte[]转为各种进制的字符串
* @param bytes byte[]
* @param radix 可以转换进制的范围,从Character.MIN_RADIX到Character.MAX_RADIX,超出范围后变为10进制
* @return 转换后的字符串
*/
public static String generateKeyString() {
//return UUID.randomUUID().toString().replaceAll("-", "").substring(0, 16);
return "276e35e6f91d4ec1";
public static String binary(byte[] bytes, int radix){
return new BigInteger(1, bytes).toString(radix);// 这里的1代表正数
}
/**
* 根据密钥和算法生成Key
*
* @return
* base 64 encode
* @param bytes 待编码的byte[]
* @return 编码后的base 64 code
*/
public static String base64Encode(byte[] bytes){
return Base64.encodeBase64String(bytes);
}
/**
* base 64 decode
* @param base64Code 待解码的base 64 code
* @return 解码后的byte[]
* @throws Exception
*/
private static Key generateKey(String keyString) throws Exception {
Key key = new SecretKeySpec(keyString.getBytes(), ALGORITHM);
return key;
public static byte[] base64Decode(String base64Code) throws Exception{
return StringUtils.isEmpty(base64Code) ? null : new BASE64Decoder().decodeBuffer(base64Code);
}
/**
* 旧的加密逻辑处理
*
* @param data 待加密的数据
* @return
* AES加密
* @param content 待加密的内容
* @param encryptKey 加密密钥
* @return 加密后的byte[]
* @throws Exception
*/
public static String encryptV0(String data) {
//EncryptUtils.encryptContent(mobilePhone, EncryptConstants.ENCRYPT_TYPE_MOBILE)
return EncryptCreateUtil.encrypt(data);
public static byte[] aesEncryptToBytes(String content, String encryptKey) throws Exception {
KeyGenerator kgen = KeyGenerator.getInstance("AES");
kgen.init(128);
Cipher cipher = Cipher.getInstance(ALGORITHMSTR);
cipher.init(Cipher.ENCRYPT_MODE, new SecretKeySpec(encryptKey.getBytes(), "AES"));
return cipher.doFinal(content.getBytes("utf-8"));
}
/**
* 旧的解密逻辑处理
*
* @param data 待解密的数据
* @return
* AES加密为base 64 code
* @param content 待加密的内容
* @param encryptKey 加密密钥
* @return 加密后的base 64 code
* @throws Exception
*/
public static String decryptV0(String data) {
return EncryptCreateUtil.dencrypt(data);
public static String aesEncrypt(String content, String encryptKey) throws Exception {
return base64Encode(aesEncryptToBytes(content, encryptKey));
}
/**
* 用来进行加密的操作
*
* @param data
* @return
* AES解密
* @param encryptBytes 待解密的byte[]
* @param decryptKey 解密密钥
* @return 解密后的String
* @throws Exception
*/
public static String encrypt(String keyString, String data)
throws Exception {
Key key = generateKey(keyString);
Cipher c = Cipher.getInstance(ALGORITHM);
c.init(Cipher.ENCRYPT_MODE, key);
byte[] encVal = c.doFinal(data.getBytes());
String encryptedValue = new BASE64Encoder().encode(encVal);
return encryptedValue;
public static String aesDecryptByBytes(byte[] encryptBytes, String decryptKey) throws Exception {
KeyGenerator kgen = KeyGenerator.getInstance("AES");
kgen.init(128);
Cipher cipher = Cipher.getInstance(ALGORITHMSTR);
cipher.init(Cipher.DECRYPT_MODE, new SecretKeySpec(decryptKey.getBytes(), "AES"));
byte[] decryptBytes = cipher.doFinal(encryptBytes);
return new String(decryptBytes);
}
/**
* 用来进行解密的操作
*
* @param encryptedData
* @return
* 将base 64 code AES解密
* @param encryptStr 待解密的base 64 code
* @param decryptKey 解密密钥
* @return 解密后的string
* @throws Exception
*/
public static String decrypt(String keyString, String encryptedData) throws Exception {
Key key = generateKey(keyString);
Cipher c = Cipher.getInstance(ALGORITHM);
c.init(Cipher.DECRYPT_MODE, key);
byte[] decordedValue = new BASE64Decoder().decodeBuffer(encryptedData);
byte[] decValue = c.doFinal(decordedValue);
String decryptedValue = new String(decValue);
return decryptedValue;
public static String aesDecrypt(String encryptStr, String decryptKey) throws Exception {
return StringUtils.isEmpty(encryptStr) ? null : aesDecryptByBytes(base64Decode(encryptStr), decryptKey);
}
public static void main(String[] args) throws Exception {
System.out.println(generateKeyString());
String keyString = generateKeyString();
//String keyString = "1234567890123456";
System.out.println("密钥:" + keyString);
String source = "恭喜发财!";// 要加密的字符串
System.out.println("准备用密钥加密的字符串为:" + source);
/**
* 旧的加解密逻辑
* @param data
* @return
*/
public static String encryptV0(String data) {
return EncryptCreateUtil.encrypt(data);
}
String cryptograph = encrypt(keyString, source);// 生成的密文
System.out.print("用密钥加密后的结果为:" + cryptograph);
System.out.println();
public static String decryptV0(String data) {
return EncryptCreateUtil.dencrypt(data);
}
String target = decrypt(keyString, cryptograph);// 解密密文
System.out.println("用密钥解密后的字符串为:" + target);
System.out.println();
/**
* 测试
*/
public static void main(String[] args) throws Exception {
String KEY="zJJ$c5md3$yuuhWW";
String content = "12322222222";
System.out.println("加密前:" + content);
System.out.println("加密密钥和解密密钥:" + KEY);
String encrypt = aesEncrypt(content, KEY);
System.out.println("加密后:" + encrypt);
String decrypt = aesDecrypt(encrypt, KEY);
System.out.println("解密后:" + decrypt);
}
}
......@@ -18,19 +18,19 @@ public class CryptoUtil {
* @return
* @throws Exception
*/
public static EncryptEntity encrypt(String data) throws Exception {
//1、产生AES密钥
String keyString = AESUtil.generateKeyString();
//2、用AES法加密数据
String content = AESUtil.encrypt(keyString, data);
//3、用RSA加密AES密钥
String finalKey = RSAUtil.encrypt(keyString);
EncryptEntity encryptEntity = new EncryptEntity();
encryptEntity.setContent(content);
// TODO: 2019/8/27 :获取验证码接口
// encryptEntity.setKey(finalKey);
return encryptEntity;
}
// public static EncryptEntity encrypt(String data) throws Exception {
// //1、产生AES密钥
// String keyString = AESUtil.generateKeyString();
// //2、用AES法加密数据
// String content = AESUtil.encrypt(keyString, data);
// //3、用RSA加密AES密钥
// String finalKey = RSAUtil.encrypt(keyString);
// EncryptEntity encryptEntity = new EncryptEntity();
// encryptEntity.setContent(content);
// // TODO: 2019/8/27 :获取验证码接口
//// encryptEntity.setKey(finalKey);
// return encryptEntity;
// }
/**
* 解密数据
......@@ -39,13 +39,13 @@ public class CryptoUtil {
* @param data
* @return
*/
public static String decrypt(String key, String data) throws Exception {
//获取解密密钥
String decryptKey = RSAUtil.decrypt(key);
//解密数据
String content = AESUtil.decrypt(decryptKey, data);
return content;
}
// public static String decrypt(String key, String data) throws Exception {
// //获取解密密钥
// String decryptKey = RSAUtil.decrypt(key);
// //解密数据
// String content = AESUtil.decrypt(decryptKey, data);
// return content;
// }
/**
......@@ -56,14 +56,17 @@ public class CryptoUtil {
* @throws Exception
*/
public static <T> T decrypt(EncryptEntity encryptEntity, Class<T> zClass) throws Exception {
// TODO: 2019/8/27
//获取解密密钥
//String decryptKey = RSAUtil.decrypt(encryptEntity.getKey());
//解密数据
//String content = AESUtil.decrypt(decryptKey, encryptEntity.getContent());
//反序列化成对象
// String decryptKey = RSAUtil.decrypt(encryptEntity.getKey());
// //解密数据
// String content = AESUtil.aesDecrypt(encryptEntity.getContent(),decryptKey);
// //反序列化成对象
// T request = JSONObject.parseObject(content, zClass);
// return request;
T request = JSONObject.parseObject(encryptEntity.getContent(), zClass);
return request;
}
}
Markdown 格式
0% or
您添加了 0 到此讨论。请谨慎行事。
先完成此消息的编辑!
想要评论请 注册