12 changed files with 220 additions and 3 deletions
@ -0,0 +1,107 @@ |
|||
package com.epmet.commons.tools.utils; |
|||
|
|||
import org.apache.commons.codec.binary.Base64; |
|||
import org.apache.commons.logging.Log; |
|||
import org.apache.commons.logging.LogFactory; |
|||
|
|||
import javax.crypto.Cipher; |
|||
import javax.crypto.spec.IvParameterSpec; |
|||
import javax.crypto.spec.SecretKeySpec; |
|||
|
|||
public class AESCBCUtils { |
|||
|
|||
public static final Log logger = LogFactory.getLog(AESCBCUtils.class); |
|||
public static final String KEY_ALGORITHM = "AES"; |
|||
// 加密模式为ECB,填充模式为NoPadding
|
|||
public static final String CIPHER_ALGORITHM = "AES/CBC/NoPadding"; |
|||
// 字符集
|
|||
public static final String ENCODING = "UTF-8"; |
|||
// 向量
|
|||
public static final String IV_SEED = "1234567812345678"; |
|||
|
|||
/** |
|||
* AES加密算法 |
|||
* |
|||
* @param str 密文 |
|||
* @param key 密key |
|||
* @return |
|||
*/ |
|||
public static String encrypt(String str, String key) { |
|||
try { |
|||
if (str == null) { |
|||
logger.error("AES加密出错:Key为空null"); |
|||
return null; |
|||
} |
|||
// 判断Key是否为16位
|
|||
if (key.length() != 16) { |
|||
logger.error("AES加密出错:Key长度不是16位"); |
|||
return null; |
|||
} |
|||
byte[] raw = key.getBytes(ENCODING); |
|||
SecretKeySpec skeySpec = new SecretKeySpec(raw, KEY_ALGORITHM); |
|||
Cipher cipher = Cipher.getInstance(CIPHER_ALGORITHM); |
|||
IvParameterSpec iv = new IvParameterSpec(IV_SEED.getBytes(ENCODING)); |
|||
cipher.init(Cipher.ENCRYPT_MODE, skeySpec, iv); |
|||
byte[] srawt = str.getBytes(ENCODING); |
|||
int len = srawt.length; |
|||
/* 计算补空格后的长度 */ |
|||
while (len % 16 != 0) |
|||
len++; |
|||
byte[] sraw = new byte[len]; |
|||
/* 在最后空格 */ |
|||
for (int i = 0; i < len; ++i) { |
|||
if (i < srawt.length) { |
|||
sraw[i] = srawt[i]; |
|||
} else { |
|||
sraw[i] = 32; |
|||
} |
|||
} |
|||
byte[] encrypted = cipher.doFinal(sraw); |
|||
return formatString(new String(Base64.encodeBase64(encrypted), "UTF-8")); |
|||
} catch (Exception ex) { |
|||
logger.error("AES加密出错:" + ex.toString()); |
|||
return null; |
|||
} |
|||
} |
|||
|
|||
/** |
|||
* AES解密算法 |
|||
* |
|||
* @param str 密文 |
|||
* @param key 密key |
|||
* @return |
|||
*/ |
|||
public static String decrypt(String str, String key) { |
|||
try { |
|||
// 判断Key是否正确
|
|||
if (key == null) { |
|||
logger.error("AES解密出错:Key为空null"); |
|||
return null; |
|||
} |
|||
// 判断Key是否为16位
|
|||
if (key.length() != 16) { |
|||
logger.error("AES解密出错:Key长度不是16位"); |
|||
return null; |
|||
} |
|||
byte[] raw = key.getBytes(ENCODING); |
|||
SecretKeySpec skeySpec = new SecretKeySpec(raw, KEY_ALGORITHM); |
|||
Cipher cipher = Cipher.getInstance(CIPHER_ALGORITHM); |
|||
IvParameterSpec iv = new IvParameterSpec(IV_SEED.getBytes(ENCODING)); |
|||
cipher.init(Cipher.DECRYPT_MODE, skeySpec, iv); |
|||
byte[] bytes = Base64.decodeBase64(str.getBytes("UTF-8")); |
|||
bytes = cipher.doFinal(bytes); |
|||
return new String(bytes, ENCODING); |
|||
} catch (Exception ex) { |
|||
logger.error("AES解密出错:" + ex.toString()); |
|||
return null; |
|||
} |
|||
} |
|||
|
|||
private static String formatString(String sourceStr) { |
|||
if (sourceStr == null) { |
|||
return null; |
|||
} |
|||
return sourceStr.replaceAll("\\r", "").replaceAll("\\n", ""); |
|||
} |
|||
|
|||
} |
@ -0,0 +1,31 @@ |
|||
package com.epmet.dto.result; |
|||
|
|||
import lombok.Data; |
|||
|
|||
import java.io.Serializable; |
|||
|
|||
@Data |
|||
public class RizhaoBankUserInfoResultDTO implements Serializable { |
|||
|
|||
/** |
|||
* 用户id |
|||
*/ |
|||
private String userId; |
|||
|
|||
/** |
|||
* 手机号(注册手机号) |
|||
*/ |
|||
private String mobile; |
|||
|
|||
|
|||
/** |
|||
* 身份证号 |
|||
*/ |
|||
private String idNum; |
|||
|
|||
/** |
|||
* 姓名 |
|||
*/ |
|||
private String name; |
|||
|
|||
} |
@ -0,0 +1,53 @@ |
|||
package com.epmet.controller; |
|||
|
|||
import cn.hutool.json.JSONUtil; |
|||
import com.epmet.commons.tools.annotation.LoginUser; |
|||
import com.epmet.commons.tools.redis.common.bean.ResiUserInfoCache; |
|||
import com.epmet.commons.tools.security.dto.TokenDto; |
|||
import com.epmet.commons.tools.utils.AESCBCUtils; |
|||
import com.epmet.commons.tools.utils.Result; |
|||
import com.epmet.dto.result.RizhaoBankUserInfoResultDTO; |
|||
import com.epmet.service.UserBaseInfoService; |
|||
import org.springframework.beans.factory.annotation.Value; |
|||
import org.springframework.web.bind.annotation.PostMapping; |
|||
import org.springframework.web.bind.annotation.RequestMapping; |
|||
import org.springframework.web.bind.annotation.RestController; |
|||
|
|||
import javax.annotation.Resource; |
|||
|
|||
@RestController |
|||
@RequestMapping("/rizhaobank") |
|||
public class RizhaoBankController { |
|||
|
|||
@Value("${rizhaobank.aesKey}") |
|||
private String AES_KEY; |
|||
|
|||
@Resource |
|||
private UserBaseInfoService userBaseInfoService; |
|||
|
|||
@PostMapping("/userinfo") |
|||
public Result<String> getUserInfo(@LoginUser TokenDto tokenDto) { |
|||
|
|||
ResiUserInfoCache userInfo = userBaseInfoService.getUserInfo(tokenDto.getUserId()); |
|||
|
|||
if(userInfo != null) { |
|||
RizhaoBankUserInfoResultDTO userInfoResultDTO = new RizhaoBankUserInfoResultDTO(); |
|||
|
|||
userInfoResultDTO.setUserId(userInfo.getUserId()); |
|||
userInfoResultDTO.setMobile(userInfo.getMobile()); |
|||
userInfoResultDTO.setIdNum(userInfo.getIdNum()); |
|||
userInfoResultDTO.setName(userInfo.getRealName()); |
|||
|
|||
String encryptStr = AESCBCUtils.encrypt(JSONUtil.toJsonStr(userInfoResultDTO), AES_KEY); |
|||
|
|||
// System.out.println("encrypt:\n"+encryptStr);
|
|||
//
|
|||
// System.out.println("decrypt:\n"+AESCBCUtils.decrypt(encryptStr, AES_KEY));
|
|||
|
|||
return new Result<String>().ok(encryptStr); |
|||
} else { |
|||
return new Result<String>().error(1001, "获取用户信息失败"); |
|||
} |
|||
} |
|||
|
|||
} |
Loading…
Reference in new issue