加密算法之AES

一、原理

AES 加密算法的原理详解

二、pom

<dependency>
	<groupId>commons-codec</groupId>
	<artifactId>commons-codec</artifactId>
	<version>1.13</version>
</dependency>
<dependency>
	<groupId>org.bouncycastle</groupId>
	<artifactId>bcprov-jdk16</artifactId>
	<version>1.46</version>
</dependency>

三、AES-128位-有向量-CBC/PKCS5Padding

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

import javax.crypto.Cipher;
import javax.crypto.spec.IvParameterSpec;
import javax.crypto.spec.SecretKeySpec;
import java.nio.charset.Charset;
import java.nio.charset.StandardCharsets;

public class AesUtils {
    private static final Charset EncodeCharset = StandardCharsets.UTF_8;

    private static final String EncryptAlg = "AES";
    private static final String CipherMode = "AES/CBC/PKCS5Padding";

    private static final String SecretKey = "XXXXXXXXXXXXXXXX";
    private static final String Iv = "2019090317231022";

    private static byte[] baseEncrypt(String secretKey, String iv, String message) throws Exception {
        byte[] secretKeyBytes = secretKey.getBytes(EncodeCharset);
        byte[] messageBytes = message.getBytes(EncodeCharset);
        byte[] ivBytes = iv.getBytes(EncodeCharset);

        SecretKeySpec secretKeySpec = new SecretKeySpec(secretKeyBytes, EncryptAlg);
        Cipher cipher = Cipher.getInstance(CipherMode);
        IvParameterSpec ivParameterSpec = new IvParameterSpec(ivBytes);
        cipher.init(Cipher.ENCRYPT_MODE, secretKeySpec, ivParameterSpec);

        byte[] resBytes = cipher.doFinal(messageBytes);

        return resBytes;
    }

    public static String encrypt(String secretKey, String iv, String message) throws Exception {
        byte[] resultBytes = baseEncrypt(secretKey, iv, message);

        try {
            String result=Base64.encodeBase64String(resultBytes);
            return result;
        } catch (Exception e) {
            e.printStackTrace();
        }
        return null;
    }

    private static byte[] baseDecrypt(String secretKey, String iv, String encryptText) throws Exception {
        byte[] secretKeyBytes = secretKey.getBytes(EncodeCharset);
        byte[] ivBytes = iv.getBytes(EncodeCharset);

        byte[] encryptTextBytes = Base64.decodeBase64(encryptText);

        SecretKeySpec secretKeySpec = new SecretKeySpec(secretKeyBytes, EncryptAlg);
        Cipher cipher = Cipher.getInstance(CipherMode);
        IvParameterSpec ivParameterSpec = new IvParameterSpec(ivBytes);
        cipher.init(Cipher.DECRYPT_MODE, secretKeySpec, ivParameterSpec);

        byte[] resBytes = cipher.doFinal(encryptTextBytes);

        return resBytes;
    }

    public static String decrypt(String secretKey, String iv, String encryptText) throws Exception {
        byte[] resultBytes = baseDecrypt(secretKey, iv, encryptText);
        return new String(resultBytes, EncodeCharset);
    }

    public static void main(String[] args) throws Exception{
        //密钥 加密内容(对象序列化后的内容-json格式字符串)
        String content="{\"userId\": 10, \"message\": \"nice to meet you\"}";

        String encryptText = encrypt(SecretKey, Iv, content);
        String decryptText = decrypt(SecretKey, Iv, encryptText);
        System.out.println(String.format("加密结果:%s\n解密结果:%s ",content,encryptText,decryptText));
    }
}

四、AES-128位-无向量-ECB/PKCS7Padding

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

import javax.crypto.Cipher;
import javax.crypto.spec.SecretKeySpec;
import java.nio.charset.Charset;
import java.nio.charset.StandardCharsets;
import java.security.Security;

public class Aes7 {
    // 128/192/256
    private static final int Secret_Key_Size = 32;

    private static final String EncryptAlg ="AES";
    private static final String Cipher_Mode="AES/ECB/PKCS7Padding";

    private static final Charset Encode_Charset = StandardCharsets.UTF_8;

    static {
        Security.addProvider(new org.bouncycastle.jce.provider.BouncyCastleProvider());
    }

    public static String encrypt(String content, String key) throws Exception {
        byte[] keyBytes = secretKeyBytes(key);
        byte[] contentBytes = content.getBytes(Encode_Charset);

        Cipher cipher = Cipher.getInstance(Cipher_Mode);
        cipher.init(Cipher.ENCRYPT_MODE, new SecretKeySpec(keyBytes, EncryptAlg));

        byte[] encrayptBytes = cipher.doFinal(contentBytes);
        String encrayptBase64Bytea = new Base64().encodeToString(encrayptBytes);
        return encrayptBase64Bytea;
    }

    public static String decrypt(String content, String key) throws Exception {
        byte[] keyBytes = secretKeyBytes(key);
        byte[] decodeBytes = Base64.decodeBase64(content);

        Cipher cipher = Cipher.getInstance(Cipher_Mode);
        cipher.init(Cipher.DECRYPT_MODE, new SecretKeySpec(keyBytes, EncryptAlg));

        byte[] realBytes=cipher.doFinal(decodeBytes);
        return new String(realBytes, Encode_Charset);
    }

    public static byte[] secretKeyBytes(String key) throws Exception{
        final byte paddingChar = '0';

        byte[] resKeyBytes = new byte[Secret_Key_Size];
        byte[] keyBytes = key.getBytes(Encode_Charset);
        for (int i=0; i<Secret_Key_Size; i++){
            if (i < keyBytes.length){
                resKeyBytes[i] = keyBytes[i];
            } else {
                resKeyBytes[i] = paddingChar;
            }
        }

        return resKeyBytes;
    }

    public static void main(String[] args) throws Exception{
        //密钥 加密内容(对象序列化后的内容-json格式字符串)
        String key="securitykey";
        String content="{\"userId\": 10, \"message\": \"nice to meet you\"}";

        String encryptRes = encrypt(content,key);
        String decryptRes = decrypt(encryptRes,key);
        System.out.println(String.format("加密结果:%s\n解密结果:%s",encryptRes, decryptRes));
    }
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值