简体   繁体   English

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

[英]Invalid length for a Base-64 char array or string (while Decoding)

I'm getting error ' Invalid length for a Base-64 char array or string. 我收到错误消息' Base-64 char数组或字符串的长度无效。 '. '。
I've tried with two different encoded strings viz.: 我尝试了两种不同的编码字符串,即:
1. ZGVudmVyZ29tZXMyMTEw which is decrypted correctly as: DENVERGOMES2110 , and 1. ZGVudmVyZ29tZXMyMTEw ,它已正确解密为: DENVERGOMES2110
2. c2FkZGFtaHVzc2Fpbg== which gives error. 2. c2FkZGFtaHVzc2Fpbg==给出错误。 The string is 'SADDAMHUSSAIN'. 字符串是“ SADDAMHUSSAIN”。

I'm using ASP.net MVC 4. Here's my code: 我正在使用ASP.net MVC4。这是我的代码:
Controller: 控制器:

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


DataAccessLayer.class: 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: 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;
    }
}



I'm getting error at ' ReverseEngineerDAL.class ' in string DecodeFrom64(string encodedData) on line: 在行上string DecodeFrom64(string encodedData)中的“ ReverseEngineerDAL.class ”上遇到错误:

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


Can anyone help me in solving this error.? 谁能帮助我解决这个错误。 or spot the mistake in my code? 或在我的代码中发现错误?
Any help would be appreciated. 任何帮助,将不胜感激。 Thanks. 谢谢。

Just figured out that I was fetching my encrypted values wrongly. 只是发现我错误地获取了我的加密值。
Had a javascript split method which trimmed the == after my encrypted text. 有一个JavaScript拆分方法,在我的加密文本后修整了==

My C# code was working fine. 我的C#代码运行正常。 Just a little Googling and reading several posts on Stack helped me. 稍加谷歌搜索和阅读Stack上的几篇文章对我有帮助。

Here's my Jquery code to which I made minor changes. 这是我的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;
        }
    }


The commented section was creating problem as it was trimming the =(equal) sign towards the end. 带注释的部分正在制造问题,因为它在结尾处微调了=(等于)符号。

Manuel Zelenka 's comment gave me this hint. 曼努埃尔·泽伦卡Manuel Zelenka )的评论给了我这个提示。

I appreciate everyone's comments. 我感谢大家的评论。 And very big thanks to everyone. 非常感谢大家。

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

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