简体   繁体   English

将字符串编码到base-64和从base-64编码

[英]Encoding strings to and from base-64

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.Unicode and System.Text.Encoding.UTF8 我已经尝试过使用System.Text.Encoding.UnicodeSystem.Text.Encoding.UTF8

What could be the problem? 可能是什么问题呢? Does anyone have a proper solution? 有没有人有适当的解决方案?

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 . 首先使用base64字符串,获取字节,然后再次对其进行编码 If you want to compare you will need to begin with the original string. 如果你想比较,你需要从原始字符串开始。

If text is 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 . 这将把它作为一个string Note, though, that this scenario is only useful for representing unicode text in an ascii format. 但请注意,此方案仅对以ASCII格式表示unicode文本有用。 Normally you wouldn't base-64 encode it if the original contents are string . 通常,如果原始内容是string ,则不会对其进行base-64编码。

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. myBase64String1将等于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. 您将需要使用其他方法将数据类型转换为Byte数组,反之则返回以获取数据类型。 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. 我用它来将类的内容转换为字节数组,然后转换为Base64字符串并将字符串写入文件系统。 Later I read it back into a class instance by reversing the process. 后来我通过反转过程将其读回到类实例中。

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;
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM