java中使用AES加密(加密模式为CBC,填充方式:AES/CBC/PKCS7Padding,密钥长度32位)


首先导入依赖

<dependency>
        <groupId>commons-codec</groupId>
        <artifactId>commons-codec</artifactId>
 </dependency>

  工具类

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

import javax.crypto.Cipher;
import javax.crypto.spec.IvParameterSpec;
import javax.crypto.spec.SecretKeySpec;
import java.io.UnsupportedEncodingException;
import java.security.Security;

/**
 * AES加解密工具
 */
public class EncryptUtil {
    private static final String CipherMode="AES/CBC/PKCS7Padding";
 
    private static final String SecretKey="key";
 
    private static final Integer IVSize=16;
 
    private static final String EncryptAlg ="AES";
 
    private static final String Encode="UTF-8";
 
    private static final int SecretKeySize=32;
 
    private static final String Key_Encode="UTF-8";
 
    /**
     * 创建密钥
     * @return
     */
    private static SecretKeySpec createKey(){
        StringBuilder sb=new StringBuilder(SecretKeySize);
        sb.append(SecretKey);
        if (sb.length()>SecretKeySize){
            sb.setLength(SecretKeySize);
        }
        if (sb.length()<SecretKeySize){
            while (sb.length()<SecretKeySize){
                sb.append(" ");
            }
        }
        try {
            byte[] data=sb.toString().getBytes(Encode);
            return new SecretKeySpec(data, EncryptAlg);
        }catch (Exception e){
            e.printStackTrace();
        }
        return null;
    }
 
    /**
     * 创建16位向量: 不够则用0填充
     * @return
     */
    private static IvParameterSpec createIV() {
        StringBuffer sb = new StringBuffer(IVSize);
        sb.append(SecretKey);
        if (sb.length()>IVSize){
            sb.setLength(IVSize);
        }
        if (sb.length()<IVSize){
            while (sb.length()<IVSize){
                sb.append("0");
            }
        }
        byte[] data=null;
        try {
             data=sb.toString().getBytes(Encode);
        } catch (UnsupportedEncodingException e) {
            e.printStackTrace();
        }
        return new IvParameterSpec(data);
    }
 
    /**
     * 加密:有向量16位,结果转base64
     * @param context
     * @return
     */
    public static String encrypt(String context) {
        try {
        //下面这行在进行PKCS7Padding加密时必须加上,否则报错 Security.addProvider(new org.bouncycastle.jce.provider.BouncyCastleProvider()); byte[] content=context.getBytes(Encode); SecretKeySpec key = createKey(); Cipher cipher = Cipher.getInstance(CipherMode); cipher.init(Cipher.ENCRYPT_MODE, key, createIV()); byte[] data = cipher.doFinal(content); String result= Base64.encodeBase64String(data); return result; } catch (Exception e) { e.printStackTrace(); } return null; } /** * 解密 * @param context * @return */ public static String decrypt(String context) { try { byte[] data=Base64.decodeBase64(context); SecretKeySpec key = createKey(); Cipher cipher = Cipher.getInstance(CipherMode); cipher.init(Cipher.DECRYPT_MODE, key, createIV()); byte[] content = cipher.doFinal(data); String result=new String(content,Encode); return result; } catch (Exception e) { e.printStackTrace(); } return null; } } 
注意:PKCS7Padding加密时需要调用BouncyCastleProvider让java支持PKCS7Padding
相应的依赖
  <dependency>
            <groupId>org.bouncycastle</groupId>
            <artifactId>bcprov-jdk16</artifactId>
            <version>1.46</version>
        </dependency>

  参考文章来源于:https://blog.csdn.net/u013871100/article/details/80100992

  还可参考:https://www.cnblogs.com/wrcold520/p/8931034.html






免责声明!

本站转载的文章为个人学习借鉴使用,本站对版权不负任何法律责任。如果侵犯了您的隐私权益,请联系本站邮箱yoyou2525@163.com删除。



 
粤ICP备18138465号  © 2018-2025 CODEPRJ.COM