Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Does Convert.ToBase64String return the same length as it's source byte array?

I don't know if i am asking a silly question but I want to know whether Convert.ToBase64String function in .NET returns the same length as it's source byte size or is it different? I wanted to try out the article from MSDN itself How To: Use Forms Authentication with SQL Server 2000 to hash my password but I found out that the function they used to create salt string is returning 3 more length than it is supposed to return. To clarify here is the code in that article.

private static string CreateSalt(int size)
{
   // Generate a cryptographic random number using the cryptographic
   // service provider
   RNGCryptoServiceProvider rng = new RNGCryptoServiceProvider();
   byte[] buff = new byte[size];
   rng.GetBytes(buff);
   // Return a Base64 string representation of the random number
   return Convert.ToBase64String(buff);
}
like image 916
Myat Htut Avatar asked May 12 '09 23:05

Myat Htut


People also ask

Does converting to Base64 increase size?

Encoded size increase This means that the Base64 version of a string or file will be at least 133% the size of its source (a ~33% increase). The increase may be larger if the encoded data is small.

Is Base64 encoding always the same?

The short answer is yes, unique binary/hex values will always encode to a unique base64 encoded string. BUT, multiple base64 encoded strings may represent a single binary/hex value. This is because hex bytes are not aligned with base64 'digits'.

What is difference between Base64 and byte array?

Base64 is apparently wasteful because we use just 64 different values per byte, whereas a byte can represent 256 different characters. That is, we use bytes (which are 8-bit words) as 6-bit words. There is a waste of 2 bits for each 8 bits of transmission data.

How do I convert ToBase64String?

ToBase64String(Byte[], Int32, Int32) Converts a subset of an array of 8-bit unsigned integers to its equivalent string representation that is encoded with base-64 digits. Parameters specify the subset as an offset in the input array, and the number of elements in the array to convert.


2 Answers

No, Base64 returns 4 bytes output for 3 bytes input, rounded up (padded with =) to the next 4-byte boundary.

int outputLength = ((inputLength+2)/3)*4

That's because it only uses 6 bits (basically a number 0-63) per byte in order to only use ASCII chars which are not control chars and in the 7-bit range. Therefore, you get 3*8 => 4*6 bits when encoding data with Base64.

like image 180
Lucero Avatar answered Sep 24 '22 23:09

Lucero


base-64 rarely returns a string of the same length as the input. Essentially, it is only using 6 of the available 8 bits, so large messages (in particular) will require an extra 1/3 volume. There are a few packing bytes (usually "=") at the end to make the message unambiguous.

like image 43
Marc Gravell Avatar answered Sep 21 '22 23:09

Marc Gravell