I have a security mechanism that implements symmetric algorithm RijndaelManaged. I managed to find information what is the maximum size of encrypted data using RijndaelManaged for particular IV. According to my calculations it will be 128 bytes. However I need to convert these 128 bytes to string using Base64. Is there a way to calculate maximum number of chars that Base64 encoding will use to encode input byte array of size 128?
Thanks,Pawel
Absolutely - Base64 takes 4 characters to represent every 3 bytes. (Padding is applied for binary data which isn't an exact multiple of 3 bytes.) So 128 bytes will always be 172 characters. (The way to work this out is that base64 represents 6 bits in each character (26 = 64); therefore 3 bytes = 24 bits = 4 base-64 characters.)
If you need to check this programatically, you can do so by checking the modulus. Here's some psudocode (no particular language) :
I've also implemented the same logic in golang:
http://play.golang.org/p/JK9XPAle5_
In Java:
So, where the input
bytes.length == 128
, the output will bebase64Length == 172
characters.A base 64 encoded string will use 4 characters for every 3 bytes (or part thereof). So 128 bytes would be 172 base 64 characters.