c# - 将字符串编码为 base-64 或从 base-64 编码

标签 c# string base64

我正在尝试从 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;

我已经尝试过我的方法来做到这一点,但我似乎没有得到正确的结果。我已经尝试过 System.Text.Encoding.UnicodeSystem.Text.Encoding.UTF8

可能是什么问题?有没有人有合适的解决方案?

最佳答案

string text = base64string.... //Here I have a base-64 string.
byte[] encodedByte = System.Text.ASCIIEncoding.ASCII.GetBytes(text);
string base64Encoded = Convert.ToBase64String(encodedByte);

您正在对字符串进行双重编码。您从 base64 字符串开始,获取字节,然后再次对其进行编码。如果要进行比较,则需要从原始字符串开始。

关于c# - 将字符串编码为 base-64 或从 base-64 编码,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/9878449/

相关文章:

java - 是否有任何内置实用程序可以检查 "null"字符串而不是 null?

javascript - 我如何使用 for in 循环遍历嵌套对象并返回连接的每个属性字符串?

url - Url 查询字符串的最佳压缩算法

c# - 使用 LINQ 进行简单的语言识别

c# - 使用提供程序 'RsaProtectedConfigurationProvider' 解密失败?

python - 如何解决读取 csv 的问题,该文件是分号分隔的文件,其中某些字符串包含分号?

java - 将 MD5 - base64 从 JAVA 转换为 PHP

c# - 数字大于 255 的 Convert.ToBase64String()

c# - Garbage Collector、call & callvirt 和 Debug/Release 代码模式执行差异

c# - 下载最新版本的 CodePlex 项目