472,142 Members | 1,350 Online
Bytes | Software Development & Data Engineering Community
Post +

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 472,142 software developers and data experts.

Invalid length for a Base-64 char array.

Hi all ,


I am using encryption to encrypt the query string passed from one
page to other. In works fine most of the time but occationally i get this error
-

The line that throws that exception is

Dim cipherTextBytes As Byte()

cipherTextBytes = Convert.FromBase64String(cipherText)

The Error name is

Run-time exception thrown : System.FormatException - Invalid length for a
Base-64 char array.

Its Very Urgent .Please anyone help me wat to do

Thanks in advance
Selvamariapan.c
Nov 8 '08 #1
2 9699
joedeene
583 512MB
Have a look at a similar thread..

with base64, every three bytes (24 bits) of data is converted to four bytes
of printable data (6 bit). when you convert base64 back, you read 4 bytes at
a time and convert back to 3 bytes. you get this error when you don't have a
mulitple of 4. most likely the data is bad, you can append 1 to 3 '='s
to get the correct length (these convert to nulls). you should figure out
why your base64 string are actually invalid.
Basically whatever data you're reading into your byte, it isn't an equal multiple of four, therefore it throws that exception. You can try appending one byte at the end and then test it, if it throws an exception add another byte. You shouldn't have to append more than 3 bytes. And make sure the appended character(s) are something you can tell apart from the rest of them.

joedeene
Nov 8 '08 #2
mldisibio
190 Expert 100+
Joe is correct, but just to clarify, using the quoted post:

- the Base64 standard already stipulates using "=" as the filler character for the last four bytes (so you shouldn't have to make-up your own filler)

and

- (as the previous post said) the exception indicates an underlying problem that the base64 string (that is, the encrypted string) was somehow incorrectly formed to begin with. The base64 encryption algorithm used should already have added this filler while encrypting.

The only reason I emphasize that, is because although Joe's solution would work to eliminate the exception, I would worry about it masking an underlying encryption problem.

Just two cents.

Mike
Nov 9 '08 #3

Post your reply

Sign in to post your reply or Sign up for a free account.

Similar topics

2 posts views Thread by Brian Genisio | last post: by
5 posts views Thread by Nick Flandry | last post: by
reply views Thread by dba123 | last post: by
1 post views Thread by banagani | last post: by
3 posts views Thread by =?Utf-8?B?Vmlub2Q=?= | last post: by
reply views Thread by parun | last post: by

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.