1、crypto.js簡介
??CryptoJS 是一個 JavaScript 庫,提供了一系列密碼學函數(shù)和工具,用于加密、解密、生成摘要等任務。它支持多種加密算法,包括常見的對稱加密算法(如 AES、DES)和非對稱加密算法(如 RSA)。
??同時,CryptoJS還包括了ECB和CBC兩種模式,其中ECB模式:全稱Electronic Codebook(電碼本),在ECB模式中,每個明文分組都被單獨加密,且每個明文分組都被加密為相同的密文分組。也就是說,如果兩個明文分組相同,那么它們的密文分組也相同。CBC模式:全稱Cipher Block Chaining(密文分組鏈接模式),在CBC模式中,每個明文分組都與前一個密文分組進行XOR運算(相異為一)然后再進行加密。因此,密文分組是相互連接的,如果兩個明文分組相同,那么它們的密文分組也會不同。
??這里,我們使用了AES對稱加密算法,并使用了CBC模式實現(xiàn)登錄密碼的加密,實現(xiàn)步驟如下:
2、Vue前端步驟
2.1、安裝CryptoJS
npm install crypto-js
2.2、引入CryptoJS
import CryptoJS from 'crypto-js';
2.3、加密方法
//設置秘鑰和秘鑰偏移量
const SECRET_KEY = CryptoJS.enc.Utf8.parse("1234567890123456");
const SECRET_IV = CryptoJS.enc.Utf8.parse("1234567890123456");
/**
* 加密方法
* @param word
* @returns {string}
*/
function encrypt(word) {
let srcs = CryptoJS.enc.Utf8.parse(word);
let encrypted = CryptoJS.AES.encrypt(srcs, SECRET_KEY, {
iv: SECRET_IV ,
mode: CryptoJS.mode.CBC,
padding: CryptoJS.pad.ZeroPadding
})
return CryptoJS.enc.Base64.stringify(encrypted.ciphertext);
}
2.4、解密方法
??該方法,在前端一般是不需要的,前端只需要使用加密方法進行加密即可。
function decrypt(word) {
let base64 = CryptoJS.enc.Base64.parse(word);
let srcs = CryptoJS.enc.Base64.stringify(base64);
const decrypt = CryptoJS.AES.decrypt(srcs, SECRET_KEY, {
iv: SECRET_IV ,
mode: CryptoJS.mode.CBC,
padding: CryptoJS.pad.ZeroPadding
});
const decryptedStr = decrypt.toString(CryptoJS.enc.Utf8);
return decryptedStr;
}
3、Java實現(xiàn)解密的工具類
??CryptoUtil 工具類提供了基于前端CryptoJS一致的加密和解密方法,在后端主要使用到的其中的解密方法。
/**
* Description: 配合前端CryptoJS實現(xiàn)加密、解密工作。
* CryptoJS 是一個 JavaScript 庫,提供了一系列密碼學函數(shù)和工具,用于加密、解密、生成摘要等任務。
* 它支持多種加密算法,包括常見的對稱加密算法(如 AES、DES)和非對稱加密算法(如 RSA)。
*/
public class CryptoUtil {
private final static String IV = "1234567890123456";//需要前端與后端配置一致
private final static String KEY = "1234567890123456";
/**
* 加密算法,使用默認的IV、KEY
* @param content
* @return
*/
public static String encrypt(String content){
return encrypt(content,KEY,IV);
}
/**
* 解密算法,使用默認的IV、KEY
* @param content
* @return
*/
public static String decrypt(String content){
return decrypt(content,KEY,IV);
}
/**
* 加密方法
* @param content
* @param key
* @param iv
* @return
*/
public static String encrypt(String content, String key, String iv){
try{
// "算法/模式/補碼方式"NoPadding PkcsPadding
Cipher cipher = Cipher.getInstance("AES/CBC/NoPadding");
int blockSize = cipher.getBlockSize();
byte[] dataBytes = content.getBytes();
int plaintextLength = dataBytes.length;
if (plaintextLength % blockSize != 0) {
plaintextLength = plaintextLength + (blockSize - (plaintextLength % blockSize));
}
byte[] plaintext = new byte[plaintextLength];
System.arraycopy(dataBytes, 0, plaintext, 0, dataBytes.length);
SecretKeySpec keyspec = new SecretKeySpec(key.getBytes("UTF-8"), "AES");
IvParameterSpec ivspec = new IvParameterSpec(iv.getBytes("UTF-8"));
cipher.init(Cipher.ENCRYPT_MODE, keyspec, ivspec);
byte[] encrypted = cipher.doFinal(plaintext);
return new Base64().encodeToString(encrypted);
}catch (Exception e) {
throw new RuntimeException("加密算法異常 CryptoUtil encrypt()加密方法,異常信息:" + e.getMessage());
}
}
/**
* 解密方法
* @param content
* @param key
* @param iv
* @return
*/
public static String decrypt(String content, String key, String iv){
try {
byte[] encrypted1 = new Base64().decode(content);
Cipher cipher = Cipher.getInstance("AES/CBC/NoPadding");
SecretKeySpec keySpec = new SecretKeySpec(key.getBytes(), "AES");
IvParameterSpec ivSpec = new IvParameterSpec(iv.getBytes());
cipher.init(Cipher.DECRYPT_MODE, keySpec, ivSpec);
byte[] original = cipher.doFinal(encrypted1);
return new String(original).trim();
} catch (Exception e) {
throw new RuntimeException("加密算法異常 CryptoUtil decrypt()解密方法,異常信息:" + e.getMessage());
}
}
}
4、在SpringSecurity項目中如何使用CryptoUtil 解密工具類
??不同項目用戶密碼存儲方式,登錄密碼校驗都有自己的邏輯,在我的項目里,我使用了SpringSecurity框架作為鑒權,同時基于MD5實現(xiàn)了PasswordEncoder接口(QriverMD5PasswordEncoder),其中使用了DigestUtils.md5DigestAsHex()方法對用戶登錄密碼進行了加密保存。因此,我在PasswordEncoder接口的實現(xiàn)方法matches()中,實現(xiàn)了前端傳遞密碼的解密,然后再進行MD5加密后,參與到密碼的對比。QriverMD5PasswordEncoder的實現(xiàn)如下:文章來源:http://www.zghlxwxcb.cn/news/detail-759039.html
/**
* PasswordEncoder實現(xiàn)類,從5.0版本開始強制要求設置,主要用來配置加密方式
*/
@Component("md5PasswordEncoder")
public class QriverMD5PasswordEncoder implements PasswordEncoder {
@Override
public String encode(CharSequence rawPassword) {
return DigestUtils.md5DigestAsHex(rawPassword.toString().getBytes());
}
@Override
public boolean matches(CharSequence rawPassword, String encodedPassword) {
//前端登錄密碼解密
String deRawPassword = CryptoUtil.decrypt(rawPassword.toString());
String encode = DigestUtils.md5DigestAsHex(deRawPassword.toString().getBytes());
return encodedPassword.equals( encode );
}
public static void main(String[] args) {
String rawPassword = "EVFon/Y9ed2W/0zc6iQlkg==";
System.out.println(CryptoUtil.decrypt(rawPassword.toString()));
}
}
??至此就完成了前端加密傳遞登錄密碼,后端解密使用密碼,同時也保證了登錄密碼MD5加密存在數(shù)據(jù)庫中,關于PasswordEncoder 實現(xiàn)類如何配置,這里不再贅述。文章來源地址http://www.zghlxwxcb.cn/news/detail-759039.html
到了這里,關于使用CryptoJS實現(xiàn)Vue前端加密,Java后臺解密的步驟和方法的文章就介紹完了。如果您還想了解更多內(nèi)容,請在右上角搜索TOY模板網(wǎng)以前的文章或繼續(xù)瀏覽下面的相關文章,希望大家以后多多支持TOY模板網(wǎng)!