简体   繁体   English

Base-64 char数组的长度无效

[英]Invalid length for a Base-64 char array

I am working on Decrypt Password and i stuck on this error Invalid length for a Base-64 char array.I am trying too many things but all in vain my project is stuck due to this error .Here is my code. 我正在使用解密密码,并且卡住了这个错误Base-64 char数组的长度无效。我正在尝试太多事情,但是由于这个错误,所有我的项目都被卡住了。这是我的代码。

public string PasswordDecrypt(string sQueryString)
    {

        byte[] buffer;
        TripleDESCryptoServiceProvider loCryptoClass = new TripleDESCryptoServiceProvider();
        MD5CryptoServiceProvider loCryptoProvider = new MD5CryptoServiceProvider();


        try
        {
            string base64String;
            char[] base64CharArray;
            base64CharArray = new char[sQueryString.Length];
            base64String = new string(base64CharArray);
            Convert.FromBase64String(sQueryString);
            buffer = Convert.FromBase64String(sQueryString);
            loCryptoClass.Key = loCryptoProvider.ComputeHash(ASCIIEncoding.ASCII.GetBytes(sQueryString.Replace("","+")));
            loCryptoClass.IV = lbtVector;
            return ASCIIEncoding.ASCII.GetString(loCryptoClass.CreateDecryptor().TransformFinalBlock(buffer, 0, buffer.Length));
        }
        catch (Exception ex)
        {
            throw ex;
        }
        finally
        {
            loCryptoClass.Clear();
            loCryptoProvider.Clear();
            loCryptoClass = null;
            loCryptoProvider = null;
        }

    }

Passwords should never be decrypted (or encrypted, for that matter). 永远不要解密(或加密)密码。 You should be creating a hash of the password (preferably salted) when creating/updating your credential store and then comparing that hash to a hash derived from whatever the user enters when attempting to authenticate. 在创建/更新凭证存储时,您应该创建密码的哈希(最好是盐化),然后将该哈希与尝试进行身份验证的用户输入的哈希进行比较。

This is an implementation I've used in the past (which has some faults but works in non-critical apps): 这是我过去使用的实现(有一些错误,但可以在非关键应用程序中使用):

public class HashProvider
{
    /// <summary>
    /// Computes the SHA1 hash from the given string.
    /// </summary>
    /// <param name="stringToHash">The string to hash.</param>
    /// <returns></returns>
    public static string GetSHA1Hash(string stringToHash)
    {
        var data = Encoding.UTF8.GetBytes(stringToHash);
        var hashData = new SHA1CryptoServiceProvider().ComputeHash(data);

        return String.Concat(hashData.Select(b => b.ToString("X2")));
    }


    /// <summary>
    /// Computes the SHA1 hash from the given string, and then encodes the hash as a Base64 string.
    /// </summary>
    /// <param name="stringToHash">The string to hash.</param>
    /// <returns></returns>
    public static string GetSHA1toBase64Hash(string stringToHash)
    {
        var data = Encoding.UTF8.GetBytes(stringToHash);
        var hashData = new SHA1CryptoServiceProvider().ComputeHash(data);

        return Convert.ToBase64String(hashData);
    }
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM