使用8位字节的编码格式将字节流安全的转换成String

我们常用的编码格式有ASCII,Unicode,UTF-8,GB2312等,如何在这些编码之间安全转换呢?

最近做邮件系统,采用了OpenPOP组件,这是老外写的,没想到用到中文环境中,出了很多问题,主要就是编码问题。

通常,邮件内容都会经过Base64编码,在邮件接收端,需要对其解码,得到字节流,再进一步解码为正确的字符串,如 Base64.cs文件中:

 

复制代码
 public static class Base64
    {
        
public  static byte[] DecodeToBytes(string strText)
        {
            
try
            {
                
return Convert.FromBase64String(strText);
            }
            
catch (Exception e)
            {
                Utility.LogError(
"decodeToBytes:" + e.Message);
                
                
return Encoding.Default.GetBytes("\0");
            }
        }

        
/// <summary>
        
/// Decoded a Base64 encoded string using the Default encoding of the system
        
/// </summary>
        
/// <param name="base64Encoded">Source string to decode</param>
        
/// <returns>A decoded string</returns>
        public static string Decode(string base64Encoded)
        {
            
//有可能因为二进制问题不能正确解码 dth,2010.12.15
            
//return Encoding.Default.GetString(DecodeToBytes(base64Encoded));
            
//ISO8859-1 字符串,8位,只有这种可以完整保留二进制
            Encoding _encoding = Encoding.GetEncoding(28591);
            
return _encoding.GetString(DecodeToBytes(base64Encoded));
        }

        
/// <summary>
        
/// Decoded a Base64 encoded string using a specified encoding
        
/// </summary>
        
/// <param name="base64Encoded">Source string to decode</param>
        
/// <param name="nameOfEncoding">The name of the encoding to use</param>
        
/// <returns>A decoded string</returns>
        public static string Decode(string base64Encoded, string nameOfEncoding)
        {
            
try
            {
                
return Encoding.GetEncoding(nameOfEncoding).GetString(DecodeToBytes(base64Encoded));
            }
            
catch(Exception e)
            {
                Utility.LogError(
"decode: " + e.Message);
                
return Decode(base64Encoded);
            }
        }
    }
复制代码

 

其中有一个方法Decode,这是原来的代码:

public static string Decode(string base64Encoded)
        {
            
   return Encoding.Default.GetString(DecodeToBytes(base64Encoded));
         
}

原作者使用了 Encoding.Default 编码格式来获取字符串,在英文环境或许没有问题,但如果发信方用的编码格式跟你不一样,这样就会出问题,比如对方是UTF-8编码,而自己的默认编码是GB2312。

另外一种情况就是对于Base64编码的二进制数据,比如邮件中的图片等,原代码的方式更是成问题,我们的Encoding.Default 编码会破坏原始的二进制字节信息,但这些信息又想作为字符串在系统中使用,该怎么办呢?

二进制字节都是8位编码的,只有采用8位编码格式的方案才可以完整保留二进制数据。在所有的系统编码中,ISO8859-1 是8位编码,所以我们采用它来作为系统中

byte[] <=> String

转换的桥梁,我对原始代码做了修改,成为下面的样子:

 public static string Decode(string base64Encoded)
        {
            
//有可能因为二进制问题不能正确解码 bluedoctor,2010.12.15
            
//return Encoding.Default.GetString(DecodeToBytes(base64Encoded));
            
//ISO8859-1 字符串,8位,只有这种可以完整保留二进制
            Encoding _encoding = Encoding.GetEncoding(28591);
            
return _encoding.GetString(DecodeToBytes(base64Encoded));
        }

对系统中所有类似的地方进行修改,OpenPOP组件终于可以安全的处理多种格式的邮件了。

 

posted on   深蓝医生  阅读(1917)  评论(0编辑  收藏  举报

编辑推荐:
· .NET Core 中如何实现缓存的预热?
· 从 HTTP 原因短语缺失研究 HTTP/2 和 HTTP/3 的设计差异
· AI与.NET技术实操系列:向量存储与相似性搜索在 .NET 中的实现
· 基于Microsoft.Extensions.AI核心库实现RAG应用
· Linux系列:如何用heaptrack跟踪.NET程序的非托管内存泄露
阅读排行:
· TypeScript + Deepseek 打造卜卦网站:技术与玄学的结合
· 阿里巴巴 QwQ-32B真的超越了 DeepSeek R-1吗?
· 【译】Visual Studio 中新的强大生产力特性
· 10年+ .NET Coder 心语 ── 封装的思维:从隐藏、稳定开始理解其本质意义
· 【设计模式】告别冗长if-else语句:使用策略模式优化代码结构

导航

< 2011年3月 >
27 28 1 2 3 4 5
6 7 8 9 10 11 12
13 14 15 16 17 18 19
20 21 22 23 24 25 26
27 28 29 30 31 1 2
3 4 5 6 7 8 9
点击右上角即可分享
微信分享提示