前端密码加密十六位十六进制数作为密钥偏移量,后端Java解码

10 篇文章 0 订阅
5 篇文章 0 订阅

前端密码加密十六位十六进制数作为密钥偏移量,后端Java解码,前端使用CryptoJS,亲测可用 ,注意事项注意事项key和iv都是十六位

注意事项key和iv都是十六位
注意事项key和iv都是十六位
注意事项key和iv都是十六位
前端需要安装CryptoJs,不懂的可以执行百度,google都行,非常简单,一行命令
安装命令:

npm install crypto-js

增加一个js文件 编写加解密代码

//引入刚刚安装的crypto-js
import CryptoJS from '../node_modules/crypto-js/crypto-js.js';

//十六位十六进制数作为密钥
const securityKey = CryptoJS.enc.Utf8.parse("****************");  

//十六位十六进制数作为密钥偏移量
const securityIv = CryptoJS.enc.Utf8.parse('****************');   


// 解密方法
function decrypt(content,key,iv) {
	return CryptoJS.AES.decrypt(content, key, {
	    mode: CryptoJS.mode.CBC,
	    iv: iv,
	    padding: CryptoJS.pad.Pkcs7
	  }).toString(CryptoJS.enc.Utf8);
}
 
// 加密方法
function encrypt(content,key,iv) {
	const newContent = CryptoJS.enc.Utf8.parse(content);
	return CryptoJS.AES.encrypt(newContent, key, {
	  mode: CryptoJS.mode.CBC,
	  iv: iv,
	  padding: CryptoJS.pad.Pkcs7
	}).toString();
}
export default {
    decrypt,
    encrypt,
	securityKey,
	securityIv
}


java 后台加解密

package com.fast.cloud.common.core.util;

import sun.misc.BASE64Decoder;

import javax.crypto.Cipher;
import javax.crypto.spec.IvParameterSpec;
import javax.crypto.spec.SecretKeySpec;
import org.apache.commons.lang3.StringUtils;
import sun.misc.BASE64Encoder;
import java.nio.charset.StandardCharsets;



/**
 * @author 
 * AES工具类
 */
public class AESUtil {

    private static final String ALGORITHMSTR = "AES/CBC/PKCS5Padding";
    /**
     * base 64 decode
     * @param base64Code 待解码的base 64 code
     * @return 解码后的byte[]
     * @throws Exception
     */
    public static byte[] base64Decode(String base64Code) throws Exception{
        return StringUtils.isEmpty(base64Code) ? null : new BASE64Decoder().decodeBuffer(base64Code);
    }

    /**
     * AES解密
     * @param content 待解密的
     * @return 解密后的String
     * @throws Exception Exception
     */
    public static String aesDecrypt(String content,String key,String iv) throws Exception {
        if (StringUtils.isNotBlank(content)){
            Cipher cipher = Cipher.getInstance(ALGORITHMSTR);
            byte[] temp = iv.getBytes(StandardCharsets.UTF_8);
            IvParameterSpec ivParameterSpec = new IvParameterSpec(temp);
            cipher.init(Cipher.DECRYPT_MODE, new SecretKeySpec(key.getBytes(), "AES"), ivParameterSpec);
            byte[] decryptBytes = cipher.doFinal(base64Decode(content));
            return new String(decryptBytes);
        }
        return null;
    }

    /**
     * AES加密
     * @param content 待加密的
     * @return 加密后的String
     * @throws Exception Exception
     */
    public static String aesEncrypt(String content,String key,String iv) throws Exception {
        if (StringUtils.isNotBlank(content)){
            Cipher cipher = Cipher.getInstance(ALGORITHMSTR);
            byte[] temp = iv.getBytes(StandardCharsets.UTF_8);
            IvParameterSpec ivParameterSpec = new IvParameterSpec(temp);
            cipher.init(Cipher.ENCRYPT_MODE, new SecretKeySpec(key.getBytes(), "AES"), ivParameterSpec);
            byte[] encryptBytes = cipher.doFinal(content.getBytes());
            return base64Encoder(encryptBytes);
        }
        return null;
    }

    /**
     * base 64 加码
     */
    public static String base64Encoder(byte[] base64Code){
        return new BASE64Encoder().encodeBuffer(base64Code);
    }

}

key 和iv都是16位哦

  • 1
    点赞
  • 4
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

IT界的渣

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值