Aspdotnet-Suresh

aspdotnet-suresh offers C#.net articles and tutorials,csharp dot net,asp.net articles and tutorials,VB.NET Articles,Gridview articles,code examples of asp.net 2.0 /3.5,AJAX,SQL Server Articles,examples of .net technologies

Invalid length for a base-64 char array error during decryption of password using asp.net

May 28, 2011
Introduction:

Here I will explain how to solve the problem invalid length for a base-64 char array during decrypt of encrypted password using asp.net.

Description:

In previous post I explained clearly how to encrypt and decrypt password using asp.net . After completion code for encrypt and decrypt password if I run the program I get problem like

Invalid length for a base-64 char array


Invalid length for a Base-64 char array.

Description: An unhandled exception occurred during the execution of the current web request. Please review the stack trace for more information about the error and where it originated in the code.

Exception Details: System.FormatException: Invalid length for a Base-64 char array.

Source Error:

An unhandled exception was generated during the execution of the current web request. Information regarding the origin and location of the exception can be identified using the exception stack trace below.    
This error occurs during decryption of passwords to eliminate this error I change decrypted code to

Oldone

 byte[] todecode_byte = Convert.FromBase64String(encryptpwd);

To

Newone

byte[] todecode_byte = Convert.FromBase64String(encryptpwd.Replace("","+"));
After change my code works perfectly. I hope it helps to solve your problem.

Happy Coding………

If you enjoyed this post, please support the blog below. It's FREE!

Get the latest Asp.net, C#.net, VB.NET, jQuery, Plugins & Code Snippets for FREE by subscribing to our Facebook, Twitter, RSS feed, or by email.

subscribe by rss Subscribe by RSS subscribe by email Subscribe by Email

39 comments :

ruma said...
This comment has been removed by a blog administrator.
Jatin said...

After fixing the code with above said solution another error is being generated as:

String cannot be of zero length.
Parameter name: oldValue

Anonymous said...

I used this Replace method but another error is being generated "String cannot be of zero length", please provide the solution for the same.

sivakumar said...

byte[] todecode_byte = Convert.FromBase64String(encryptpwd.Replace("","+"));

please change double quotes to single quotes

byte[] todecode_byte = Convert.FromBase64String(encryptpwd.Replace(' ','+'));

now it will work perfectly

P_IT said...
This comment has been removed by the author.
P_IT said...

still its not working i have changed double quote to single quote it shows error "Empty Character Literal"

Anonymous said...

No idea it is not working.

Anonymous said...

Hi,

I Have one Encrypted string and i want to decrypt that string. I am trying on that i am getting error like this :
Invalid length for a Base-64 char array
this is my string "7W7dA3m7A+GHJHhC5Z8gEg"

can you help this one and mail code for this i tried above code it's also geting error

mail to: v.suri87@gmail.com

Suresh Dasari said...

check below posts may be it helps you
http://www.aspdotnet-suresh.com/2010/12/introduction-here-i-will-explain-how-to_28.html
and
http://www.aspdotnet-suresh.com/2011/11/encrypt-and-decrypt-configuration.html

Anonymous said...

Thanks So much Suresh

sushil kumar said...

hi sir
i have changed changed the coding from old one to new new. But still showing a error.
So please give idea what i can do for this.

DARR123 said...

Hello sir!!!!!
The post for encryption/decryption was cool but
now after changing the code the previous error of "Invalid length for a Base-64 char array"
IS GONE but however now after changing the code it is showing the following error

String cannot be of zero length.
Parameter name: oldValue

Please could you tell what the problem is!!!!

Anonymous said...

I am getting the same situation as DARR123.

Please help

Dot Net Bodhi said...

byte[] todecode_byte = Convert.FromBase64String(encryptpwd.Replace(" ","+"));

try this

Anonymous said...
This comment has been removed by a blog administrator.
Anonymous said...
This comment has been removed by a blog administrator.
Anonymous said...

suresh garu, pls clarify this
I used this Replace method but another error is being generated "String cannot be of zero length", please provide the solution for the same.

Anonymous said...

Hello, This is Abu here.

i have tryied with single and double qoatation (encryptpwd.Replace("","+") and (encryptpwd.Replace(' ','+'))). But couldn't find any solution.

could anyone give any solution?

If its possible, Please send me an email here rossyhaider@yahoo.com
Or,
Post here.

Thank you.

Ramawatar sharma said...

Invalid length for a base-64 char array
has been solved but a new error has arise
String cannot be of zero length.
Parameter name: oldValue
please provide solution

Anonymous said...

