欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页

Java基于SM4算法实现文件加密 SM4FileUtils

程序员文章站 2024-03-14 14:17:04
...

public class SM4FileUtils {
    public SM4FileUtils() {
    }

    public static String encrypt(String key, String filePath) {
        String secretKey = "";

        try {
            SM4Utils sm4 = new SM4Utils();
            sm4.setSecretKey(key);
            byte[] fileByte = ByteUtil.getBytes(filePath);
            byte[] encryptByte = sm4.encryptData_ECB(fileByte);
            boolean res = ByteUtil.getFile(encryptByte, filePath);
            if (res) {
                secretKey = key;
            }
        } catch (Exception var7) {
            var7.printStackTrace();
        }

        return secretKey;
    }

    public static boolean decrypt(String filePath, String secretKey) {
        boolean res = false;

        try {
            SM4Utils sm4 = new SM4Utils();
            sm4.setSecretKey(secretKey);
            byte[] encryptByte = ByteUtil.getBytes(filePath);
            byte[] decryptByte = sm4.decryptData_ECB(encryptByte);
            res = ByteUtil.getFile(decryptByte, filePath);
        } catch (Exception var6) {
            var6.printStackTrace();
        }

        return res;
    }

    public static void main(String[] args) {
    }
}