我的對象從數據庫中出來後需要解密一個密碼,我該如何實現?這是我的encrpytion解密代碼:在構造器的實體框架中編輯屬性
public class EncryptionHelper {
public static string Encrypt(string strToEncrypt) {
try {
TripleDESCryptoServiceProvider objDESCrypto = new TripleDESCryptoServiceProvider();
MD5CryptoServiceProvider objHashMD5 = new MD5CryptoServiceProvider();
byte[] byteHash, byteBuff;
string strTempKey = Settings.Default.Keyword;
byteHash = objHashMD5.ComputeHash(ASCIIEncoding.ASCII.GetBytes(strTempKey));
objHashMD5 = null;
objDESCrypto.Key = byteHash;
objDESCrypto.Mode = CipherMode.ECB; //CBC, CFB
byteBuff = ASCIIEncoding.ASCII.GetBytes(strToEncrypt);
return Convert.ToBase64String(objDESCrypto.CreateEncryptor().
TransformFinalBlock(byteBuff, 0, byteBuff.Length));
} catch (Exception ex) {
return strToEncrypt;
}
}
public static string Decrypt(string strEncrypted) {
try {
TripleDESCryptoServiceProvider objDESCrypto =
new TripleDESCryptoServiceProvider();
MD5CryptoServiceProvider objHashMD5 = new MD5CryptoServiceProvider();
byte[] byteHash, byteBuff;
string strTempKey = Settings.Default.Keyword;
byteHash = objHashMD5.ComputeHash(ASCIIEncoding.ASCII.GetBytes(strTempKey));
objHashMD5 = null;
objDESCrypto.Key = byteHash;
objDESCrypto.Mode = CipherMode.ECB; //CBC, CFB
byteBuff = Convert.FromBase64String(strEncrypted);
string strDecrypted = ASCIIEncoding.ASCII.GetString
(objDESCrypto.CreateDecryptor().TransformFinalBlock
(byteBuff, 0, byteBuff.Length));
objDESCrypto = null;
return strDecrypted;
} catch (Exception ex) {
return "Wrong Input. " + ex.Message;
}
}
}
這是我的實體:
public partial class S0Pin
{
public int ID { get; set; }
public string Serialnumber { get; set; }
public string Pin { get; set; }
public virtual User Creator { get; set; }
}
這是插入對象
public bool AddS0Pin(S0Pin pin) {
log.Debug("add s0pin with serialnumber " + pin.Serialnumber);
var s0pin = S0Pin.SingleOrDefault(pn => pn.Serialnumber == pin.Serialnumber);
if (s0pin != null) {
s0pin.Pin = EncryptionHelper.Encrypt(pin.Pin);
} else {
this.S0Pin.Add(pin);
}
return this.SaveChanges() > 0;
}
的方法我怎樣才能解密數據後我得到了數據庫?最好的方法是在實體中。
出於好奇,你爲什麼要解密密碼?使查看密碼成爲可能是一個巨大的安全缺陷,最好的方法是使用MD5 Hash,這對許多輸入來說是1散列,並且不能(確定地)顛倒過來。它的方式應該是再次加密密碼以便進行比較 對不起,我不能給你一個答案,只是觀察 –
我不能使用解密的密碼,因爲我需要存儲安全令牌的密碼和證書;) – Knerd