欧美bbbwbbbw肥妇,免费乱码人妻系列日韩,一级黄片

java前后端加密解密crypto-js的實(shí)現(xiàn)

 更新時(shí)間:2023年05月22日 10:19:47   作者:漁夫搬磚  
這篇文章主要介紹了java前后端加密解密crypto-js的實(shí)現(xiàn),文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)學(xué)習(xí)吧

項(xiàng)目場(chǎng)景:

適用于接口數(shù)據(jù)敏感信息,比如 明文傳輸姓名、居住地址、手機(jī)號(hào)等信息,如果存在明文傳輸敏感數(shù)據(jù)問(wèn)題、及數(shù)據(jù)泄漏風(fēng)險(xiǎn),則可使用此方法加密解密。

一、下載crypot-js

1.如果為vue項(xiàng)目,安裝cnpm,采用命令安裝即可:

安裝命令:cnpm install crypto-js;

2.如果為一般web項(xiàng)目,https://github.com/brix/crypto-js,進(jìn)行下載;

二、前端引入crypto-js文件,并命名為secrt.js

import CryptoJS from 'crypto-js'
// 默認(rèn)的 KEY 與 iv 如果沒有給,可自行設(shè)定,但必須16位
const KEY = CryptoJS.enc.Utf8.parse("abcdefgh12345678");
const IV = CryptoJS.enc.Utf8.parse('12345678abcdefgh');
/**
?* AES加密 :字符串 key iv ?返回base64
?*/
export function Encrypt(word, keyStr, ivStr) {
? ? let key = KEY;
? ? let iv = IV;
? ? if (keyStr) {
? ? ? ? key = CryptoJS.enc.Utf8.parse(keyStr);
? ? ? ? iv = CryptoJS.enc.Utf8.parse(ivStr);
? ? }
? ? let srcs = CryptoJS.enc.Utf8.parse(word);
? ? var encrypted = CryptoJS.AES.encrypt(srcs, key, {
? ? ? ? iv: iv,
? ? ? ? mode: CryptoJS.mode.CBC,
? ? ? ? padding: CryptoJS.pad.ZeroPadding
? ? });
? ? return CryptoJS.enc.Base64.stringify(encrypted.ciphertext);
}
/**
?1. AES 解密 :字符串 key iv ?返回base64
?2. ?3. @return {string}
?*/
export function Decrypt(word, keyStr, ivStr) {
? ? let key = KEY;
? ? let iv = IV;
? ? if (keyStr) {
? ? ? ? key = CryptoJS.enc.Utf8.parse(keyStr);
? ? ? ? iv = CryptoJS.enc.Utf8.parse(ivStr);
? ? }
? ? let base64 = CryptoJS.enc.Base64.parse(word);
? ? let src = CryptoJS.enc.Base64.stringify(base64);
? ? let decrypt = CryptoJS.AES.decrypt(src, key, {
? ? ? ? iv: iv,
? ? ? ? mode: CryptoJS.mode.CBC,
? ? ? ? padding: CryptoJS.pad.ZeroPadding
? ? });
? ? let decryptedStr = decrypt.toString(CryptoJS.enc.Utf8);
? ? return decryptedStr.toString();
}

三、前端頁(yè)面引入并加密數(shù)據(jù)

import {Encrypt} from '../../secrt.js'
//text為需要加密的內(nèi)容
var userId= Encrypt(text);

四、后端解密數(shù)據(jù)

1.pom引用

2.創(chuàng)建工具類

import javax.crypto.Cipher;
import javax.crypto.spec.IvParameterSpec;
import javax.crypto.spec.SecretKeySpec;
import org.apache.commons.codec.binary.Base64;
public class SecretUtil {
? ? /***
? ? ?* key和iv值可以隨機(jī)生成,確保與前端的key,iv對(duì)應(yīng)
? ? ?*/
? ? private static String KEY = "abcdefgh12345678";
? ? private static String IV = "12345678abcdefgh";
? ? /***
? ? ?* 加密
? ? ?*?
? ? ?* @param data 要加密的數(shù)據(jù)
? ? ?* @return encrypt
? ? ?*/
? ? public static String encrypt(String data) {
? ? ? ? return encrypt(data, KEY, IV);
? ? }
? ? /***
? ? ?* ?解密
? ? ?* @param data 要解密的數(shù)據(jù)
? ? ?*/
? ? public static String desEncrypt(String data) {
? ? ? ? return desEncrypt(data, KEY, IV);
? ? }
? ? /**
? ? ?* 加密方法
? ? ?*/
? ? private static String encrypt(String data, String key, String iv) {
? ? ? ? try {
? ? ? ? ? ? // "算法/模式/補(bǔ)碼方式"NoPadding PkcsPadding
? ? ? ? ? ? Cipher cipher = Cipher.getInstance("AES/CBC/NoPadding");
? ? ? ? ? ? int blockSize = cipher.getBlockSize();
? ? ? ? ? ? byte[] dataBytes = data.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(), "AES");
? ? ? ? ? ? IvParameterSpec ivspec = new IvParameterSpec(iv.getBytes());
? ? ? ? ? ? cipher.init(Cipher.ENCRYPT_MODE, keyspec, ivspec);
? ? ? ? ? ? byte[] encrypted = cipher.doFinal(plaintext);
? ? ? ? ? ? return new Base64().encodeToString(encrypted);
? ? ? ? } catch (Exception e) {
? ? ? ? ? ? e.printStackTrace();
? ? ? ? ? ? return null;
? ? ? ? }
? ? }
? ? /**
? ? ?* 解密方法
? ? ?*/
? ? private static String desEncrypt(String data, String key, String iv) {
? ? ? ? try {
? ? ? ? ? ? byte[] encrypted1 = new Base64().decode(data);
? ? ? ? ? ? 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) {
? ? ? ? ? ? e.printStackTrace();
? ? ? ? ? ? return null;
? ? ? ? }
? ? }
}

3.數(shù)據(jù)解密

@GetMapping(value = "/userInfo")
 public getUserInfo(@RequestParam(name = "userId") String userId)
    String userId= desEncrypt(userId);
}

到此這篇關(guān)于java前后端加密解密crypto-js的實(shí)現(xiàn)的文章就介紹到這了,更多相關(guān)java 加密解密crypto-js內(nèi)容請(qǐng)搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!

相關(guān)文章

最新評(píng)論