赞
踩
SM4.0(原名SMS4.0)是中华人民共和国政府采用的一种分组密码标准,由国家密码管理局于2012年3月21日发布。相关标准为“GM/T 0002-2012《SM4分组密码算法》(原SMS4分组密码算法)”。
在商用密码体系中,SM4主要用于数据加密,其算法公开,分组长度与密钥长度均为128bit,加密算法与密钥扩展算法都采用32轮非线性迭代结构,S盒为固定的8比特输入8比特输出。
SM4.0中的指令长度被提升到大于64K(即64×1024)的水平,这是SM 3.0规格(渲染指令长度允许大于512)的128倍。
maven依赖:
<dependency>
<groupId>org.bouncycastle</groupId>
<artifactId>bcprov-jdk15on</artifactId>
<version>1.70</version>
</dependency>
工具类:
import cn.hutool.crypto.symmetric.SymmetricCrypto; import org.bouncycastle.util.encoders.Hex; /** * @author guoqs */ public class SM4DecryptionExample { public static void main(String arg[]) throws Exception { String paramStr = "pass$123"; String key = "0123456789abcdeffedcba9876543210"; String arfter = encrypt(key,paramStr); String brfore = decrypt(key,"92b11e31a38d9ca312a12107774366b8"); System.out.println("解密后明文:---------------------"+brfore); } /** * 加密 * * @param key 密钥 * @param data 加密前的明文 * @return String 返回密文 * @author guoqs * @date 2023-12-08 */ public static String encrypt(String key, String data) { byte[] sm4KeyBytes = Hex.decode(key); SymmetricCrypto sm4 = new SymmetricCrypto("SM4/ECB/PKCS5Padding", sm4KeyBytes); return sm4.encryptHex(data).toUpperCase(); } /** * * 解密 * @param key 密钥 * @param data 加密后的密文 * @return String 返回明文 * @author guoqs * @date 2023-12-08 */ public static String decrypt(String key,String data) { try{ byte[] sm4KeyBytes = Hex.decode(key); SymmetricCrypto sm4 = new SymmetricCrypto("SM4/ECB/PKCS5Padding", sm4KeyBytes); return sm4.decryptStr(data); }catch (Exception e){ return data; } } }
使用sm-crypto依赖实现,在vue中package.json文件的dependencies添加: “sm-crypto”: “^0.3.13”,然后重新下载依赖。
实现:
//解密的方法
const sm4 = require('sm-crypto').sm4
const key = '12b11a31a3829cD112a1210666436632' // 16 字节的十六进制密钥
const sm4Password ="60F2DD9CEDDBCCC3AD34A136168BD6D434010F05F9ABF9ABCDA861AE076A0A3948F838701883DCE4FB6AB41CE50E0AB91FD94EDFD2AE4C3B747018A9A6085816252993F609375C52EA505C7F6F2F6ED70504141A366B12CA227794DFDBA992A5312E01CA8D7AF1AC35D206C18FAE470DEEE6A831E9A33698C6EA69D4C92CB205DF663D38BB6BE4D52EF2BAFD1346D544D15591475A075402C9574514CBFD53DFF751E3A3CCF7C936A34023697D89FB8D7C96DA4B5E350CD89481BE72608739A21FE835C3D6A826C4B3A91D1B3BECF6454828069D3FC9B3FBDE22008C981672AEA6A9BD79333DC3BB01C2D821C9EEA32AB1CA4166B18D5635191BE469E2218D7490268AE3C9362A5AE764305EED8D162B6F0E7B7A11B9D9F0D465D6A74F595A3DA3989D8161865F6A39A8FE9E71AEC2FAD9A024E94B1640E1EFC6B204F71F92D542F6069E41A9899142AD515879CFF948B151ACD5A22E16EEA8BD00524B097469266C09195733F0BD73AA43EB952CEC3F0019E3B5DAC7BE7BA909017EF584970CDF4A5EE083B0A7DC7AFB14F3363E69B4D8081E5447C49F7918980F1F8B8D1D29F76741FE6E5CCBA2A4718C8958585C1B1FC2E278C22C54E2CDA7422A380CD08916B47672B53383EA6D17277B4D78BD6E6FA150F2EBFC879B9E16C21CA32EB6852B0102743E3C3E4FAE5088739CA9D0F300C6D4638F4F0A4DDAED530847D7125CF1B8EA7E2D9081522FBB7999B5C3FBBE0750FE4AC66E30B2A8B1399079ECB88AF5000E04A53F36F7F93642C947D2C3D3BA5D0DE3D4F771378C23A6B8674C6A70227EFE7045D8E486F62B5F98153207D5F07B143575D5659D9FB4F167CF571F3F2783E16DBB73A6A5A4A9E68E7E8F29C45F61783FD13E627D6153BCB05652C3247CC6ABEBDFED2B6E3DA2108C1F771A902045CE1D5D668E77BF37A0276E0240782663034718F6E8044717CAE89BE103223779C38B6ADB1796132B848634DAC3002B6325A45015F6A2CAD859F310422C456D5E156608AF126D164F931F1A86A6AC34B18B926070588259657092AA90B8FD069549B0208E316CB4528DBE38F590420E62FF11C18A2972520A397A17E741B17844A346754A931315189A975431A3BCFE65C73C234F2A481A6A393AE3C832708BDA0BACBCBB7CC5E7CA408C833FA13706ED0147A8FD9FB20272622599D880C3480DD650949E8E5B14FF306D6FECC567E140EBAF34E98FE651A44A4AE63FEFD1A12EC47A9F218414E1DC44A6A2AFF03903A25115C2F87E323993A95C504615B15F9FDD44387F572E6D58F4DC6D4DEBE6452275013AF8DF52E294DC8CC461A0DD1F6777197B32DEA117A94EBEEE2DAA4F5681DD33DD9A65F29CD025991DB5A81AF23EB44952C48F133A69ECE0EC9DC53C38E0972434EFCF49DE6127675B6859B60CDFE22AA8CE14E3F45BA9D00C01E09B78245111C3005243F6D1831CF97A373807D002F5BF68A8C67517606EC055D0747665EEFDCABFA4E5571C317DF73D5F7C4C26A56F9EFBF7AF32863E84BC7830FA038BB95D0C55DEF97F830A211B3E8F9DE7D094144D520A0D7CD8709FC8E61885CA34FDEE69B9BDA210BA75968ADB59CA4CFC527BD273846F6D3C6B938CBAD348612A15AB403016B8D84B81A47A5DCED6A74134E834D1D1977A93B0941956F27896147AB0629D0357271445C6C3ED60742C8F4504B33813C3E35EFD5041F713E54CF81EBA0AABB7BD5840B120730E7F1FF97FA7399FEB5F796768CF789C1C00009F82B1BD7B98D0AFA75CC8B1D2F760771C8D61DE1D8AE449FC627FD03CA79BFB834502B1D894E5B80167F08E20EFDD9F34672E96D976CDEDCE17831E40693EE339F6774956446ED8FA66AA8D7B39B4B6D49D56AD5EAD4E36068EDA2D8C89A1B7D94C253450DFACE9616B07DDCFD914E973470AAA369362AB935CB0E84DBA15B35B5E7582A0005E0E10814139D8CD8DC62D1B24148F48460B7B1772E24B3DBB78CAFFEBDD1187A5ED61B03DD0B713BDE3AED74ECC02711ADA06B037A24ADA8D20F1F910BDB0ADE9EDA147693A84ECDD028CD6B83303EBE9D5A7F250F0A03D64119C8B3C3C24860CB9F129E83F1D3ADEED6ED40C8EE470F373A82F72469A6FADA4B06AC16C12FA20106D2A7503EE852B732A7EA16B250F4CBB8F9131A1C5F41C2AAA73ADD31C4208B67F36A768DD54892048A9488C66100E282079E9895641E3A56297312DDD894680F93AD0F3BD766116366FC61DD8037ADD85F755B99597AC18"
const password= sm4.decrypt(sm4Password , key)
console.log(password)
引入SpringAop依赖:
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-aop</artifactId>
</dependency>
使用SpringAop对接口进行统一加密:
import com.lezhi.video.utils.R; import com.lezhi.video.utils.SM4DecryptionExample; import org.aspectj.lang.ProceedingJoinPoint; import org.aspectj.lang.annotation.Around; import org.aspectj.lang.annotation.Aspect; import org.aspectj.lang.annotation.Pointcut; import org.springframework.beans.factory.annotation.Value; import org.springframework.stereotype.Component; /** * @author guoqs */ @Aspect @Component public class DecryptionAspect { //密钥 @Value("${decrypt.secret}") private String secret; //加密开关 @Value("${decrypt.open}") private boolean open; @Pointcut("execution(* com.lezhi.video.controller..*(..))") public void Pointcut() { } @Around("Pointcut()") public Object encryptResponse(ProceedingJoinPoint joinPoint) throws Throwable { // 执行原方法并获取返回值 Object result = joinPoint.proceed(); if (result != null && open) { // 调用加密工具类对返回结果进行加密 String encryptedResult = SM4DecryptionExample.encrypt(secret, result.toString()); // 返回加密后的结果 return R.data(encryptedResult); } else { return result; } } }
Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。