2011-12-25 56 views
2

如何在sql數據庫中存儲密碼,是否有任何需要轉換用戶輸入密碼?我試圖將密碼從文本框保存爲數據庫中存儲爲空/空的表單中的密碼模式。當我禁用密碼模式文本工作正常。如何在sql server中存儲和檢索密碼?

+0

沒有足夠的信息提供有意義的答案。你能發表一些代碼,SQL,表格定義等嗎? – RickNZ 2011-12-25 06:53:45

+0

如果您將來因某種原因需要密碼,請使用其他一些2路算法進行加密。例如,如果它不是密碼,但是需要顯示一個祕密字符串。 – tgkprog 2014-04-14 13:51:57

+0

@tgkprog謝謝! – Rembo 2014-04-14 14:04:02

回答

2

以加密格式存儲密碼將是一個好習慣。你可以使用md5哈希算法來加密它。下面是散列串

using System; 
using System.Text; 
using System.Security.Cryptography; 

// Create an md5 sum string of this string 
static public string GetMd5Sum(string str) 
{ 
// First we need to convert the string into bytes, which 
// means using a text encoder. 
Encoder enc = System.Text.Encoding.Unicode.GetEncoder(); 

// Create a buffer large enough to hold the string 
byte[] unicodeText = new byte[str.Length * 2]; 
enc.GetBytes(str.ToCharArray(), 0, str.Length, unicodeText, 0, true); 

// Now that we have a byte array we can ask the CSP to hash it 
MD5 md5 = new MD5CryptoServiceProvider(); 
byte[] result = md5.ComputeHash(unicodeText); 

// Build the final string by converting each byte 
// into hex and appending it to a StringBuilder 
StringBuilder sb = new StringBuilder(); 
for (int i=0;i<result.Length;i++) 
{ 
    sb.Append(result[i].ToString("X2")); 
} 

// And return it 
return sb.ToString(); 
} 

不能轉回加密後的文本,以正常的字符串..

聯繫,如果有任何疑問,示例代碼。

+0

感謝您的回覆Meherzad,sb.Append中的「x2」的工作是什麼(result [i] .ToString ( 「X2」)); ? – Rembo 2011-12-25 09:08:50

+0

它用於將每個字節轉換爲十六進制。 – Zlatan 2011-12-25 09:32:03

0

一般沒有,它沒有需要將其轉換(至少根據我的經驗),但更多的安全原因,某些用戶使用加密/解密方法或將其轉換爲的base64而最好的方法是單向算法類似哈希

阿里

0

我假設你正在使用某種類型的數據綁定控件。

我會創建一個onInserting事件並將tbPassword.text值分配給某個變量,然後調試以查看密碼是否首先分配給該變量。這也可能是您對密碼進行哈希和加密以防止某人直接盜用您的數據庫而無法模擬您的任何用戶的地方。

如果您要將密碼值保存在後面的代碼中,那麼您可以跳過從上面添加onInserting步驟並僅檢查那裏的值。

如果您還使用存儲過程進行保存,則可能需要打印出參數並使用從網站傳遞的值運行該參數,然後查看顯示的內容。

0

我認爲你應該隱蔽密碼,並將其保存在數據庫中,通常,我們不保存在數據庫

純文本,我們應該加密密碼。

對於數據庫設計,

我們應該添加兩個字段密文和DataTable中關鍵,當用戶inputed密碼

程序應該加密密碼,然後保存將密文轉換爲數據表。

因此,用戶再次登錄,只是比較密文等於或不會。

您可以參考下面的代碼。

/// <summary> 
    /// Encrypts the specified hash algorithm. 
    /// 1. Generates a cryptographic Hash Key for the provided text data. 
    /// </summary> 
    /// <param name="hashAlgorithm">The hash algorithm.</param> 
    /// <param name="dataToHash">The data to hash.</param> 
    /// <returns></returns> 
    public static string Encrypt(HashAlgorithm hashAlgorithm, string dataToHash) 
    { 

     var tabStringHex = new string[16]; 
     var UTF8 = new System.Text.UTF8Encoding(); 
     byte[] data = UTF8.GetBytes(dataToHash); 
     byte[] result = hashAlgorithm.ComputeHash(data); 
     var hexResult = new StringBuilder(result.Length); 

     for (int i = 0; i < result.Length; i++) 
     { 
      //// Convert to hexadecimal 
      hexResult.Append(result[i].ToString("X2")); 
     } 


return hexResult.ToString(); 
} 


/// <summary> 
/// Determines whether [is hash match] [the specified hash algorithm]. 
/// </summary> 
/// <param name="hashAlgorithm">The hash algorithm.</param> 
/// <param name="hashedText">The hashed text.</param> 
/// <param name="unhashedText">The unhashed text.</param> 
/// <returns> 
/// <c>true</c> if [is hash match] [the specified hash algorithm]; 
/// otherwise, <c>false</c>. 
/// </returns> 
public static bool IsHashMatch(HashAlgorithm hashAlgorithm, 
    string hashedText, string unhashedText) 
{ 
    string hashedTextToCompare = Encrypt(
     hashAlgorithm, unhashedText); 
    return (String.Compare(hashedText, 
     hashedTextToCompare, false) == 0); 
} 
相關問題