MD5加密-用做生成数据库访问密码

来源:互联网 发布:中国石油大学北京网络 编辑:程序博客网 时间:2024/05/22 06:30
using System;
using System.Drawing;
using System.Collections;
using System.ComponentModel;
using System.Windows.Forms;
using System.Data;
using System.Security.Cryptography;
using System.IO;
using System.Text; 
namespace WindowsApplication1
{
 
/// <summary>
 
/// Form1 的摘要说明。
 
/// </summary>

 public class Form1 : System.Windows.Forms.Form
 
{
  
private System.Windows.Forms.Button button1;
  
private System.Windows.Forms.TextBox textBox1;
  
private System.Windows.Forms.TextBox textBox2;
  
private System.Windows.Forms.Button button2;
  
private System.Windows.Forms.Button button3;
  
/// <summary>
  
/// 必需的设计器变量。
  
/// </summary>

  private System.ComponentModel.Container components = null
  
public Form1()
  
{
   
//
   
// Windows 窗体设计器支持所必需的
   
//
   InitializeComponent(); 
   
//
   
// TODO: 在 InitializeComponent 调用后添加任何构造函数代码
   
//
  }
 
  
/// <summary>
  
/// 清理所有正在使用的资源。
  
/// </summary>

  protected override void Dispose( bool disposing )
  
{
   
if( disposing )
   
{
    
if (components != null)
    
{
     components.Dispose();
    }

   }

   
base.Dispose( disposing );
  }
 
  
Windows 窗体设计器生成的代码 
  
/// <summary>
  
/// 应用程序的主入口点。
  
/// </summary>

  [STAThread]
  
static void Main()
  
{
   Application.Run(
new Form1());
  }
 
  
private void Form1_Load(object sender, System.EventArgs e)
  
{
    
  }
 
  
private void button1_Click(object sender, System.EventArgs e)
  
{
   
string dsn="Provider=Microsoft.Jet.OLEDB.4.0;Data source={0};";
   
string dsn2="~/news.DanielCrown817"
   StreamReader sr 
= new StreamReader(@"d:a.txt",UTF8Encoding.UTF8);
   
string readpublickey = sr.ReadToEnd();
   sr.Close();
    
   RSACryptoServiceProvider crypt
=new RSACryptoServiceProvider();
   UTF8Encoding enc
=new UTF8Encoding();
   
byte[] bytes=enc.GetBytes(dsn);
   
byte[] bytes2=enc.GetBytes(dsn2);
   crypt.FromXmlString( readpublickey );
   bytes 
= crypt.Encrypt( bytes,false );
   bytes2
=crypt.Encrypt(bytes2,false);
   
string encryttext=Convert.ToBase64String(bytes);
   
string encryttext2=Convert.ToBase64String(bytes2); 
   
this.textBox1.Text= encryttext;
   
this.textBox2.Text=encryttext2; 

    
  }
 
  
private void button2_Click(object sender, System.EventArgs e)
  
{
   RSACryptoServiceProvider crypt
=new RSACryptoServiceProvider();
   
string publickey=crypt.ToXmlString(false);//公匙
   string privatekey=crypt.ToXmlString(true);//私匙
   crypt.Clear();
    
   
//写入文本文件中
   StreamWriter one=new StreamWriter(@"d:a.txt",true,UTF8Encoding.UTF8);
   one.Write(publickey);
   StreamWriter two
=new StreamWriter(@"d:.txt",true,UTF8Encoding.UTF8);
   two.Write(privatekey);
   one.Flush();
   two.Flush();
   one.Close();
   two.Close();
   MessageBox.Show(
"成功保存公匙和密匙!");
  }
 
  
private void button3_Click(object sender, System.EventArgs e)
  
{
   StreamReader sr 
= new StreamReader(@"d:.txt",UTF8Encoding.UTF8);
   
string readprivatekey = sr.ReadToEnd();
   sr.Close();
    
    
   RSACryptoServiceProvider crypt
=new RSACryptoServiceProvider();
   UTF8Encoding enc
=new UTF8Encoding();
   
byte[] bytes = Convert.FromBase64String(this.textBox1.Text);
   
byte[] bytes2=Convert.FromBase64String(this.textBox2.Text);
   crypt.FromXmlString ( readprivatekey ) ;
   
byte[] decryptbyte = crypt.Decrypt( bytes,false );
   
byte[] decryptbyte2=crypt.Decrypt(bytes2,false); 
   
string d=enc.GetString(decryptbyte);
   
string d2=enc.GetString(decryptbyte2); 
   
this.textBox1.Text=d;
   
this.textBox2.Text=d2;
  }

 }

}

原创粉丝点击