1. 导入依赖

在pom.xml中加入以下依赖:

        <!-- commons-codec 依赖-->
        <dependency>
            <groupId>commons-codec</groupId>
            <artifactId>commons-codec</artifactId>
            <version>1.15</version>
        </dependency>
        <!-- commons-io 依赖-->
        <dependency>
            <groupId>commons-io</groupId>
            <artifactId>commons-io</artifactId>
            <version>2.11.0</version>
        </dependency>
2.编写加密解密工具类
import org.apache.commons.codec.binary.Base64;
import org.apache.commons.io.FileUtils;
import org.apache.commons.io.IOUtils;

import javax.crypto.Cipher;
import javax.crypto.spec.SecretKeySpec;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;

public class AESUtils {

    private static final String KEY_ALGORITHM = "AES";
    private static final String CIPHER_ALGORITHM = "AES/ECB/PKCS5Padding";

    /**
     * 加密文件
     * @param srcFile 原文件
     * @param destFile 加密后的文件
     * @param key 密钥
     * @throws Exception
     */
    public static void encryptFile(File srcFile, File destFile, String key) throws Exception {
        byte[] data = FileUtils.readFileToByteArray(srcFile);
        byte[] encryptedData = encrypt(data, key);
        FileUtils.writeByteArrayToFile(destFile, encryptedData);
    }

    /**
     * 解密文件
     * @param srcFile 加密后的文件
     * @param destFile 解密后的文件
     * @param key 密钥
     * @throws Exception
     */
    public static void decryptFile(File srcFile, File destFile, String key) throws Exception {
        byte[] encryptedData = FileUtils.readFileToByteArray(srcFile);
        byte[] data = decrypt(encryptedData, key);
        FileUtils.writeByteArrayToFile(destFile, data);
    }

    /**
     * 加密数据
     * @param data 原数据
     * @param key 密钥
     * @return 加密后的数据
     * @throws Exception
     */
    public static byte[] encrypt(byte[] data, String key) throws Exception {
        SecretKeySpec secretKeySpec = new SecretKeySpec(key.getBytes(), KEY_ALGORITHM);
        Cipher cipher = Cipher.getInstance(CIPHER_ALGORITHM);
        cipher.init(Cipher.ENCRYPT_MODE, secretKeySpec);
        return cipher.doFinal(data);
    }

    /**
     * 解密数据
     * @param encryptedData 加密后的数据
     * @param key 密钥
     * @return 解密后的数据
     * @throws Exception
     */
    public static byte[] decrypt(byte[] encryptedData, String key) throws Exception {
        SecretKeySpec secretKeySpec = new SecretKeySpec(key.getBytes(), KEY_ALGORITHM);
        Cipher cipher = Cipher.getInstance(CIPHER_ALGORITHM);
        cipher.init(Cipher.DECRYPT_MODE, secretKeySpec);
        return cipher.doFinal(encryptedData);
    }

    /**
     * 加密字符串
     * @param str 原字符串
     * @param key 密钥
     * @return 加密后的字符串
     * @throws Exception
     */
    public static String encryptString(String str, String key) throws Exception {
        byte[] data = str.getBytes();
        byte[] encryptedData = encrypt(data, key);
        return Base64.encodeBase64String(encryptedData);
    }

    /**
     * 解密字符串
     * @param encryptedStr 加密后的字符串
     * @param key 密钥
     * @return 解密后的字符串
     * @throws Exception
     */
    public static String decryptString(String encryptedStr, String key) throws Exception {
        byte[] encryptedData = Base64.decodeBase64(encryptedStr);
        byte[] data = decrypt(encryptedData, key);
        return new String(data);
    }

}
3.ASE加密解密测试类
import com.songzixan.ase.util.AESUtils;

import java.io.File;

/**
 * <p>
 * Description: ASE加密解密测试类
 * </p>
 *
 * @author songzixian
 * @version v2.0.0
 * @create 2023-03-12 11:36
 * @see com.songzixan.ase.test
 */
public class TestAse {
    public static void main(String[] args) throws Exception {
        // 加密解密的Key
        String key = "SzxBlog123";
        // 加密文件
        // 加密的原文件
        File srcFile = new File("src/main/resources/test.txt");
        // 加密后的新文件
        File destFile = new File("src/main/resources/test_encrypted.txt");
        AESUtils.encryptFile(srcFile, destFile, key);

        // 解密文件
        // 解密的原文件
        File encryptedFile = new File("src/main/resources/test_encrypted.txt");
        //解密后新的文件
        File decryptedFile = new File("src/main/resources/test_decrypted.txt");
        AESUtils.decryptFile(encryptedFile, decryptedFile, key);
    }
}
Last modification:March 12, 2023
如果觉得这篇技术文章对你有用,请随意赞赏