我有 2 个网站:一个用经典 ASP 编写,另一个用 ASP.NET(1.1 框架)编写。这两个应用程序都使用登录机制来验证基于共享数据库表的用户凭据。到目前为止,密码存储在单向 MD5 哈希中,这意味着如果人们丢失旧密码,则必须为其提供新生成的密码。我现在想更改此设置并使密码可解密。
我发现这个 Rijndael 代码可以与经典 asp 一起使用:http://www.frez.co.uk/freecode.htm#rijndael 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 时做错了什么,但我找不到解决方案。
Phil Fresle 提供了此代码的 C# 版本,可在此处下载:http://www.frez.co.uk/csharp.aspx http://www.frez.co.uk/csharp.aspx。该实现仍然与经典版本不同,因为它采用初始化向量以及块和密钥大小的参数。
您可以使用此实现来匹配经典版本,如下所示:
// Convert the input values to byte[]'s representing ASCII encoding.
// This is what the classic version does
byte[] dataToEncrypt = ASCIIEncoding.ASCII.GetBytes("Ryno");
byte[] password = ASCIIEncoding.ASCII.GetBytes("Saurus");
// Encrypt the data into an array of types
// Notice the block size is 256 bits and the initialization vector is empty.
byte[] results = Rijndael.EncryptData(
dataToEncrypt,
password,
new byte[] { }, // Initialization vector
Rijndael.BlockSize.Block256, // Typically 128 in most implementations
Rijndael.KeySize.Key256,
Rijndael.EncryptionMode.ModeEBC
);
// Convert bytes into a HEX string representation
StringBuilder hex = new StringBuilder(results.Length * 2);
foreach (byte b in results)
hex.AppendFormat("{0:x2}", b);
// FINAL OUTPUT: This matches output of classic ASP Rijndael encryption
string hexEncodedString= hex.ToString();
大多数默认实现将使用密钥大小128, 192, or 256位。块大小为128位是标准的。尽管某些实现允许 128 位以外的块大小,但更改块大小只会将另一项添加到混合中,从而在尝试在一种实现中加密数据以在另一种实现中正确解密时造成混乱。
希望这有帮助。
Update
Phil 的链接不再可用,因此我创建了 2 个要点:
- ASP/VB实现:https://gist.github.com/ryno1234/2fc83faff281babea7f8 https://gist.github.com/ryno1234/2fc83faff281babea7f8
- 使用用于 ASP 互操作的帮助程序类的 C# 实现:https://gist.github.com/ryno1234/ea34415643d109974b6f https://gist.github.com/ryno1234/ea34415643d109974b6f
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)