提供一个AES128位/CBC模式加解密工具类

每次都从头写太烦了,直接贴出来作为备忘
package com.lihong.DDPush;

import sun.misc.BASE64Decoder;
import sun.misc.BASE64Encoder;

import javax.crypto.Cipher;
import javax.crypto.KeyGenerator;
import javax.crypto.SecretKey;
import javax.crypto.spec.IvParameterSpec;
import javax.crypto.spec.SecretKeySpec;
import java.io.UnsupportedEncodingException;
import java.security.SecureRandom;
import java.util.Arrays;
import java.util.Random;

/**
 * Created by lihong10 on 2017/8/16.
 */
public class AESUtils {

    //算法/模式/填充
    private static final String CipherMode = "AES/CBC/PKCS5Padding";
    private static final String DEFAULST_SECURITY_KEY = "XXX-123-!@#$%^&*()_+=,./";
    private static final String encodFormat = "UTF-8";

    /**
     * 创建密钥
     *
     * @param key
     * @return
     */
    private static SecretKeySpec createKey(String key) {
        key = key == null ? "" : key;
        StringBuilder sb = new StringBuilder(16);
        sb.append(key);
        while (sb.length() < 16) {
            sb.append("0");
        }
        if (sb.length() > 16) {
            sb.setLength(16);
        }

        byte[] data = null;
        try {
            data = sb.toString().getBytes("UTF-8");
        } catch (UnsupportedEncodingException e) {
            e.printStackTrace();
        }
        return new SecretKeySpec(data, "AES");
    }

    /**
     * 创建初始化向量
     *
     * @param password
     * @return
     */
    private static IvParameterSpec createIV(String password) {
        password = password == null ? "" : password;
        StringBuilder sb = new StringBuilder(16);
        sb.append(password);
        while (sb.length() < 16) {
            sb.append("0");
        }
        if (sb.length() > 16) {
            sb.setLength(16);
        }

        byte[] data = null;
        try {
            data = sb.toString().getBytes("UTF-8");
        } catch (UnsupportedEncodingException e) {
            e.printStackTrace();
        }
        return new IvParameterSpec(data);
    }

    /**
     * AES-128/CBC算法加密字节数据
     *
     * @param content
     * @param password 密钥
     * @param iv       初始化向量
     * @return byte[]
     */
    public static byte[] aes128CBCEncrypt(byte[] content, String password, String iv) {
        try {
            Cipher cipher = Cipher.getInstance(CipherMode);
            cipher.init(Cipher.ENCRYPT_MODE, createKey(password), createIV(iv));
            byte[] result = cipher.doFinal(content);
            return result;
        } catch (Exception e) {
            e.printStackTrace();
        }
        return null;
    }


    /**
     * AES-128/CBC算法解密字节数组
     *
     * @param content
     * @param password 密钥
     * @param iv       初始化向量
     * @return byte[]
     */
    public static byte[] aes128CBCDecrypt(byte[] content, String password, String iv) {
        try {
            Cipher cipher = Cipher.getInstance(CipherMode);
            cipher.init(Cipher.DECRYPT_MODE, createKey(password), createIV(iv));
            byte[] result = cipher.doFinal(content);
            return result;
        } catch (Exception e) {
            e.printStackTrace();
        }
        return null;
    }


    /**
     * AES-128加密字符串
     *
     * @param content
     * @return
     */
    public static String encrypt(String content) {
        try {
            KeyGenerator kgen = KeyGenerator.getInstance("AES");
            kgen.init(128, new SecureRandom(DEFAULST_SECURITY_KEY.getBytes(encodFormat)));
            byte[] bytes = kgen.generateKey().getEncoded();
            Cipher cipher = Cipher.getInstance("AES");
            cipher.init(Cipher.ENCRYPT_MODE, new SecretKeySpec(bytes, "AES"));
            byte[] result = cipher.doFinal(content.getBytes(encodFormat));
            StringBuffer sb = new StringBuffer();
            for (int i = 0; i < result.length; i++) {
                String hex = Integer.toHexString(result[i] & 0xFF);
                if (hex.length() == 1) {
                    hex = '0' + hex;
                }
                sb.append(hex.toUpperCase());
            }
            return sb.toString();
        } catch (Exception e) {
            e.printStackTrace();
        }
        return null;
    }

