Example #1
0
        /// <summary>
        /// 获取远程服务器ATN结果
        /// </summary>
        /// <param name="strUrl">指定URL路径地址</param>
        /// <param name="timeout">超时时间设置</param>
        /// <returns>服务器ATN结果</returns>
        private string Get_Http(string strUrl, int timeout)
        {
            string strResult;

            try
            {
                HttpWebRequest myReq = (HttpWebRequest)HttpWebRequest.Create(strUrl);
                myReq.Timeout = timeout;
                HttpWebResponse HttpWResp  = (HttpWebResponse)myReq.GetResponse();
                Stream          myStream   = HttpWResp.GetResponseStream();
                StreamReader    sr         = new StreamReader(myStream, Encoding.Default);
                StringBuilder   strBuilder = new StringBuilder();
                while (-1 != sr.Peek())
                {
                    strBuilder.Append(sr.ReadLine());
                }

                strResult = strBuilder.ToString();
            }
            catch (Exception exp)
            {
                strResult = "错误:" + exp.Message;
                WriteLogHelper.WriteError(exp);
            }

            return(strResult);
        }
Example #2
0
        /// <summary>
        /// 发送短信通用接口
        /// </summary>
        /// <param name="signName">管理控制台中配置的短信签名(状态必须是验证通过)</param>
        /// <param name="templateCode">管理控制台中配置的审核通过的短信模板的模板CODE(状态必须是验证通过)</param>
        /// <param name="recNum">接收号码,多个号码可以逗号分隔</param>
        /// <param name="paramString">短信模板中的变量;数字需要转换为字符串;个人用户每个变量长度必须小于15个字符。例:{"code":"123456","product":"登录"}</param>
        /// <returns>成功返回200</returns>
        public string SingleSendSms(string signName, string templateCode, string recNum, string paramString)
        {
            IClientProfile       profile = DefaultProfile.GetProfile("cn-hangzhou", "PrDPRjqAl2epRSnX", "H7vLdyxHi23Xz7hDsAevGruVAWxsFP");
            IAcsClient           client  = new DefaultAcsClient(profile);
            SingleSendSmsRequest request = new SingleSendSmsRequest();

            try
            {
                request.SignName     = signName;     // "管理控制台中配置的短信签名(状态必须是验证通过)";
                request.TemplateCode = templateCode; //"管理控制台中配置的审核通过的短信模板的模板CODE(状态必须是验证通过)";
                request.RecNum       = recNum;       //"接收号码,多个号码可以逗号分隔";
                request.ParamString  = paramString;  //"短信模板中的变量;数字需要转换为字符串;个人用户每个变量长度必须小于15个字符。";
                SingleSendSmsResponse httpResponse = client.GetAcsResponse(request);
                return("200");
            }
            catch (ServerException ex)
            {
                WriteLogHelper.WriteError(ex);
                throw;
            }
            catch (ClientException ex)
            {
                WriteLogHelper.WriteError(ex);
                throw;
            }
        }
Example #3
0
        /// <summary>
        /// 验证签名
        /// </summary>
        /// <param name="content">需要验证的内容</param>
        /// <param name="signedString">签名结果</param>
        /// <param name="publicKey">公钥</param>
        /// <param name="input_charset">编码格式</param>
        /// <returns></returns>
        public static bool Verify(string content, string signedString, string publicKey, string input_charset)
        {
            try
            {
                bool result = false;

                Encoding                 code    = Encoding.GetEncoding(input_charset);
                byte[]                   Data    = code.GetBytes(content);
                byte[]                   data    = Convert.FromBase64String(signedString);
                RSAParameters            paraPub = ConvertFromPublicKey(publicKey);
                RSACryptoServiceProvider rsaPub  = new RSACryptoServiceProvider();
                rsaPub.ImportParameters(paraPub);

                SHA1 sh = new SHA1CryptoServiceProvider();
                result = rsaPub.VerifyData(Data, sh, data);
                return(result);
            }
            catch (System.Exception ex)
            {
                WriteLogHelper.WriteError(ex);
                throw ex;
            }
        }
Example #4
0
 private static RSACryptoServiceProvider LoadCertificateFile(string filename)
 {
     using (System.IO.FileStream fs = System.IO.File.OpenRead(filename))
     {
         byte[] data = new byte[fs.Length];
         byte[] res  = null;
         fs.Read(data, 0, data.Length);
         if (data[0] != 0x30)
         {
             res = GetPem("RSA PRIVATE KEY", data);
         }
         try
         {
             RSACryptoServiceProvider rsa = DecodeRSAPrivateKey(res);
             return(rsa);
         }
         catch (Exception ex)
         {
             WriteLogHelper.WriteError(ex);
             throw ex;
         }
     }
 }
