如何在sql數據庫中存儲密碼,是否有任何需要轉換用戶輸入密碼?我試圖將密碼從文本框保存爲數據庫中存儲爲空/空的表單中的密碼模式。當我禁用密碼模式文本工作正常。如何在sql server中存儲和檢索密碼?
2
A
回答
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
一般沒有,它沒有需要將其轉換(至少根據我的經驗),但更多的安全原因,某些用戶使用加密/解密方法或將其轉換爲的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);
}
相關問題
- 1. 在SQL Server中存儲,檢索和驗證密碼(SecureString)
- 2. 密碼存儲和檢索
- 3. 在SQL Server中存儲密碼
- 4. 如何在SQL Server 2005中存儲和檢索html文件?
- 5. 檢索存儲在db中的密碼
- 6. 如何在SQL Server中檢索非sa密碼?
- 7. 如何在SQL Server 2008 R2中存儲散列密碼
- 8. 在SQL Server中存儲和檢索Active Directory objectGUID
- 9. 在SQL SERVER 2005中存儲和檢索.ZIP文件
- 10. 如何檢索SQL Server中存儲過程的名稱?
- 11. 如何使用C#在SQL Server中存儲和檢索DICOM文件
- 12. 如何通過VB.NET在sql server數據庫中存儲和檢索圖像
- 13. 如何檢索存儲在Webkit瀏覽器中的用戶名和密碼?
- 14. 將密碼存儲爲散列值時檢索密碼
- 15. 存儲加密密碼和salt或僅存儲加密密碼?
- 16. 如何在SQL Server 2008中解密存儲過程
- 17. 適當的密碼存儲和服務帳戶檢索?
- 18. 如何存儲和檢索neo4j索引
- 19. 在SQL Server 2008中存儲其他語言並檢索返回
- 20. 使用vb.net檢索存儲在sql server中的excel文件
- 21. 密碼加密和密碼存儲 - Perl
- 22. nvarchar(n)如何存儲在SQL Server中?
- 23. 在SQL Server中密碼salting
- 24. 在SQL Server 2008中加密和存儲敏感數據
- 25. 使用加密算法來存儲和檢索用戶名和用戶密碼
- 26. 在SQL Server和經典ASP中處理以varbinary存儲的哈希密碼
- 27. 如何在存儲過程中創建過濾索引(SQL Server)
- 28. SQL Server密碼
- 29. 在sql server中存儲和檢索評論回覆的最佳方法
- 30. 如何從SQL Server中的存儲過程中檢索參數列表
沒有足夠的信息提供有意義的答案。你能發表一些代碼,SQL,表格定義等嗎? – RickNZ 2011-12-25 06:53:45
如果您將來因某種原因需要密碼,請使用其他一些2路算法進行加密。例如,如果它不是密碼,但是需要顯示一個祕密字符串。 – tgkprog 2014-04-14 13:51:57
@tgkprog謝謝! – Rembo 2014-04-14 14:04:02