经典asp和ASP.NET之间的密码加密/解密

Sti*_*ckx 4 asp.net cryptography rijndael asp-classic rijndaelmanaged

我有 2 个网站:一个用经典 asp 编写,另一个用 ASP.NET(1.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
Run Code Online (Sandbox Code Playgroud)

我想我在生成密钥或 iv 时做错了,但我找不到解决方案。

Rya*_*ith 5

Phil Fresle 提供了此代码的 C# 版本,可在此处下载: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();
Run Code Online (Sandbox Code Playgroud)

大多数默认实现将使用128192256位的密钥大小。128位的块大小是标准的。尽管某些实现允许块大小不是 128 位,但更改块大小只会将另一项添加到混合中,从而在尝试在一种实现中加密数据以在另一种实现中正确解密时造成混淆。

希望这会有所帮助。

更新

Phil 的链接不再可用,所以我创建了 2 个要点: