/*     */ package cn.com.esgcc.ai.matrix.sdk.util.encrypt.sm4;
/*     */ 
/*     */

import cn.hutool.core.lang.TypeReference;
import cn.hutool.json.JSONUtil;
import sun.misc.BASE64Decoder;
import sun.misc.BASE64Encoder;

import java.io.UnsupportedEncodingException;
import java.nio.charset.StandardCharsets;
import java.util.HashMap;
import java.util.Map;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ public class SM4Utils
/*     */ {
/*  21 */   private String iv = "isjngyfhxbfoldnu";
/*     */ 
/*     */   
/*     */   private boolean hexString = false;
/*     */ 
/*     */   
/*     */   public static String encryptData_ECB(String plainText, String secretKey) {
/*     */     try {
/*  29 */       SM4_Context ctx = new SM4_Context();
/*  30 */       ctx.isPadding = true;
/*  31 */       ctx.mode = 1;
/*     */ 
/*     */       
/*  34 */       byte[] keyBytes = secretKey.getBytes(StandardCharsets.UTF_8);
/*  35 */       SM4 sm4 = new SM4();
/*  36 */       sm4.sm4_setkey_enc(ctx, keyBytes);
/*  37 */       byte[] encrypted = sm4.sm4_crypt_ecb(ctx, plainText.getBytes(StandardCharsets.UTF_8));
/*  38 */       String cipherText = (new BASE64Encoder()).encode(encrypted);
/*  39 */       if (cipherText != null && cipherText.trim().length() > 0) {
/*  40 */         Pattern p = Pattern.compile("\\s*|\t|\r|\n");
/*  41 */         Matcher m = p.matcher(cipherText);
/*  42 */         cipherText = m.replaceAll("");
/*     */       } 
/*  44 */       return cipherText;
/*  45 */     } catch (Exception e) {
/*  46 */       e.printStackTrace();
/*  47 */       return null;
/*     */     } 
/*     */   }
/*     */ 
/*     */ 
/*     */   
/*     */   public static String decryptData_ECB(String cipherText, String secretKey) {
/*     */     try {
/*  55 */       SM4_Context ctx = new SM4_Context();
/*  56 */       ctx.isPadding = true;
/*  57 */       ctx.mode = 0;
/*     */ 
/*     */       
/*  60 */       byte[] keyBytes = secretKey.getBytes();
/*  61 */       SM4 sm4 = new SM4();
/*  62 */       sm4.sm4_setkey_dec(ctx, keyBytes);
/*  63 */       byte[] decrypted = sm4.sm4_crypt_ecb(ctx, (new BASE64Decoder()).decodeBuffer(cipherText));
/*  64 */       return new String(decrypted, StandardCharsets.UTF_8.name());
/*  65 */     } catch (Exception e) {
/*  66 */       e.printStackTrace();
/*  67 */       return null;
/*     */     } 
/*     */   }
/*     */   
/*     */   public String encryptData_CBC(String plainText, String secretKey) {
/*     */     try {
/*  73 */       SM4_Context ctx = new SM4_Context();
/*  74 */       ctx.isPadding = true;
/*  75 */       ctx.mode = 1;
/*     */ 
/*     */ 
/*     */ 
/*     */       
/*  80 */       byte[] keyBytes = secretKey.getBytes(StandardCharsets.UTF_8.name());
/*  81 */       byte[] ivBytes = this.iv.getBytes(StandardCharsets.UTF_8.name());
/*     */       
/*  83 */       SM4 sm4 = new SM4();
/*  84 */       sm4.sm4_setkey_enc(ctx, keyBytes);
/*  85 */       byte[] encrypted = sm4.sm4_crypt_cbc(ctx, ivBytes, plainText.getBytes(StandardCharsets.UTF_8.name()));
/*  86 */       String cipherText = (new BASE64Encoder()).encode(encrypted);
/*  87 */       if (cipherText != null && cipherText.trim().length() > 0) {
/*  88 */         Pattern p = Pattern.compile("\\s*|\t|\r|\n");
/*  89 */         Matcher m = p.matcher(cipherText);
/*  90 */         cipherText = m.replaceAll("");
/*     */       } 
/*  92 */       return cipherText;
/*  93 */     } catch (Exception e) {
/*  94 */       e.printStackTrace();
/*  95 */       return null;
/*     */     } 
/*     */   }
/*     */   public String decryptData_CBC(String cipherText, String secretKey) {
/*     */     try {
/*     */       byte[] keyBytes, ivBytes;
/* 101 */       SM4_Context ctx = new SM4_Context();
/* 102 */       ctx.isPadding = true;
/* 103 */       ctx.mode = 0;
/*     */ 
/*     */ 
/*     */       
/* 107 */       if (this.hexString) {
/* 108 */         keyBytes = Util.hexStringToBytes(secretKey);
/* 109 */         ivBytes = Util.hexStringToBytes(this.iv);
/*     */       } else {
/* 111 */         keyBytes = secretKey.getBytes(StandardCharsets.UTF_8.name());
/* 112 */         ivBytes = this.iv.getBytes(StandardCharsets.UTF_8.name());
/*     */       } 
/*     */       
/* 115 */       SM4 sm4 = new SM4();
/* 116 */       sm4.sm4_setkey_dec(ctx, keyBytes);
/* 117 */       byte[] decrypted = sm4.sm4_crypt_cbc(ctx, ivBytes, (new BASE64Decoder()).decodeBuffer(cipherText));
/* 118 */       return new String(decrypted, StandardCharsets.UTF_8.name());
/* 119 */     } catch (Exception e) {
/* 120 */       e.printStackTrace();
/* 121 */       return null;
/*     */     } 
/*     */   }
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */   
/*     */   public String getIv() {
/* 130 */     return this.iv;
/*     */   }
/*     */   
/*     */   public void setIv(String iv) {
/* 134 */     this.iv = iv;
/*     */   }
/*     */   
/*     */   public static String robotEncryption(String appkey, String robotCode, String secretKey) {
/* 138 */     SM4Utils sm4 = new SM4Utils();
/* 139 */     sm4.iv = "OISwW9fW7cFhrSNS";
/* 140 */     Map<String, Object> map = new HashMap<>();
/* 141 */     map.put("appKey", appkey);
/* 142 */     map.put("robotCode", robotCode);
/* 143 */     String jiami = sm4.encryptData_CBC(JSONUtil.toJsonStr(map), secretKey);
/* 144 */     return jiami;
/*     */   }
/*     */   
/*     */   public static Map<String, String> decryptionRootCode(String robotCode, String secretKey) {
/* 148 */     SM4Utils sm4 = new SM4Utils();
/*     */     
/* 150 */     sm4.iv = "OISwW9fW7cFhrSNS";
/* 151 */     robotCode = sm4.decryptData_CBC(robotCode, secretKey);
/* 152 */     if (robotCode == null) {
/* 153 */       Map<String, String> map1 = new HashMap<>();
/* 154 */       map1.put("appKey", "0");
/* 155 */       return map1;
/*     */     } 
/* 157 */     TypeReference<Map<String, String>> typeReference = new TypeReference<Map<String, String>>() {  };
/* 158 */     Map<String, String> map = (Map<String, String>)JSONUtil.toBean(robotCode, typeReference, false);
/* 159 */     return map;
/*     */   }
/*     */ 
/*     */   
/*     */   public static String passwordResolve(String password, String secretKey) {
/* 164 */     SM4Utils sm4 = new SM4Utils();
/*     */     
/* 166 */     password = decryptData_ECB(password, secretKey);
/* 167 */     if (password == null) {
/* 168 */       return null;
/*     */     }
/* 170 */     return password;
/*     */   }
/*     */   
/*     */   public static void main(String[] args) throws UnsupportedEncodingException {
/* 174 */     String admin = encryptData_ECB("webAPP", "1234567887654321");
/* 175 */     System.out.println(admin);
/* 176 */     System.out.println(decryptData_ECB(admin, "1234567887654321"));
/*     */   }
/*     */ }


/* Location:              C:\Users\ningj\Desktop\java-sdk-1.0.0-SNAPSHOT\!\cn\com\esgcc\ai\matrix\sd\\util\encrypt\sm4\SM4Utils.class
 * Java compiler version: 8 (52.0)
 * JD-Core Version:       1.1.3
 */