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 = htmlEncode(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);
}