I am using the following function to encrypt a string:
public string Encrypt(string stringToEncrypt, string SEncryptionKey)
{
try {
key = System.Text.Encoding.UTF8.GetBytes(Strings.Left(SEncryptionKey, 8));
DESCryptoServiceProvider des = new DESCryptoServiceProvider();
byte[] inputByteArray = Encoding.UTF8.GetBytes(stringToEncrypt);
MemoryStream ms = new MemoryStream();
CryptoStream cs = new CryptoStream(ms, des.CreateEncryptor(key, IV), CryptoStreamMode.Write);
cs.Write(inputByteArray, 0, inputByteArray.Length);
cs.FlushFinalBlock();
return Convert.ToBase64String(ms.ToArray());
} catch (Exception e) {
return e.Message;
}
}
I am wondering if there is some kind of mathematical algorithm that will allow me to determine, in advance, what the length of the Base64 encrypted string length will be. So if my string is 15 characters long, what will the length of the Base64 encrypted string be?
A 15 character string will be at least 15 bytes. It could become 20 or even 30 if you have a lot of non-ASCII characters.
The Encryption will round it up to a multiple of the
keyblock size, lets say 64 bytes.Then Base64 goes to encode 8 bit bytes into 6 bit tokens, so you get (64 * 8) / 6 tokens (chars).