使用AES加密并进行Base64转码

AES 加密,编码使用UTF-8,然后 BASE64 转码,再然后进行urlEncode, 使用utf-8 字符集

今天遇到这样的需求:
接口请求传输数据须做AES 加密, 编码使用UTF-8,进行BASE64 转码
加密模式:AES/CBC/PKCS5Padding
加密初始化向量:长度为16 的空字节数组
加密后并进行urlEncode, 使用utf-8 字符集

知识点:

1. AES加密

javax.crypto.spec.SecretKeySpec skeySpec = new SecretKeySpec(raw, "AES");

2. 加密模式:AES/CBC/PKCS5Padding

javax.crypto.Cipher.Cipher cipher = Cipher.getInstance("AES/CBC/PKCS5Padding");

3. 加密初始化向量:长度为16 的空字节数组

IvParameterSpec IV = new IvParameterSpec(new byte[16]);
cipher.init(Cipher.ENCRYPT_MODE, skeySpec, IV);

4. 编码使用 utf-8

byte[] encrypt = cipher.doFinal(content.getBytes(Charsets.UTF_8));

5. BASE64 转码

Base64.encodeBase64(encrypt)

6. 加密后并进行urlEncode, 使用utf-8 字符集

String encode = URLEncoder.encode(encrypt, "UTF-8");

代码示例(包含Base64转码再解密的解密方式):

package com.sc.qunarservice.util;

import com.google.common.base.Charsets;
import org.apache.commons.codec.binary.Base64;

import javax.crypto.Cipher;
import javax.crypto.spec.IvParameterSpec;
import javax.crypto.spec.SecretKeySpec;

/**
 * @author zcy
 * @date 2020/3/27
 */
public class AES {
    /**
     * 测试秘钥
     */
    private final static String KEY = "1234567890123456";


    /**
     * AES加密并进行Base64转码
     * @param content
     * @return
     * @throws Exception
     */
    public static String encrypt(String content) throws Exception {
        if (KEY == null) {
            System.out.print("Key为空 null");
            return null;
        }
        // 判断Key是否为16位
        if (KEY.length() != 16) {
            System.out.print("Key长度不是16位");
            return null;
        }
        byte[] raw = KEY.getBytes();
        SecretKeySpec skeySpec = new SecretKeySpec(raw, "AES");
        Cipher cipher = Cipher.getInstance("AES/CBC/PKCS5Padding");
        cipher.init(Cipher.ENCRYPT_MODE, skeySpec,new IvParameterSpec(new byte[16]));
        byte[] encrypt2Base64 = cipher.doFinal(content.getBytes(Charsets.UTF_8));
        return new String(Base64.encodeBase64(encrypt2Base64));
    }

    /**
     * Base64转码后AES解密
     * @param content
     * @return
     * @throws Exception
     */
    public static String decrypt(String content) throws Exception {
        if (KEY == null) {
            System.out.print("Key为空 null");
            return null;
        }
        // 判断Key是否为16位
        if (KEY.length() != 16) {
            System.out.print("Key长度不是16位");
            return null;
        }
        byte[] raw = KEY.getBytes();
        SecretKeySpec skeySpec = new SecretKeySpec(raw, "AES");
        Cipher cipher = Cipher.getInstance("AES/CBC/PKCS5Padding");
        cipher.init(Cipher.DECRYPT_MODE, skeySpec,new IvParameterSpec(new byte[16]));
        //先用Base64解密
        byte[] decodeBase64 = Base64.decodeBase64(content.getBytes(Charsets.UTF_8));
        byte[] original = cipher.doFinal(decodeBase64);
        String decryptString = new String(original, Charsets.UTF_8);
        return decryptString;
    }
}

	@Test
    public void testEncrypt(){
        try {
        	String s = "你想要AES加密并进行Base64转码的内容";
            String encrypt = AES.encrypt(s);
            System.out.println("URLEncoder加密内容,使用 utf-8 字符集");
            String encode = URLEncoder.encode(encrypt, "UTF-8");
            System.out.println(encode);

        } catch (Exception e) {
            e.printStackTrace();
        }

    }

你可能感兴趣的:(加密解密转码压缩,java)