File content file path encryption and decryption tools for minio (AES symmetric encryption)

In the case of using minio, the official provides two schemes for object encryption, namely SSE-C and SSE-S3. However, in some cases, we are limited by the conditions and cannot quickly realize the encryption through the above scheme, so this tool can help you. This encryption process has been tested to be very efficient. The entire process of encrypting and decrypting a 5MB file is within 800ms. 

import cn.hutool.crypto.symmetric.SymmetricAlgorithm;
import cn.hutool.crypto.symmetric.SymmetricCrypto;

import javax.crypto.SecretKey;
import javax.crypto.spec.SecretKeySpec;
import java.nio.charset.Charset;
import java.nio.charset.StandardCharsets;
import java.util.Base64;

import io.minio.MinioClient;

public class EncryptionUtil {
    static final String originKeyStr = "0123456789Abc@@@"; // 必须16个字符
    private static SymmetricCrypto aes;

 
    // 加密并编码字符串
    public static String encryptURLEncodeStr(String str) {
        try {
            if (aes == null) {
                SecretKey aesKey = new SecretKeySpec(originKeyStr.getBytes(StandardCharsets.UTF_8), "AES");
                byte[] key = aesKey.getEncoded();
//构建
                aes = new SymmetricCrypto(SymmetricAlgorithm.AES, key);
            }

//加密
            byte[] encrypt = aes.encrypt(str.getBytes(StandardCharsets.UTF_8));
            String s = Base64.getUrlEncoder().encodeToString(encrypt);
            return s;
        }catch (Exception e){
            e.printStackTrace();
        }
        return "";
    }

    // 解码并解密字符串
    public static String decryptURLDecodeStr(String encStr) {
        try {
            if (aes == null) {
                SecretKey aesKey = new SecretKeySpec(originKeyStr.getBytes(StandardCharsets.UTF_8), "AES");
                byte[] key = aesKey.getEncoded();
    //构建
                aes = new SymmetricCrypto(SymmetricAlgorithm.AES, key);
            }

    //加密
                byte[] bytes = Base64.getUrlDecoder().decode(encStr);
                String s = aes.decryptStr(bytes);
                return s;
            }catch (Exception e){
                e.printStackTrace();
            }
        return "";
    }


// 加密并上传文件
    @SneakyThrows
    public static void encryptUpload(MinioClient minioClient, String bucketName, String objectName, InputStream inputStream) {
        if (aes == null) {
            SecretKey aesKey = new SecretKeySpec(originKeyStr.getBytes(StandardCharsets.UTF_8), "AES");
            byte[] key = aesKey.getEncoded();
//构建
            aes = new SymmetricCrypto(SymmetricAlgorithm.AES, key);
        }

        byte[] bytes = new byte[inputStream.available()];
        inputStream.read(bytes, 0, bytes.length);
//加密
        byte[] encrypt = aes.encrypt(bytes);
        ByteArrayInputStream bais = new ByteArrayInputStream(encrypt);

        PutObjectOptions putObjectOptions = new PutObjectOptions(bais.available(), bais.available()< 5*1024*1024 ? 5*1024*1024 :  bais.available()*8);
        minioClient.putObject(bucketName, objectName, bais, putObjectOptions);
    }


// 下载文件并解密
    @SneakyThrows
    public static byte[] decryptDownload(MinioClient minioClient, String bucketName, String objectName) {
        if (aes == null) {
            SecretKey aesKey = new SecretKeySpec(originKeyStr.getBytes(StandardCharsets.UTF_8), "AES");
            byte[] key = aesKey.getEncoded();
            aes = new SymmetricCrypto(SymmetricAlgorithm.AES, key);
        }

        InputStream inputStream = minioClient.getObject(bucketName, objectName);
        byte[] decrypt = aes.decrypt(inputStream);
        return decrypt;
    }

}

Guess you like

Origin blog.csdn.net/wangxudongx/article/details/130451389