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) {
}
}
上一篇: centos7 修改主机名
下一篇: Java字符串的GZIP压缩和解压