赞
踩
1.SM2
国密算法SM2是由国家密码管理局制定的一种非对称密码算法,包括SM2密钥交换、数字签名和公钥加密(非对称加密,公钥加密,私钥解密)等三部分。它基于椭圆曲线(ECC)密码理论,具有较高的安全性和效率(同RSA比较)。
2.SM4
SM4算法是对称加密算法,国标 GB/T 32907 对 SM4 对称加密算法进行了详细描述。SM4 算法密钥长度固定为128bit,加密解密采用相同的密钥,加解密速度较快,优于AES算法。
3.SM2和SM4加密使用示例
引入jar包
<dependency> <groupId>cn.hutool</groupId> <artifactId>hutool-all</artifactId> <version>5.8.5</version> </dependency><dependency> <groupId>org.bouncycastle</groupId> <artifactId>bcprov-jdk15on</artifactId> <version>1.70</version> </dependency>
- public class SmEncryptUtil {
- private static final String IV = "0000000000000000";
- private static final String CHARSET_NAME = "UTF-8";
-
- static {
- Security.addProvider(new BouncyCastleProvider());
- }
-
- public static void main(String[] args) throws Exception {
- long startTime = System.currentTimeMillis();
- for (int i = 0; i < 10000; i++) {
- try {
- //测试参数
- String randomKey = "1234567812345678";
- String content = "hello world! 你好世界#~";
-
- //客户端请求
- String encryptKey = sm2Encrypt(randomKey, null);
- System.out.println("key sm2base64加密:" + encryptKey);
-
- String encryptContent = sm4Encrypt(randomKey, content, IV);
- System.out.println("content sm4base64加密:" + encryptContent);
-
- //服务端解析
- String decryptKey = sm2Decrypt(encryptKey, null);
- System.out.println("key sm2base64解密:" + decryptKey);
-
- String decryptContent = sm4Decrypt(decryptKey, encryptContent, IV);
- sm4Decrypt(decryptKey, encryptContent, IV);
- sm4Decrypt(decryptKey, encryptContent, IV);
- sm4Decrypt(decryptKey, encryptContent, IV);
- System.out.println(i + "content sm4base64解密:" + decryptContent);
- } catch (Exception ex) {
- System.out.println("加解密失败:" + ex.getMessage());
- break;
- }
- }
- long endTime = System.currentTimeMillis();
- System.out.println("总耗时ms:" + (endTime - startTime));
- }
-
- /**
- * sm2加密
- * 1.非对称加密 加密结果每次变化 C1C3C2模式
- * C1 生成随机数的计算出的椭圆曲线点
- * C3 SM3的摘要值
- * C2 密文数据
- * 2.base64格式:在ASCII码的可显示字符上选出一个子集,来表示所有的二进制流.
- * 避免软件和硬件对于某些二进制值不兼容的情况,将二进制数转换为6bit一组的base64编码格式(a~z,A~Z,0~9,/,+),可解决该问题
- *
- * @param sourceData 需要加密的明文
- * @param publicKey 加密使用的公钥 base64格式,为空使用默认公钥
- * @return 加密后密文,base64格式
- */
- public static String sm2Encrypt(String sourceData, String publicKey) {
- if (StringUtils.isBlank(publicKey)) {
- publicKey = ResponseBuilder.GATEWAY_PUBLIC_KEY_SM2;
- }
- SM2 sm2 = SmUtil.sm2(null, publicKey);
- return sm2.encryptBase64(sourceData, StandardCharsets.UTF_8, KeyType.PublicKey);
- }
-
- /**
- * sm2解密
- *
- * @param encryptData base64格式的密文
- * @param privateKey 解密使用的私钥,base64格式,为空使用默认私钥
- * @return 解密后的明文
- */
- public static String sm2Decrypt(String encryptData, String privateKey) {
- if (StringUtils.isBlank(privateKey)) {
- privateKey = ResponseBuilder.GATEWAY_PRIVATE_KEY_SM2;
- }
- SM2 sm2 = SmUtil.sm2(privateKey, null);
- return sm2.decryptStr(encryptData, KeyType.PrivateKey, StandardCharsets.UTF_8);
- }
-
- /**
- * sm4加密
- * 对称加密 加密结果不变 SM4/CBC/PKCS7Padding iv初始向量值为0
- *
- * @param randomKey 对称加密的密钥
- * @param sourceData 待加密的明文
- * @param iv 初始向量,为空则使用默认初始向量
- * @return 加密后的密文,base64格式
- */
- public static String sm4Encrypt(String randomKey, String sourceData, String iv) {
- try {
- if (StringUtils.isBlank(iv)) {
- iv = IV;
- }
- return Base64.encodeBase64String(sm4EncryptCBC(randomKey.getBytes(CHARSET_NAME),
- sourceData.getBytes(CHARSET_NAME), iv.getBytes(CHARSET_NAME)));
- } catch (Exception ex) {
- throw new BizException(BizRespCodeEnums.SENSITIVE_INFO_ENCRYPT_EXCEPTION.getCode(),
- BizRespCodeEnums.SENSITIVE_INFO_ENCRYPT_EXCEPTION.getMsg() + ex.getMessage());
- }
- }
-
- /**
- * sm4解密
- *
- * @param randomKey 对称加密的密钥
- * @param encryptData 待解密的密文
- * @param iv 初始向量,为空则使用默认初始向量
- * @return 解密后的明文
- */
- public static String sm4Decrypt(String randomKey, String encryptData, String iv) {
- try {
- if (StringUtils.isBlank(iv)) {
- iv = IV;
- }
- return new String(sm4DecryptCBC(randomKey.getBytes(CHARSET_NAME),
- Base64.decodeBase64(encryptData), iv.getBytes(CHARSET_NAME)));
- } catch (Exception ex) {
- throw new BizException(BizRespCodeEnums.SENSITIVE_INFO_DECRYPT_EXCEPTION.getCode(),
- BizRespCodeEnums.SENSITIVE_INFO_DECRYPT_EXCEPTION.getMsg() + ex.getMessage());
- }
- }
-
- private static byte[] sm4EncryptCBC(byte[] keyBytes, byte[] plain, byte[] iv) {
- if (keyBytes.length != 16) {
- throw new RuntimeException("err key length");
- }
-
- try {
- Key key = new SecretKeySpec(keyBytes, "SM4");
- Cipher out = Cipher.getInstance("SM4/CBC/PKCS7Padding", "BC");
- IvParameterSpec ivSpec = new IvParameterSpec(iv);
- out.init(Cipher.ENCRYPT_MODE, key, ivSpec);
- return out.doFinal(plain);
- } catch (Exception e) {
- throw new RuntimeException(e);
- }
- }
-
- private static byte[] sm4DecryptCBC(byte[] keyBytes, byte[] cipher, byte[] iv) {
- if (keyBytes.length != 16) {
- throw new RuntimeException("err key length");
- }
-
- try {
- Key key = new SecretKeySpec(keyBytes, "SM4");
- Cipher in = Cipher.getInstance("SM4/CBC/PKCS7Padding", "BC");
- IvParameterSpec ivSpec = new IvParameterSpec(iv);
- in.init(Cipher.DECRYPT_MODE, key, ivSpec);
- return in.doFinal(cipher);
- } catch (Exception e) {
- throw new RuntimeException(e);
- }
- }
- }
![](https://csdnimg.cn/release/blogv2/dist/pc/img/newCodeMoreWhite.png)
Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。