Ejemplo n.º 1
0
        public virtual ECPoint Init_enc(SM2CryptoServiceProvider sm2, ECPoint userKey)
        {
            //BigInteger k = null;
            //ECPoint c1 = null;

            //AsymmetricCipherKeyPair key = sm2.ecc_key_pair_generator.GenerateKeyPair();
            //ECPrivateKeyParameters ecpriv = (ECPrivateKeyParameters)key.Private;
            //ECPublicKeyParameters ecpub = (ECPublicKeyParameters)key.Public;
            //k = ecpriv.D;
            //c1 = ecpub.Q;

            //p2 = userKey.Multiply(k);
            //Reset();

            //return c1;

            var keySTR = userKey.GetEncoded().byteToHex();

            if (keySTR.Length > 64)
            {
                keySTR = keySTR.Substring(0, 64);
            }
            BigInteger k  = new BigInteger(keySTR, 16);  //ecpriv.getD();
            ECPoint    c1 = sm2.ecc_point_g.Multiply(k); //ecpub.getQ();

            this.p2 = userKey.Multiply(k);
            Reset();
            return(c1);
        }
Ejemplo n.º 2
0
        /// <summary>
        /// SM2签名Hard转soft
        /// </summary>
        /// <param name="hardSign"></param>
        /// <returns></returns>
        public static String SM2SignHardToSoft(String hardSign)
        {
            byte[] bytes = hardSign.hexToByte();
            byte[] r     = new byte[bytes.Length / 2];
            byte[] s     = new byte[bytes.Length / 2];
            System.Array.Copy(bytes, 0, r, 0, bytes.Length / 2);
            System.Array.Copy(bytes, bytes.Length / 2, s, 0, bytes.Length / 2);
            var d_r = new DerInteger(SM2CryptoServiceProvider.byteConvertInteger(r));
            var d_s = new DerInteger(SM2CryptoServiceProvider.byteConvertInteger(s));
            var v2  = new Asn1EncodableVector();

            v2.Add(d_r);
            v2.Add(d_s);
            var sign = new DerSequence(v2);

            String result = null;

            try
            {
                result = sign.GetEncoded().ByteArrayToHex();
            }
            catch (IOException e)
            {
                //e.printStackTrace();
                throw (e);
            }
            //SM2加密机转软加密编码格式
            //return SM2SignHardKeyHead+hardSign.substring(0, hardSign.Length()/2)+SM2SignHardKeyMid+hardSign.substring(hardSign.Length()/2);
            return(result);
        }
Ejemplo n.º 3
0
        public static Dictionary <string, string> GetKeyPair()
        {
            SM2CryptoServiceProvider sm2    = SM2CryptoServiceProvider.Instance;
            AsymmetricCipherKeyPair  key    = sm2.ecc_key_pair_generator.GenerateKeyPair();
            ECPrivateKeyParameters   ecpriv = (ECPrivateKeyParameters)key.Private;
            ECPublicKeyParameters    ecpub  = (ECPublicKeyParameters)key.Public;
            BigInteger privateKey           = ecpriv.D;
            ECPoint    publicKey            = ecpub.Q;

            var result = new Dictionary <string, string>();

            result.Add("公钥", Encoding.UTF8.GetString(Hex.Encode(publicKey.GetEncoded())).ToUpper());
            result.Add("私钥", Encoding.UTF8.GetString(Hex.Encode(privateKey.ToByteArray())).ToUpper());
            return(result);
        }
Ejemplo n.º 4
0
        public static byte[] Decrypt(byte[] privateKey, byte[] encryptedData)
        {
            if (null == privateKey || privateKey.Length == 0)
            {
                return(null);
            }
            if (encryptedData == null || encryptedData.Length == 0)
            {
                return(null);
            }
            //加密字节数组转换为十六进制的字符串 长度变为encryptedData.length * 2
            var data = encryptedData.byteToHex();// Encoding.UTF8.GetString(Hex.Encode(encryptedData));

            byte[] c1Bytes = data.Substring(0, 130).hexToByte();
            int    c2Len   = encryptedData.Length - 97;

            byte[] c2 = data.Substring(130, 2 * c2Len).hexToByte();
            byte[] c3 = data.Substring(130 + 2 * c2Len, 64).hexToByte();

            //byte[] c1Bytes = Hex.Decode(Encoding.UTF8.GetBytes(data.Substring(0, 130)));
            //int c2Len = encryptedData.Length - 97;
            //byte[] c2 = Hex.Decode(Encoding.UTF8.GetBytes(data.Substring(130, 2 * c2Len)));
            //byte[] c3 = Hex.Decode(Encoding.UTF8.GetBytes(data.Substring(130 + 2 * c2Len, 64)));

            SM2CryptoServiceProvider sm2 = SM2CryptoServiceProvider.Instance;
            BigInteger userD             = new BigInteger(1, privateKey);

            ECPoint   c1     = sm2.ecc_curve.DecodePoint(c1Bytes);
            SM2Cipher cipher = new SM2Cipher();

            cipher.Init_dec(userD, c1);
            cipher.Decrypt(c2);
            cipher.Dofinal(c3);

            return(c2);
        }