如何使用Challenge Key进行MD5加密

时间:2013-08-15 15:03:54

标签: c# authentication encryption md5 challenge-response

我正在尝试使用MD5身份验证建立与服务器的连接。 服务器返回挑战键。 如何使用它来加密我的密码并将其发送回服务器。 目前我所知的加密方法需要Password Hash,Salt Key和VI Code。 我怎么能做到。

    static readonly string PasswordHash = "PasswordHash";
    static readonly string SaltKey = "SaltKey";
    static readonly string VIKey = "@1B2c3D4e5F6g7H8";

    public static string Encrypt(string plainText)
    {
        byte[] plainTextBytes = Encoding.UTF8.GetBytes(plainText);

        byte[] keyBytes = new Rfc2898DeriveBytes(PasswordHash, Encoding.ASCII.GetBytes(SaltKey)).GetBytes(256 / 8);
        var symmetricKey = new RijndaelManaged() { Mode = CipherMode.CBC, Padding = PaddingMode.Zeros };
        var encryptor = symmetricKey.CreateEncryptor(keyBytes, Encoding.ASCII.GetBytes(VIKey));

        byte[] cipherTextBytes;

        using (var memoryStream = new MemoryStream())
        {
            using (var cryptoStream = new CryptoStream(memoryStream, encryptor, CryptoStreamMode.Write))
            {
                cryptoStream.Write(plainTextBytes, 0, plainTextBytes.Length);
                cryptoStream.FlushFinalBlock();
                cipherTextBytes = memoryStream.ToArray();
                cryptoStream.Close();
            }
            memoryStream.Close();
        }
        return Convert.ToBase64String(cipherTextBytes);

0 个答案:

没有答案