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

import org.bouncycastle.asn1.gm.GMNamedCurves;
import org.bouncycastle.asn1.x9.X9ECParameters;
import org.bouncycastle.crypto.AsymmetricCipherKeyPair;
import org.bouncycastle.crypto.CipherParameters;
import org.bouncycastle.crypto.InvalidCipherTextException;
import org.bouncycastle.crypto.KeyGenerationParameters;
import org.bouncycastle.crypto.engines.SM2Engine;
import org.bouncycastle.crypto.generators.ECKeyPairGenerator;
import org.bouncycastle.crypto.params.*;
import org.bouncycastle.math.ec.ECCurve;
import org.bouncycastle.math.ec.ECPoint;
import org.bouncycastle.util.BigIntegers;
import org.bouncycastle.util.encoders.Hex;

import java.math.BigInteger;
import java.security.NoSuchAlgorithmException;
import java.security.SecureRandom;
import java.util.HashMap;
import java.util.Map;

/*     */
/*     */
/*     */
/*     */
/*     */
/*     */
/*     */
/*     */
/*     */
/*     */
/*     */
/*     */
/*     */
/*     */
/*     */
/*     */
/*     */
/*     */
/*     */
/*     */
/*     */
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ public class Sm2Utils
/*     */ {
/*  42 */   private static ECDomainParameters domainParameters = null;
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */   
/*     */   static {
/*  49 */     X9ECParameters sm2ECParameters = GMNamedCurves.getByName("sm2p256v1");
/*  50 */     domainParameters = new ECDomainParameters(sm2ECParameters.getCurve(), sm2ECParameters.getG(), sm2ECParameters.getN());
/*     */   }
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */   
/*     */   public static void main(String[] args) throws NoSuchAlgorithmException, InvalidCipherTextException {}
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */   
/*     */   public static Sm2KeyVO genPUPRkey() throws NoSuchAlgorithmException {
/*  74 */     Sm2KeyVO sm2Key = new Sm2KeyVO();
/*     */     
/*  76 */     ECKeyPairGenerator keyPairGenerator = new ECKeyPairGenerator();
/*  77 */     keyPairGenerator.init((KeyGenerationParameters)new ECKeyGenerationParameters(domainParameters, SecureRandom.getInstance("SHA1PRNG")));
/*  78 */     AsymmetricCipherKeyPair asymmetricCipherKeyPair = keyPairGenerator.generateKeyPair();
/*     */ 
/*     */     
/*  81 */     BigInteger privatekey = ((ECPrivateKeyParameters)asymmetricCipherKeyPair.getPrivate()).getD();
/*  82 */     String privateKeyHex = privatekey.toString(16);
/*  83 */     sm2Key.setPrivateKey(privateKeyHex);
/*     */ 
/*     */     
/*  86 */     ECPoint ecPoint = ((ECPublicKeyParameters)asymmetricCipherKeyPair.getPublic()).getQ();
/*  87 */     String publicKeyHex = Hex.toHexString(ecPoint.getEncoded(false));
/*  88 */     sm2Key.setPublicKey(publicKeyHex);
/*  89 */     return sm2Key;
/*     */   }
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */   
/*     */   public static String decrypt(String ciphertext, String privatekey) throws InvalidCipherTextException {
/* 103 */     String cipherData = ciphertext;
/* 104 */     byte[] cipherDataByte = Hex.decode(cipherData);
/*     */     
/* 106 */     BigInteger privateKeyD = new BigInteger(privatekey, 16);
/* 107 */     ECPrivateKeyParameters privateKeyParameters = new ECPrivateKeyParameters(privateKeyD, domainParameters);
/*     */ 
/*     */     
/* 110 */     SM2Engine sm2Engine = new SM2Engine();
/* 111 */     sm2Engine.init(false, (CipherParameters)privateKeyParameters);
/*     */ 
/*     */     
/* 114 */     byte[] arrayOfBytes = sm2Engine.processBlock(cipherDataByte, 0, cipherDataByte.length);
/*     */ 
/*     */     
/* 117 */     String mtext = new String(arrayOfBytes);
/* 118 */     return mtext;
/*     */   }
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */   
/*     */   public static String encrypt(String text, String publickey) throws InvalidCipherTextException {
/* 131 */     Map<String, String> pubilcKeys = createPubilcKey(publickey);
/* 132 */     String cipherData = text;
/* 133 */     byte[] cipherDataByte = text.getBytes();
/*     */     
/* 135 */     SM2Engine sm2Engine = new SM2Engine();
/* 136 */     CipherParameters pubKeyParameters = getPubKey(pubilcKeys.get("x"), pubilcKeys.get("y"), domainParameters);
/*     */     
/* 138 */     sm2Engine.init(true, pubKeyParameters);
/*     */ 
/*     */     
/* 141 */     byte[] arrayOfBytes = sm2Engine.processBlock(cipherDataByte, 0, cipherDataByte.length);
/*     */ 
/*     */     
/* 144 */     String mtext = Hex.toHexString(arrayOfBytes);
/* 145 */     return mtext;
/*     */   }
/*     */   
/*     */   private static Map<String, String> createPubilcKey(String pubKeyHex) {
/* 149 */     Map<String, String> keys = new HashMap<>();
/* 150 */     if (pubKeyHex.length() > 128) {
/* 151 */       pubKeyHex = pubKeyHex.substring(pubKeyHex.length() - 128);
/*     */     }
/*     */     
/* 154 */     String stringX = pubKeyHex.substring(0, 64);
/* 155 */     String stringY = pubKeyHex.substring(stringX.length());
/* 156 */     keys.put("x", stringX);
/* 157 */     keys.put("y", stringY);
/* 158 */     return keys;
/*     */   }
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */   
/*     */   public static CipherParameters getPubKey(String x, String y, ECDomainParameters domain) {
/* 170 */     ECCurve curve = domain.getCurve();
/* 171 */     ECPoint point = curve.createPoint(BigIntegers.fromUnsignedByteArray(Hex.decode(x)), BigIntegers.fromUnsignedByteArray(Hex.decode(y)));
/* 172 */     ECPublicKeyParameters PublicKey = new ECPublicKeyParameters(point, domain);
/* 173 */     return (CipherParameters)new ParametersWithRandom((CipherParameters)PublicKey);
/*     */   }
/*     */ }


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