Maximum Base64 encoded data size

I have a security mechanism that implements the symmetric RijndaelManaged algorithm. I managed to find information about the maximum size of encrypted data using RijndaelManaged for a specific IV. According to my calculations, it will be 128 bytes. However, I need to convert these 128 bytes to a string using Base64. Is there a way to calculate the maximum number of characters Base64 will use to encode an input byte array of size 128?

Thanks Pawel

+4
source share
4 answers

Absolutely - Base64 accepts 4 characters to represent every 3 bytes. (Padding applies to binary data that is not an exact multiple of 3 bytes.) Thus, 128 bytes will always be 172 characters. (The way to solve this issue is that base64 represents 6 bits in each character (2 6 = 64), so 3 bytes = 24 bits = 4 base 64 characters.)

+10
source

Basic encoded string 64 will use 4 characters for every 3 bytes (or part of it). Thus, 128 bytes will contain 172 base 64 characters.

+4
source

If you need to check this programmatically, you can do this by checking the module. Here's some psudocode (no specific language):

  function base64Inflation (numBytes)
     minimumBase64Bytes = roundDown (numBytes / 3 * 4)    
     modulus = numberOfBytes% 3 // Assuming% is the modulo operator
     if modulus == 0 
         return minimumBase64Bytes // Exact fit!  No padding required.
     else
         return minimumBase64Bytes + 4 // Doesn't quite fit.  We need to pad.

I also implemented the same logic in golang:

http://play.golang.org/p/JK9XPAle5_

0
source

In Java :

byte[] bytes = new byte[128]; int base64Length = bytes.length / 3 * 4; // Strictly integer division if (bytes.length % 3 != 0) { base64Length += 4; // Extra padding characters will be added } System.out.println(bytes.length + " bytes will be encoded in " + base64Length + " characters."); 

So where the input is bytes.length == 128 , the output will be base64Length == 172 characters.

0
source

All Articles