admin管理员组文章数量:1639832
@[JAVA]SM4文件加密以及解密
SM4文件加密
本加密只需要填写一个 key就可以了,需要导入两个包,一个是bcprov-jdk15on-1.59.jar
另一个是hutool-all-4.6.17.jar
百度网盘自取地址,提取码wwmy
向maven中加入自己的jar包
如果搜索不出来,那么将jar包导入到自己项目当中
在pom.xml中添加自己的依赖
<dependency>
<groupId>com.hutool</groupId>
<artifactId>all</artifactId>
<version>4.6.17</version>
<scope>system</scope>
<systemPath>${project.basedir}/src/main/webapp/WEB-INF/lib/hutool-all-4.6.17.jar</systemPath>
</dependency>
<dependency>
<groupId>com.bcprov</groupId>
<artifactId>jdk15on</artifactId>
<version>1.59</version>
<scope>system</scope>
<systemPath>${project.basedir}/src/main/webapp/WEB-INF/lib/bcprov-jdk15on-1.59.jar</systemPath>
</dependency>
system根据自己放的位置进行填写
工具类用到的jar包
import java.io.ByteArrayInputStream;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.OutputStream;
import java.security.InvalidKeyException;
import java.security.Key;
import java.security.NoSuchAlgorithmException;
import java.security.NoSuchProviderException;
import java.security.Security;
import javax.crypto.Cipher;
import javax.crypto.CipherOutputStream;
import javax.crypto.NoSuchPaddingException;
import javax.crypto.spec.SecretKeySpec;
import cn.hutool.core.io.FileUtil;
import cn.hutool.core.io.IoUtil;
import org.bouncycastle.jcajce.io.CipherInputStream;
import org.bouncycastle.jce.provider.BouncyCastleProvider;
import org.bouncycastle.pqc.math.linearalgebra.ByteUtils;
SM4File工具类
public class SM4Tools {
private static String key = "密钥自己填写自己的密钥";
private static byte[] keyData = ByteUtils.fromHexString(key);
static{
if (Security.getProvider(BouncyCastleProvider.PROVIDER_NAME) == null){
//No such provider: BC
Security.addProvider(new BouncyCastleProvider());
}
}
//生成 Cipher
public static Cipher generateCipher(int mode,byte[] keyData) throws InvalidKeyException, NoSuchPaddingException, NoSuchAlgorithmException, NoSuchProviderException {
Cipher cipher = Cipher.getInstance("SM4/ECB/PKCS5Padding", BouncyCastleProvider.PROVIDER_NAME);
Key sm4Key = new SecretKeySpec(keyData, "SM4");
cipher.init(mode, sm4Key);
return cipher;
}
//加密文件
public static void encryptFile(String sourcePath,String targetPath){
//加密文件
try {
Cipher cipher = generateCipher(Cipher.ENCRYPT_MODE,keyData);
CipherInputStream cipherInputStream = new CipherInputStream(new FileInputStream(sourcePath), cipher);
FileUtil.writeFromStream(cipherInputStream, targetPath);
IoUtil.close(cipherInputStream);
} catch (InvalidKeyException e) {
e.printStackTrace();
} catch (NoSuchPaddingException e) {
e.printStackTrace();
} catch (NoSuchAlgorithmException e) {
e.printStackTrace();
} catch (NoSuchProviderException e) {
e.printStackTrace();
} catch (FileNotFoundException e) {
e.printStackTrace();
}
}
/**
* 解密文件
* @param sourcePath 待解密的文件路径
* @param targetPath 解密后的文件路径
*/
public static void decryptFile(String sourcePath, String targetPath) {
FileInputStream in =null;
ByteArrayInputStream byteArrayInputStream =null;
OutputStream out = null;
CipherOutputStream cipherOutputStream=null;
try {
in = new FileInputStream(sourcePath);
byte[] bytes = IoUtil.readBytes(in);
byteArrayInputStream = IoUtil.toStream(bytes);
Cipher cipher = generateCipher(Cipher.DECRYPT_MODE,keyData);
out = new FileOutputStream(targetPath);
cipherOutputStream = new CipherOutputStream(out, cipher);
IoUtil.copy(byteArrayInputStream, cipherOutputStream);
} catch (IOException e) {
e.printStackTrace();
} catch (NoSuchPaddingException e) {
e.printStackTrace();
} catch (NoSuchAlgorithmException e) {
e.printStackTrace();
} catch (InvalidKeyException e) {
e.printStackTrace();
} catch (NoSuchProviderException e) {
e.printStackTrace();
}finally {
IoUtil.close(cipherOutputStream);
IoUtil.close(out);
IoUtil.close(byteArrayInputStream);
IoUtil.close(in);
}
}
}
测试案例
public static void main(String[] args) throws Exception {
String sp = "C:\\Users\\nci-1\\Desktop\\1.zip";//原始文件
String dp = "C:\\Users\\nci-1\\Desktop\\加密文件.zip";//加密后文件
String dp2 = "C:\\Users\\nci-1\\Desktop\\解密文件.zip";//解密后文件
String key = "05d986b1141227cb20d46d0b56202024";
byte[] keyData = ByteUtils.fromHexString(key);
//加密文件
encryptFile(keyData,sp,dp);
//解密文件
decryptFile(keyData,dp,dp2);
}
版权声明:本文标题:基于JAVA的SM4文件加密解密,可以直接使用 内容由热心网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:https://www.elefans.com/dongtai/1729293155a1194423.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论