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.
我不知道我是否在问一个愚蠢的问题,但我想知道.NET中的Convert.ToBase64String函数是否返回与源字节大小相同的长度或者它是否不同?我想尝试MSDN本身的文章如何:使用SQL Server 2000的表单身份验证来哈希我的密码,但我发现他们用来创建salt字符串的函数返回的长度比它应该返回的长3。这里要澄清的是该文章中的代码。
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);
}
3 个解决方案
#1
The base64 encoding of a byte string is longer than the byte string because that byte string has 2^8 possibilities per "location", while a base 64 string has only 2^6 possibilities per location (that's why we call it base 64).
字节字符串的base64编码比字节字符串长,因为该字节字符串每个“位置”有2 ^ 8种可能性,而基本64字符串每个位置只有2 ^ 6种可能性(这就是我们称之为base 64的原因)。
Just think of the logarithms and pigeon holes. Take the number 5000. How many locations (pigeon holes, bytes) do you need in order to store it in base 256?
想想对数和鸽子洞。取数字5000.为了将它存储在256基数中,你需要多少个位置(鸽子洞,字节)?
"Locations in base256" = ceil(log_2(5000) / 8) = ceil(1.54) = 2
Where log_2 tells you how many bits you need. Now how many in base64?
log_2告诉您需要多少位。现在base64有多少?
"Locations in base64" = ceil(log_2(5000) / 6) = ceil(2.04) = 3
#2
No, Base64 returns 4 bytes output for 3 bytes input, rounded up (padded with =) to the next 4-byte boundary.
不,Base64为3字节输入返回4字节输出,向上舍入(用=填充)到下一个4字节边界。
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.
这是因为它每个字节仅使用6位(基本上是0-63),以便仅使用不是控制字符且在7位范围内的ASCII字符。因此,使用Base64编码数据时,您将获得3 * 8 => 4 * 6位。
#3
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.
base-64很少返回与输入长度相同的字符串。本质上,它仅使用6个可用的8位,因此大的消息(特别是)需要额外的1/3音量。最后有一些打包字节(通常是“=”)使消息明确无误。
#1
The base64 encoding of a byte string is longer than the byte string because that byte string has 2^8 possibilities per "location", while a base 64 string has only 2^6 possibilities per location (that's why we call it base 64).
字节字符串的base64编码比字节字符串长,因为该字节字符串每个“位置”有2 ^ 8种可能性,而基本64字符串每个位置只有2 ^ 6种可能性(这就是我们称之为base 64的原因)。
Just think of the logarithms and pigeon holes. Take the number 5000. How many locations (pigeon holes, bytes) do you need in order to store it in base 256?
想想对数和鸽子洞。取数字5000.为了将它存储在256基数中,你需要多少个位置(鸽子洞,字节)?
"Locations in base256" = ceil(log_2(5000) / 8) = ceil(1.54) = 2
Where log_2 tells you how many bits you need. Now how many in base64?
log_2告诉您需要多少位。现在base64有多少?
"Locations in base64" = ceil(log_2(5000) / 6) = ceil(2.04) = 3
#2
No, Base64 returns 4 bytes output for 3 bytes input, rounded up (padded with =) to the next 4-byte boundary.
不,Base64为3字节输入返回4字节输出,向上舍入(用=填充)到下一个4字节边界。
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.
这是因为它每个字节仅使用6位(基本上是0-63),以便仅使用不是控制字符且在7位范围内的ASCII字符。因此,使用Base64编码数据时,您将获得3 * 8 => 4 * 6位。
#3
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.
base-64很少返回与输入长度相同的字符串。本质上,它仅使用6个可用的8位,因此大的消息(特别是)需要额外的1/3音量。最后有一些打包字节(通常是“=”)使消息明确无误。