2010-06-06 57 views
-1

我有我的數據庫中的表有密碼字段等數據,我想在將數據插入表之前加密數據,然後我不想解密數據,但我只想比較加密的密碼和輸入的密碼沒有解密C#加密函數

+0

不清楚問題是...? – 2010-06-06 10:19:14

回答

0

如果您不需要解密實際需要散列的數據,例如SHA1或由System.Security.Cryptography命名空間中的.NET提供的類似算法。

1

我會建議你hashing the passwords with salt並存儲哈希密碼和salt到數據庫中。這個話題還有另一個article

+0

一些附加信息http://stackoverflow.com/questions/1300890/md5-hash-with-salt-for-keeping-password-in-db-in-c – abatishchev 2010-06-06 09:47:48

0

我有2個函數來加密和解密數據:

第一個功能被用於解密數據和用來加密數據的第二個。

using System.Security.Cryptography; 
using System.Collections.Generic; 
using System.ComponentModel; 

private static readonly byte[] _key = { 0xA1, 0xF1, 0xA6, 0xBB, 0xA2, 0x5A, 0x37, 0x6F, 0x81, 0x2E, 0x17, 0x41, 0x72, 0x2C, 0x43, 0x27 }; 
private static readonly byte[] _initVector = { 0xE1, 0xF1, 0xA6, 0xBB, 0xA9, 0x5B, 0x31, 0x2F, 0x81, 0x2E, 0x17, 0x4C, 0xA2, 0x81, 0x53, 0x61 }; 


private static string Decrypt(string Value) 
    { 
     SymmetricAlgorithm mCSP; 
     ICryptoTransform ct = null; 
     MemoryStream ms = null; 
     CryptoStream cs = null; 
     byte[] byt; 
     byte[] _result; 

     mCSP = new RijndaelManaged(); 

     try 
     { 
      mCSP.Key = _key; 
      mCSP.IV = _initVector; 
      ct = mCSP.CreateDecryptor(mCSP.Key, mCSP.IV); 


      byt = Convert.FromBase64String(Value); 

      ms = new MemoryStream(); 
      cs = new CryptoStream(ms, ct, CryptoStreamMode.Write); 
      cs.Write(byt, 0, byt.Length); 
      cs.FlushFinalBlock(); 

      cs.Close(); 
      _result = ms.ToArray(); 
     } 
     catch 
     { 
      _result = null; 
     } 
     finally 
     { 
      if (ct != null) 
       ct.Dispose(); 
      if (ms != null) 
       ms.Dispose(); 
      if (cs != null) 
       cs.Dispose(); 
     } 

     return ASCIIEncoding.UTF8.GetString(_result); 
    } 



private static string Encrypt(string Password) 
    { 
     if (string.IsNullOrEmpty(Password)) 
      return string.Empty; 

     byte[] Value = Encoding.UTF8.GetBytes(Password); 
     SymmetricAlgorithm mCSP = new RijndaelManaged(); 
     mCSP.Key = _key; 
     mCSP.IV = _initVector; 
     using (ICryptoTransform ct = mCSP.CreateEncryptor(mCSP.Key, mCSP.IV)) 
     { 
      using (MemoryStream ms = new MemoryStream()) 
      { 
       using (CryptoStream cs = new CryptoStream(ms, ct, CryptoStreamMode.Write)) 
       { 
        cs.Write(Value, 0, Value.Length); 
        cs.FlushFinalBlock(); 
        cs.Close(); 
        return Convert.ToBase64String(ms.ToArray()); 
       } 
      } 
     } 
    } 

我希望這兩個功能可以幫助你。

0

使用散列函數像SHA256:

using System.Security.Cryptography; 
/// <summary> 
/// Hash the given string with sha256 
/// </summary> 
/// <param name="password">the string to hash</param> 
/// <returns>The hex representation of the hash</returns> 
static string sha256(string password) 
{ 
    SHA256Managed crypt = new SHA256Managed(); 
    string hash = String.Empty; 
    byte[] crypto = crypt.ComputeHash(Encoding.ASCII.GetBytes(password), 0, Encoding.ASCII.GetByteCount(password)); 
    foreach (byte bit in crypto) 
    { 
     hash += bit.ToString("x2"); 
    } 
    return hash; 
} 

它總是會給出相同的輸入相同的輸出,這樣你可以比較的哈希值。您還應該考慮對輸入進行醃製(預先添加或附加一些特定於您的應用/程序的值,以期使彩虹桌不再有用)