Example #5
0
        private static RSACryptoServiceProvider DecodeRSAPrivateKey(byte[] privkey)
        {
            byte[] MODULUS, E, D, P, Q, DP, DQ, IQ;

            // ---------  Set up stream to decode the asn.1 encoded RSA private key  ------
            MemoryStream mem      = new MemoryStream(privkey);
            BinaryReader binr     = new BinaryReader(mem); //wrap Memory Stream with BinaryReader for easy reading
            byte         bt       = 0;
            ushort       twobytes = 0;
            int          elems    = 0;

            try
            {
                twobytes = binr.ReadUInt16();
                if (twobytes == 0x8130) //data read as little endian order (actual data order for Sequence is 30 81)
                {
                    binr.ReadByte();    //advance 1 byte
                }
                else if (twobytes == 0x8230)
                {
                    binr.ReadInt16();   //advance 2 bytes
                }
                else
                {
                    return(null);
                }

                twobytes = binr.ReadUInt16();
                if (twobytes != 0x0102) //version number
                {
                    return(null);
                }
                bt = binr.ReadByte();
                if (bt != 0x00)
                {
                    return(null);
                }


                //------  all private key components are Integer sequences ----
                elems   = GetIntegerSize(binr);
                MODULUS = binr.ReadBytes(elems);

                elems = GetIntegerSize(binr);
                E     = binr.ReadBytes(elems);

                elems = GetIntegerSize(binr);
                D     = binr.ReadBytes(elems);

                elems = GetIntegerSize(binr);
                P     = binr.ReadBytes(elems);

                elems = GetIntegerSize(binr);
                Q     = binr.ReadBytes(elems);

                elems = GetIntegerSize(binr);
                DP    = binr.ReadBytes(elems);

                elems = GetIntegerSize(binr);
                DQ    = binr.ReadBytes(elems);

                elems = GetIntegerSize(binr);
                IQ    = binr.ReadBytes(elems);

                // ------- create RSACryptoServiceProvider instance and initialize with public key -----
                RSACryptoServiceProvider RSA       = new RSACryptoServiceProvider();
                RSAParameters            RSAparams = new RSAParameters();
                RSAparams.Modulus  = MODULUS;
                RSAparams.Exponent = E;
                RSAparams.D        = D;
                RSAparams.P        = P;
                RSAparams.Q        = Q;
                RSAparams.DP       = DP;
                RSAparams.DQ       = DQ;
                RSAparams.InverseQ = IQ;
                RSA.ImportParameters(RSAparams);
                return(RSA);
            }
            catch (Exception ex)
            {
                WriteLogHelper.WriteError(ex);
                throw ex;
            }
            finally { binr.Close(); }
        }
Example #6
0
        private static RSACryptoServiceProvider DecodePrivateKeyInfo(byte[] pkcs8)
        {
            byte[] SeqOID = { 0x30, 0x0D, 0x06, 0x09, 0x2A, 0x86, 0x48, 0x86, 0xF7, 0x0D, 0x01, 0x01, 0x01, 0x05, 0x00 };
            byte[] seq    = new byte[15];

            MemoryStream mem       = new MemoryStream(pkcs8);
            int          lenstream = (int)mem.Length;
            BinaryReader binr      = new BinaryReader(mem); //wrap Memory Stream with BinaryReader for easy reading
            byte         bt        = 0;
            ushort       twobytes  = 0;

            try
            {
                twobytes = binr.ReadUInt16();
                if (twobytes == 0x8130) //data read as little endian order (actual data order for Sequence is 30 81)
                {
                    binr.ReadByte();    //advance 1 byte
                }
                else if (twobytes == 0x8230)
                {
                    binr.ReadInt16();   //advance 2 bytes
                }
                else
                {
                    return(null);
                }


                bt = binr.ReadByte();
                if (bt != 0x02)
                {
                    return(null);
                }

                twobytes = binr.ReadUInt16();

                if (twobytes != 0x0001)
                {
                    return(null);
                }

                seq = binr.ReadBytes(15);               //read the Sequence OID
                if (!CompareBytearrays(seq, SeqOID))    //make sure Sequence for OID is correct
                {
                    return(null);
                }

                bt = binr.ReadByte();
                if (bt != 0x04) //expect an Octet string
                {
                    return(null);
                }

                bt = binr.ReadByte();           //read next byte, or next 2 bytes is  0x81 or 0x82; otherwise bt is the byte count
                if (bt == 0x81)
                {
                    binr.ReadByte();
                }
                else
                if (bt == 0x82)
                {
                    binr.ReadUInt16();
                }
                //------ at this stage, the remaining sequence should be the RSA private key

                byte[] rsaprivkey = binr.ReadBytes((int)(lenstream - mem.Position));
                RSACryptoServiceProvider rsacsp = DecodeRSAPrivateKey(rsaprivkey);
                return(rsacsp);
            }

            catch (Exception ex)
            {
                WriteLogHelper.WriteError(ex);
                throw ex;
            }

            finally { binr.Close(); }
        }