|
com.hutool4.6.17system${project.basedir}/src/main/webapp/lib/sm4/hutool-all-4.6.17.jarcom.bcprov1.59system${project.basedir}/src/main/webapp/lib/sm4/bcprov-jdk15on-1.59.jar首先我们在idae开发工具导入导入pom.xml的两个必要依赖 jar包下载地址:百度网盘请输入提取码 npn8 图上systemPath为jar包的文件路径,我们需要使用以下的路径存储jar包。(也可以自己设置)java包的文件路径如图所示然后创建所需要加密的文件,需要加密的文件内容,并保存文件加密的地址。以下是代码实现:使用密钥"0123456789abcdeffedcba9876543210"对文件加解密importjava.io.ByteArrayInputStream;importjava.io.FileInputStream;importjava.io.FileNotFoundException;importjava.io.FileOutputStream;importjava.io.IOException;importjava.io.OutputStream;importjava.security.InvalidKeyException;importjava.security.Key;importjava.security.NoSuchAlgorithmException;importjava.security.NoSuchProviderException;importjava.security.Security;importjavax.crypto.Cipher;importjavax.crypto.CipherOutputStream;importjavax.crypto.NoSuchPaddingException;importjavax.crypto.spec.SecretKeySpec;importcn.hutool.core.io.FileUtil;importcn.hutool.core.io.IoUtil;importorg.bouncycastle.jcajce.io.CipherInputStream;importorg.bouncycastle.jce.provider.BouncyCastleProvider;importorg.bouncycastle.pqc.math.linearalgebra.ByteUtils;publicclassSM4Tools{publicstaticvoidmain(String[]args)throwsException{Stringsp="D:\\sm4jiami\\tt\\tt.txt";//原始文件Stringdp="D:\\sm4jiami\\tt\\tt.txt加密文件";//加密后文件Stringdp2="D:\\sm4jiami\\tt\\tt.txt解密文件";//解密后文件//Stringkey="05d986b1141227cb20d46d0b56202024";//byte[]keyData=ByteUtils.fromHexString(key);//加密文件encryptFile(sp,dp);System.out.println("加密成功");//解密文件decryptFile(dp,dp2);System.out.println("解密成功");}privatestaticStringkey="0123456789abcdeffedcba9876543210";//使用到的密钥对文件加密privatestaticbyte[]keyData=ByteUtils.fromHexString(key);static{if(Security.getProvider(BouncyCastleProvider.PROVIDER_NAME)==null){//Nosuchprovider:BCSecurity.addProvider(newBouncyCastleProvider());}}//生成CipherpublicstaticCiphergenerateCipher(intmode,byte[]keyData)throwsInvalidKeyException,NoSuchPaddingException,NoSuchAlgorithmException,NoSuchProviderException{Ciphercipher=Cipher.getInstance("SM4/ECB/PKCS5Padding",BouncyCastleProvider.PROVIDER_NAME);Keysm4Key=newSecretKeySpec(keyData,"SM4");cipher.init(mode,sm4Key);returncipher;}//加密文件publicstaticvoidencryptFile(StringsourcePath,StringtargetPath){//加密文件try{Ciphercipher=generateCipher(Cipher.ENCRYPT_MODE,keyData);CipherInputStreamcipherInputStream=newCipherInputStream(newFileInputStream(sourcePath),cipher);FileUtil.writeFromStream(cipherInputStream,targetPath);IoUtil.close(cipherInputStream);}catch(InvalidKeyExceptione){e.printStackTrace();}catch(NoSuchPaddingExceptione){e.printStackTrace();}catch(NoSuchAlgorithmExceptione){e.printStackTrace();}catch(NoSuchProviderExceptione){e.printStackTrace();}catch(FileNotFoundExceptione){e.printStackTrace();}}/***解密文件*@paramsourcePath待解密的文件路径*@paramtargetPath解密后的文件路径*/publicstaticvoiddecryptFile(StringsourcePath,StringtargetPath){FileInputStreamin=null;ByteArrayInputStreambyteArrayInputStream=null;OutputStreamout=null;CipherOutputStreamcipherOutputStream=null;try{in=newFileInputStream(sourcePath);byte[]bytes=IoUtil.readBytes(in);byteArrayInputStream=IoUtil.toStream(bytes);Ciphercipher=generateCipher(Cipher.DECRYPT_MODE,keyData);out=newFileOutputStream(targetPath);cipherOutputStream=newCipherOutputStream(out,cipher);IoUtil.copy(byteArrayInputStream,cipherOutputStream);}catch(IOExceptione){e.printStackTrace();}catch(NoSuchPaddingExceptione){e.printStackTrace();}catch(NoSuchAlgorithmExceptione){e.printStackTrace();}catch(InvalidKeyExceptione){e.printStackTrace();}catch(NoSuchProviderExceptione){e.printStackTrace();}finally{IoUtil.close(cipherOutputStream);IoUtil.close(out);IoUtil.close(byteArrayInputStream);IoUtil.close(in);}}}运行代码代码运行成功我们可以看到文件路径上多了两个文件,分别是加解密后的文件我们用右键选择打开方式,用文本的方式打开可以看到加密后的信息如下ok我们加密成功了!同理解密的文件也能解密成功以上就是java对sm4国密算法的加密解密方式。
|
|