AES前后端的使用

前端

  使用 [email protected] 进行加密

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

  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);
};

后端


  • 依赖

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

       注意:前端使用 [email protected] 加密模式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=="));
    }

 

 

你可能感兴趣的:(spring-security)