C# 将字符串编码为 base-64 或从 base-64 编码
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/9878449/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Encoding strings to and from base-64
提问by Erik Larsson
I'm trying to encode some strings back and forth from base-64 string and I'm having truble to get the right result.
我正在尝试从 base-64 字符串来回编码一些字符串,但我无法获得正确的结果。
string text = base64string.... //Here I have a base-64 string.
byte[] encodedByte = System.Text.ASCIIEncoding.ASCII.GetBytes(text);
string base64Encoded = Convert.ToBase64String(encodedByte);
if (text == base64Encoded) //If the new encoded string is equal to its original value
return base64Encoded;
I have tried my ways to do this and I don't seem to get the right result. I have tried both with System.Text.Encoding.Unicodeand System.Text.Encoding.UTF8
我已经尝试了我的方法来做到这一点,但我似乎没有得到正确的结果。我已经尝试过System.Text.Encoding.Unicode和System.Text.Encoding.UTF8
What could be the problem? Does anyone have a proper solution?
可能是什么问题呢?有没有人有适当的解决方案?
回答by Ed S.
string text = base64string.... //Here I have a base-64 string.
byte[] encodedByte = System.Text.ASCIIEncoding.ASCII.GetBytes(text);
string base64Encoded = Convert.ToBase64String(encodedByte);
You are double encoding the string. You begin with a base64 string, get the bytes, and then encode it again. If you want to compare you will need to begin with the original string.
您正在对字符串进行双重编码。您从 base64 字符串开始,获取字节,然后再次对其进行编码。如果要比较,则需要从原始字符串开始。
回答by Marc Gravell
If textis a base-64 string, then you are doing it backwards:
如果text是 base-64 字符串,那么您是在反向操作:
byte[] raw = Convert.FromBase64String(text); // unpack the base-64 to a blob
string s = Encoding.UTF8.GetString(raw); // assume the blob is UTF-8, and
// decode to a string
which will get you it as a string. Note, though, that this scenario is only useful for representing unicode text in an ascii format. Normally you wouldn't base-64 encode it if the original contents are string.
这将使您将其作为string. 但请注意,此方案仅适用于以 ascii 格式表示 unicode 文本。通常,如果原始内容是string.
回答by code4life
You have the encoding code correctly laid out. To confirm whether the base64-encoded string is correct, you can try decoding it and comparing the decoded contents to the original:
您已正确布置编码代码。要确认base64编码的字符串是否正确,您可以尝试对其进行解码并将解码后的内容与原始内容进行比较:
var decodedBytes = Convert.FromBase64String(base64encoded);
var compareText = System.Text.Encoding.ASCII.GetString(decodedText);
if (text == compareText)
{
// carry on...
return base64encoded;
}
回答by trancify
Convert whatever it is that you need in Base64 into a Byte array then use the FromBase64String and ToBase64String to convert to and from Base64:
将 Base64 中需要的任何内容转换为 Byte 数组,然后使用 FromBase64String 和 ToBase64String 与 Base64 进行转换:
Byte[] buffer = Convert.FromBase64String(myBase64String1);
myBase64String2 = Convert.ToBase64String(buffer);
myBase64String1 will be equal to myBase64String2. You will need to use other methods to get your data type into a Byte array and the reverse to get your data type back. I have used this to convert the content of a class into a byte array and then to Base64 string and write the string to the filesystem. Later I read it back into a class instance by reversing the process.
myBase64String1 将等于 myBase64String2。您将需要使用其他方法将您的数据类型放入 Byte 数组中,反之亦然以获取您的数据类型。我使用它将类的内容转换为字节数组,然后转换为 Base64 字符串并将字符串写入文件系统。后来我通过反转这个过程将它读回一个类实例。

