SpringBoot+VUE登录加密解密
vue
vuejs/vue: 是一个用于构建用户界面的 JavaScript 框架,具有简洁的语法和丰富的组件库,可以用于开发单页面应用程序和多页面应用程序。
项目地址:https://gitcode.com/gh_mirrors/vu/vue
免费下载资源
·
- 前端首先调用后端的公钥接口,再在前端加密密码传输至后端登录接口, 后端用私钥解密码,拿着用户名去数据库查询出来的盐值加密的密码1,用私钥解密密码登录密码加盐值得到密码2, 比较密码1与密码2,相同则登录成功
1.前端
methods:{
login(formName) {
var param = new FormData()
param.append('account', this.form.account)
this.password = this.passwordEncryption(this.form.password)
param.append('password', this.password)
this.$refs[formName].validate((valid) => {
if (valid) {
postLogin(param, res=> {
if (res.code === 200) {
window.sessionStorage.setItem('token', res.data.token)
setCookie('token', res.data.token)
setCookie('userName', res.data.userInfo.account)
setCookie('userId', res.data.userInfo.id)
let loginData = JSON.stringify(res.data.permission)
setCookie('permission', loginData)
this.$notify({
type: 'success',
message: '登录成功'
})
this.$router.push({ path: '/Home' })
} else {
this.$notify({
type: 'error',
message: res.msg || '登录失败'
})
}
})
} else {
console.log('error submit!!');
return false;
}
})
},
/**
* 加密
* @param {String} 需要加密的参数
*/
passwordEncryption (param) {
// 后台给的公钥
let encryptor = new JsEncrypt() // 新建JSEncrypt对象
encryptor.setPublicKey(this.publicKey) // 设置公钥
let passwordEncryp = encryptor.encrypt(param) // 对密码进行加密
return passwordEncryp
}
},
created() {
getpublicKey(res => {
if (res.code === 200) {
this.publicKey = res.data
}
})
}
2.后端
- 2.1
/**
* 获取公钥
* @param request
* @return
*/
@ApiOperation(value = "获取公钥", httpMethod = "GET")
@RequestMapping("/publicKey")
@ResponseBody
public CommonResult publicKey(HttpServletRequest request) {
String publicKey = generateKey(request);
return CommonResult.success("操作成功!",publicKey);
}
/**
* 生成随机码
* @param request
*/
private String generateKey(HttpServletRequest request) {
try {
Map<String, Object> keyMap = RSAUtils.genKeyPair();
String publicKey = RSAUtils.getPublicKey(keyMap);
String privateKey = RSAUtils.getPrivateKey(keyMap);
request.getSession().setAttribute("privateKey", privateKey);
return publicKey;
// request.setAttribute("publicKey", publicKey);
// request.setAttribute("privateKey", privateKey);
} catch (Exception e) {
e.printStackTrace();
}
return null;
}
- 2.2
@ApiOperation(value = "登录", httpMethod = "POST")
@RequestMapping(value = "/login")
@ResponseBody
public CommonResult login(HttpServletRequest request,
@RequestParam(value = "account", required = true) String account,
@RequestParam(value = "password", required = true) String password,
@RequestParam(value = "rememberMe", required = true, defaultValue = "1") int rememberMe) {
try {
String privateKey = (String) request.getSession().getAttribute("privateKey");
if (privateKey == null) {
return CommonResult.failed("鉴权失败,请刷新页面,重新登陆!");
}
try {
password = RSAUtils.decryptDataOnJava(password, privateKey);
} catch (Exception e) {
return CommonResult.failed("鉴权失败,请刷新页面,重新登陆!");
}
RcSysUserEntity byAccount = rcSysUserRepository.findByAccount(account);
if (null!=byAccount) {
if (byAccount.getStatus()==0) {
return CommonResult.failed("账户已禁用!");
}
String md5_pwd = EncryptUtil.MD5(password+"{"+byAccount.getSalt()+"}");
if (md5_pwd.equals(byAccount.getPassword())) {
return loginService.loginV2(byAccount);
}else{
return CommonResult.failed("用户名或密码输入错误!");
}
}else{
return CommonResult.failed("用户名或密码输入错误!");
}
} catch (Exception e) {
e.printStackTrace();
return CommonResult.failed("用户名或密码输入错误!");
}
}
3.工具类
package com.zkdj.data.common.domain;
import lombok.Data;
import java.io.Serializable;
/**
* 通用返回对象
*
* @author Administrator
*/
@Data
public class CommonResult<T> implements Serializable {
private static final long serialVersionUID = 5190371721822680303L;
/**
* 状态码
*/
private int code;
/**
* 提示信息
*/
private String message;
/**
* 数据封装
*/
private T data;
public CommonResult(int code, String message, T data) {
this.code = code;
this.message = message;
this.data = data;
}
/**
* 返回成功信息
*
* @return
*/
public static <T> CommonResult<T> success() {
return new CommonResult<T>(ResultCode.SUCCESS.getCode(), ResultCode.SUCCESS.getMessage(), null);
}
public static <T> CommonResult<T> success(T data) {
return new CommonResult<T>(ResultCode.SUCCESS.getCode(), ResultCode.SUCCESS.getMessage(), data);
}
public static <T> CommonResult<T> success(String message, T data) {
return new CommonResult<T>(ResultCode.SUCCESS.getCode(), message, data);
}
/**
* 返回失败信息
*
* @return
*/
public static <T> CommonResult<T> failed() {
return new CommonResult<T>(ResultCode.FAILED.getCode(), ResultCode.FAILED.getMessage(), null);
}
public static <T> CommonResult<T> failed(String message) {
return new CommonResult<T>(ResultCode.FAILED.getCode(), message, null);
}
public static <T> CommonResult<T> failed(int code, String message) {
return new CommonResult<T>(code, message, null);
}
/**
* 返回参数校验失败信息
*
* @return
*/
public static <T> CommonResult<T> validateFailed() {
return new CommonResult<T>(ResultCode.VALIDATE_FAILED.getCode(), ResultCode.VALIDATE_FAILED.getMessage(), null);
}
public static <T> CommonResult<T> validateFailed(String message) {
return new CommonResult<T>(ResultCode.VALIDATE_FAILED.getCode(), message, null);
}
public static <T> CommonResult<T> forbidden() {
return new CommonResult<T>(ResultCode.FORBIDDEN.getCode(), ResultCode.FORBIDDEN.getMessage(), null);
}
}
package com.zkdj.data.utils;
import javax.crypto.Cipher;
import javax.crypto.KeyGenerator;
import javax.crypto.SecretKey;
import javax.crypto.SecretKeyFactory;
import javax.crypto.spec.DESedeKeySpec;
import javax.crypto.spec.IvParameterSpec;
import javax.crypto.spec.SecretKeySpec;
import java.io.UnsupportedEncodingException;
import java.security.Key;
import java.security.MessageDigest;
import java.security.NoSuchAlgorithmException;
import java.security.SecureRandom;
import java.util.Base64;
/**
* Java加密解密工具.3DES SHA1 MD5 BASE64编码 AES加密
* @author gq
*/
public class EncryptUtil {
// 无需创建对象
private EncryptUtil() {
}
/**
* SHA1加密Bit数据
*
* @param source
* byte数组
* @return 加密后的byte数组
*/
public static byte[] SHA1Bit(byte[] source) {
try {
MessageDigest sha1Digest = MessageDigest.getInstance("SHA-1");
sha1Digest.update(source);
byte targetDigest[] = sha1Digest.digest();
return targetDigest;
} catch (NoSuchAlgorithmException e) {
throw new RuntimeException(e);
}
}
/**
* SHA1加密字符串数据
*
* @param source
* 要加密的字符串
* @return 加密后的字符串
*/
public static String SHA1(String source) {
return byte2HexStr(SHA1Bit(source.getBytes()));
}
/**
* MD5加密Bit数据
*
* @param source
* byte数组
* @return 加密后的byte数组
*/
public static byte[] MD5Bit(byte[] source) {
try {
// 获得MD5摘要算法的 MessageDigest对象
MessageDigest md5Digest = MessageDigest.getInstance("MD5");
// 使用指定的字节更新摘要
md5Digest.update(source);
// 获得密文
return md5Digest.digest();
} catch (NoSuchAlgorithmException e) {
throw new RuntimeException(e);
}
}
/**
* MD5加密字符串,32位长
*
* @param source
* 要加密的内容
* @return 加密后的内容
*/
public static String MD5(String source) {
return byte2HexStr(MD5Bit(source.getBytes()));
}
/**
* BASE64编码
*
* @param source
* 要编码的字符串
* @return 编码过的字符串
* @throws UnsupportedEncodingException
*/
public static String encodeBASE64(String source) throws UnsupportedEncodingException {
return Base64.getEncoder().encodeToString(source.getBytes(encoding));
}
/**
* BASE64解码
*
* @param encodeSource
* 编码过的字符串
* @return 编码前的字符串
* @throws UnsupportedEncodingException
*/
public static String decodeBASE64(String encodeSource) throws UnsupportedEncodingException {
return new String(Base64.getDecoder().decode(encodeSource),encoding);
}
/**
* AES加密
*
* @param content
* 待加密的内容
* @param password
* 加密密码
* @return
*/
public static byte[] encryptBitAES(byte[] content, String password) {
try {
Cipher encryptCipher = Cipher.getInstance("AES/ECB/PKCS5Padding");// 创建密码器
encryptCipher.init(Cipher.ENCRYPT_MODE, getKey(password));// 初始化
byte[] result = encryptCipher.doFinal(content);
return result; // 加密
} catch (Exception e) {
throw new RuntimeException(e);
}
// return null;
}
/**
* AES解密
*
* @param content
* 待解密内容
* @param password
* 解密密钥
* @return
*/
public static byte[] decryptBitAES(byte[] content, String password) {
try {
Cipher decryptCipher = Cipher.getInstance("AES/ECB/PKCS5Padding");// 创建密码器
decryptCipher.init(Cipher.DECRYPT_MODE, getKey(password));// 初始化
byte[] result = decryptCipher.doFinal(content);
return result; // 加密结果
} catch (Exception e) {
throw new RuntimeException(e);
}
// return null;
}
/**
* AES字符串加密
*
* @param content
* 待加密的内容
* @param password
* 加密密码
* @return
*/
public static String encryptAES(String content, String password) {
return byte2HexStr(encryptBitAES(content.getBytes(), password));
}
/**
* AES字符串解密
*
* @param content
* 待解密内容
* @param password
* 解密密钥
* @return
*/
public static String decryptAES(String content, String password) {
return new String(decryptBitAES(hexStr2Bytes(content), password));
}
/**
* 从指定字符串生成密钥
*
* @param password
* 构成该秘钥的字符串
* @return 生成的密钥
* @throws NoSuchAlgorithmException
*/
private static Key getKey(String password) throws NoSuchAlgorithmException {
SecureRandom secureRandom = new SecureRandom(password.getBytes());
// 生成KEY
KeyGenerator kgen = KeyGenerator.getInstance("AES");
kgen.init(128, secureRandom);
SecretKey secretKey = kgen.generateKey();
byte[] enCodeFormat = secretKey.getEncoded();
// 转换KEY
SecretKeySpec key = new SecretKeySpec(enCodeFormat, "AES");
return key;
}
/**
* 将byte数组转换为表示16进制值的字符串. 如:byte[]{8,18}转换为:0812 和 byte[]
* hexStr2Bytes(String strIn) 互为可逆的转换过程.
*
* @param bytes
* 需要转换的byte数组
* @return 转换后的字符串
*/
public static String byte2HexStr(byte[] bytes) {
int bytesLen = bytes.length;
// 每个byte用两个字符才能表示,所以字符串的长度是数组长度的两倍
StringBuffer hexString = new StringBuffer(bytesLen * 2);
for (int i = 0; i < bytesLen; i++) {
// 将每个字节与0xFF进行与运算,然后转化为10进制,然后借助于Integer再转化为16进制
String hex = Integer.toHexString(bytes[i] & 0xFF);
if (hex.length() < 2) {
hexString.append(0);// 如果为1位 前面补个0
}
hexString.append(hex);
}
return hexString.toString();
}
/**
* 将表示16进制值的字符串转换为byte数组, 和 String byte2HexStr(byte[] bytes) 互为可逆的转换过程.
*
* @param bytes
* @return 转换后的byte数组
*/
public static byte[] hexStr2Bytes(String strIn) {
byte[] arrB = strIn.getBytes();
int iLen = arrB.length;
// 两个字符表示一个字节,所以字节数组长度是字符串长度除以2
byte[] arrOut = new byte[iLen / 2];
for (int i = 0; i < iLen; i = i + 2) {
String strTmp = new String(arrB, i, 2);
arrOut[i / 2] = (byte) Integer.parseInt(strTmp, 16);
}
return arrOut;
}
// 密钥
private final static String secretKey = "gongquan2008@lx100$#365#";
// 向量
private final static String iv = "01234567";
// 加解密统一使用的编码方式
private final static String encoding = "utf-8";
/**
* 3DES加密
*
* @param plainText
* 普通文本
* @return
* @throws Exception
*/
public static String d3esEncode(String plainText) {
Key deskey = null;
byte[] encryptData = null;
try {
DESedeKeySpec spec = new DESedeKeySpec(secretKey.getBytes());
SecretKeyFactory keyfactory = SecretKeyFactory.getInstance("desede");
deskey = keyfactory.generateSecret(spec);
Cipher cipher = Cipher.getInstance("desede/CBC/PKCS5Padding");
IvParameterSpec ips = new IvParameterSpec(iv.getBytes());
cipher.init(Cipher.ENCRYPT_MODE, deskey, ips);
encryptData = cipher.doFinal(plainText.getBytes(encoding));
} catch (Exception e) {
e.printStackTrace();
}
return Base64.getEncoder().encodeToString(encryptData);
}
/**
* 3DES解密
*
* @param encryptText
* 加密文本
* @return
* @throws Exception
*/
public static String d3esDecode(String encryptText) {
Key deskey = null;
byte[] decryptData = null;
String result = "";
if (null != encryptText && !"".equals(encryptText)) {
try {
DESedeKeySpec spec = new DESedeKeySpec(secretKey.getBytes());
SecretKeyFactory keyfactory = SecretKeyFactory.getInstance("desede");
deskey = keyfactory.generateSecret(spec);
Cipher cipher = Cipher.getInstance("desede/CBC/PKCS5Padding");
IvParameterSpec ips = new IvParameterSpec(iv.getBytes());
cipher.init(Cipher.DECRYPT_MODE, deskey, ips);
decryptData = cipher.doFinal(Base64.getDecoder().decode(encryptText));
result = new String(decryptData, encoding);
} catch (Exception e) {
e.printStackTrace();
}
}
return result;
}
public static void main(String[] args) {
// byte[] arr1 = new byte[] { 1, 2, 3, 4, 5, 6, 7, 22 };
// System.out.println("SHA1 加密 : " + SHA1Bit(arr1));
// System.out.println("SHA1 解密 : " + SHA1("arr1"));
// System.out.println("MD5 加密 : " + MD5Bit(arr1));
// System.out.println("MD5 加密 : " + MD5("arr1"));// 54f04b52a75382a157f69457810c41bd
// System.out.println("BASE64 加密 : " + encodeBASE64("arr1"));
// System.out.println("BASE64 解密 : " + decodeBASE64(encodeBASE64("arr1")));
// System.out.println("3DES 加密 : " + d3esEncode("arr1"));
// System.out.println("d3esDecode(decodeBASE64(encodeBASE64(d3esEncode('arr1')))) = "
// + d3esDecode(decodeBASE64(encodeBASE64(d3esEncode("arr1")))));
// System.out.println("3DES 解密 : " + d3esDecode(d3esEncode("arr1")));
// System.out.println("AES 加密 : " + encryptBitAES(arr1, "arr1"));
// System.out.println("AES 解密 : " + decryptBitAES(encryptBitAES(arr1, "arr1"), "arr1"));
try{
String password = EncryptUtil.encodeBASE64("11111111111111");
System.out.println(password+":"+EncryptUtil.decodeBASE64(password));
}catch(Exception ex){
ex.printStackTrace();
}
}
}
package com.zkdj.data.utils;
import javax.crypto.Cipher;
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.HashMap;
import java.util.Map;
/** */
/**
* <p>
* RSA公钥/私钥/签名工具包
* </p>
* <p>
* 罗纳德·李维斯特(Ron [R]ivest)、阿迪·萨莫尔(Adi [S]hamir)和伦纳德·阿德曼(Leonard [A]dleman)
* </p>
* <p>
* 字符串格式的密钥在未在特殊说明情况下都为BASE64编码格式<br/>
* 由于非对称加密速度极其缓慢,一般文件不使用它来加密而是使用对称加密,<br/>
* 非对称加密算法可以用来对对称加密的密钥加密,这样保证密钥的安全也就保证了数据的安全
* </p>
*
* @author IceWee
* @date 2012-4-26
* @version 1.0
*/
public class RSAUtils {
/** */
/**
* 加密算法RSA
*/
public static final String KEY_ALGORITHM = "RSA";
/** */
/**
* 签名算法
*/
public static final String SIGNATURE_ALGORITHM = "MD5withRSA";
/** */
/**
* 获取公钥的key
*/
private static final String PUBLIC_KEY = "RSAPublicKey";
/** */
/**
* 获取私钥的key
*/
private static final String PRIVATE_KEY = "RSAPrivateKey";
/** */
/**
* RSA最大加密明文大小
*/
private static final int MAX_ENCRYPT_BLOCK = 117;
/** */
/**
* RSA最大解密密文大小
*/
private static final int MAX_DECRYPT_BLOCK = 128;
/** */
/**
* <p>
* 生成密钥对(公钥和私钥)
* </p>
*
* @return
* @throws Exception
*/
public static Map<String, Object> genKeyPair() throws Exception {
KeyPairGenerator keyPairGen = KeyPairGenerator.getInstance(KEY_ALGORITHM);
keyPairGen.initialize(1024);
KeyPair keyPair = keyPairGen.generateKeyPair();
RSAPublicKey publicKey = (RSAPublicKey) keyPair.getPublic();
RSAPrivateKey privateKey = (RSAPrivateKey) keyPair.getPrivate();
Map<String, Object> keyMap = new HashMap<String, Object>(2);
keyMap.put(PUBLIC_KEY, publicKey);
keyMap.put(PRIVATE_KEY, privateKey);
return keyMap;
}
/** */
/**
* <p>
* 用私钥对信息生成数字签名
* </p>
*
* @param data
* 已加密数据
* @param privateKey
* 私钥(BASE64编码)
*
* @return
* @throws Exception
*/
public static String sign(byte[] data, String privateKey) throws Exception {
byte[] keyBytes = Base64Utils.decode(privateKey);
PKCS8EncodedKeySpec pkcs8KeySpec = new PKCS8EncodedKeySpec(keyBytes);
KeyFactory keyFactory = KeyFactory.getInstance(KEY_ALGORITHM);
PrivateKey privateK = keyFactory.generatePrivate(pkcs8KeySpec);
Signature signature = Signature.getInstance(SIGNATURE_ALGORITHM);
signature.initSign(privateK);
signature.update(data);
return Base64Utils.encode(signature.sign());
}
/** */
/**
* <p>
* 校验数字签名
* </p>
*
* @param data
* 已加密数据
* @param publicKey
* 公钥(BASE64编码)
* @param sign
* 数字签名
*
* @return
* @throws Exception
*
*/
public static boolean verify(byte[] data, String publicKey, String sign) throws Exception {
byte[] keyBytes = Base64Utils.decode(publicKey);
X509EncodedKeySpec keySpec = new X509EncodedKeySpec(keyBytes);
KeyFactory keyFactory = KeyFactory.getInstance(KEY_ALGORITHM);
PublicKey publicK = keyFactory.generatePublic(keySpec);
Signature signature = Signature.getInstance(SIGNATURE_ALGORITHM);
signature.initVerify(publicK);
signature.update(data);
return signature.verify(Base64Utils.decode(sign));
}
/** */
/**
* <P>
* 私钥解密
* </p>
*
* @param encryptedData
* 已加密数据
* @param privateKey
* 私钥(BASE64编码)
* @return
* @throws Exception
*/
public static byte[] decryptByPrivateKey(byte[] encryptedData, String privateKey) throws Exception {
byte[] keyBytes = Base64Utils.decode(privateKey);
PKCS8EncodedKeySpec pkcs8KeySpec = new PKCS8EncodedKeySpec(keyBytes);
KeyFactory keyFactory = KeyFactory.getInstance(KEY_ALGORITHM);
Key privateK = keyFactory.generatePrivate(pkcs8KeySpec);
Cipher cipher = Cipher.getInstance(keyFactory.getAlgorithm());
cipher.init(Cipher.DECRYPT_MODE, privateK);
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 decryptedData;
}
/** */
/**
* <p>
* 公钥解密
* </p>
*
* @param encryptedData
* 已加密数据
* @param publicKey
* 公钥(BASE64编码)
* @return
* @throws Exception
*/
public static byte[] decryptByPublicKey(byte[] encryptedData, String publicKey) throws Exception {
byte[] keyBytes = Base64Utils.decode(publicKey);
X509EncodedKeySpec x509KeySpec = new X509EncodedKeySpec(keyBytes);
KeyFactory keyFactory = KeyFactory.getInstance(KEY_ALGORITHM);
Key publicK = keyFactory.generatePublic(x509KeySpec);
Cipher cipher = Cipher.getInstance(keyFactory.getAlgorithm());
cipher.init(Cipher.DECRYPT_MODE, publicK);
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 decryptedData;
}
/** */
/**
* <p>
* 公钥加密
* </p>
*
* @param data
* 源数据
* @param publicKey
* 公钥(BASE64编码)
* @return
* @throws Exception
*/
public static byte[] encryptByPublicKey(byte[] data, String publicKey) throws Exception {
byte[] keyBytes = Base64Utils.decode(publicKey);
X509EncodedKeySpec x509KeySpec = new X509EncodedKeySpec(keyBytes);
KeyFactory keyFactory = KeyFactory.getInstance(KEY_ALGORITHM);
Key publicK = keyFactory.generatePublic(x509KeySpec);
// 对数据加密
Cipher cipher = Cipher.getInstance(keyFactory.getAlgorithm());
cipher.init(Cipher.ENCRYPT_MODE, publicK);
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();
return encryptedData;
}
/** */
/**
* <p>
* 私钥加密
* </p>
*
* @param data
* 源数据
* @param privateKey
* 私钥(BASE64编码)
* @return
* @throws Exception
*/
public static byte[] encryptByPrivateKey(byte[] data, String privateKey) throws Exception {
byte[] keyBytes = Base64Utils.decode(privateKey);
PKCS8EncodedKeySpec pkcs8KeySpec = new PKCS8EncodedKeySpec(keyBytes);
KeyFactory keyFactory = KeyFactory.getInstance(KEY_ALGORITHM);
Key privateK = keyFactory.generatePrivate(pkcs8KeySpec);
Cipher cipher = Cipher.getInstance(keyFactory.getAlgorithm());
cipher.init(Cipher.ENCRYPT_MODE, privateK);
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();
return encryptedData;
}
/** */
/**
* <p>
* 获取私钥
* </p>
*
* @param keyMap
* 密钥对
* @return
* @throws Exception
*/
public static String getPrivateKey(Map<String, Object> keyMap) throws Exception {
Key key = (Key) keyMap.get(PRIVATE_KEY);
return Base64Utils.encode(key.getEncoded());
}
/** */
/**
* <p>
* 获取公钥
* </p>
*
* @param keyMap
* 密钥对
* @return
* @throws Exception
*/
public static String getPublicKey(Map<String, Object> keyMap) throws Exception {
Key key = (Key) keyMap.get(PUBLIC_KEY);
return Base64Utils.encode(key.getEncoded());
}
/**
* java端公钥加密
*/
public static String encryptedDataOnJava(String data, String PUBLICKEY) {
try {
data = Base64Utils.encode(encryptByPublicKey(data.getBytes(), PUBLICKEY));
} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
return data;
}
/**
* java端私钥解密
*/
public static String decryptDataOnJava(String data, String PRIVATEKEY) {
String temp = "";
try {
byte[] rs = Base64Utils.decode(data);
temp = new String(RSAUtils.decryptByPrivateKey(rs, PRIVATEKEY),"UTF-8"); //以utf-8的方式生成字符串
} catch (Exception e) {
e.printStackTrace();
}
return temp;
}
}
4.实体类
package com.zkdj.data.entity.model;
import com.zkdj.data.entity.pojo.TMenu;
import io.swagger.annotations.ApiModel;
import io.swagger.annotations.ApiModelProperty;
import lombok.Data;
import javax.persistence.*;
import java.io.Serializable;
import java.util.List;
/**
* @description 系统登录用户信息
* @author gxf
* @date 2023-05-31
*/
@Entity
@Data
@Table(name="t_rc_sys_user")
@ApiModel("系统登录用户信息")
public class RcSysUserEntity implements Serializable {
private static final long serialVersionUID = 1L;
/**
* 主键
*/
@Id
@ApiModelProperty("主键")
@GeneratedValue(strategy = GenerationType.IDENTITY)
@Column(name = "id", nullable = false)
private Long id;
/**
* 账号
*/
@ApiModelProperty("账号")
@Column(name="account")
private String account;
/**
* 密码
*/
@ApiModelProperty("密码")
@Column(name="password")
private String password;
/**
* 真实姓名
*/
@ApiModelProperty("真实姓名")
@Column(name="realname")
private String realname;
/**
* 角色id
*/
@ApiModelProperty("角色id")
@Column(name="role_id")
private String roleId;
/**
* 盐值
*/
@ApiModelProperty("盐值")
@Column(name="salt")
private String salt;
/**
* 类型
*/
@ApiModelProperty("类型")
@Column(name="type")
private Integer type;
/**
* 创建时间
*/
@ApiModelProperty("创建时间")
@Column(name="ctime")
private String ctime;
/**
* 创建用户
*/
@ApiModelProperty("创建用户")
@Column(name="cuser")
private String cuser;
/**
* 更新时间
*/
@ApiModelProperty("更新时间")
@Column(name="utime")
private String utime;
/**
* 令牌
*/
@ApiModelProperty("令牌")
@Column(name="token")
private String token;
/**
* 电话
*/
@ApiModelProperty("电话")
@Column(name="tel")
private String tel;
/**
* 邮箱
*/
@ApiModelProperty("邮箱")
@Column(name="email")
private String email;
/**
* 启用禁用 0禁用1启用
*/
@ApiModelProperty("启用禁用 0禁用1启用")
@Column(name="status")
private Integer status;
/**
* 列表的角色名称
*/
@ApiModelProperty("列表的角色名称")
@Transient
private String rname;
/**
* 登录的角色权限树
*/
@ApiModelProperty("登录的角色权限树")
@Transient
private List<TMenu> menus;
/**
* 修改密码时的新密码
*/
@ApiModelProperty("修改密码时的新密码")
@Transient
private String newPassword;
public RcSysUserEntity() {
}
}
GitHub 加速计划 / vu / vue
207.54 K
33.66 K
下载
vuejs/vue: 是一个用于构建用户界面的 JavaScript 框架,具有简洁的语法和丰富的组件库,可以用于开发单页面应用程序和多页面应用程序。
最近提交(Master分支:2 个月前 )
73486cb5
* chore: fix link broken
Signed-off-by: snoppy <michaleli@foxmail.com>
* Update packages/template-compiler/README.md [skip ci]
---------
Signed-off-by: snoppy <michaleli@foxmail.com>
Co-authored-by: Eduardo San Martin Morote <posva@users.noreply.github.com> 4 个月前
e428d891
Updated Browser Compatibility reference. The previous currently returns HTTP 404. 5 个月前
更多推荐
已为社区贡献1条内容
所有评论(0)