开发者

Is it possible to include symbols like +,*,/ and - to encode in base64 format?

I'm using base64 encrypting of html tags due to solve a postback issue with my code. My html tag contain symbols like + , - , / or * . While decrypting the encrypted string im getting the following error :

Invalid length for a Base-64 char array.

Can anybody suggest a workaround here please?

JavaScript Calling from aspx page.

 var keyStr = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/=";

    function encode64(input) {
        var output = "";
        var chr1, chr2, chr3;
        var enc1, enc2, enc3, enc4;
        var i = 0;

        while (i < input.length) {
            chr1 = input.charCodeAt(i++);
            chr2 = input.charCodeAt(i++);
            chr3 = input.charCodeAt(i++);

            enc1 = chr1 >> 2;
            enc2 = ((chr1 & 3) << 4) | (chr2 >> 4);
            enc3 = ((chr2 & 15) << 2) | (chr3 >> 6);
            enc4 = chr3 & 63;

            if (isNaN(chr2)) {
                enc3 = enc4 = 64;
            }

            else if (isNaN(chr3)) {
                enc4 = 64;
            }

            output += keyStr.charAt(enc1) + keyStr.charAt(enc2) + keyStr.charAt(enc3) + keyStr.charAt(enc4);
        }

        return output;
    }

C# Code to decode the string @ pageload:

 public string DecodeBase64String(string encodedData)
    {
        byte[] encodedDataAsBytes = System.Convert.FromBase64String(encodedData);
        string returnValue = System.Text.ASCIIEncoding.ASCII.GetString(encodedDataAsBytes);

        return returnValue;
    }

Error can be generated by giving a simple string that contain any of the symbols i'd mentioned or even a space character.

Html String :

"<tbody id=\"selectedColumnsTbody\">\n                    <tr style=\"cursor: move;\" id=\"ExprCountryMasterID10\"><td></td><td><input id=\"chk\" checked=\"checked\" class=\"textChangeClass\" type=\"checkbox\"></td><td>CountryMaster.ID + 10</td><td><input id=\"aliastextCountryMasterID10\" class=\"aliasTextChangeClass\" value=\"\" type=\"text\"></td>><td><input id=\"hiddenIDSortCountryMasterID10\" value=\"\" type=\"hidden\"></td></tr></tbody>\n        

Calling decrypt method from cs page:

protected void Page_Load(object sender, EventArgs e) {

        //HtmlTextWriter htmlTable = new HtmlTextWriter();

        //htmlTable.InnerHtml = htmlContent;
        //Master.FindControl("ContentPlaceHolder1").Controls.Add(htmlTable);
        if (Session["HtmlTable"] != null)
        {

            htmlContent = Session["HtmlTable"].ToString();
            //htmlContent = htmlContent.Replace(" ", "+");
            htmlContent = DecodeBase64String(htmlContent);
            htmlTable = new HtmlGenericControl();
            ContentPlaceHolder contentPlaceHolder = (ContentPlaceHolder)this.Master.FindControl("ContentPlaceHolder1");
            if (contentPlaceHolder != null)
            {
                htmlContent = "<table cellspacing=\"0\" cellpadding=\"0\" id=\"selectedColumns\" width=\"100%\">" + htmlContent + "</table>";
                htmlTable.InnerHtml = htmlContent;
                test.InnerHtml = htmlContent;
            }
        }

}

Javascript where im calling htmlEncode function StoreSessionForHtml(htmlContent) {

        //            var encodedObject = htmlEnc开发者_如何学编程ode(htmlContent);
        //            var decodedObject = htmlDecode(encodedObject);
        //htmlContent = htmlContent.replace(/ /g, "+");


        var encodedObject = encode64(htmlContent);

        var requesthtmlContentParameter = '{' +
                        'htmlContentToServer:"' + encodedObject + '"}';

        $.ajax({
            type: "POST",
            url: "Webtop.aspx/HTMLTableContent",
            data: requesthtmlContentParameter,
            contentType: "application/json; charset=utf-8",
            dataType: "json",
            success: function(msg) {
                //alert("Success", msg.d);
            }, //Event that'll be fired on Success
            error: function() {
               // alert("Try Again");

            } //Event that'll be fired on Error
        });
        $("#<%= HiddenHtmlContainer.ClientID %>").val(encodedObject);
    }


Why don't use the HttpUtility.HtmlEncode Method

to prevent attack


There shouldn't be any problems with encoding any character since you are not encoding characters, you are encoding bytes.

The error you are getting I would assume is due to the legnth of your base64 encoded string not being correct. Base 64 encoding effectively changes groups of three bytes into groups of four characters. This means that decoders will want a string to decode that is a multiple of four characters. This is achieved by using the special character "=" to pad it out. This allows the decoder to know that those bytes don't exist (as opposed to just being blank or indeed missing).

The chances are that you had a problem purely because of the number of characters in the string you were decoding.

http://base64encode.org/ may be helpful for validating the strings you are generating to check whether your problem is with the encoding or decoding.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