AES前后端的使用

前端

  使用 crypto-js@4.0.0 进行加密

  详细配置介绍参考下面后端的内容

  CryptoJS支持AES-128,AES-192和AES-256,后端支持AES-512。它将根据您传入的密钥的大小来选择。

  例如 :key和iv都为32字节,为256位即AES-256 。

import CryptoJS from "crypto-js";

/* 都是字符类型
 * key和iv 可选 128 位 (16个字节)/192 位 (24个字节)/256 位 (32个字节) 
 * 都是字符类型
 * content加密的内容
 */
function encrypt (key, iv, content) {
  key = CryptoJS.enc.Utf8.parse(key);
  iv = CryptoJS.enc.Utf8.parse(iv);
  return CryptoJS.AES.encrypt(content, key, {
    iv: iv,
    mode: CryptoJS.mode.OFB,
    padding: CryptoJS.pad.Pkcs7
  }).toString();
};

/* 都是字符类型
 * key和iv 可选 128 位 (16个字节)/192 位 (24个字节)/256 位 (32个字节) 
 * cipherText加密后的内容
 */
function decrypt (key, iv, cipherText) {
  key = CryptoJS.enc.Utf8.parse(key);
  iv = CryptoJS.enc.Utf8.parse(iv);
  return CryptoJS.AES.decrypt(cipherText, key, {
    iv: iv,
    mode: CryptoJS.mode.OFB,
    padding: CryptoJS.pad.Pkcs7
  }).toString(CryptoJS.enc.Utf8);
};

后端


  • 依赖
<dependency>
  <groupId>org.bouncycastle</groupId>
  <artifactId>bcprov-jdk15on</artifactId>
  <version>1.65.01</version>
</dependency>
  • 代码
  • key和iv长度的计算 可选 128 位 (16个字节)/192 位 (24个字节)/256 位 (32个字节)

       注意:前端使用 crypto-js@4.0.0 加密模式CBC测试加密和后端不一致(测试和后端无关),替换为其它模式。

import org.bouncycastle.crypto.BufferedBlockCipher;
import org.bouncycastle.crypto.InvalidCipherTextException;
import org.bouncycastle.crypto.engines.AESEngine;
import org.bouncycastle.crypto.modes.CBCBlockCipher;
import org.bouncycastle.crypto.paddings.PKCS7Padding;
import org.bouncycastle.crypto.paddings.PaddedBufferedBlockCipher;
import org.bouncycastle.crypto.params.KeyParameter;
import org.bouncycastle.crypto.params.ParametersWithIV;

public class AESUtils {

    private AESUtils() {
    }

    /**
     * 加密为: AES (AESEngine) / 128 / OFB (OFBBlockCipher) / Pkcs7 (PKCS7Padding)
     * key和iv长度保持一致
     * @param forEncryption 如果为true,则初始化密码进行加密;如果为false,则进行解密。
     * @param secretKey     密钥 可选 128 位 (16个字节)/192 位 (24个字节)/256 位 (32个字节)
     * @param iv            iv 可选 128 位 (16个字节)/192 位 (24个字节)/256 位 (32个字节)
     * @param content       解析的内容
     * @return 解析的内容
     * @throws InvalidCipherTextException 解析异常
     */
    public static String parser(boolean forEncryption, String secretKey, String iv, String content) throws InvalidCipherTextException {
        AESEngine aes = new AESEngine();
        BufferedBlockCipher cipher = new PaddedBufferedBlockCipher(new OFBBlockCipher(aes, aes.getBlockSize() * 8), new PKCS7Padding());
        byte[] secretKeyBytes = Arrays.copyOf(secretKey.getBytes(UTF_8), 16);
        byte[] ivBytes = Arrays.copyOf(iv.getBytes(UTF_8), 16);
        cipher.init(forEncryption, new ParametersWithIV(new KeyParameter(secretKeyBytes), ivBytes));
        byte[] contentBytes;
        if (forEncryption) {
            contentBytes = content.getBytes();
        } else {
            contentBytes = Hex.decode(content);
        }
        byte[] parsing = new byte[cipher.getOutputSize(contentBytes.length)];
        int length = cipher.processBytes(contentBytes, 0, contentBytes.length, parsing, 0);
        length += cipher.doFinal(parsing, length);
        byte[] parsingBytes = Arrays.copyOf(parsing, length);
        if (forEncryption) {
            return Hex.toHexString(parsingBytes);
        } else {
            return new String(parsingBytes, UTF_8);
        }
    }

    public static void main(String[] args) throws InvalidCipherTextException {
        String content = "1024";
        String key = "1234567812345678";
        String iv = "1234567887654321";
        System.out.println("加密:" + parser(true, key, iv, content));
        System.out.println("解密:" + parser(false, key, iv, "9kxLYFZe4SWoWVk6FZampA=="));
    }

 

 

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值