    /**
     * AES-128 CBC加密方式, 加密后使用Base64转码
     *
     * @param content        待加密内容
     * @param encodingFormat
     * @param key            密钥
     * @param initVector     初始化向量
     * @return
     * @throws Exception
     */
    public static String aesCBCEncrypt(String content, String encodingFormat, String key, String initVector) {
        try {
            SecretKeySpec keySpec = new SecretKeySpec(key.getBytes(encodingFormat), "AES");
            //使用CBC模式,需要一个向量iv,可增加加密算法的强度
            IvParameterSpec vector = new IvParameterSpec(initVector.getBytes(encodingFormat));
            Cipher cipher = Cipher.getInstance("AES/CBC/PKCS5Padding");
            cipher.init(Cipher.ENCRYPT_MODE, keySpec, vector);
            byte[] encrypted = cipher.doFinal(content.getBytes(encodingFormat));
            //此处使用BASE64做转码。
            String result = new BASE64Encoder().encode(encrypted);
            return result;
        } catch (Exception e) {
            e.printStackTrace();
        }
        return null;
    }

    /**
     * AES-128 CBC解密方式
     *
     * @param content        待解密的Base64字符串
     * @param encodingFormat
     * @param key            密钥
     * @param initVector     初始化向量
     * @return
     */
    public static String aesCBCDecrypt(String content, String encodingFormat, String key, String initVector) {
        try {
            SecretKeySpec keySpec = new SecretKeySpec(key.getBytes(encodingFormat), "AES");
            IvParameterSpec vector = new IvParameterSpec(initVector.getBytes());
            Cipher cipher = Cipher.getInstance("AES/CBC/PKCS5Padding");
            cipher.init(Cipher.DECRYPT_MODE, keySpec, vector);
            //先用base64编码,因为对应的加密使用Base64解码
            byte[] base64Bytes = new BASE64Decoder().decodeBuffer(content);
            byte[] original = cipher.doFinal(base64Bytes);
            String result = new String(original, encodingFormat);
            return result;
        } catch (Exception e) {
            e.printStackTrace();
        }
        return null;
    }


    public static String encrypt(String content, String key) {
        if (isEmpty(key)) {
            return encrypt(content);
        }
        try {
            KeyGenerator kgen = KeyGenerator.getInstance("AES");
            kgen.init(128, new SecureRandom(key.getBytes(encodFormat)));
            byte[] bytes = kgen.generateKey().getEncoded();
            Cipher cipher = Cipher.getInstance("AES");
            cipher.init(Cipher.ENCRYPT_MODE, new SecretKeySpec(bytes, "AES"));
            byte[] result = cipher.doFinal(content.getBytes(encodFormat));
            StringBuffer sb = new StringBuffer();
            for (int i = 0; i < result.length; i++) {
                String hex = Integer.toHexString(result[i] & 0xFF);
                if (hex.length() == 1) {
                    hex = '0' + hex;
                }
                sb.append(hex.toUpperCase());
            }
            return sb.toString();
        } catch (Exception e) {
            e.printStackTrace();
        }

        return null;
    }

    /**
     * 解密
     *
     * @param content
     * @return
     */
    public static String decrypt(String content) {
        if (isEmpty(content)) {
            return null;
        }
        byte[] bytes = new byte[content.length() / 2];
        for (int i = 0; i < content.length() / 2; i++) {
            int high = Integer.parseInt(content.substring(i * 2, i * 2 + 1), 16);
            int low = Integer.parseInt(content.substring(i * 2 + 1, i * 2 + 2), 16);
            bytes[i] = (byte) (high * 16 + low);
        }
        try {
            KeyGenerator kgen = KeyGenerator.getInstance("AES");
            kgen.init(128, new SecureRandom(DEFAULST_SECURITY_KEY.getBytes()));
            SecretKey secretKey = kgen.generateKey();
            SecretKeySpec secretKeySpec = new SecretKeySpec(secretKey.getEncoded(), "AES");
            Cipher cipher = Cipher.getInstance("AES");
            cipher.init(Cipher.DECRYPT_MODE, secretKeySpec);
            byte[] result = cipher.doFinal(bytes);
            return new String(result);
        } catch (Exception e) {
            e.printStackTrace();
        }

        return null;
    }

    /**
     * 解密
     *
     * @param content
     * @param securityKey
     * @return
     */
    public static String decrypt(String content, String securityKey) {

        if (isEmpty(securityKey)) {
            return decrypt(content);
        }
        byte[] bytes = new byte[content.length() / 2];
        for (int i = 0; i < content.length() / 2; i++) {
            int high = Integer.parseInt(content.substring(i * 2, i * 2 + 1), 16);
            int low = Integer.parseInt(content.substring(i * 2 + 1, i * 2 + 2), 16);
            bytes[i] = (byte) (high * 16 + low);
        }
        try {
            KeyGenerator kgen = KeyGenerator.getInstance("AES");
            kgen.init(128, new SecureRandom(securityKey.getBytes()));
            SecretKey secretKey = kgen.generateKey();
            SecretKeySpec secretKeySpec = new SecretKeySpec(secretKey.getEncoded(), "AES");
            Cipher cipher = Cipher.getInstance("AES");
            cipher.init(Cipher.DECRYPT_MODE, secretKeySpec);
            byte[] result = cipher.doFinal(bytes);
            return new String(result);
        } catch (Exception e) {
            e.printStackTrace();
        }

        return null;
    }

