SpringSecurity利⽤RAS加密对登录账号密码请求明⽂加密
1:场景
在⽤户登录时,只要对浏览⽹页有点基础的⼈都知道,打开浏览器控制台,可以在请求抓包块中可以获取到post请求的头部信息,⽽我们⽤户登录的账号密码恰恰就保存在这⾥,加⼊没有加密的话,别⼈可以爬取到⽤户的信息,⼀点都不安全,所以纠结问题,我们在请求中对账号密码全部加密传后台解密,这样这个问题就解决了,思路就是这么个逻辑,⽽具体怎么实现就是我们接下来要讲的问题;如下
2:技术点
采⽤springsecurity实现⽤户认证和授权,RAS⾮对称加密对数据进⾏加密
3:技术描述
RAS简称⾮对称加密,安全系数算是⾮常⾼的,他采⽤了钥匙对对数据加密,⾸先后台⽣成⼀对钥匙(公钥 , 私钥),公钥是可以给⽤户的,也可以给多个⼈,它只负责对数据加密,⽽私钥不能够泄漏,他能对对应的公钥解密.⽽公钥解密只有这⼀钟⽅式,所以只要你对应公钥的私钥不泄漏,别⼈就获取不到你对应的加密⽅式了.
springsecuritysecurity认证⽅式要纠结源码,所以先不再本⽂中讲解,后期会出⼀篇对应的⽂章讲解怎么⾃定义⽤户认证以及动态授权,从源码的⾓度来讲
4:话不多说,上代码
RSAUtils:封装了RSA⽣成规则已经解密⽅式,调⽤即可
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;
/** */
/**
* RSA 位数如果采⽤2048 上⾯最⼤加密和最⼤解密则须填写:  245 256
* RSA 位数如果采⽤2048 上⾯最⼤加密和最⼤解密则须填写:  245 256
*/
private static final int INITIALIZE_LENGTH =1024;
/** */
/**
* <p>
* ⽣成密钥对(公钥和私钥)
* </p>
*
* @return
* @throws Exception
*/
public static Map<String, Object>genKeyPair()throws Exception {
KeyPairGenerator keyPairGen = Instance(KEY_ALGORITHM);        keyPairGen.initialize(INITIALIZE_LENGTH);
KeyPair keyPair = ateKeyPair();
RSAPublicKey publicKey =(RSAPublicKey) Public();
RSAPrivateKey privateKey =(RSAPrivateKey) Private();
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 = Base64.decodeBase64(privateKey);
PKCS8EncodedKeySpec pkcs8KeySpec =new PKCS8EncodedKeySpec(keyBytes);        KeyFactory keyFactory = Instance(KEY_ALGORITHM);
PrivateKey privateK = atePrivate(pkcs8KeySpec);
Signature signature = Instance(SIGNATURE_ALGORITHM);
signature.initSign(privateK);
signature.update(data);
deBase64String(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 = Base64.decodeBase64(publicKey);
X509EncodedKeySpec keySpec =new X509EncodedKeySpec(keyBytes);
KeyFactory keyFactory = Instance(KEY_ALGORITHM);
PublicKey publicK = atePublic(keySpec);
Signature signature = Instance(SIGNATURE_ALGORITHM);
signature.initVerify(publicK);
signature.update(data);
return signature.verify(Base64.decodeBase64(sign));
}
/** */
/**
* <P>
* 私钥解密
* </p>
*
* @param encryptedData
*            已加密数据
* @param privateKey
*            私钥(BASE64编码)
* @return
* @throws Exception
*/
public static byte[]decryptByPrivateKey(byte[] encryptedData, String privateKey)throws Exception { byte[] keyBytes = Base64.decodeBase64(privateKey);
PKCS8EncodedKeySpec pkcs8KeySpec =new PKCS8EncodedKeySpec(keyBytes);
KeyFactory keyFactory = Instance(KEY_ALGORITHM);
Key privateK = atePrivate(pkcs8KeySpec);
Cipher cipher = Algorithm());
cipher.init(Cipher.DECRYPT_MODE, privateK);
int inputLen = encryptedData.length;
ByteArrayOutputStream out =new ByteArrayOutputStream();
java源代码加密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 = ByteArray();
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 = Base64.decodeBase64(publicKey);
X509EncodedKeySpec x509KeySpec =new X509EncodedKeySpec(keyBytes);
KeyFactory keyFactory = Instance(KEY_ALGORITHM);
Key publicK = atePublic(x509KeySpec);
Cipher cipher = Algorithm());
cipher.init(Cipher.DECRYPT_MODE, publicK);
int inputLen = encryptedData.length;
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 = ByteArray();
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 = Base64.decodeBase64(publicKey);
X509EncodedKeySpec x509KeySpec =new X509EncodedKeySpec(keyBytes);
KeyFactory keyFactory = Instance(KEY_ALGORITHM);
Key publicK = atePublic(x509KeySpec);
// 对数据加密
Cipher cipher = Algorithm());
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 = ByteArray();
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 = Base64.decodeBase64(privateKey);
PKCS8EncodedKeySpec pkcs8KeySpec =new PKCS8EncodedKeySpec(keyBytes);        KeyFactory keyFactory = Instance(KEY_ALGORITHM);
Key privateK = atePrivate(pkcs8KeySpec);
Cipher cipher = Algorithm());
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 = ByteArray();
out.close();
return encryptedData;
}
/** */
/**
* <p>
* 获取私钥
* </p>
*
* @param keyMap
*            密钥对
* @return
* @throws Exception
*/
public static String getPrivateKey(Map<String, Object> keyMap)throws Exception {
Key key =(Key) (PRIVATE_KEY);
Encoded());
}
/** */
/**
* <p>
* 获取公钥
* </p>
*
* @param keyMap
*            密钥对
* @return
* @throws Exception
*/
public static String getPublicKey(Map<String, Object> keyMap)throws Exception {
Key key =(Key) (PUBLIC_KEY);
Encoded());
}
/**
* java端公钥加密

版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系QQ:729038198,我们将在24小时内删除。