C# 连接数据库(MD5加密存储)演示注册与登录
来源:互联网 发布:淘宝宠物药店 编辑:程序博客网 时间:2024/06/01 23:40
工具:Visual Studio 2008
SQL Server 2008
Windows XP
Form1:
using System;using System.Collections.Generic;using System.ComponentModel;using System.Data;using System.Drawing;using System.Linq;using System.Text;using System.Windows.Forms;using System.Data.SqlClient;using System.Security.Cryptography;namespace loginVs{ public partial class Form1 : Form { public bool b1 = false; public Form1() { InitializeComponent(); } private string MD5Create(string STR) //STR为待加密的string { string pwd = ""; //pwd为加密结果 MD5 md5 = MD5.Create(); byte[] s = md5.ComputeHash(Encoding.UTF8.GetBytes(STR)); //这里的UTF8是编码方式,你可以采用你喜欢的方式进行,比如UNcode等等 for (int i = 0; i < s.Length; i++) { pwd = pwd + s[i].ToString(); } return pwd; } private void Form1_Load(object sender, EventArgs e) { } private void button2_Click(object sender, EventArgs e) { if (names.Text.Trim() == "") { MessageBox.Show("用户名不能为空!", "提示信息"); names.Focus(); return; } if (pwd.Text.Trim() == "") { MessageBox.Show("密码不能为空!", "提示信息"); pwd.Focus(); return; } int i = 0; string connextionString = "server=localhost;database=TestSQL;uid=sa;pwd=huxiao1225"; SqlConnection mySqlConnection = new SqlConnection(connextionString); using (mySqlConnection) { mySqlConnection.Open(); string username; string pwdx; username = MD5Create(names.Text.Trim()); pwdx= MD5Create(pwd.Text.Trim()); //用户名与密码经过MD5加密后于数据库表进行比对 string sql = "select count(*) from tb where names='" + username + "' and pwd='" + pwdx + "'"; using (SqlCommand cmd = mySqlConnection.CreateCommand()) { cmd.CommandText = sql; i = (int)cmd.ExecuteScalar(); } } if (i > 0) { b1 = true; MessageBox.Show("登陆成功", "提示信息"); //MDIParent1.TxtUser = names.Text.Trim(); this.Close(); } else { MessageBox.Show("用户名或密码错误!请重新登陆!", "提示信息"); } } private void button1_Click(object sender, EventArgs e) { LoginForm LoginForm = new LoginForm(); LoginForm.ShowDialog(); } }}
Form2:
using System;using System.Collections.Generic;using System.ComponentModel;using System.Data;using System.Drawing;using System.Linq;using System.Text;using System.Windows.Forms;using System.Data.SqlClient;using System.Security.Cryptography;namespace loginVs{ public partial class LoginForm : Form { public LoginForm() { InitializeComponent(); label4.Visible = false; label5.Visible = false; label6.Visible = false; label7.Visible = false; label8.Visible = false; } private string MD5Create(string STR) //STR为待加密的string { string pwd = ""; //pwd为加密结果 MD5 md5 = MD5.Create(); byte[] s = md5.ComputeHash(Encoding.UTF8.GetBytes(STR)); //这里的UTF8是编码方式,你可以采用你喜欢的方式进行,比如UNcode等等 for (int i = 0; i < s.Length; i++) { pwd = pwd + s[i].ToString(); } return pwd; } private void btnLogin_Click(object sender, EventArgs e) { if (tbxUsername.Text==string.Empty) { label4.Visible = true; tbxUsername.Focus(); return; } if (tbxPwd.Text==string.Empty) { label5.Visible = true; tbxPwd.Focus(); return; } if (tbxPwdSure.Text==string.Empty) { label6.Visible = true; tbxPwdSure.Focus(); return; } if (tbxPwd.Text.Trim()!=tbxPwdSure.Text.Trim()) { label7.Visible = true; tbxPwdSure.Focus(); tbxPwdSure.Text = string.Empty; return; } SqlConnection mySqlConnection = new SqlConnection("server=localhost;database=TestSQL;uid=sa;pwd=huxiao1225"); using(mySqlConnection) { if (mySqlConnection.State==ConnectionState.Closed) { mySqlConnection.Open(); } SqlCommand cmd = new SqlCommand(); cmd.Connection = mySqlConnection; string username; string pwd; username = MD5Create(tbxUsername.Text.Trim()); pwd = MD5Create(tbxPwd.Text.Trim()); string sql1 = "INSERT INTO tb(names,pwd) VALUES('" + username+ "','" + pwd + "')";//加密后添加到数据库中 cmd.CommandText = sql1; if (cmd.ExecuteNonQuery() != -1) { label8.Visible = true; timer1.Enabled = true; } else if (cmd.ExecuteNonQuery() == -1) { MessageBox.Show("数据输入失败"); } } } private void timer1_Tick(object sender, EventArgs e) { timer1.Enabled = false; this.Close(); } }}
- C# 连接数据库(MD5加密存储)演示注册与登录
- 前后端演示SHA1,MD5加密登录(带数据库)
- shiro 用户权限管理(2)-----注册md5加密,登录验证
- c# MD5 加密登录密码
- JAVA简单实现MD5注册登录加密
- Android----- MD5加密(登录注册得到与IOS相同的加密值,并且解决两个平台汉字加密不相同问题)
- Nodo登陆注册表单,数据库md5加密
- MD5加密(C#与Delphi)
- C# MD5加密与解密
- C#编写注册界面将密码通过MD5加密后存入数据库
- 登录功能(MD5加密)
- struts2连接数据库实现用户注册与登录
- C#中MD5加密与SHA1加密
- python3.x-hashlib-md5加密用户登录注册
- JAVA用户注册登录使用MD5非对称加密解密
- 32位 MD5加密(C# 与Java版)
- c# 注册窗体 连接数据库
- MySQL数据库与登录注册
- 汇编语言基础之 移位指令
- linux 查找文件
- 详解Java解析XML的四种方法
- 技术角度分析:中国谁能玩转大数据
- poj 1222 解题报告
- C# 连接数据库(MD5加密存储)演示注册与登录
- repo 切分支
- 1D.1 大师之言——译自《编译器设计之路》
- Oracle Alerts 与 Metrics(警告与度量)说明
- 汽车驾驶初学要领
- Oracle synonyms总结
- 嵌入式GUI设计——Directfb
- 取得两个 FILETIME 的时间差
- sp、cp与移动运营商的三方博弈