/*     */ package com.ruoyi.hywz.utils;
/*     */ 
/*     */

import com.alibaba.fastjson.JSON;
import org.bouncycastle.crypto.digests.SM3Digest;
import org.bouncycastle.jce.provider.BouncyCastleProvider;
import org.bouncycastle.pqc.math.linearalgebra.ByteUtils;
import org.bouncycastle.util.encoders.Hex;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import javax.crypto.Cipher;
import javax.crypto.KeyGenerator;
import javax.crypto.spec.SecretKeySpec;
import java.io.UnsupportedEncodingException;
import java.net.URLEncoder;
import java.security.Key;
import java.security.Provider;
import java.security.SecureRandom;
import java.security.Security;
import java.util.*;

/*     */
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ public class SMUtil
/*     */ {
/*  31 */   private static final Logger logger = LoggerFactory.getLogger(SMUtil.class);
/*     */   
/*     */   static {
/*  34 */     Security.addProvider((Provider)new BouncyCastleProvider());
/*     */   }
/*     */ 
/*     */ 
/*     */   
/*     */   private static final String ENCODING = "UTF-8";
/*     */ 
/*     */   
/*     */   public static final String ALGORITHM_NAME = "SM4";
/*     */ 
/*     */   
/*     */   public static final String ALGORITHM_NAME_ECB_PADDING = "SM4/ECB/PKCS5Padding";
/*     */ 
/*     */   
/*     */   public static final int DEFAULT_KEY_SIZE = 128;
/*     */ 
/*     */ 
/*     */   
/*     */   public static String generateKey() throws Exception {
/*  53 */     return new String(Hex.encode(generateKey(128)));
/*     */   }
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */   
/*     */   public static String generateKey(String apikey) throws Exception {
/*  63 */     return new String(Hex.encode(generateKey(128, apikey)));
/*     */   }
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */   
/*     */   public static byte[] generateKey(int keySize) throws Exception {
/*  73 */     KeyGenerator kg = KeyGenerator.getInstance("SM4", "BC");
/*  74 */     kg.init(keySize, new SecureRandom());
/*  75 */     return kg.generateKey().getEncoded();
/*     */   }
/*     */   public static byte[] generateKey(int keySize, String name) throws Exception {
/*  78 */     KeyGenerator kg = KeyGenerator.getInstance("SM4", "BC");
/*  79 */     kg.init(keySize, new SecureRandom(name.getBytes("UTF-8")));
/*  80 */     return kg.generateKey().getEncoded();
/*     */   }
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */   
/*     */   private static Cipher generateSM4EcbCipher(String algorithmName, int mode, byte[] key) throws Exception {
/*  94 */     Cipher cipher = Cipher.getInstance(algorithmName, "BC");
/*  95 */     Key sm4Key = new SecretKeySpec(key, "SM4");
/*  96 */     cipher.init(mode, sm4Key);
/*  97 */     return cipher;
/*     */   }
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */   
/*     */   public static String encryptSM4EcbByApikey(String apikey, String paramStr) throws Exception {
/* 109 */     return encryptSM4Ecb(generateKey(apikey), paramStr);
/*     */   }
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */   
/*     */   public static String encryptSM4Ecb(String hexKey, String paramStr) {
/*     */     try {
/* 123 */       String cipherText = "";
/*     */       
/* 125 */       byte[] keyData = ByteUtils.fromHexString(hexKey);
/*     */       
/* 127 */       byte[] srcData = paramStr.getBytes("UTF-8");
/*     */       
/* 129 */       byte[] cipherArray = encryptSM4EcbPadding(keyData, srcData);
/*     */       
/* 131 */       cipherText = ByteUtils.toHexString(cipherArray);
/* 132 */       return cipherText;
/* 133 */     } catch (Exception e) {
/* 134 */       return paramStr;
/*     */     } 
/*     */   }
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */   
/*     */   public static byte[] encryptSM4EcbPadding(byte[] key, byte[] data) throws Exception {
/* 148 */     Cipher cipher = generateSM4EcbCipher("SM4/ECB/PKCS5Padding", 1, key);
/* 149 */     return cipher.doFinal(data);
/*     */   }
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */   
/*     */   public static String decryptSM4EcbByApiKey(String apikey, String cipherText) throws Exception {
/* 160 */     return decryptSM4Ecb(generateKey(apikey), cipherText);
/*     */   }
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */   
/*     */   public static String decryptSM4Ecb(String hexKey, String cipherText) throws Exception {
/* 174 */     String decryptStr = "";
/*     */     
/* 176 */     byte[] keyData = ByteUtils.fromHexString(hexKey);
/*     */     
/* 178 */     byte[] cipherData = ByteUtils.fromHexString(cipherText);
/*     */     
/* 180 */     byte[] srcData = new byte[0];
/* 181 */     srcData = decryptSM4EcbPadding(keyData, cipherData);
/*     */     
/* 183 */     decryptStr = new String(srcData, "UTF-8");
/* 184 */     return decryptStr;
/*     */   }
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */   
/*     */   public static byte[] decryptSM4EcbPadding(byte[] key, byte[] cipherText) throws Exception {
/* 197 */     Cipher cipher = generateSM4EcbCipher("SM4/ECB/PKCS5Padding", 2, key);
/* 198 */     return cipher.doFinal(cipherText);
/*     */   }
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */   
/*     */   public static boolean verifySM4Ecb(String hexKey, String cipherText, String paramStr) throws Exception {
/* 213 */     boolean flag = false;
/*     */     
/* 215 */     byte[] keyData = ByteUtils.fromHexString(hexKey);
/*     */     
/* 217 */     byte[] cipherData = ByteUtils.fromHexString(cipherText);
/*     */     
/* 219 */     byte[] decryptData = decryptSM4EcbPadding(keyData, cipherData);
/*     */     
/* 221 */     byte[] srcData = paramStr.getBytes("UTF-8");
/*     */     
/* 223 */     flag = Arrays.equals(decryptData, srcData);
/* 224 */     return flag;
/*     */   }
/*     */ 
/*     */   
/*     */   public static String sm3(String originStr) throws UnsupportedEncodingException {
/* 229 */     byte[] srcData = originStr.getBytes("UTF-8");
/* 230 */     byte[] hash = sm3Hash(srcData);
/* 231 */     String resultHexStr = ByteUtils.toHexString(hash);
/* 232 */     return resultHexStr;
/*     */   }
/*     */   
/*     */   public static byte[] sm3Hash(byte[] srcData) {
/* 236 */     SM3Digest digest = new SM3Digest();
/* 237 */     digest.update(srcData, 0, srcData.length);
/* 238 */     byte[] hash = new byte[digest.getDigestSize()];
/* 239 */     digest.doFinal(hash, 0);
/* 240 */     return hash;
/*     */   }
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */   
/*     */   public static String paraFilter(Map<String, Object> sArray) {
/* 250 */     if (sArray == null || sArray.size() <= 0) {
/* 251 */       return "";
/*     */     }
/*     */     
/* 254 */     List<String> keys = new ArrayList<>(sArray.keySet());
/* 255 */     Collections.sort(keys);
/* 256 */     StringBuffer sb = new StringBuffer();
/* 257 */     for (int i = 0; i < keys.size(); i++) {
/* 258 */       String value, key = keys.get(i);
/* 259 */       Object val = sArray.get(key);
/*     */       
/* 261 */       if (val instanceof String) {
/* 262 */         value = (String)val;
/*     */       } else {
/* 264 */         value = JSON.toJSONString(val);
/*     */       } 
/* 266 */       if (value != null && !value.isEmpty())
/*     */       {
/*     */ 
/*     */         
/* 270 */         if (i == keys.size() - 1) {
/* 271 */           sb.append(key).append("=").append(value);
/*     */         } else {
/* 273 */           sb.append(key).append("=").append(value).append("&");
/*     */         } 
/*     */       }
/*     */     } 
/* 277 */     return sb.toString();
/*     */   }
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */   
/*     */   public static String paraFilterAndEnCode(Map<String, Object> sArray) {
/* 285 */     if (sArray == null || sArray.size() <= 0) {
/* 286 */       return "";
/*     */     }
/*     */     
/* 289 */     List<String> keys = new ArrayList<>(sArray.keySet());
/* 290 */     Collections.sort(keys);
/* 291 */     StringBuffer sb = new StringBuffer();
/* 292 */     for (int i = 0; i < keys.size(); i++) {
/* 293 */       String value, key = keys.get(i);
/* 294 */       Object val = sArray.get(key);
/*     */       
/* 296 */       if (val instanceof String) {
/* 297 */         value = (String)val;
/*     */       } else {
/* 299 */         value = JSON.toJSONString(val);
/*     */       } 
/* 301 */       if (value != null && !value.isEmpty())
/*     */       {
/*     */ 
/*     */         
/* 305 */         if (i == keys.size() - 1) {
/* 306 */           sb.append(key).append("=").append(value);
/*     */         } else {
/* 308 */           sb.append(key).append("=").append(value).append("&");
/*     */         }  } 
/*     */     } 
/*     */     try {
/* 312 */       return URLEncoder.encode(sb.toString(), "utf-8");
/* 313 */     } catch (UnsupportedEncodingException e) {
/* 314 */       logger.warn("encode request params exception.", e);
/* 315 */       return "";
/*     */     } 
/*     */   }
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */   
/*     */   public static void main(String[] args) throws Exception {
/* 349 */     System.out.println(generateKey());
/*     */   }
/*     */ }


/* Location:              C:\Users\ningj\Desktop\标准接口规范\test-api-send\smencrypt\!\com\example\SMUtil.class
 * Java compiler version: 8 (52.0)
 * JD-Core Version:       1.1.3
 */