1
votes

I have tab application which converts password using Base64 encoding. It is send to web application via web service where it gets decoded. Code for decoding is,

public static string DecryptStringPassword(string base64StringToDecrypt)
        {
            //Set up the encryption objects
            using (AesCryptoServiceProvider acsp = GetProvider(Encoding.Default.GetBytes(Key)))
            {
                byte[] RawBytes = Convert.FromBase64String(base64StringToDecrypt);
                ICryptoTransform ictD = acsp.CreateDecryptor();

                //RawBytes now contains original byte array, still in Encrypted state

                //Decrypt into stream
                MemoryStream msD = new MemoryStream(RawBytes, 0, RawBytes.Length);
                CryptoStream csD = new CryptoStream(msD, ictD, CryptoStreamMode.Read);
                //csD now contains original byte array, fully decrypted

                //return the content of msD as a regular string
                return (new StreamReader(csD)).ReadToEnd();
            }
        }

        private static AesCryptoServiceProvider GetProvider(byte[] key)
        {
            AesCryptoServiceProvider result = new AesCryptoServiceProvider();
            result.BlockSize = 128;
            result.KeySize = 128;
            result.Mode = CipherMode.CBC;
            result.Padding = PaddingMode.PKCS7;

            result.GenerateIV();
            result.IV = new byte[] { 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 };

            byte[] RealKey = GetKey(key, result);
            result.Key = RealKey;
            // result.IV = RealKey;
            return result;
        }

        private static byte[] GetKey(byte[] suggestedKey, SymmetricAlgorithm p)
        {
            byte[] kRaw = suggestedKey;
            List<byte> kList = new List<byte>();

            for (int i = 0; i < p.LegalKeySizes[0].MinSize; i += 8)
            {
                kList.Add(kRaw[(i / 8) % kRaw.Length]);
            }
            byte[] k = kList.ToArray();
            return k;
        }

Is there any maximum character limit for base64 encoding? For one of the passwords "xwYgqg8+xnynU7MpceOoJw==" is the encrypted string which gives exception "padding is invalid and can not be removed" while decoding using above code. In the DecryptStringPassword() function, last line, return (new StreamReader(csD)).ReadToEnd(); gives the exception.

Same password I encrypted using AES encryption, it gives me "xwYgqg8+xnynU7MpceOoJ70HuRIIw+OkcDPBVa18mLw=" such a big encrypted password compared to base64 encoding which does not give any exception while decoding.

Is there any limit that base64 encoding gives always 24 characters long encrypted string or any length restriction on the string to be encrypted.

Our one of the users facing this issue while decoding the password. Rest all users have no problem while decoding.

1
"padding is invalid and can not be removed" is a message relating to encryption (here, AES), not related to base 64 encoding. - Damien_The_Unbeliever
This clearly is an XY problem, but I don't see any reason to close it because of that. - Maarten Bodewes
Warning the methods GetProvider and GetKey both display horrible cryptographic practices and should not be used (repetition of key material, static IV's, badly named / unnecessary variables, the unnecessary use of lists for key material, etc.). Using Encoding.Default.GetBytes to get the key bytes is a terrible idea as well, keys are not strings. - Maarten Bodewes

1 Answers

2
votes

No limits to the string to be encoded ("encryption" is a different process) and no limit on the result that depends on how many characters you put in (consider that base64 is used to encode the body of files sent throught http, so, no limits).

The padding the error is referring to are those = appended to the end of the string. The Base64 encoding takes your original message, divides it in bytes and then groups the bit stream in segments of 6 bits before converting it to an alphanumeric representation. Those = are added when the bit length of your message is not divisible by 6.

My guess is that something is happening to that string before it reaches Convert.FromBase64String or the base64 encoding (missing in your sample) does not even work.

I'd write a minimal example with just the base64 encoding/decoding to verify that this part is working.