C#加密功能
我的数据库中有一个表,该表包含诸如密码字段之类的敏感数据。我想在将数据插入表之前对其进行加密,然后我不想解密数据,但我只想将加密的密码与输入密码进行比较,而不进行解密。我建议您将哈希密码和salt存储到数据库关于这个主题还有另外一个问题。如果您不需要解密数据,您实际上需要哈希,例如.NET在System.Security.Cryptography命名空间中提供的或类似的算法。我有两个函数来加密和解密数据: 第一个函数用于解密数据,第二个函数用于加密数据C#加密功能,c#,C#,我的数据库中有一个表,该表包含诸如密码字段之类的敏感数据。我想在将数据插入表之前对其进行加密,然后我不想解密数据,但我只想将加密的密码与输入密码进行比较,而不进行解密。我建议您将哈希密码和salt存储到数据库关于这个主题还有另外一个问题。如果您不需要解密数据,您实际上需要哈希,例如.NET在System.Security.Cryptography命名空间中提供的或类似的算法。我有两个函数来加密和解密数据: 第一个函数用于解密数据,第二个函数用于加密数据 using System.Security
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());
}
}
}
}
我希望这两个函数可以帮助您。使用像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;
}
使用System.Security.Cryptography;
///
///用sha256散列给定字符串
///
///要散列的字符串
///哈希的十六进制表示形式
静态字符串sha256(字符串密码)
{
SHA256Managed crypt=新的SHA256Managed();
string hash=string.Empty;
byte[]crypto=crypt.ComputeHash(Encoding.ASCII.GetBytes(password),0,Encoding.ASCII.GetByteCount(password));
foreach(加密中的字节位)
{
哈希+=位ToString(“x2”);
}
返回散列;
}
它总是为相同的输入提供相同的输出,因此您可以比较散列值。你也应该考虑输入(预先添加或附加一些特定于你的应用程序的值,以使彩虹表变得不那么有用)一些附加信息不清楚问题是什么……