Why does C # Convert.ToBase64String () give me 88 as length when I pass 64 bytes?

I am trying to understand the following:

If I declare 64 bytes as the length of the array (buffer). When I convert to a base 64 string, it says that the length is 88. Should the length not be 64, since I transmit 64 bytes? I might not fully understand how this works. If so, could you explain.

//Generate a cryptographic random number RNGCryptoServiceProvider rng = new RNGCryptoServiceProvider(); // Create byte array byte[] buffer = new byte[64]; // Get random bytes rng.GetBytes(buffer); // This line gives me 88 as a result. // Shouldn't it give me 64 as declared above? throw new Exception(Convert.ToBase64String(buffer).Length.ToString()); // Return a Base64 string representation of the random number return Convert.ToBase64String(buffer); 
+6
source share
3 answers

Not. Base-64 encoding uses a whole byte to represent six bits of encoded data. Lost two bits is the price of using only alphanumeric, plus and slashes as your characters (mostly excluding numbers representing invisible or special characters in plain ASCII / UTF-8 encoding). The result you get is (64 * 4/3), rounded to the nearest border of 4 bytes.

+8
source

Base64 encoding converts 3 octets to 4 encoded characters; so

(64/3) * 4 ≈ (22 * 4) = 88 bytes.

Read here.

+6
source

Should the length be only 64 since I am transmitting 64 bytes?

Not. You are missing 64 tokens in Base256 notation. Base64 has less token information, so it needs more tokens. 88 sounds right.

+2
source

Source: https://habr.com/ru/post/907432/


All Articles