- 文章目录
- 1.uniapp前端加解密
- 1.1 导入crypto-js.js工具
- 1.2 加解密工具函数
- 1.3 界面调用加解密
- 2.SpringBoot后端加解密
- 2.1 加解密工具类
- 2.2 加解密调用
终端导入crypto-js,执行一下命令
npm install crypto-js
成功后出现如图目录结构
uniapp新建utils目录
在utils目录下创建secret.js文件,文件内容如下:
//------------前后端用crypto.js加解密
//引入下载好的js文件
import CryptoJS from '../node_modules/crypto-js/crypto-js.js'
//---------------------加密方式
// 默认的 KEY 与 iv
const KEY = CryptoJS.enc.Utf8.parse('ABCDEFGHIJKLMNOP') // ''中与后台一样 密码
const IV = CryptoJS.enc.Utf8.parse('ABCDEFGHIJKLMNOP') // ''中与后台一样,
export function encrypt(word) {
const key = KEY
const iv = IV
const 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)
}
export function decrypt(word) {
const key = KEY
const iv = IV
const base64 = CryptoJS.enc.Base64.parse(word)
const src = CryptoJS.enc.Base64.stringify(base64)
var decrypt = CryptoJS.AES.decrypt(src, key, {
iv: iv,
mode: CryptoJS.mode.CBC,
padding: CryptoJS.pad.ZeroPadding
})
var decryptedStr = decrypt.toString(CryptoJS.enc.Utf8)
return decryptedStr.toString()
}
1.3 界面调用加解密
1.3.1 界面引用
//方式1,绝对定位
import {encrypt,decrypt} from './utils/secret.js'
//方式2,绝对定位
import {encrypt,decrypt} from '@/utils/secret.js'
//方式3,相对定位
import {encrypt,decrypt } from '../../utils/secret.js'
1.3.2 调用加解密
let newpass=encrypt(password);//把密码加密后的密码2.SpringBoot后端加解密 2.1 加解密工具类
MySelfEDUtil.java 工具类
package com.cn.igwapi.common.util;
import org.apache.commons.codec.binary.Base64;
import javax.crypto.Cipher;
import javax.crypto.spec.IvParameterSpec;
import javax.crypto.spec.SecretKeySpec;
public class MySelfEDUtil{
// 使用AES-128-CBC加密模式,key需要为16位,key和iv可以相同!
//密钥必须与前台的密钥保持一致
private static String KEY = "ABCDEFGHIJKLMNOP";
private static String IV = "ABCDEFGHIJKLMNOP";
public static String encrypt(String data, String key, String iv) throws Exception
{
try
{
Cipher cipher = Cipher.getInstance("AES/CBC/NoPadding");// "算法/模式/补码方式"NoPadding PkcsPadding
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;
}
}
public static String desEncrypt(String data, String key, String iv) throws Exception
{
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);
String originalString = new String(original);
return originalString;
}
catch (Exception e)
{
e.printStackTrace();
return null;
}
}
public static String encrypt(String data) throws Exception
{
return encrypt(data, KEY, IV);
}
public static String desEncrypt(String data) throws Exception
{
return desEncrypt(data, KEY, IV);
}
}
2.2 加解密调用
Main方法调试加解密
public static void main(String [] args){
try {
String realPassword = AesEncryptUtil.encrypt("123456");
System.out.println("后端加密:"+realPassword);
String jm=AesEncryptUtil.desEncrypt(realPassword);
System.out.println("解密后:"+jm.trim());
}catch (Exception e){
e.printStackTrace();
}
}
运行结果
至此前后端对应加解密就完成了,留个记录!!!



