Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

String cannot be of zero length. Parameter name: oldValue

I am working on Decrypt Password and I stuck on this error:

String cannot be of zero length. Parameter name: oldValue

Kindly help on this error or suggest me another program for decryption.

Here is the full code:

string decryptpwd = string.Empty;
UTF8Encoding encodepwd = new UTF8Encoding();
Decoder Decode = encodepwd.GetDecoder();
byte[] todecode_byte = Convert.FromBase64String(encryptpwd.Replace("+",""));
int charcount = Decode.GetCharCount(todecode_byte, 0, todecode_byte.Length);
char[] decode_char = new char[charcount];
Decode.GetChars(todecode_byte, 0, todecode_byte.Length, decode_char, 0);
decryptpwd = new String(decode_char);
return decryptpwd;
like image 919
Raheel Avatar asked May 24 '13 11:05

Raheel


3 Answers

You are asking the Replace method to change an empty string (first parameter) with a plus character (second parameter). This make no sense and Replace is complaining about this.
I think you want to do the reverse

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

A part from this I am not sure what the result will be when you change something into the input string and apply a FromBase64String to the result. Well, it really depends on what was originally in the string, but for sure (if encryptpwd is really a Base64 string) there are no spaces to replace.

Keep in mind that you can't pass a normal string to Convert.FromBase64String, you need a string that is a base 64 string

What is a base 64 string

For example

string pwd = "786";   // The original string

UnicodeEncoding u = new UnicodeEncoding();
byte[] x = u.GetBytes(pwd);  // The Unicode bytes of the string above

// Convert bytes to a base64 string
string b64 = Convert.ToBase64String(x);
Console.WriteLine(b64);

// Go back to the plain text string    
byte[] b = Convert.FromBase64String(b64);
string result = u.GetString(b);
Console.WriteLine(result);

A final word. Someone (@Slacks) already tells you that a base64 string is not an encryption technology and you shouldn't use it for crypting passwords (They are not crypted at all)

like image 154
Steve Avatar answered Nov 20 '22 10:11

Steve


encryptpwd.Replace("","+")

What exactly are you replacing? You haven't specified an original value to be replaced.

String.Replace takes two string arguments oldValue and newValue. You specified the newValue + however an empty string is not legal for the oldValue.

Therefore if you want to replace a blank space with + try:

encryptpwd.Replace(" ","+");

Or vice versa:

encryptpwd.Replace("+"," ");

http://msdn.microsoft.com/en-us/library/fk49wtc1.aspx

like image 42
Darren Avatar answered Nov 20 '22 10:11

Darren


problem is here

encryptpwd.Replace("","+")

Should have some character or string to replace

encryptpwd.Replace(" ","+")
like image 2
Rajeev Kumar Avatar answered Nov 20 '22 09:11

Rajeev Kumar