Results 1 to 3 of 3

Thread: invalid length for a base 64 char array

  1. #1

    Thread Starter
    Frenzied Member
    Join Date
    Jun 2005
    Posts
    1,170

    invalid length for a base 64 char array

    Hi I have a code below, but everytime, my "text" throwing an error invalid length for a base 64 char array, and I don't know how to fix this.

    Code:
    try
                {
                    key = Encoding.UTF8.GetBytes(stringKey.Substring(0, 8));
                    DESCryptoServiceProvider des = new DESCryptoServiceProvider();
                    Byte[] byteArray = Convert.FromBase64String(text);
                    MemoryStream memoryStream = new MemoryStream();
                    CryptoStream cryptoStream = new CryptoStream(memoryStream,
                        des.CreateDecryptor(key, IV), CryptoStreamMode.Write);
                    cryptoStream.Write(byteArray, 0, byteArray.Length);
                    cryptoStream.FlushFinalBlock();
                    return Encoding.UTF8.GetString(memoryStream.ToArray());
                }
                catch (Exception ex)
                {
                    // Handle Exception Here
                }
                return string.Empty;

  2. #2

    Thread Starter
    Frenzied Member
    Join Date
    Jun 2005
    Posts
    1,170

    Re: invalid length for a base 64 char array

    Turned out there's a "+" sign in my string, but how to get rid of it? If I put any other characters, it will screw up my string though.

  3. #3
    I'm about to be a PowerPoster! mendhak's Avatar
    Join Date
    Feb 2002
    Location
    Ulaan Baator GooGoo: Frog
    Posts
    38,170

    Re: invalid length for a base 64 char array

    The proper way to get rid of it is to tackle it at its source. If it's coming in from a database then figure out what's putting it there and get that application to do it right.

    The improper way is to perform a .Replace(), replacing the + with an empty string, but that doesn't guarantee future problems in the base64 string, since other characters may get introduced in there.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width