前言
有時(shí)候我們會(huì)遇到這樣一個(gè)需求,提交表單的時(shí)候?qū)Ρ韱沃械奶囟〝?shù)據(jù)進(jìn)行加密后傳到后臺(tái),再由后臺(tái)進(jìn)行解密酥馍。本文會(huì)針對(duì)該類需求的實(shí)現(xiàn)思路進(jìn)行講解,希望能夠讓各位讀者有所收獲阅酪。
說在前面
我們知道加解密算法可以分為對(duì)稱加密和非對(duì)稱加密旨袒。對(duì)于這個(gè)需求而言,我們不可以采用對(duì)稱加密术辐,原因是前后端使用的秘鑰為同一個(gè)砚尽,如果被中間人攔截到用戶的請(qǐng)求,那么他可以根據(jù)暴露在前端的秘鑰輕而易舉地解密出用戶的敏感信息辉词。
非對(duì)稱加密可以較好的解決這個(gè)問題必孤,把公鑰放在前端頁(yè)面上,私鑰放在后臺(tái)中用于校驗(yàn)较屿,我們本篇文章將選擇RSA算法來進(jìn)行講解隧魄。
開發(fā)思路:
前端(Js):
- 引入加密的
js
函數(shù)庫(kù) - 定義加密方法后添加到表單提交前置方法
onsubmit
中
后端(Java):
- 初始化工具類獲取公鑰和私鑰
- 私鑰放在配置文件或者靜態(tài)常量中,公鑰放在前端js方法中
- 定義解密方法隘蝎,對(duì)請(qǐng)求參數(shù)進(jìn)行解密
具體代碼如下:
前端JS
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<html>
<head>
<title>Title</title>
</head>
<body>
<form action="${pageContext.request.contextPath}/user/login" method="post" onsubmit="toEncrypt()">
用戶名<input type="text" id="u_name" name="username">
密碼<input type="password" id="pwd" name="password">
<input type="submit" value="提交">
</form>
</form>
<script src="${pageContext.request.contextPath}/js/jsencrypt.min.js"></script>
<script type="text/javascript">
function toEncrypt(value) {
var encrypt = new JSEncrypt();
var password = document.getElementById('pwd');
encrypt.setPublicKey('java生成的公鑰');
password.value = encrypt.encrypt(password.value);
return true;
}
</script>
</body>
</html>
表單提交有一個(gè)前置方法onsubmit
,常用于做表單校驗(yàn)襟企,只有當(dāng)返回值為true
的時(shí)候嘱么,表單的數(shù)據(jù)才會(huì)提交到后臺(tái)。這里的話顽悼,我們?cè)诩用芡陻?shù)據(jù)后曼振,直接返回true就行。
注:文檔中引用的函數(shù)庫(kù)大家可以自行在GitHub上面下載: jsencrypt 項(xiàng)目
后端java代碼:
- 加解密工具類
這里要注意蔚龙,工具類依賴了依賴 commons-codec 包
import org.apache.commons.codec.binary.Base64;
import javax.crypto.Cipher;
import java.security.*;
import java.security.spec.PKCS8EncodedKeySpec;
import java.security.spec.X509EncodedKeySpec;
import java.util.HashMap;
import java.util.Map;
/**
* Created by lake on 17-4-12.
*/
public class RSACoder {
public static final String KEY_ALGORITHM = "RSA";
public static final String SIGNATURE_ALGORITHM = "MD5withRSA";
private static final String PUBLIC_KEY = "RSAPublicKey";
private static final String PRIVATE_KEY = "RSAPrivateKey";
public static byte[] decryptBASE64(String key) {
return Base64.decodeBase64(key);
}
public static String encryptBASE64(byte[] bytes) {
return Base64.encodeBase64String(bytes);
}
/**
* 用私鑰對(duì)信息生成數(shù)字簽名
*
* @param data 加密數(shù)據(jù)
* @param privateKey 私鑰
* @return
* @throws Exception
*/
public static String sign(byte[] data, String privateKey) throws Exception {
// 解密由base64編碼的私鑰
byte[] keyBytes = decryptBASE64(privateKey);
// 構(gòu)造PKCS8EncodedKeySpec對(duì)象
PKCS8EncodedKeySpec pkcs8KeySpec = new PKCS8EncodedKeySpec(keyBytes);
// KEY_ALGORITHM 指定的加密算法
KeyFactory keyFactory = KeyFactory.getInstance(KEY_ALGORITHM);
// 取私鑰匙對(duì)象
PrivateKey priKey = keyFactory.generatePrivate(pkcs8KeySpec);
// 用私鑰對(duì)信息生成數(shù)字簽名
Signature signature = Signature.getInstance(SIGNATURE_ALGORITHM);
signature.initSign(priKey);
signature.update(data);
return encryptBASE64(signature.sign());
}
/**
* 校驗(yàn)數(shù)字簽名
*
* @param data 加密數(shù)據(jù)
* @param publicKey 公鑰
* @param sign 數(shù)字簽名
* @return 校驗(yàn)成功返回true 失敗返回false
* @throws Exception
*/
public static boolean verify(byte[] data, String publicKey, String sign)
throws Exception {
// 解密由base64編碼的公鑰
byte[] keyBytes = decryptBASE64(publicKey);
// 構(gòu)造X509EncodedKeySpec對(duì)象
X509EncodedKeySpec keySpec = new X509EncodedKeySpec(keyBytes);
// KEY_ALGORITHM 指定的加密算法
KeyFactory keyFactory = KeyFactory.getInstance(KEY_ALGORITHM);
// 取公鑰匙對(duì)象
PublicKey pubKey = keyFactory.generatePublic(keySpec);
Signature signature = Signature.getInstance(SIGNATURE_ALGORITHM);
signature.initVerify(pubKey);
signature.update(data);
// 驗(yàn)證簽名是否正常
return signature.verify(decryptBASE64(sign));
}
public static byte[] decryptByPrivateKey(byte[] data, String key) throws Exception{
// 對(duì)密鑰解密
byte[] keyBytes = decryptBASE64(key);
// 取得私鑰
PKCS8EncodedKeySpec pkcs8KeySpec = new PKCS8EncodedKeySpec(keyBytes);
KeyFactory keyFactory = KeyFactory.getInstance(KEY_ALGORITHM);
Key privateKey = keyFactory.generatePrivate(pkcs8KeySpec);
// 對(duì)數(shù)據(jù)解密
Cipher cipher = Cipher.getInstance(keyFactory.getAlgorithm());
cipher.init(Cipher.DECRYPT_MODE, privateKey);
return cipher.doFinal(data);
}
/**
* 解密<br>
* 用私鑰解密
*
* @param data
* @param key
* @return
* @throws Exception
*/
public static byte[] decryptByPrivateKey(String data, String key)
throws Exception {
return decryptByPrivateKey(decryptBASE64(data),key);
}
/**
* 解密<br>
* 用公鑰解密
*
* @param data
* @param key
* @return
* @throws Exception
*/
public static byte[] decryptByPublicKey(byte[] data, String key)
throws Exception {
// 對(duì)密鑰解密
byte[] keyBytes = decryptBASE64(key);
// 取得公鑰
X509EncodedKeySpec x509KeySpec = new X509EncodedKeySpec(keyBytes);
KeyFactory keyFactory = KeyFactory.getInstance(KEY_ALGORITHM);
Key publicKey = keyFactory.generatePublic(x509KeySpec);
// 對(duì)數(shù)據(jù)解密
Cipher cipher = Cipher.getInstance(keyFactory.getAlgorithm());
cipher.init(Cipher.DECRYPT_MODE, publicKey);
return cipher.doFinal(data);
}
/**
* 加密<br>
* 用公鑰加密
*
* @param data
* @param key
* @return
* @throws Exception
*/
public static byte[] encryptByPublicKey(String data, String key)
throws Exception {
// 對(duì)公鑰解密
byte[] keyBytes = decryptBASE64(key);
// 取得公鑰
X509EncodedKeySpec x509KeySpec = new X509EncodedKeySpec(keyBytes);
KeyFactory keyFactory = KeyFactory.getInstance(KEY_ALGORITHM);
Key publicKey = keyFactory.generatePublic(x509KeySpec);
// 對(duì)數(shù)據(jù)加密
Cipher cipher = Cipher.getInstance(keyFactory.getAlgorithm());
cipher.init(Cipher.ENCRYPT_MODE, publicKey);
return cipher.doFinal(data.getBytes());
}
/**
* 加密<br>
* 用私鑰加密
*
* @param data
* @param key
* @return
* @throws Exception
*/
public static byte[] encryptByPrivateKey(byte[] data, String key)
throws Exception {
// 對(duì)密鑰解密
byte[] keyBytes = decryptBASE64(key);
// 取得私鑰
PKCS8EncodedKeySpec pkcs8KeySpec = new PKCS8EncodedKeySpec(keyBytes);
KeyFactory keyFactory = KeyFactory.getInstance(KEY_ALGORITHM);
Key privateKey = keyFactory.generatePrivate(pkcs8KeySpec);
// 對(duì)數(shù)據(jù)加密
Cipher cipher = Cipher.getInstance(keyFactory.getAlgorithm());
cipher.init(Cipher.ENCRYPT_MODE, privateKey);
return cipher.doFinal(data);
}
/**
* 取得私鑰
*
* @param keyMap
* @return
* @throws Exception
*/
public static String getPrivateKey(Map<String, Key> keyMap)
throws Exception {
Key key = (Key) keyMap.get(PRIVATE_KEY);
return encryptBASE64(key.getEncoded());
}
/**
* 取得公鑰
*
* @param keyMap
* @return
* @throws Exception
*/
public static String getPublicKey(Map<String, Key> keyMap)
throws Exception {
Key key = keyMap.get(PUBLIC_KEY);
return encryptBASE64(key.getEncoded());
}
/**
* 初始化密鑰
*
* @return
* @throws Exception
*/
public static Map<String, Key> initKey() throws Exception {
KeyPairGenerator keyPairGen = KeyPairGenerator
.getInstance(KEY_ALGORITHM);
keyPairGen.initialize(1024);
KeyPair keyPair = keyPairGen.generateKeyPair();
Map<String, Key> keyMap = new HashMap(2);
keyMap.put(PUBLIC_KEY, keyPair.getPublic());// 公鑰
keyMap.put(PRIVATE_KEY, keyPair.getPrivate());// 私鑰
return keyMap;
}
}
通過工具類生成公鑰和私鑰 冰评, 分別保存到前端和后端
/**
* 初始化密鑰,我們可以從initKey方法中直接獲取公鑰和私鑰
* 這里要注意的是木羹,每次生成的秘鑰對(duì)都是不一樣的甲雅,我們選一個(gè)進(jìn)行保存就行
*
* @return
* @throws Exception
*/
public static void initKey() throws Exception {
KeyPairGenerator keyPairGen = KeyPairGenerator
.getInstance(KEY_ALGORITHM);
keyPairGen.initialize(1024);
KeyPair keyPair = keyPairGen.generateKeyPair();
System.out.println("公鑰是:" + keyPair.getPublic());
System.out.println("私鑰是:" + keyPair.getPrivate());
}
接收到前端的代碼后進(jìn)行解密
public void login(HttpServletRequest request,HttpServletResponse response){
... 獲取前端傳進(jìn)來的密碼
password = RSACoder.decryptByPrivateKey("解密前的密碼"解孙,"后端的私鑰");
}
需求外的優(yōu)化點(diǎn)
我們會(huì)發(fā)現(xiàn),由于加密動(dòng)作的存在抛人,用戶提交代碼后弛姜,可以肉眼看到原密碼會(huì)變成32位的加密后字符,這樣樣式上會(huì)不太好看妖枚。我們可以采用一個(gè)隱藏的<input>
標(biāo)簽來做密碼的間接傳輸廷臼,并將name屬性賦給新的input
標(biāo)簽(JS要配套著去改)。這樣我們就可以在后臺(tái)接收到轉(zhuǎn)換的參數(shù)了
前端JS改動(dòng)如下:
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<html>
<head>
<title>Title</title>
</head>
<body>
<form action="${pageContext.request.contextPath}/user/login" method="post" onsubmit="toEncrypt()">
用戶名<input type="text" id="u_name" name="username">
密碼<input type="password" id="pwd" >
<input type="hidden" name="password" id="tem_pwd" >
<input type="submit" value="提交">
</form>
</form>
<script src="${pageContext.request.contextPath}/js/jsencrypt.min.js"></script>
<script type="text/javascript">
function toEncrypt(value) {
var encrypt = new JSEncrypt();
var u_password = document.getElementById('pwd');
var tem_password = document.getElementById('pwd');
encrypt.setPublicKey('java生成的公鑰');
tem_password.value = encrypt.encrypt(u_password .value);
return true;
}
</script>
</body>
</html>
參考文章:
http://www.reibang.com/p/ff8281f034f4 Java 與 js完美RSA非對(duì)稱加密
https://www.cnblogs.com/web-wjg/p/7894657.html js實(shí)現(xiàn)表單提交submit()绝页,onsubmit