如何从数据库中读取加密值并将其与 ASP.NET Core 中的另一个值进行比较?
Posted
技术标签:
【中文标题】如何从数据库中读取加密值并将其与 ASP.NET Core 中的另一个值进行比较?【英文标题】:How to read an encrypted value from database and compare it with another value in ASP.NET Core? 【发布时间】:2021-10-22 20:32:57 【问题描述】:使用的工具:Visual Studio 2019、ASP.NET Core 5.0、SQL Server 2014
我加密了一个日期(Datetime.Today.AddDays(8))
,并成功将其保存到数据库中。
我尝试使用@Model.Decrypt(Model.Date.DateEnc)
解密该值,但它不起作用。
如何从数据库中检索加密值、解密并将解密后的值与今天的日期进行比较?
我的 Decrypt.cshtml 页面是:
@if(Model.Decrypt(encryptedValue).toString() == Datetime.Today.toString())
<p>Correct</p>
else
<p>Wrong</p>
我的 Decrypt.cshtml.cs 页面是:
public string Decrypt(string cipherText)
string EncryptionKey = " Bt2a";
cipherText = cipherText.Replace(" ", "+");
byte[] cipherBytes = Convert.FromBase64String(cipherText);
using (Aes encryptor = Aes.Create())
Rfc2898DeriveBytes pdb = new(EncryptionKey, new byte[] 0x01, 0x22, 0xcc, 0x0e, 0x50, 0x4d, 0x05, 0xa4, 0x4f, 0xff, 0xe3, 0x69, 0x7a );
encryptor.Key = pdb.GetBytes(32);
encryptor.IV = pdb.GetBytes(16);
using MemoryStream ms = new();
using (CryptoStream cs = new(ms, encryptor.CreateDecryptor(), CryptoStreamMode.Write))
cs.Write(cipherBytes, 0, cipherBytes.Length);
cs.Close();
cipherText = Encoding.Unicode.GetString(ms.ToArray());
return cipherText;
加密方法:
public string Encrypt(string icu)
string EncryptionKey = "Bt2a";
byte[] clearBytes = Encoding.Unicode.GetBytes(icu);
using (Aes encryptor = Aes.Create())
Rfc2898DeriveBytes pdb = new(EncryptionKey, new byte[] 0x01, 0x22, 0xcc, 0x0e, 0x50, 0x4d, 0x05, 0xa4, 0x4f, 0xff, 0xe3, 0x69, 0x7a );
encryptor.Key = pdb.GetBytes(32);
encryptor.IV = pdb.GetBytes(16);
using MemoryStream ms = new();
using (CryptoStream cs = new(ms, encryptor.CreateEncryptor(), CryptoStreamMode.Write))
cs.Write(clearBytes, 0, clearBytes.Length);
cs.Close();
icu = Convert.ToBase64String(ms.ToArray());
return icu;
【问题讨论】:
你的加密方法怎么样? 字符串太长,无法在此处发表评论,所以我提供了一个 mega.nz 链接 --- mega.nz/file/…string EncryptionKey = " Bt2a";
和 string EncryptionKey = "Bt2a";
... 这不对。
那是我复制粘贴的错误,但关键是两边都可以(“Bt2a”)。
您提供的代码工作正常,您确定加密值在保存到数据库之前或之后没有改变吗?
【参考方案1】:
我终于设法解决了这个问题。
我忘记声明构造函数“ED = await _context.EncDates.FirstOrDefaultAsync();”在 Decrypt.cshtml.cs 里面 --> OnGetAsync() 这在比较日期字符串时给出了“null”值。
感谢您的帮助。
以下是完整代码。
--- Decrypt.cshtml ---
@if(Model.Decrypt(Model.EncDates.encryptedValue).toString() == Datetime.Today.toString())
正确
否则错误
--- Decrypt.cshtml.cs ---
...
[BindProperty]
public EncDate ED get; set;
public IList<EncDate> EDL get; set;
public async Task OnGetAsync()
EDL = await _context.EncDates
.Include(a => a.Lic).ToListAsync();
ED = await _context.EncDates.FirstOrDefaultAsync();
public string Decrypt(string cipherText)
string EncryptionKey = "Bt2a";
cipherText = cipherText.Replace(" ", "+");
byte[] cipherBytes = Convert.FromBase64String(cipherText);
using (Aes encryptor = Aes.Create())
Rfc2898DeriveBytes pdb = new(EncryptionKey, new byte[] 0x01, 0x22, 0xcc, 0x0e, 0x50, 0x4d, 0x05, 0xa4, 0x4f, 0xff, 0xe3, 0x69, 0x7a );
encryptor.Key = pdb.GetBytes(32);
encryptor.IV = pdb.GetBytes(16);
using MemoryStream ms = new();
using (CryptoStream cs = new(ms, encryptor.CreateDecryptor(), CryptoStreamMode.Write))
cs.Write(cipherBytes, 0, cipherBytes.Length);
cs.Close();
cipherText = Encoding.Unicode.GetString(ms.ToArray());
return cipherText;
public string Encrypt(string icu)
string EncryptionKey = "Bt2a";
byte[] clearBytes = Encoding.Unicode.GetBytes(icu);
using (Aes encryptor = Aes.Create())
Rfc2898DeriveBytes pdb = new(EncryptionKey, new byte[] 0x01, 0x22, 0xcc, 0x0e, 0x50, 0x4d, 0x05, 0xa4, 0x4f, 0xff, 0xe3, 0x69, 0x7a );
encryptor.Key = pdb.GetBytes(32);
encryptor.IV = pdb.GetBytes(16);
using MemoryStream ms = new();
using (CryptoStream cs = new(ms, encryptor.CreateEncryptor(), CryptoStreamMode.Write))
cs.Write(clearBytes, 0, clearBytes.Length);
cs.Close();
icu = Convert.ToBase64String(ms.ToArray());
return icu;
【讨论】:
以上是关于如何从数据库中读取加密值并将其与 ASP.NET Core 中的另一个值进行比较?的主要内容,如果未能解决你的问题,请参考以下文章
如何从 AppSettings 读取配置值并将配置注入接口实例