private void CopyDirectory(string sourceDirName, string destDirName) { try { if (!Directory.Exists(destDirName)) { Directory.CreateDirectory(destDirName); File.SetAttributes(destDirName, File.GetAttributes(sourceDirName)); } if (destDirName[destDirName.Length - 1] != Path.DirectorySeparatorChar) { destDirName = destDirName + Path.DirectorySeparatorChar; } string[] files = Directory.GetFiles(sourceDirName); foreach (string file in files) { File.Copy(file, destDirName + Path.GetFileName(file), true); File.SetAttributes(destDirName + Path.GetFileName(file), FileAttributes.Normal); } string[] dirs = Directory.GetDirectories(sourceDirName); foreach (string dir in dirs) { CopyDirectory(dir, destDirName + Path.GetFileName(dir)); } } catch (Exception ex) { WriteLogHelper.WriteLog_client("CopyDirectory error", ex); MessageBox.Show(ex.Message); throw; } }
/// <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); }
/// <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; } }
/// <summary> /// 接收消息 /// </summary> /// <param name="queueName"></param> /// <param name="delegateHandle"></param> public void GetQueueMessage(string queueName, ReceiveActionDelegate delegateHandle) { using (var connection = _factory.CreateConnection()) using (var channel = connection.CreateModel()) { channel.QueueDeclare(queue: queueName, durable: true, exclusive: false, autoDelete: false, arguments: null); channel.BasicQos(prefetchSize: 0, prefetchCount: 1, global: false); var consumer = new EventingBasicConsumer(channel); consumer.Received += (model, ea) => { channel.BasicAck(deliveryTag: ea.DeliveryTag, multiple: false); var body = ea.Body; object objval; objval = CommonHelper.BytesToObject(body); delegateHandle(objval); WriteLogHelper.WriteLogDoc("GetQueueLog", "队列" + queueName + "获取数据成功", "rabbitMqLog"); }; channel.BasicConsume(queue: queueName, autoAck: false, consumer: consumer); } }
/// <summary> /// 接收消息 /// </summary> /// <param name="exchangeName"></param> /// <param name="queueName"></param> /// <param name="rootingkey"></param> /// <param name="delegateHandle"></param> public void GetQueueMessage(string exchangeName, string queueName, string rootingkey, ReceiveActionDelegate delegateHandle) { using (var connection = _factory.CreateConnection()) using (var channel = connection.CreateModel()) { channel.ExchangeDeclare(exchange: exchangeName, type: "direct"); channel.QueueDeclare(queue: queueName, durable: true, exclusive: false, autoDelete: false, arguments: null); ///绑定交换机队列 channel.QueueBind(queue: queueName, exchange: exchangeName, routingKey: rootingkey); var consumer = new EventingBasicConsumer(channel); //声明方法 consumer.Received += (model, ea) => { channel.BasicAck(deliveryTag: ea.DeliveryTag, multiple: false); var body = ea.Body; object objval; objval = CommonHelper.BytesToObject(body); delegateHandle(objval); WriteLogHelper.WriteLogDoc("GetExchangeQueueLog", "交换机:" + exchangeName + "绑定队列:" + queueName + "获取数据成功", "rabbitMqLog"); }; channel.BasicConsume(queue: queueName, autoAck: false, consumer: consumer); } }
private void UnZipFile(string zipFilePath, string targetDir) { try { ICCEmbedded.SharpZipLib.Zip.FastZipEvents evt = new ICCEmbedded.SharpZipLib.Zip.FastZipEvents(); ICCEmbedded.SharpZipLib.Zip.FastZip fz = new ICCEmbedded.SharpZipLib.Zip.FastZip(evt); fz.ExtractZip(zipFilePath, targetDir, ""); } catch (Exception ex) { WriteLogHelper.WriteLog_client("UnZipFile error", ex); MessageBox.Show(ex.Message); } }
/// <summary> /// 开始下载更新的zip /// </summary> private void DownloadUpdateFile() { try { string url = Constants.RemoteUrl_zip; var client = new System.Net.WebClient(); client.DownloadProgressChanged += (sender, e) => { UpdateProcess(e.BytesReceived, e.TotalBytesToReceive); }; client.DownloadDataCompleted += client_DownloadDataCompleted_File; client.DownloadDataAsync(new Uri(url)); SetButtonState(); } catch (Exception ex) { WriteLogHelper.WriteLog_client("DownloadUpdateFile error", ex); MessageBox.Show(ex.Message); } }
/// <summary> /// 创建消息 /// </summary> /// <param name="queueName"></param> /// <param name="obj"></param> public void CreateExchangeMessage(string exchangename, string rootingkey, object obj) { using (var connection = _factory.CreateConnection()) using (var channel = connection.CreateModel()) { channel.ExchangeDeclare(exchange: exchangename, type: "direct"); var body = CommonHelper.ObjectToBytes(obj); IBasicProperties props = channel.CreateBasicProperties(); props.ContentType = "text/plain"; props.DeliveryMode = 2; channel.BasicPublish(exchange: exchangename, routingKey: rootingkey, basicProperties: props, body: body); WriteLogHelper.WriteLogDoc("CreateExchangeQueueLog", "创建交换机" + exchangename + "加了数据", "rabbitMqLog"); } }
/// <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; } }
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; } } }
/// <summary> /// 创建消息 /// </summary> /// <param name="queueName"></param> /// <param name="obj"></param> public void CreateQueueMessage(string queueName, object obj) { using (var connection = _factory.CreateConnection()) using (var channel = connection.CreateModel()) { channel.QueueDeclare(queue: queueName, durable: true, exclusive: false, autoDelete: false, arguments: null); var body = CommonHelper.ObjectToBytes(obj); var properties = channel.CreateBasicProperties(); properties.Persistent = true; channel.BasicPublish(exchange: "", routingKey: queueName, basicProperties: properties, body: body); WriteLogHelper.WriteLogDoc("CreateQueueLog", "队列" + queueName + "加了数据", "rabbitMqLog"); } }
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(); } }
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(); } }