加密分为对称加密和非对称加密
需求:密码加密传输
要求:
思路:
后端:
<dependency>
<groupId>commons-codec</groupId>
<artifactId>commons-codec</artifactId>
<version>1.10</version>
</dependency>
package com.kafamiao.security;
import javax.crypto.Cipher;
import javax.crypto.spec.IvParameterSpec;
import javax.crypto.spec.SecretKeySpec;
import org.apache.commons.codec.binary.Base64;
public class AesUtil {
public static String byteToString(byte[] byte1){
return new String(byte1);
}
public static byte[] AES_CBC_Encrypt(byte[] content, byte[] keyBytes, byte[] iv){
try {
SecretKeySpec key = new SecretKeySpec(keyBytes, "AES");
Cipher cipher = Cipher.getInstance("AES/CBC/PKCS5Padding");
cipher.init(Cipher.ENCRYPT_MODE,key, new IvParameterSpec(iv));
byte[] result = cipher.doFinal(content);
return result;
} catch (Exception e) {
System.out.println("exception:"+e.toString());
}
return null;
}
public static byte[] AES_CBC_Decrypt(byte[] content, byte[] keyBytes, byte[] iv){
try {
SecretKeySpec key = new SecretKeySpec(keyBytes, "AES");
Cipher cipher = Cipher.getInstance("AES/CBC/PKCS5Padding");
cipher.init(Cipher.DECRYPT_MODE,key, new IvParameterSpec(iv));
byte[] result = cipher.doFinal(content);
return result;
} catch (Exception e) {
System.out.println("exception:"+e.toString());
return null;
}
}
//字符串装换成base64
public static byte[] decryptBASE64(String key) throws Exception {
return Base64.decodeBase64(key.getBytes());
}
//base64装换成字符串
public static String encryptBASE64(byte[] key) throws Exception {
return new String(Base64.encodeBase64(key));
}
}
public class Login {
public static void main(String[] args) throws Exception {
String username = "HdSLYf9i0fPUwryaqRYyRg=="; // 前端传过来的密文用户名
String password = "HdSLYf9i0fPUwryaqRYyRg=="; // 前端传过来的密文密码
String key = "aBc123&&^ew12mM"; // 密钥,推荐密码随机生成,这里硬编码了
String iv = "xyz123&&^ew12mM"; // 向量,推荐向量随机生成,这里硬编码了
try {
// 对 username 解密
String usernameDecry = AesUtil.byteToString(AesUtil.AES_CBC_Decrypt(AesUtil.decryptBASE64(username), key.getBytes(), iv.getBytes()));
// 对 password 解密
String passwordDecry = AesUtil.byteToString(AesUtil.AES_CBC_Decrypt(AesUtil.decryptBASE64(password), key.getBytes(), iv.getBytes()));
System.out.println(usernameDecry);
System.out.println(passwordDecry);
} catch (Exception e) {
e.printStackTrace();
}
}
}
前端:
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Document</title>
<script src="aes.js" type="text/javascript"></script>
<script>
function loginSubmit() {
var key = 'aBc123&&^ew12mM' //密钥,推荐密码随机生成,这里硬编码了
var iv = 'xyz123&&^ew12mM' // 向量,推荐向量随机生成,这里硬编码了
// 原文用户名
var username = document.getElementById("username").value
// 原文密码
var password = document.getElementById("password").value
// 加密用户名
var aesUsername = getAesString(username, key, iv)
// 加密密码
var aesPassword = getAesString(password, key, iv)
// 在界面上显示加密后的用户名和密码,密文传到后端,后端对密文解密后得到原文
document.getElementById("d1").innerHTML = aesUsername
document.getElementById("d2").innerHTML = aesPassword
}
function getAesString(data, key, iv) {
var key = CryptoJS.enc.Utf8.parse(key);
var iv = CryptoJS.enc.Utf8.parse(iv);
var encrypted = CryptoJS.AES.encrypt(data, key, {
iv: iv,
mode: CryptoJS.mode.CBC,
padding: CryptoJS.pad.Pkcs7
});
return encrypted.toString(); //返回的是base64格式的密文
}
</script>
</head>
<body>
<form id="login-form" method="post">
<input type="text" id="username" name="username"><br />
<input type="text" id="password" name="password"><br />
<input type="button" value="登录" onclick="loginSubmit()" />
</form>
<div id="d1"></div>
<div id="d2"></div>
</body>
</html>
至此,加密的小demo做完了
powered by kaifamiao