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.
GetProviderandGetKeyboth 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.). UsingEncoding.Default.GetBytesto get the key bytes is a terrible idea as well, keys are not strings. - Maarten Bodewes