Base-64字符数组或字符串的长度无效(解码时)

本文关键字:无效 解码 数组 字符 字符串 Base-64 | 更新日期: 2023-09-27 18:28:17

我收到错误'Base-64字符数组或字符串的长度无效'
我试过使用两个不同的编码字符串即:
1。ZGVudmVyZ29tZXMyMTEw,正确解密为:DENVERGOMES2110,并且
2.CCD_ 3,其给出错误。字符串为"SADDAMHUSSAIN"

我使用的是ASP.net MVC 4。这是我的代码:
控制器:

public string EncryptUsername(string userID, int status)
{
    string strResult = string.Empty;
    StudentRegistrationDAL dal = new StudentRegistrationDAL();
    strResult = dal.EncryptDecryptUsername(userID, status);
    return strResult;
}


DataAccessLayer.class:

public string EncryptDecryptUsername(string userID, int status)
{
    string encUsername = string.Empty;
     ReverseEngineerDAL encryptDecryptDAL = new ReverseEngineerDAL();
     if (status == 1)
     {
         encUsername = encryptDecryptDAL.EncodePasswordToBase64(userID);
     }
     else if (status == 2)
     {
         encUsername = encryptDecryptDAL.DecodeFrom64(userID).ToUpper();
     }
     return encUsername;
}


ReverseEngineerDAL.class:

public class ReverseEngineerDAL
{
    public string EncodePasswordToBase64(string password)
    {
        try
        {
            byte[] encData_byte = new byte[password.Length];
            encData_byte = System.Text.Encoding.UTF8.GetBytes(password);
            string encodedData = Convert.ToBase64String(encData_byte);
            return encodedData;
        }
        catch (Exception ex)
        {
            throw new Exception("Error in base64Encode" + ex.Message);
        }
    }
    public string DecodeFrom64(string encodedData)
    {
        System.Text.UTF8Encoding encoder = new System.Text.UTF8Encoding();
        System.Text.Decoder utf8Decode = encoder.GetDecoder();
        byte[] todecode_byte = Convert.FromBase64String(encodedData);
        int charCount = utf8Decode.GetCharCount(todecode_byte, 0, todecode_byte.Length);
        char[] decoded_char = new char[charCount];
        utf8Decode.GetChars(todecode_byte, 0, todecode_byte.Length, decoded_char, 0);
        string result = new String(decoded_char);
        return result;
    }
}



我在string DecodeFrom64(string encodedData)中的"ReverseEngineerDAL.class"处收到错误:

byte[] todecode_byte = Convert.FromBase64String(encodedData);


有人能帮我解决这个错误吗。?或者发现我代码中的错误
如有任何帮助,我们将不胜感激。谢谢

Base-64字符数组或字符串的长度无效(解码时)

刚刚发现我错误地获取了加密值
有一个javascript拆分方法,在我的加密文本之后修剪==

我的C#代码运行良好。只需在谷歌上搜索一下并阅读Stack上的几篇帖子就对我有所帮助。

这是我的Jquery代码,我对它做了一些小的更改

        function GetParameterValues(param) {
        var url = window.location.href.slice(window.location.href.indexOf('?') + 1).split('&');
        for (var i = 0; i < url.length; i++) {
            /*var urlparam = url[i].split('=');
            if (urlparam[0] == param) {
            return urlparam[1];
            }*/
            var urlparam = url[0].substring(2, url[0].length);
            return urlparam;
        }
    }


评论的部分造成了问题,因为它正在向末尾修剪=(等号)

曼努埃尔·泽伦卡的评论给了我这个暗示

我感谢大家的评论。非常感谢大家。