converting to a base64 string and back again.

Posted on 2012-08-11
Last Modified: 2012-08-11
I have the following code:
 var md = orderID.ToString() + "*" + cardXML;
                var unicodeEncoding = new UnicodeEncoding();
                byte[] secret = ProtectedData.Protect(unicodeEncoding.GetBytes(md), null, DataProtectionScope.CurrentUser);
                string base64 = Convert.ToBase64String(secret);
                string data = String.Format("PaReq={0}&TermUrl={1}&MD={2}", Server.UrlEncode(threeDResponse.ParEQ), "http://localhost:1784/app/Test3D.aspx", base64);
                byte[] buffer = Encoding.UTF8.GetBytes(data);
                req.Method = "POST";
                req.ContentType = "application/x-www-form-urlencoded";
                req.ContentLength = buffer.Length;
                req.CookieContainer = new CookieContainer(); // enable cookies

                Stream reqst = req.GetRequestStream(); // add form data to request stream
                reqst.Write(buffer, 0, buffer.Length);

                HttpWebResponse res = (HttpWebResponse)req.GetResponse();

                Stream resst = res.GetResponseStream();
                StreamReader sr = new StreamReader(resst);
                string response = sr.ReadToEnd();
                LitTest.Text = response.ToString();

Open in new window

Basically I am trying to construct a string that gets added as a hiddenfield on the webpage i am grabbing and then putting into my literal.

That page then gets posted to my test3d.aspx page.

At the other end I then do:
byte[] backagain = Convert.FromBase64String(Request["MD"].ToString());

Open in new window

However I am getting:
Invalid length for a Base-64 char array.

Can anyone see what I am doing wrong?

The solution is to integrate 3d secure.
Question by:scm0sml
    LVL 21

    Accepted Solution

    You need to UrlEncode the base64 string. It likely contains + and / characters that are getting lost.

    string data = String.Format("PaReq={0}&TermUrl={1}&MD={2}", Server.UrlEncode(threeDResponse.ParEQ), "http://localhost:1784/app/Test3D.aspx", Server.UrlEncode(base64));

    Open in new window

    However, you do not need to UrlDecode at the other end. Pulling the value out of the Request collection does that for you automatically.

    P.S. I figured this out just by plunking your code into a new application and walking through it with the debugger.

    Author Comment

    Superb, top man works a treat!

    Write Comment

    Please enter a first name

    Please enter a last name

    We will never share this with anyone.

    Featured Post

    Top 6 Sources for Identifying Threat Actor TTPs

    Understanding your enemy is essential. These six sources will help you identify the most popular threat actor tactics, techniques, and procedures (TTPs).

    This article introduced a TextBox that supports transparent background.   Introduction TextBox is the most widely used control component in GUI design. Most GUI controls do not support transparent background and more or less do not have the…
    This article is for Object-Oriented Programming (OOP) beginners. An Interface contains declarations of events, indexers, methods and/or properties. Any class which implements the Interface should provide the concrete implementation for each Inter…
    It is a freely distributed piece of software for such tasks as photo retouching, image composition and image authoring. It works on many operating systems, in many languages.
    Hi everyone! This is Experts Exchange customer support.  This quick video will show you how to change your primary email address.  If you have any questions, then please Write a Comment below!

    758 members asked questions and received personalized solutions in the past 7 days.

    Join the community of 500,000 technology professionals and ask your questions.

    Join & Ask a Question

    Need Help in Real-Time?

    Connect with top rated Experts

    10 Experts available now in Live!

    Get 1:1 Help Now