国密算法SM4 对文件加解密
说明:调用开源bcprov-jdk15on 加密算法工具,使用SM4算法,对文件进行加密、解密;文件流的操作使用hutool工具包来实现。
引用依赖
<dependency> <groupId>org.bouncycastle</groupId> <artifactId>bcprov-jdk15on</artifactId> <version>1.54</version> </dependency> <!-- hutool 工具包 --> <dependency> <groupId>cn.hutool</groupId> <artifactId>hutool-all</artifactId> <version>4.6.10</version> </dependency>
生成128位的Key
public static final int KEY_SIZE = 128; public static byte[] generateKey(int keySize) throws Exception { KeyGenerator kg = KeyGenerator.getInstance(ALGORITHM_NAME, BouncyCastleProvider.PROVIDER_NAME); kg.init(keySize, new SecureRandom()); return kg.generateKey().getEncoded(); }
生成密钥
public static void main(String[] args) throws Exception { //生成Key byte[] bytes = generateKey(KEY_SIZE); String key = ByteUtils.toHexString(bytes); System.out.println("密钥:"); System.out.println(key); }
文件加密、解密,贴上完整的可执行的代码
public class SM4FileEncrypt { 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(byte[] keyData,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(byte[] keyData,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 = "E:\\bjsasc\\文件加解密\\原始文件.docx";//原始文件 String dp = "E:\\bjsasc\\文件加解密\\加密文件.docx";//加密后文件 String dp2 = "E:\\bjsasc\\文件加解密\\解密文件.docx";//解密后文件 String key = "05d986b1141227cb20d46d0b5687c4f5"; byte[] keyData = ByteUtils.fromHexString(key); //加密文件 encryptFile(keyData,sp,dp); //解密文件 decryptFile(keyData,dp,dp2); } }