经典asp和ASP.NET之间的密码加密/解密
我有 2 个网站:一个用经典 ASP 编写,另一个用 ASP.NET(1.1 框架)编写。这两个应用程序都使用登录机制来验证基于共享数据库表的用户凭据。到目前为止,密码存储在单向 MD5 哈希中,这意味着如果人们丢失旧密码,则必须为其提供新生成的密码。我现在想更改此设置并使密码可解密。
我发现这个 Rijndael 代码可以与经典 asp 一起使用: http://www.frez.co.uk/freecode.htm#rijndael
但我找不到 ASP.NET 的相同解决方案。我尝试了这个,但它给了我经典 asp 和 ASP.NET 代码之间不同的加密和解密结果:
If Not String.IsNullOrEmpty(TextBox1.Text) And Not String.IsNullOrEmpty(TextBox2.Text) Then
Dim password = TextBox1.Text
Dim key = TextBox2.Text
Dim keyGenerator = New Rfc2898DeriveBytes(key, 8)
Dim r = New RijndaelManaged
r.Mode = CipherMode.CBC
r.Padding = PaddingMode.Zeros
r.BlockSize = 256
r.KeySize = 256
r.FeedbackSize = 256
r.IV = keyGenerator.GetBytes(CType(r.BlockSize / 8, Integer))
r.Key = keyGenerator.GetBytes(CType(r.KeySize / 8, Integer))
Dim transform As ICryptoTransform = r.CreateEncryptor()
Dim encoded As Byte() = Encoding.ASCII.GetBytes(password)
Dim target As Byte() = transform.TransformFinalBlock(encoded, 0, encoded.Length)
TextBox3.Text = Encoding.ASCII.GetString(target)
End If
我认为我在生成密钥或 iv 时做错了什么,但我找不到解决方案。
I have 2 websites: one written in classic asp and another written in ASP.NET (1.1 framework). Both applications use a login mechanism to validate user credentials based on a shared database table. Up to now passwords are stored in a 1-way MD5 hash, meaning people must be given a new generated password if they lose the old one. I now want to change this and make the passwords decryptable.
I found this Rijndael code to use with classic asp:
http://www.frez.co.uk/freecode.htm#rijndael
But I cannot find the same solution for ASP.NET. I tried this, but it gives me different encryption and decryption results between the classic asp and ASP.NET code:
If Not String.IsNullOrEmpty(TextBox1.Text) And Not String.IsNullOrEmpty(TextBox2.Text) Then
Dim password = TextBox1.Text
Dim key = TextBox2.Text
Dim keyGenerator = New Rfc2898DeriveBytes(key, 8)
Dim r = New RijndaelManaged
r.Mode = CipherMode.CBC
r.Padding = PaddingMode.Zeros
r.BlockSize = 256
r.KeySize = 256
r.FeedbackSize = 256
r.IV = keyGenerator.GetBytes(CType(r.BlockSize / 8, Integer))
r.Key = keyGenerator.GetBytes(CType(r.KeySize / 8, Integer))
Dim transform As ICryptoTransform = r.CreateEncryptor()
Dim encoded As Byte() = Encoding.ASCII.GetBytes(password)
Dim target As Byte() = transform.TransformFinalBlock(encoded, 0, encoded.Length)
TextBox3.Text = Encoding.ASCII.GetString(target)
End If
I think I'm doing something wrong with generating the key or iv, but I can't find a solution.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
Phil Fresle 提供了此代码的 C# 版本,可在此处下载: http://www.frez.co.uk /csharp.aspx。该实现仍然与经典版本不同,因为它采用初始化向量以及块和密钥大小的参数。
您可以使用此实现来匹配经典版本,如下所示:
大多数默认实现将使用 128、192 或 256 位的密钥大小。 128 位的块大小是标准的。尽管某些实现允许 128 位以外的块大小,但更改块大小只会将另一项添加到混合中,从而在尝试在一种实现中加密数据以在另一种实现中正确解密时造成混乱。
希望这有帮助。
更新
Phil 的链接不再可用,所以我创建了 2 个要点:
Phil Fresle provides a C# version of this code downloadable here: http://www.frez.co.uk/csharp.aspx. The implementation is still different than the classic version as it takes arguments for an initialization vector as well as the block and key size.
You can use this implementation to match the classic version like so:
Most default implementations will use a key size of 128, 192, or 256 bits. A block size at 128 bits is standard. Although some implementations allow block sizes other than 128 bits, changing the block size will just add another item into the mix to cause confusion when trying to get data encrypted in one implementation to properly decrypt in another.
Hopefully this helps.
Update
Phil's link is no longer available, so I've created a 2 Gists:
我快速浏览了经典的 asp 文件,它没有提到使用的块模式,而您的 .net 代码指定了 CBC 模式以及填充。此外,经典的实现指出:
您是否使用这些函数,如果您也在加密大小字节。
请放心,.net 加密工作良好,我猜您的问题出在您找到的经典解决方案中。如果我处在你的位置,我会从简化事情开始,只用每种方法加密一个块,然后从那里扩展......祝你好运
I had a quick look at the classic asp files and it doesn't mention the block mode used, whereas your .net code specifies CBC mode and also the padding. Further the classic implementation states:
Are you using those functions, if you are then your encrypting the size bytes too.
Be assured the .net encryption works well, I'd guess your problem is in the classic solution you've found. If I were in your position I'd start by simplifying things and just encrypt a single block with each method and then expand from there... good luck
由于 ASP classic 没有本机哈希函数,因此您可能需要将 MD5 VBScript 代码移植到 .NET 语言,或者使用常见的加密组件,因为遗留代码中存在一些错误。
Since ASP classic doesn't have native hash functions, you'll probably need to port your MD5 VBScript code to your .NET language, or to use a common cryptography component, due some error on your legacy code.