still its not working i have changed double quote to single quote it shows error "Empty Character Literal"

please...help

Unknown said...

My name is Basudev
I am getting the error in this code
byte[] todecode_byte = Convert.FromBase64String(encryptpwd.Replace("","+")); as
Invalid length for a Base-64 char array
why this error happen Please help me Sir

Unknown said...

Hello Sir

i have tryied with single and double qoatation (encryptpwd.Replace("","+") and (encryptpwd.Replace(' ','+'))). But couldn't find any solution.

could anyone give any solution?

Please send me an email here naiemahmad29@gmail.com

Thank you.

Anonymous said...

byte[] todecode_byte = Convert.FromBase64String(encryptpwd.Replace("","+"));
string cannot be of zeo length
parameter name:old one

Anonymous said...

Copy following code,this code will work for you .
byte[] todecode_byte = Convert.FromBase64String(encryptpwd.Replace(" ", "+"));

Unknown said...

Sir,
I am also getting the same error.Kindly give clarify on this error. Thanks in advance

Panchdev said...

if getting below type of Error: Put this
byte[] todecode_byte = Convert.FromBase64String(encryptpwd);

protected void gvdecryption_RowDataBound(object sender, GridViewRowEventArgs e)
{
if (e.Row.RowType == DataControlRowType.DataRow)
{
string decryptpassword = e.Row.Cells[2].Text;
e.Row.Cells[2].Text = Decryptdata(decryptpassword);
}
}

Instead of above:
protected void gvdecryption_RowDataBound(object sender, GridViewRowEventArgs e)
{
if (e.Row.RowType == DataControlRowType.DataRow)
{
string decryptpassword = e.Row.Cells[1].Text;
e.Row.Cells[1].Text = Decryptdata(decryptpassword);
}
}

Anonymous said...

not yet find any answer for this.

Anonymous said...

It works perfectly fine for me even without the Replace(",","+") in the code.
Try to use Encryptdata instead of Decryptdata in your code.

Anonymous said...

1) Encrypt :: string cipherText = CryptorEngine.Encrypt(FileName.First().ToString(),true);
string FInalFileName= cipherText.Replace("/", "+++");

some string to Encrypt prolblem face :: output directory is invalid .so its problem is Encrlypt key mainly special character "/" is not to allow any file rename.so first of change this name.and whenever you Decrypt time this string to replace actually key data.its working perfectly.after 2 day finally solution.


2) Decrypt ::string FileName= OnlyFileName.First().Replace("+++", "/");
string cipherText = CryptorEngine.Decrypt(FileName, true);

Anonymous said...

please give space between (" ","+")

it will work

EssayProgramming said...

Invalid length for a Base-64 char array

Sachin Sharma said...

You need to try following:-

Base64 encoding encodes 6 bits per character. So the length of the string, multiplied by 6, must be divisible by 8. If it is not then it doesn't have enough bits to fill every byte and you'll get this exception.

So good odds that encodedMsg just isn't a properly encoded base64 string. You can append some = characters to bypass the exception and see if anything recognizable pops out. The = character is the padding character for base64:

while ((encodedMsg.Length * 6) % 8 != 0) encodedMsg += "=";

Anonymous said...

So, it's basically unusable until the author helps.

Manoj said...

Try using
byte[] todecode_byte = Convert.FromBase64String(encryptpwd.Replace(" ","+"));


T@M!L said...

use single Quote with space like Convert.FromBase64String(encryptpwd.Replace(' ','+'));

Unknown said...

Invalid length for a base-64 char array error during decryption:

In my case i changed the encrypt Password column size in database and it works .

using this command.
byte[] todecode_byte = Convert.FromBase64String(encryptpwd);

Anonymous said...

For vb.net try the following line you will get solution for "string cannot be of zeo length."
Dim todecode_byte As Byte() = Convert.FromBase64String(encryptpwd.Replace(" "C, "+"C))

Unknown said...

Please add this code before you convert the string to FromBase64String.
int mod4 = cipherText.Length % 4;
if (mod4 > 0)
{
cipherText += new string('=', 4 - mod4);
}

FromBase64String need string that devide by 4. If string is not devided by 4 then it throw exception.

For ex

// This string works fine
byte[] b1 = Convert.FromBase64String("test1234");

// Error
byte[] b2 = Convert.FromBase64String("test123");

Unknown said...

thanks nandan it's working now..

Give your Valuable Comments

Note: Only a member of this blog may post a comment.

© 2015 Aspdotnet-Suresh.com. All Rights Reserved.
The content is copyrighted to Suresh Dasari and may not be reproduced on other websites without permission from the owner.