栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 软件开发 > 后端开发 > Java

CBC加密 AES前端加密,Java后端解密

Java 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

CBC加密 AES前端加密,Java后端解密

CBC加密 AES前端加密,Java后端解密`

JAVA代码

package com.hbck.common.utils;

import com.alibaba.fastjson.JSONObject;

import javax.crypto.Cipher;
import javax.crypto.spec.IvParameterSpec;
import javax.crypto.spec.SecretKeySpec;
import java.util.Objects;

public class AESUtil {
    private final static String KEY = "c6b50dag9r645e63"; // key:必须16个字符,且要和前端保持一致
    private final static String IV = "0b0ef659ehb8ea3a";  // 偏移量:必须16个字符,且要和前端保持一致

//    public static void main(String[] args) {
//        JSONObject map = new JSONObject();
//        map.put("username","admin");
//        map.put("password","123456");
//        //加密
//        String encrypted = encrypt(map.toString());
//        //解密
//        String decrypted = decrypt("密文");
//        System.out.println("加密前:" + map);
//        System.out.println("加密后:" + encrypted);
//        System.out.println("解密后:" + decrypted);
//    }

    
    public static String encrypt(String content) {
        return parseByte2HexStr(Objects.requireNonNull(aesCbcEncrypt(content.getBytes(), KEY.getBytes(), IV.getBytes())));
    }

    
    public static String decrypt(String content) {
        return new String(Objects.requireNonNull(aesCbcDecrypt(parseHexStr2Byte(content), KEY.getBytes(), IV.getBytes())));
    }

    private static byte[] aesCbcEncrypt(byte[] content, byte[] keyBytes, byte[] iv) {
        try {
            SecretKeySpec key = new SecretKeySpec(keyBytes, "AES");
            //设置模式,编码,后端为PKCS5Padding,对应前端是Pkcs7
            Cipher cipher = Cipher.getInstance("AES/CBC/PKCS5Padding");
            cipher.init(Cipher.ENCRYPT_MODE, key, new IvParameterSpec(iv));
            return cipher.doFinal(content);
        } catch (Exception e) {
            System.out.println("exception:" + e.toString());
        }
        return null;
    }

    private static byte[] aesCbcDecrypt(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));
            return cipher.doFinal(content);
        } catch (Exception e) {
            System.out.println("exception:" + e.toString());
        }
        return null;
    }

    
    public static String parseByte2HexStr(byte[] buf) {
        StringBuilder sb = new StringBuilder();
        for (byte b : buf) {
            String hex = Integer.toHexString(b & 0xFF);
            if (hex.length() == 1) {
                hex = '0' + hex;
            }
            sb.append(hex.toUpperCase());
        }
        return sb.toString();
    }

    
    public static byte[] parseHexStr2Byte(String hexStr) {
        if (hexStr.length() < 1) {
            return null;
        }
        byte[] result = new byte[hexStr.length() / 2];
        for (int i = 0; i < hexStr.length() / 2; i++) {
            int high = Integer.parseInt(hexStr.substring(i * 2, i * 2 + 1), 16);
            int low = Integer.parseInt(hexStr.substring(i * 2 + 1, i * 2 + 2), 16);
            result[i] = (byte) (high * 16 + low);
        }
        return result;
    }
}

VUE代码

import CryptoJS from 'crypto-js'
// 需要npm crypto-js
export default {
    // AES加密
    encrypt(data) {
        let key = CryptoJS.enc.Utf8.parse('c6b50dag9r645e63') // key:必须16个字符
        let iv = CryptoJS.enc.Utf8.parse('0b0ef659ehb8ea3a') // 偏移量:必须16个字符    
        let encrypted = CryptoJS.AES.encrypt(data, key, {
            iv: iv,
            mode: CryptoJS.mode.CBC,
            padding: CryptoJS.pad.Pkcs7
        });
        // 返回的是base64格式的密文
        return encrypted.ciphertext.toString().toUpperCase();
    },

    // AES解密
    decrypt(data) {
        let key = CryptoJS.enc.Utf8.parse('c6b50dag9r645e63') // key:必须16个字符
        let iv = CryptoJS.enc.Utf8.parse('0b0ef659ehb8ea3a') // 偏移量:必须16个字符   
        let encryptedHexStr = CryptoJS.enc.Hex.parse(data);
        let srcs = CryptoJS.enc.Base64.stringify(encryptedHexStr);
        let decrypted = CryptoJS.AES.decrypt(srcs, key, {
            iv: iv,
            mode: CryptoJS.mode.CBC,
            padding: CryptoJS.pad.Pkcs7
        });
        let decryptedStr = decrypted.toString(CryptoJS.enc.Utf8);
        return decryptedStr.toString();
    }
}

总结
转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/845659.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号