Love the tubes..thank you Kiran Patil - base64 won't trick me again :)
Thank God for the tubes!!
I was banging my head against some errors today in some code which seemed pretty straightforward.
- Take some data - encrypt it via 3DES - encode it in base64
- Toss it over to some other system - decode - decrypt.
However, randomly I would fail here:
System.FormatException was unhandled by user code
Message=Invalid length for a Base-64 char array.
Source=mscorlib
StackTrace:
at System.Convert.FromBase64String(String s)
at proofPointTestAuthNServer._Default.Page_Load(Object sender, EventArgs e) in c:\users\spat\documents\visual studio 2010\Projects\TestServer\TestServer\Default.aspx.cs:line 105
at System.Web.Util.CalliHelper.EventArgFunctionCaller(IntPtr fp, Object o, Object t, EventArgs e)
at System.Web.Util.CalliEventHandlerDelegateProxy.Callback(Object sender, EventArgs e)
at System.Web.UI.Control.OnLoad(EventArgs e)
at System.Web.UI.Control.LoadRecursive()
at System.Web.UI.Page.ProcessRequestMain(Boolean includeStagesBeforeAsyncPoint, Boolean includeStagesAfterAsyncPoint)
InnerException:
After spending some time on it I decided to hit the tubes - bang. Kiran Patil hit upon this and blogged about it here:
https://kiranpatils.wordpress.com/2008/03/13/invalid-length-for-a-base-64-char-arrayrijandelmanagedencryption/#comment-707
In case Kiran Patil's site goes away here is his solution which worked like a charm for me:
*********THE SOLUTION**********
Plain Text[My guid] = 72e3f882-9e0e-434f-bf51-28cffc1903c1
Cipher TEXT[generatd by my Encrypt block] = 5NGETiEwyYy3F+P+QnzkRUeB5l7xUTRJuhhGZmFwi5WxCfeo3+kCvTs0z9s+CnZW
Querystring[encrypted guid] = 5NGETiEwyYy3F P QnzkRUeB5l7xUTRJuhhGZmFwi5WxCfeo3 kCvTs0z9s CnZW
Have you seen the change in Querystring???
My Cipher contains "+" and when i pass it in Querystring it " " it passes[space]...
That's the solution dude....
so for solving it just have to change the following block of Decrypt string
byte[] data = Convert.FromBase64String(ciphertext); //OLD ONE
byte[] data = Convert.FromBase64String(ciphertext.Replace(" ","+")); //NEW ONE
Hope it wiill save your time which i had wasted/invested...
Happy Encrypting/Decrypting!!!
Hoorah!!
spatdsg