[英]Decrypt the encrypted value from database
我想在數據庫中保存一個加密的值,並且在從數據庫中獲取值時,我想解密並在UI中顯示該值。 這是我使用的代碼
private string Decryptdata(string encryptpwd)
{
string decryptpwd = string.Empty;
UTF8Encoding encodepwd = new UTF8Encoding();
Decoder Decode = encodepwd.GetDecoder();
encryptpwd = encryptpwd.Replace(" ", "+");
byte[] todecode_byte = Convert.FromBase64String(encryptpwd);
int charCount = Decode.GetCharCount(todecode_byte, 0, todecode_byte.Length);
char[] decoded_char = new char[charCount];
Decode.GetChars(todecode_byte, 0, todecode_byte.Length, decoded_char, 0);
decryptpwd = new String(decoded_char);
return decryptpwd;
}
但是我收到錯誤,因為Base-64 char數組的長度無效。 我正在使用c#.net
加密功能:
Encryptdata(string password) {
string strmsg = string.Empty;
byte[] encode = new byte[password.Length];
encode = Encoding.UTF8.GetBytes(password);
strmsg = Convert.ToBase64String(encode);
return strmsg;
}
如果您將密碼存儲在數據庫中,除非確實有充分的理由需要獲取純文本,否則您應該對密碼進行哈希處理,而不是將其加密或以純文本格式存儲。
加密和散列之間的區別在於,加密可以從您無法獲取的地方檢索純文本,而散列則無法。 當您存儲密碼時,您應該使用用戶提供的密碼並對其進行哈希處理(最好是加鹽),然后當用戶下次嘗試使用該密碼登錄時,您再次對其進行哈希處理,然后將存儲的哈希與您剛剛生成的一個,如果它們匹配,那么它們是相同的。
我已經在這里寫了有關此內容( 密碼存儲,如何正確執行 )的詳細說明。
我的網站上有一些帶有代碼的哈希函數(在VB.NET中,但是它們很容易移至C#),可能最好的方法是使用SHA512( 計算string或file的SHA512哈希 )。
如果您仍然不確定哈希等,請隨時說您不了解的內容,我會盡力提供幫助的:)
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.