XML口令檔案描述 (轉)

worldblog發表於2007-12-04
XML口令檔案描述 (轉)[@more@]

 

口令描述

  adduser.檔案用來向XML檔案中增加新的元素。作為一個羽翼豐滿的應用,還需要建立編輯和刪除功能,但是如果需要的話,這些能夠手工完成,而增加新則只能用這個頁面來完成。這是因為口令和salt都是在使用者提供的口令的基礎上用AspEncrypt生成的。adduser.asp中的大部分程式碼用來 操作XML檔案並建立一個新元素,但是因為有其它文章專門解釋如何用ASP處理XML(見 XML in ASP),所以我在這裡只涉及AspEncrypt程式碼:

'Create Hash of pass chosen Ranize Salt = "" For i = 1 to 10 Salt = Salt & chr(int(Rnd * 26) + 65) '65 is ASCII for "A" Next ' Calculate Hash of Password + Salt Set CM = Server.Create("Persits.CryptoManager") Set Context = CM.OpenContext("mycontainer", True) Set Hash = Context.CreateHash Hash.AddText Request("Password") & Salt HashValue = Hash.Value.Hex Set Hash = Nothing Set CM = Nothing


   首先,我們用Randomize生成一個任意salt ,用 Rnd建立一個任意的10字元字串。 然後,salt 把增加到提交給頁面的口令中,併傳送給CryptoHash 。然後,單程隨機值被提取 為HashValue,然後將其在XML檔案中。

   validateuser.asp 文件具有同樣的功能。為了確定使用者名稱/口令組合是否有效,頁面首先要看在XML檔案中是否存在使用者名稱。如果沒有,它就返回使用者沒有找到。否則,它就要將提交口令加提交使用者salt 的隨機值與為使用者儲存的口令相比較。以下程式碼這個測試:

Found = False I = 0 While I < objectElement.childNodes.Length And Not Found If objectRootElement.childNodes(I).attributes _ .getNamedItem("name").nodeValue = username Then Found = True Else I = I + 1 End If Wend If Found Then Response.Write "User " & username & " Found" ' Calculate Hash of specified password + Salt from HashValue = objRootElement.childNodes(i).attributes _ .getNamedItem("password").nodeValue Salt = objectRootElement.childNodes(i).attributes _ .getNamedItem("salt").nodeValue Set CM = Server.CreateObject("Persits.CryptoManager") Set Context = CM.OpenContext("mycontainer", True) Set Hash = Context.CreateHash Hash.AddText password & Salt HashValue2 = Hash.Value.Hex Set Hash = Nothing Set CM = Nothing If HashValue = HashValue2 Then Response.Write "User " & username & " Validated" Else Response.Write "User Password Invalid." End If Else Response.Write "User " & username & " Not Found" End If


   Hashvalue用來在XML檔案中儲存經過加密的口令的值(正確的經過加密的口令)。Hashvalue2 是用提交的口令和中的salt 進行計算的。如果它們匹配,使用者就被確認。否則就提交一個無效口令。


來自 “ ITPUB部落格 ” ,連結:http://blog.itpub.net/10752043/viewspace-988492/,如需轉載,請註明出處,否則將追究法律責任。

相關文章