String cannot be of zero length. Parameter name: oldValue

26,796

Solution 1

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)

Solution 2

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

Solution 3

problem is here

encryptpwd.Replace("","+")

Should have some character or string to replace

encryptpwd.Replace(" ","+")
Share:
26,796

Related videos on Youtube

Author by

Raheel

Software Engineer with three years of experience in Php,C#,ASP.Net ,Silverlight but still feel like a student

Updated on July 09, 2022

Comments

  • Raheel 6 months

    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;
    
    • SLaks
      SLaks over 9 years
      Base64 is not encryption. You must hash passwords.
  • Raheel over 9 years
    @Rajev i try your solution after it i get that error Invalid length for a Base-64 char array
  • Raheel over 9 years
    i also try your solution i get that error Invalid length for a Base-64 char array
  • Steve
    Steve over 9 years
    I can only suggest to search on the subject Crypting and store passwords. The argument is very broad. Look at this question where in the second part of my answer I explain how to crypt and decrypt a plain text
  • Raheel over 9 years
    Thanks Steve so kind of you

Related