c 程式中對密碼進行加密的方法

2022-03-11 17:36:52 字數 3224 閱讀 5943

在ado.net中,向資料庫新增資料時,怎樣對資料中的密碼進行加密?(也就是說在資料表中也看不到使用者的密

碼,只是一些經過編譯後的字串,以防止資料庫管理員利用使用者的密碼進行非法操作。)

首先,在c#winform程式中引入命名空間,"using system.web.security;",此命名空間是專門用來對程式進 

行安全設定的;

其次,定義乙個string型別的變數,用來接收用輸入的密碼;

string password = this.textbox1.text.trim();

取到密碼之後,接下來便是對密碼進行加密處理:

string pwd = formsauthentication.hashpasswordforstoringinconfigfile(pwd, "md5");

最後,將加密後的密碼pwd新增到資料庫中去。

insert into userinfo(uname,pwd) values('','');select @@identity", this.txtuid.text.trim   

(),passwrod);

示例**:

using system.web.security;

//取得文字框中的密碼

string pwd = this.txtpwd1.text.trim();

//對密碼加密

string passwrod = formsauthentication.hashpasswordforstoringinconfigfile(pwd, "md5");

//建立sql語句,將加密後的密碼儲存到資料庫中

string inscmd =

string.format("insert into userinfo(uname,pwd) values('','');select @@identity",  

this.txtuid.text.trim(),passwrod);

using (sqlcommand cmd = new sqlcommand(inscmd, form1.connection))

",uid);

messagebox.show(mess);

}else

}這樣加密之後保證了使用者密碼的安全,但是又出現了乙個問題,即使用者登入時怎樣對密碼進行驗證,該不會讓

使用者去記住加密後的那一長串字串吧? 答案當然是否定的,那怎樣解決呢?

應該這樣解決:

在使用者登入時,得到使用者輸入的密碼;

然後,將取到的密碼再次進行加密;

之後,根據使用者名稱取出該使用者在資料庫中的真實密碼;

最後,將剛剛進行加密的密碼與資料庫密碼進行比對,即可完成使用者登入操作。

示例**:

string pwd = this.txtpwd1.text.trim();

string pwd1 = formsauthentication.hashpasswordforstoringinconfigfile(pwd, "md5");

string uid = this.txtuid.text.trim();

string selcmd = string.format("select pwd from userinfo where uname=''", uid);

string password = "";

using (sqlcommand cmd = new sqlcommand(selcmd, form1.connection))

if (password == pwd1)

else

完整例項(複製即可用):

1.資料庫**:

use tempdb

goif exists (select * from sysobjects where name = 'userinfo')

drop table userinfo

gocreate table userinfo

(uid int identity(1,1) not null,

uname nvarchar(20) not null,

uage int not null,

password nvarchar(200) not null)go

alter table userinfo

add constraint pk_uid primary key (uid)

alter table userinfo

add constraint ck_uage check (uage between 0 and 100)

goselect * from userinfo

2.c#**

using system;

using system.collections.generic;

using system.componentmodel;

using system.data;

using system.data.sqlclient;

using system.drawing;

using system.text;

using system.windows.forms;

using system.web.security;  //安全加密

namespace 密碼加密示例

return form1.connection;  //返回乙個連線}}

public form1()

///

/// 檢查使用者輸入

///

///

private bool checkinput()

else

if (string.isnullorempty(this.txtage.text))

else

if (string.isnullorempty(this.txtpass.text))

else

return true;

}///

/// 新增資料

///

///

///

private void btnadd_click(object sender, eventargs e)

','','')",

this.txtname.text.trim(), this.txtage.text.trim(),pwd); 

using (sqlcommand cmd = new sqlcommand(inscmd,form1.connection))

else}}

}}}完!

對訂單進行加密解密的方法

加密演算法 privatestatic string algo aes 轉換模式 演算法 工作模式 填充模式 privatestatic string transformation aes cbc nopadding privatestatic string algo mode aes cbc no...

前端有對使用者密碼進行加密嗎

我的個人 www.ryzeyang.top 內容概覽 前端有對使用者密碼進行加密嗎?加密演算法又是哪類呢?一直以來都有個疑問,就是在那些平台上建立使用者時,前端有沒有對密碼進行加密,再進行傳輸?今晚決定一 竟 博主也不知道之前咋那麼忙,都沒時間給自己解答這個疑問?明明沒有在峽谷裡奔跑的 畢竟昨天讓二...

對txt檔案進行加密的小程式

include include void main printf c c1 while c1 n 在wo.txt這個檔案裡,輸完要加密的內容記得在最後加上乙個回車,表示程式完成。system pause 但是如果我們想要進一步完善程式,使程式自動開啟乙個記事本,然後讓你在記事本輸入要加密的內容,然後...