Java教程

Java AES CBC模式加解密

本文主要是介绍Java AES CBC模式加解密,对大家解决编程问题具有一定的参考价值,需要的程序猿们随着小编来一起学习吧!

密钥key是前端传递过来的密码,由于前端的密码长度是不固定的,AES CBC模式下的加解密只支持16/32/64位,所以先将密码进行MD5加密生成32位字符串再进行加解密。

import org.apache.commons.codec.binary.Base64;

import javax.crypto.Cipher;
import javax.crypto.spec.IvParameterSpec;
import javax.crypto.spec.SecretKeySpec;
import java.security.MessageDigest;

public class AESUtils {

    private static final String initVector = "encryptionIntVec";

    /**
     * 根据密码进行文件内容加密
     *
     * @param password 密码
     * @param value    文件内容
     * @return 先对前端输入的 md5 加密 生成32位字符串
     * @throws Exception
     */
    public static String encrypt(String password, String value) {
        try {
            String key = md5(password);
            IvParameterSpec iv = new IvParameterSpec(initVector.getBytes("UTF-8"));
            SecretKeySpec skeySpec = new SecretKeySpec(key.getBytes("UTF-8"), "AES");

            Cipher cipher = Cipher.getInstance("AES/CBC/PKCS5PADDING");
            cipher.init(Cipher.ENCRYPT_MODE, skeySpec, iv);

            byte[] encrypted = cipher.doFinal(value.getBytes());
            return Base64.encodeBase64String(encrypted);
        } catch (Exception ex) {
            ex.printStackTrace();
        }
        return null;
    }


    public static String decrypt(String password, String encrypted) {
        try {
            String key = md5(password);
            IvParameterSpec iv = new IvParameterSpec(initVector.getBytes("UTF-8"));
            SecretKeySpec skeySpec = new SecretKeySpec(key.getBytes("UTF-8"), "AES");
            Cipher cipher = Cipher.getInstance("AES/CBC/PKCS5PADDING");
            cipher.init(Cipher.DECRYPT_MODE, skeySpec, iv);
            byte[] original = cipher.doFinal(Base64.decodeBase64(encrypted));
            return new String(original);
        } catch (Exception ex) {
            ex.printStackTrace();
        }

        return null;
    }

    /***
     * MD5加密 生成32位md5码
     * @param inStr 待加密字符串
     * @return 返回32位md5码
     */
    public static String md5(String inStr) throws Exception {
        MessageDigest md5 = null;
        try {
            md5 = MessageDigest.getInstance("MD5");
        } catch (Exception e) {
            System.out.println(e.toString());
            e.printStackTrace();
            return "";
        }

        byte[] byteArray = inStr.getBytes("UTF-8");
        byte[] md5Bytes = md5.digest(byteArray);
        StringBuffer hexValue = new StringBuffer();
        for (int i = 0; i < md5Bytes.length; i++) {
            int val = ((int) md5Bytes[i]) & 0xff;
            if (val < 16) {
                hexValue.append("0");
            }
            hexValue.append(Integer.toHexString(val));
        }
        return hexValue.toString();
    }


    public static void main(String[] args) throws Exception {
        String password = "123456789&";
        System.out.println(md5(password));
        String originalString = "62647476889578背后的而韩国回复";
        System.out.println("Original String to encrypt:" + originalString);
        String encryptedString = encrypt(password, originalString);
        System.out.println("Encrypted String:" + encryptedString);
        System.out.println(decrypt(password, encryptedString));
    }
}
这篇关于Java AES CBC模式加解密的文章就介绍到这儿,希望我们推荐的文章对大家有所帮助,也希望大家多多支持为之网!