PDA

View Full Version : ASP.NET: ถามเรื่องการเข้ารหัสข้อมูลเพื่อเก็บใน SQL Server ครับ



tonynuc
29-08-2007, 09:38 PM
ไม่ทราบว่า asp.net มี built-in function ที่ใช้ในการเข้ารหัสข้อมูล (เช่น password, account number) ก่อนเก็บไว้ใน sql database ไหมครับ แนะนำด้วยครับ

hina_lovex
30-08-2007, 09:14 AM
มีครับ ปกติเห็นใช้ MD5 ในการเข้ารหัสและ ถอดรหัส ลองหา code ใน Internet โดย search คำว่า MD5 +ASP.Net เห็นมีตัวอย่างอยู่เต็มเลยครับ
<%@ Import Namespace="System.Security.Cryptography" %>
<%@ Import Namespace="System.Text" %>
<script language="VB" runat="server">
Sub DisplayEncryptedText(sender as Object, e as EventArgs)
If Page.IsValid then
Dim md5Hasher as New MD5CryptoServiceProvider()

Dim hashedDataBytes as Byte()
Dim encoder as New UTF8Encoding()

hashedDataBytes = md5Hasher.ComputeHash(encoder.GetBytes(txtPassword.Text))

ltlResults.Text = "Encrypted Results
The results are encrypted into " & _
"an array of 16 bytes. These 16 bytes contain the values:

"

Dim b as Byte
For Each b in hashedDataBytes
ltlResults.Text &= " " & b & ""
Next b

ltlResults.Text &= ""
End If
End Sub
</script>

<form runat="server">
Enter a string:
<asp:TextBox id="txtPassword" runat="server" />
<asp:RequiredFieldValidator runat="server" ControlToValidate="txtPassword"
Display="Dynamic" ErrorMessage="You must provide a value here..." />
<asp:RegularExpressionValidator runat="server" ControlToValidate="txtPassword"
Display="Dynamic" ErrorMessage="The string must be 20 characters or less..."
ValidationExpression="^.{1,20}$" />


<asp:Button runat="server" Text="View the String as Encrypted Text"
OnClick="DisplayEncryptedText" />



<asp:Literal runat="server" id="ltlResults" />
</form>