    private static boolean equal(byte[] a1, byte[] a2) {
        if (a1 != null && a2 != null && a1.length == a2.length) {
            for (int i = 0; i < a1.length; i++) {
                if (a1[i] != a2[i]) {
                    return false;
                }
            }
            return true;
        }
        return false;
    }

    private static boolean isEmpty(String str) {
        return str == null || str.length() == 0;
    }


    public static void main(String... args) throws UnsupportedEncodingException {

        String initVector = "AaBbCcDd1234!@#$";
        String key = "WwXxYyZz1234!@#$";

        int total = 100000;
        int run = 0;
        int count = 0;
        String[] strArr = new String[]{
                "//\\\\+++、、",
                "+++、、//\\++!",
                "++!@#%¥……,。、……&*(",
                "【】)&(\\\" + \"中国",
                "事实上",
                "&(\\\" + \"中国谁哟的事实上122你的",
                "12343567"};
        Random random = new Random(System.currentTimeMillis());
        for (int i = 0; i < total; i++) {
            run++;
            StringBuilder builder = new StringBuilder();
            builder.append(random.nextInt(strArr.length));
            builder.append(random.nextInt(strArr.length));
            builder.append(random.nextInt(strArr.length));
            builder.append(random.nextInt(strArr.length));
            builder.append(random.nextInt(strArr.length));
            byte[] original = builder.toString().getBytes("UTF-8");
            byte[] encrypted = aes128CBCEncrypt(original, key, initVector);
            byte[] decrypted = aes128CBCDecrypt(encrypted, key, initVector);
            if (equal(original, decrypted)) {
                count++;
            } else {
                System.out.println("字符串加解密后内容不一致");
                System.out.println("原始字符串:  " + Arrays.toString(encrypted));
                System.out.println("解密字符串:  " + Arrays.toString(decrypted));
            }
        }
        if (total == count) {
            System.out.println("测试全部通过");
        } else {
            System.out.println("加解密出现缺陷数据");
        }

        System.out.println("运行总次数是: " + run);

    }


}
  • 0
    点赞
  • 6
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
当然,我可以为您提供一个使用Golang编写的AES/ECB/PKCS5Padding的加密解密方法工具类。下面是示例代码: ```go package main import ( "crypto/aes" "crypto/cipher" "encoding/base64" "fmt" ) func main() { key := []byte("0123456789abcdef") plaintext := []byte("Hello, World!") ciphertext, err := encrypt(key, plaintext) if err != nil { fmt.Println("Error encrypting:", err) return } fmt.Println("Ciphertext:", base64.StdEncoding.EncodeToString(ciphertext)) decryptedText, err := decrypt(key, ciphertext) if err != nil { fmt.Println("Error decrypting:", err) return } fmt.Println("Decrypted text:", string(decryptedText)) } func encrypt(key, plaintext []byte) ([]byte, error) { block, err := aes.NewCipher(key) if err != nil { return nil, err } // Add PKCS5 padding to the plaintext padding := aes.BlockSize - len(plaintext)%aes.BlockSize padText := append(plaintext, bytes.Repeat([]byte{byte(padding)}, padding)...) ciphertext := make([]byte, len(padText)) // ECB mode does not require an IV mode := cipher.NewCBCEncrypter(block, key[:aes.BlockSize]) mode.CryptBlocks(ciphertext, padText) return ciphertext, nil } func decrypt(key, ciphertext []byte) ([]byte, error) { block, err := aes.NewCipher(key) if err != nil { return nil, err } // ECB mode does not require an IV mode := cipher.NewCBCDecrypter(block, key[:aes.BlockSize]) mode.CryptBlocks(ciphertext, ciphertext) // Remove PKCS5 padding padding := ciphertext[len(ciphertext)-1] return ciphertext[:len(ciphertext)-int(padding)], nil } ``` 在这个示例中,我们使用了crypto/aes和crypto/cipher库来进行AES加密和解密。`encrypt`函数接受一个密钥和明文作为输入,并返回加密后的密文。`decrypt`函数接受同样的密钥和密文,返回解密后的明文。 请注意,这里使用的是ECB模式,这是一种基本的加密模式,不含有初始化向量(IV)。在实际应用中,推荐使用更安全的加密模式,如CBC模式,并生成随机的IV。 希望这个示例对您有帮助!如有任何疑问,请随时提问。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值