converting to a base64 string and back again.

Posted on 2012-08-11
Medium Priority
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

Craig Wagner earned 2000 total points
ID: 38283699
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

ID: 38283789
Superb, top man works a treat!

Featured Post

Free Tool: ZipGrep

ZipGrep is a utility that can list and search zip (.war, .ear, .jar, etc) archives for text patterns, without the need to extract the archive's contents.

One of a set of tools we're offering as a way to say thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Introduction This article shows how to use the open source plupload control to upload multiple images. The images are resized on the client side before uploading and the upload is done in chunks. Background I had to provide a way for user…
Entity Framework is a powerful tool to help you interact with the DataBase but still doesn't help much when we have a Stored Procedure that returns more than one resultset. The solution takes some of out-of-the-box thinking; read on!
In a question here at Experts Exchange (https://www.experts-exchange.com/questions/29062564/Adobe-acrobat-reader-DC.html), a member asked how to create a signature in Adobe Acrobat Reader DC (the free Reader product, not the paid, full Acrobat produ…
Is your OST file inaccessible, Need to transfer OST file from one computer to another? Want to convert OST file to PST? If the answer to any of the above question is yes, then look no further. With the help of Stellar OST to PST Converter, you can e…
Suggested Courses
Course of the Month17 days, left to enroll

864 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