Java笔记-AES加解密(PKCS7padding可用)

Posted IT1995

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了Java笔记-AES加解密(PKCS7padding可用)相关的知识,希望对你有一定的参考价值。

如下代码:

package cn.it1995.tool;


import org.apache.tomcat.util.codec.binary.Base64;
import sun.misc.BASE64Decoder;
import sun.misc.BASE64Encoder;

import javax.crypto.*;
import javax.crypto.spec.IvParameterSpec;
import javax.crypto.spec.SecretKeySpec;
import java.io.UnsupportedEncodingException;
import java.security.InvalidAlgorithmParameterException;
import java.security.InvalidKeyException;
import java.security.Key;
import java.security.NoSuchAlgorithmException;

public class AESUtil 

    public static String decrypt(String content, String key, String vi) throws NoSuchPaddingException, NoSuchAlgorithmException, InvalidKeyException, BadPaddingException, IllegalBlockSizeException, UnsupportedEncodingException, UnsupportedEncodingException, InvalidAlgorithmParameterException 

//        System.out.println("content : " + content);
//        System.out.println("key : " + key);
//        System.out.println("vi : " + vi);
        //Key k = toKey(Base64.decodeBase64(key));
        Key k = toKey(key.getBytes());
        byte[] encoded = k.getEncoded();
        SecretKeySpec aes = new SecretKeySpec(encoded, "AES");
        Cipher cipher = Cipher.getInstance("AES/CBC/PKCS7Padding");
        IvParameterSpec iv = new IvParameterSpec(vi.getBytes());
        cipher.init(Cipher.DECRYPT_MODE, aes, iv);

        byte[] bytes = cipher.doFinal(Base64.decodeBase64(content));

        return new String(bytes, "UTF-8");
    

    public static String encrypt(String data, String key, String vi) throws NoSuchPaddingException, NoSuchAlgorithmException, InvalidKeyException, UnsupportedEncodingException, BadPaddingException, IllegalBlockSizeException, InvalidAlgorithmParameterException 

        //Key k = toKey(Base64.decodeBase64(key));
        Key k = toKey(key.getBytes());
        byte[] encoded = k.getEncoded();
        SecretKeySpec aes = new SecretKeySpec(encoded, "AES");
        Cipher cipher = Cipher.getInstance("AES/CBC/PKCS7Padding");
        IvParameterSpec iv = new IvParameterSpec(vi.getBytes());
        cipher.init(Cipher.ENCRYPT_MODE, aes, iv);
        byte[] bytes = cipher.doFinal(data.getBytes("UTF-8"));
        return Base64.encodeBase64String(bytes);
    

    private static Key toKey(byte[] key)

        SecretKeySpec aes = new SecretKeySpec(key, "AES");
        return aes;
    

    public static void main(String[] args) throws NoSuchPaddingException, BadPaddingException, InvalidAlgorithmParameterException, NoSuchAlgorithmException, IllegalBlockSizeException, UnsupportedEncodingException, InvalidKeyException 

        String content = "7mv2MJPHj1o/rdar1I4i0Q==";
        String key = "sN1DEJAVZNf3OdM3";
        String vi = "GDHgt7hbKpsIR4b4";

        System.out.println("原文 : root");

        String e = AESUtil.encrypt("root", key, vi);
        System.out.println("密文 : " + e);
        String f = AESUtil.decrypt(e, key, vi);
        System.out.println("解密 : " + f);
    

运行截图如下:

这里Cipher.getInstance中使用PKCS7padding。需要Maven中新增:

<dependency>
    <groupId>org.bouncycastle</groupId>
    <artifactId>bcprov-ext-jdk16</artifactId>
    <version>1.45</version>
</dependency>

 这里可以接的值有

AES加密模式:ECB、CBC、CTR、OFB、CFB;

AES填充模式:pkcs5padding、pkcs7padding、zeropadding、ISO0126、ansix923、no padding。

以上是关于Java笔记-AES加解密(PKCS7padding可用)的主要内容,如果未能解决你的问题,请参考以下文章

Java使用AES加解密

PHP笔记-AES加解密(PHP7)

7.java 加解密技术系列之 AES

JAVA AES文件加解密

PHP如何实现AES加解密

Nodejs与Java通用AES加解密