我正在开发一个在多台服务器上运行的应用程序。其中一台服务器处理输入,其他服务器执行输入。根据公司政策,一切都必须通过数据库,并且不允许实例之间的套接字。
现在这对所有事情都适用,但让我担心的一件事是密码。我目前正在实现一些需要用户密码才能以纯文本形式到达这些应用程序之一的东西。现在我知道这永远不会 100% 安全,但我该如何将风险降到最低?目前我计划将 pw 放入数据库并在它到达后从数据库中删除它。日志每天删除一次。我怎样才能进一步提高这种安全性?
最佳答案
您可以使用此方法来加密密码:
public string Encrypt(string stringToEncrypt, string SEncryptionKey)
{
key = System.Text.Encoding.UTF8.GetBytes(SEncryptionKey);
DESCryptoServiceProvider des = new DESCryptoServiceProvider();
byte[] inputByteArray = Encoding.UTF8.GetBytes(stringToEncrypt);
MemoryStream ms = new MemoryStream();
CryptoStream cs = new CryptoStream(ms, des.CreateEncryptor(key, IV), CryptoStreamMode.Write);
cs.Write(inputByteArray, 0, inputByteArray.Length);
cs.FlushFinalBlock();
return Convert.ToBase64String(ms.ToArray());
}
通过使用:
private byte[] key = { };
private byte[] IV = { 0x12, 0x34, 0x56, 0x78, 0x90, 0xab, 0xcd, 0xef };
然后就可以插入数据库了
password = ...
password = Encrypt(password.Trim(), "r0b1nr0y");
// Insert in db
对于解密,你可以使用这个方法:
public string Decrypt(string stringToDecrypt, string sEncryptionKey)
{
byte[] inputByteArray = new byte[stringToDecrypt.Length + 1];
key = System.Text.Encoding.UTF8.GetBytes(sEncryptionKey);
DESCryptoServiceProvider des = new DESCryptoServiceProvider();
inputByteArray = Convert.FromBase64String(stringToDecrypt);
MemoryStream ms = new MemoryStream();
CryptoStream cs = new CryptoStream(ms, des.CreateDecryptor(key, IV), CryptoStreamMode.Write);
cs.Write(inputByteArray, 0, inputByteArray.Length);
cs.FlushFinalBlock();
System.Text.Encoding encoding = System.Text.Encoding.UTF8;
return encoding.GetString(ms.ToArray());
}
关于c# - 通过数据库传递密码如何使其安全,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31915043/