WebClient实现下载txt文件并与用户输入进行匹配 WebClient实现用户序列号验证 txt文件在服务器端密文存储 RSA解密加密

{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }

        private void label1_Click(object sender, EventArgs e)
        {

        }
        /// <summary>
        /// RSA加密
        /// </summary>
        /// <param name="publickey"></param>
        /// <param name="content"></param>
        /// <returns></returns>
        public static string RSAEncrypt(string content)
        {
            string publickey = @"<RSAKeyValue><Modulus>5m9m14XH3oqLJ8bNGw9e4rGpXpcktv9MSkHSVFVMjHbfv+SJ5v0ubqQxa5YjLN4vc49z7SVju8s0X4gZ6AzZTn06jzWOgyPRV54Q4I0DCYadWW4Ze3e+BOtwgVU1Og3qHKn8vygoj40J6U85Z/PTJu3hN1m75Zr195ju7g9v4Hk=</Modulus><Exponent>AQAB</Exponent></RSAKeyValue>";
            RSACryptoServiceProvider rsa = new RSACryptoServiceProvider();
            byte[] cipherbytes;
            rsa.FromXmlString(publickey);
            cipherbytes = rsa.Encrypt(Encoding.UTF8.GetBytes(content), false);

            return Convert.ToBase64String(cipherbytes);
        }

        /// <summary>
        /// RSA解密
        /// </summary>
        /// <param name="privatekey"></param>
        /// <param name="content"></param>
        /// <returns></returns>
        public static string RSADecrypt(string content)
        {
            string privatekey = @"<RSAKeyValue><Modulus>5m9m14XH3oqLJ8bNGw9e4rGpXpcktv9MSkHSVFVMjHbfv+SJ5v0ubqQxa5YjLN4vc49z7SVju8s0X4gZ6AzZTn06jzWOgyPRV54Q4I0DCYadWW4Ze3e+BOtwgVU1Og3qHKn8vygoj40J6U85Z/PTJu3hN1m75Zr195ju7g9v4Hk=</Modulus><Exponent>AQAB</Exponent><P>/hf2dnK7rNfl3lbqghWcpFdu778hUpIEBixCDL5WiBtpkZdpSw90aERmHJYaW2RGvGRi6zSftLh00KHsPcNUMw==</P><Q>6Cn/jOLrPapDTEp1Fkq+uz++1Do0eeX7HYqi9rY29CqShzCeI7LEYOoSwYuAJ3xA/DuCdQENPSoJ9KFbO4Wsow==</Q><DP>ga1rHIJro8e/yhxjrKYo/nqc5ICQGhrpMNlPkD9n3CjZVPOISkWF7FzUHEzDANeJfkZhcZa21z24aG3rKo5Qnw==</DP><DQ>MNGsCB8rYlMsRZ2ek2pyQwO7h/sZT8y5ilO9wu08Dwnot/7UMiOEQfDWstY3w5XQQHnvC9WFyCfP4h4QBissyw==</DQ><InverseQ>EG02S7SADhH1EVT9DD0Z62Y0uY7gIYvxX/uq+IzKSCwB8M2G7Qv9xgZQaQlLpCaeKbux3Y59hHM+KpamGL19Kg==</InverseQ><D>vmaYHEbPAgOJvaEXQl+t8DQKFT1fudEysTy31LTyXjGu6XiltXXHUuZaa2IPyHgBz0Nd7znwsW/S44iql0Fen1kzKioEL3svANui63O3o5xdDeExVM6zOf1wUUh/oldovPweChyoAdMtUzgvCbJk1sYDJf++Nr0FeNW1RB1XG30=</D></RSAKeyValue>";
            RSACryptoServiceProvider rsa = new RSACryptoServiceProvider();
            byte[] cipherbytes;
            rsa.FromXmlString(privatekey);
            cipherbytes = rsa.Decrypt(Convert.FromBase64String(content), false);

            return Encoding.UTF8.GetString(cipherbytes);
        }

        /// <summary>
        /// 验证
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void button1_Click(object sender, EventArgs e)
        {
            //WebClient client = new WebClient();
            //byte[] buffer = client.DownloadData("http://ygb.nankai.edu.cn/liangshiyiyou/aaa.txt");
            //string res = System.Text.ASCIIEncoding.ASCII.GetString(buffer);
            //string f = Encoding.Default.GetString(buffer);

        
            //if (s == this.textBox1.Text)
            //{
            //    MessageBox.Show("验证成功!", "系统提示 ", MessageBoxButtons.OK, MessageBoxIcon.Information);
            //    this.Close();


            //}
            DownloadFile();
            //这样一个TXT就存在一个STR字符串数组里了,str[0]是第一行数据,一次类推。
            if (File.Exists("D:\\aaa.txt"))
            {
                string[] str = File.ReadAllLines("D:\\aaa.txt");
                //解密
             
          
             

                for (int i = 0; i < str.Length; i++)
                {
                    string jiemiwen =RSADecrypt(str[i]);
                    if (jiemiwen == this.textBox1.Text)//找到唯一的MAC地址
                    {
                        MessageBox.Show("验证成功!", "系统提示 ", MessageBoxButtons.OK, MessageBoxIcon.Information);

                    }
                   
                   
                }
               File.Delete(@"D:\\aaa.txt");
            }



          
        

        }
        ///// <summary>
        ///// RSA加密
        ///// </summary>
        ///// <param name="publickey"></param>
        ///// <param name="content"></param>
        ///// <returns></returns>
        //public static string RSAEncrypt(string publickey, string content)
        //{
        //    publickey = @"<RSAKeyValue><Modulus>5m9m14XH3oqLJ8bNGw9e4rGpXpcktv9MSkHSVFVMjHbfv+SJ5v0ubqQxa5YjLN4vc49z7SVju8s0X4gZ6AzZTn06jzWOgyPRV54Q4I0DCYadWW4Ze3e+BOtwgVU1Og3qHKn8vygoj40J6U85Z/PTJu3hN1m75Zr195ju7g9v4Hk=</Modulus><Exponent>AQAB</Exponent></RSAKeyValue>";
        //    RSACryptoServiceProvider rsa = new RSACryptoServiceProvider();
        //    byte[] cipherbytes;
        //    rsa.FromXmlString(publickey);
        //    cipherbytes = rsa.Encrypt(Encoding.UTF8.GetBytes(content), false);

        //    return Convert.ToBase64String(cipherbytes);
        //}

        ///// <summary>
        ///// RSA解密
        ///// </summary>
        ///// <param name="privatekey"></param>
        ///// <param name="content"></param>
        ///// <returns></returns>
        //public static string RSADecrypt(string privatekey, string content)
        //{
        //    privatekey = @"<RSAKeyValue><Modulus>5m9m14XH3oqLJ8bNGw9e4rGpXpcktv9MSkHSVFVMjHbfv+SJ5v0ubqQxa5YjLN4vc49z7SVju8s0X4gZ6AzZTn06jzWOgyPRV54Q4I0DCYadWW4Ze3e+BOtwgVU1Og3qHKn8vygoj40J6U85Z/PTJu3hN1m75Zr195ju7g9v4Hk=</Modulus><Exponent>AQAB</Exponent><P>/hf2dnK7rNfl3lbqghWcpFdu778hUpIEBixCDL5WiBtpkZdpSw90aERmHJYaW2RGvGRi6zSftLh00KHsPcNUMw==</P><Q>6Cn/jOLrPapDTEp1Fkq+uz++1Do0eeX7HYqi9rY29CqShzCeI7LEYOoSwYuAJ3xA/DuCdQENPSoJ9KFbO4Wsow==</Q><DP>ga1rHIJro8e/yhxjrKYo/nqc5ICQGhrpMNlPkD9n3CjZVPOISkWF7FzUHEzDANeJfkZhcZa21z24aG3rKo5Qnw==</DP><DQ>MNGsCB8rYlMsRZ2ek2pyQwO7h/sZT8y5ilO9wu08Dwnot/7UMiOEQfDWstY3w5XQQHnvC9WFyCfP4h4QBissyw==</DQ><InverseQ>EG02S7SADhH1EVT9DD0Z62Y0uY7gIYvxX/uq+IzKSCwB8M2G7Qv9xgZQaQlLpCaeKbux3Y59hHM+KpamGL19Kg==</InverseQ><D>vmaYHEbPAgOJvaEXQl+t8DQKFT1fudEysTy31LTyXjGu6XiltXXHUuZaa2IPyHgBz0Nd7znwsW/S44iql0Fen1kzKioEL3svANui63O3o5xdDeExVM6zOf1wUUh/oldovPweChyoAdMtUzgvCbJk1sYDJf++Nr0FeNW1RB1XG30=</D></RSAKeyValue>";
        //    RSACryptoServiceProvider rsa = new RSACryptoServiceProvider();
        //    byte[] cipherbytes;
        //    rsa.FromXmlString(privatekey);
        //    cipherbytes = rsa.Decrypt(Convert.FromBase64String(content), false);

        //    return Encoding.UTF8.GetString(cipherbytes);
        //}
        public void DownloadFile()
        {
            /// 下载服务器文件至客户端(不带进度条)  
            /// </summary>  

            /// <param name="strUrlFilePath">要下载的Web服务器上的文件地址(全路径 如:http://www.dzbsoft.com/test.rar</param>  

            /// <param name="Dir">下载到的目录(存放位置,机地机器文件夹)</param>  

            /// <returns>True/False是否上传成功</returns>  
            string strUrlFilePath = "http://ygb.nankai.edu.cn/liangshiyiyou/aaa.txt";
            string strLocalDirPath = "D:/";


            // 创建WebClient实例  
            WebClient client = new WebClient();

            //被下载的文件名  
            string fileName = strUrlFilePath.Substring(strUrlFilePath.LastIndexOf("/"));

            //另存为的绝对路径+文件名  
            string Path = strLocalDirPath + fileName;

            try
            {

                WebRequest myWebRequest = WebRequest.Create(strUrlFilePath);

            }

            catch (Exception exp)
            {

                MessageBox.Show("文件下载失败:" + exp.Message, "系统提示", MessageBoxButtons.OK, MessageBoxIcon.Information);

            }

            try
            {

                client.DownloadFile(strUrlFilePath, Path);

                MessageBox.Show("文件下载成功:", "系统提示", MessageBoxButtons.OK, MessageBoxIcon.Information);

            }

            catch (Exception exp)
            {

                MessageBox.Show("文件下载失败:" + exp.Message, "系统提示", MessageBoxButtons.OK, MessageBoxIcon.Information);


            }
        }
    }
}

 

 

posted on 2014-11-04 15:52  Jessica's  阅读(289)  评论(0编辑  收藏  举报

导航