问:
我想问是否有一个用于加密和解密字符串的类,我在网上搜索,我找到了一些但它们不太安全或在完成过程中有一些问题:
我之前发现的一类是:
public static string Encrypt(string text)
{
try
{
key = Encoding.UTF8.GetBytes(stringKey.Substring(0, 8));
DESCryptoServiceProvider des = new DESCryptoServiceProvider();
Byte[] byteArray = Encoding.UTF8.GetBytes(text);
MemoryStream memoryStream = new MemoryStream();
CryptoStream cryptoStream = new CryptoStream(memoryStream,des.CreateEncryptor(key, IV), CryptoStreamMode.Write);
cryptoStream.Write(byteArray, 0, byteArray.Length);
cryptoStream.FlushFinalBlock();
return Convert.ToBase64String(memoryStream.ToArray());
}
catch (Exception ex)
{
string message = ex.Message;
}
return string.Empty;
}
public static string Decrypt(string text)
{
try
{
key = Encoding.UTF8.GetBytes(stringKey.Substring(0, 8));
DESCryptoServiceProvider des = new DESCryptoServiceProvider();
Byte[] byteArray = Convert.FromBase64String(text);
MemoryStream memoryStream = new MemoryStream();
CryptoStream cryptoStream = new CryptoStream(memoryStream,
des.CreateDecryptor(key, IV), CryptoStreamMode.Write);
cryptoStream.Write(byteArray, 0, byteArray.Length);
cryptoStream.FlushFinalBlock();
return Encoding.UTF8.GetString(memoryStream.ToArray());
}
catch (Exception ex)
{
string message = ex.Message;
}`
关于这门课的一些评论是:
Encoding.UTF8.GetBytes 是一种从密码中获取密钥的非常糟糕且不安全的方法。
http://www.codinghorror.com/blog/2010/12/the-dirty-truth-about-web-passwords.html
- 其他问题
我搜索了一个类来完成没有这些问题的加密和解密,如果对这个类进行任何修改或增强以避免这些问题并使其更安全,那就太好了。
如果有其他类经过长期测试和使用以确保其稳定性,那就太好了..