天天看點

基于JAVA的SM4檔案加密解密,可以直接使用SM4檔案加密

@[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);
    }