编码系列--Base32编码的完整示例

原创 2007年10月02日 11:48:00
 0、前言

几年前由于做一些和加密有关的工作,所以写了几个编码的示例,包括Base32Base24,当年示例代码写得比较随手,而且不是完整的部分,仅演示编码解码的过程,存在不少问题。Blog很少打理,发觉不少朋友对这部分比较感兴趣,不更新一下有误导大家的嫌疑呀,所以重新更新一下示例,希望对大家有帮助,同时也希望和大家讨论是否有更有效的算法。

 

1、完整的示例

再次声明,代码中不保证没有错误,如果给各位的工作代码了困扰,本人只能深表歉意。同时也希望各位问题如果发现错误,请及时指正。

 

编码函数

        public static String Encode(Byte[] abData)
        
{
            Int32 dwLoop 
= 0, dwCharIndex = 0, dwCharCount;
            Char[] acPart 
= null;
            StringBuilder sbOutput 
= null;

            
if (abData == null || m_acBaseMap == null || m_acBaseMap.Length < MapLength)
                
return null;

            
try
            
{
                dwCharCount 
= (Int32)(abData.Length / 5f * 8f) + 1;
                sbOutput 
= new StringBuilder(dwCharCount);
                acPart 
= new Char[8];
            }

            
catch (Exception e)
            
{
                Trace.WriteLine(
"CBase32.Encode: Initialize buffer failed! " + e.Message);
            }

            
if (acPart == null || sbOutput == null)
                
return null;

            dwCharCount 
= 0;
            
for (dwLoop = 0; dwLoop < abData.Length; dwLoop += 5)
            
{
                Array.Clear(acPart, 
0, acPart.Length);
                
// every 5 bytes is a unit,can convert to 8 chars
                
// data format:
                
//            AAAAABBB BBCCCCCD DDDDEEEE EFFFFFGG GGGHHHHH
                switch (abData.Length - dwLoop)
                
{
                    
case 1:
                        dwCharIndex 
= abData[dwLoop] >> 3;                // AAAAA
                        acPart[0= m_acBaseMap[dwCharIndex];
                        dwCharIndex 
= (abData[dwLoop] & 0x7<< 2;        // BBB00
                        acPart[1= m_acBaseMap[dwCharIndex];
                        dwCharCount 
= 2;
                        
break;

                    
case 2:
                        dwCharIndex 
= abData[dwLoop] >> 3;                // AAAAA
                        acPart[0= m_acBaseMap[dwCharIndex];
                        dwCharIndex 
= ((abData[dwLoop] & 0x7<< 2+ (abData[dwLoop + 1>> 6);    // BBBBB
                        acPart[1= m_acBaseMap[dwCharIndex];
                        dwCharIndex 
= (abData[dwLoop + 1& 0x3F>> 1;    // CCCCC
                        acPart[2= m_acBaseMap[dwCharIndex];
                        dwCharIndex 
= abData[dwLoop + 1& 0x1;            // D0000
                        acPart[3= m_acBaseMap[dwCharIndex];
                        dwCharCount 
= 4;
                        
break;

                    
case 3:
                        dwCharIndex 
= abData[dwLoop] >> 3;                // AAAAA
                        acPart[0= m_acBaseMap[dwCharIndex];
                        dwCharIndex 
= ((abData[dwLoop] & 0x7<< 2+ (abData[dwLoop + 1>> 6);    // BBBBB
                        acPart[1= m_acBaseMap[dwCharIndex];
                        dwCharIndex 
= (abData[dwLoop + 1& 0x3F>> 1;    // CCCCC
                        acPart[2= m_acBaseMap[dwCharIndex];
                        dwCharIndex 
= ((abData[dwLoop + 1& 0x1<< 4+ (abData[dwLoop + 2>> 4);// DDDDD
                        acPart[3= m_acBaseMap[dwCharIndex];
                        dwCharIndex 
= (abData[dwLoop + 2& 0xF<< 1;    // EEEE0
                        acPart[4= m_acBaseMap[dwCharIndex];
                        dwCharCount 
= 5;
                        
break;

                    
case 4:
                        dwCharIndex 
= abData[dwLoop] >> 3;                // AAAAA
                        acPart[0= m_acBaseMap[dwCharIndex];
                        dwCharIndex 
= ((abData[dwLoop] & 0x7<< 2+ (abData[dwLoop + 1>> 6);    // BBBBB
                        acPart[1= m_acBaseMap[dwCharIndex];
                        dwCharIndex 
= (abData[dwLoop + 1& 0x3F>> 1;    // CCCCC
                        acPart[2= m_acBaseMap[dwCharIndex];
                        dwCharIndex 
= ((abData[dwLoop + 1& 0x1<< 4+ (abData[dwLoop + 2>> 4);// DDDDD
                        acPart[3= m_acBaseMap[dwCharIndex];
                        dwCharIndex 
= ((abData[dwLoop + 2& 0xF<< 1+ (abData[dwLoop + 3>> 7);// EEEEE
                        acPart[4= m_acBaseMap[dwCharIndex];
                        dwCharIndex 
= (abData[dwLoop + 3& 0x7F>> 2;    // FFFFF
                        acPart[5= m_acBaseMap[dwCharIndex];
                        dwCharIndex 
= (abData[dwLoop + 3& 0x3<< 3;    // GG000
                        acPart[6= m_acBaseMap[dwCharIndex];
                        dwCharCount 
= 7;
                        
break;

                    
default:        // >= 5
                        dwCharIndex = abData[dwLoop] >> 3;                // AAAAA
                        acPart[0= m_acBaseMap[dwCharIndex];
                        dwCharIndex 
= ((abData[dwLoop] & 0x7<< 2+ (abData[dwLoop + 1>> 6);    // BBBBB
                        acPart[1= m_acBaseMap[dwCharIndex];
                        dwCharIndex 
= (abData[dwLoop + 1& 0x3F>> 1;    // CCCCC
                        acPart[2= m_acBaseMap[dwCharIndex];
                        dwCharIndex 
= ((abData[dwLoop + 1& 0x1<< 4+ (abData[dwLoop + 2>> 4);// DDDDD
                        acPart[3= m_acBaseMap[dwCharIndex];
                        dwCharIndex 
= ((abData[dwLoop + 2& 0xF<< 1+ (abData[dwLoop + 3>> 7);// EEEEE
                        acPart[4= m_acBaseMap[dwCharIndex];
                        dwCharIndex 
= (abData[dwLoop + 3& 0x7F>> 2;    // FFFFF
                        acPart[5= m_acBaseMap[dwCharIndex];
                        dwCharIndex 
= ((abData[dwLoop + 3& 0x3<< 3+ (abData[dwLoop + 4>> 5);// GGGGG
                        acPart[6= m_acBaseMap[dwCharIndex];
                        dwCharIndex 
= abData[dwLoop + 4& 0x1F;        // HHHHH
                        acPart[7= m_acBaseMap[dwCharIndex];
                        dwCharCount 
= 8;
                        
break;
                }


                sbOutput.Append(acPart, 
0, dwCharCount);
            }


            
return sbOutput.ToString();
        }

 

解码函数

 

        public static Byte[] Decode(String sData)
        {
            Int32 dwLoop 
= 0, dwLength = 0;
            Int32[] dwCharIndex 
= null;
            Byte[] abOutput 
= null;
            Char[] acInput 
= null;

            
if (sData == null || sData == String.Empty)
                
return null;

            acInput 
= sData.ToCharArray();
            
if (acInput == null)
                
return null;

            
try
            {
                dwLength 
= (Int32) (acInput.Length / 8f * 5f) + 1;
                abOutput 
= new Byte[dwLength];
                dwCharIndex 
= new Int32[8];
            }
            
catch (Exception e)
            {
                Trace.WriteLine(
"CBase32.Decode: Initialize buffer failed! " + e.Message);
            }
            
if (acInput == null)
                
return null;

            dwLength 
= 0;
            
for (dwLoop = 0; dwLoop < acInput.Length; dwLoop += 8)
            {
                Array.Clear(dwCharIndex, 
0, dwCharIndex.Length);
                
// 根据Encode计算,这里差值的长度只可能是2、4、5、7、8
                switch (acInput.Length - dwLoop)  
                {
                    
case 2:
                        dwCharIndex[
0= GetCharIndex(acInput[dwLoop]);
                        dwCharIndex[
1= GetCharIndex(acInput[dwLoop + 1]);

                        abOutput[dwLength] 
= (Byte)((dwCharIndex[0<< 3+ (dwCharIndex[1>> 2));
                        abOutput[dwLength 
+ 1= (Byte)((dwCharIndex[1& 0x3<< 6);
                        
break;

                    
case 4:
                        dwCharIndex[
0= GetCharIndex(acInput[dwLoop]);
                        dwCharIndex[
1= GetCharIndex(acInput[dwLoop + 1]);
                        dwCharIndex[
2= GetCharIndex(acInput[dwLoop + 2]);
                        dwCharIndex[
3= GetCharIndex(acInput[dwLoop + 3]);

                        abOutput[dwLength] 
= (Byte)((dwCharIndex[0<< 3+ (dwCharIndex[1>> 2));
                        abOutput[dwLength 
+ 1= (Byte)(((dwCharIndex[1& 0x3<< 6+ (dwCharIndex[2<< 1+ (dwCharIndex[3>> 4));
                        abOutput[dwLength 
+ 2= (Byte)((dwCharIndex[3& 0xF<< 4);
                        
break;

                    
case 5:
                        dwCharIndex[
0= GetCharIndex(acInput[dwLoop]);
                        dwCharIndex[
1= GetCharIndex(acInput[dwLoop + 1]);
                        dwCharIndex[
2= GetCharIndex(acInput[dwLoop + 2]);
                        dwCharIndex[
3= GetCharIndex(acInput[dwLoop + 3]);
                        dwCharIndex[
4= GetCharIndex(acInput[dwLoop + 4]);

                        abOutput[dwLength] 
= (Byte)((dwCharIndex[0<< 3+ (dwCharIndex[1>> 2));
                        abOutput[dwLength 
+ 1= (Byte)(((dwCharIndex[1& 0x3<< 6+ (dwCharIndex[2<< 1+ (dwCharIndex[3>> 4));
                        abOutput[dwLength 
+ 2= (Byte)(((dwCharIndex[3& 0xF<< 4+ (dwCharIndex[4>> 1));
                        abOutput[dwLength 
+ 3= (Byte)(((dwCharIndex[4& 0x1<< 7));
                        
break;

                    
case 7:
                        dwCharIndex[
0= GetCharIndex(acInput[dwLoop]);
                        dwCharIndex[
1= GetCharIndex(acInput[dwLoop + 1]);
                        dwCharIndex[
2= GetCharIndex(acInput[dwLoop + 2]);
                        dwCharIndex[
3= GetCharIndex(acInput[dwLoop + 3]);
                        dwCharIndex[
4= GetCharIndex(acInput[dwLoop + 4]);
                        dwCharIndex[
5= GetCharIndex(acInput[dwLoop + 5]);
                        dwCharIndex[
6= GetCharIndex(acInput[dwLoop + 6]);

                        abOutput[dwLength] 
= (Byte)((dwCharIndex[0<< 3+ (dwCharIndex[1>> 2));
                        abOutput[dwLength 
+ 1= (Byte)(((dwCharIndex[1& 0x3<< 6+ (dwCharIndex[2<< 1+ (dwCharIndex[3>> 4));
                        abOutput[dwLength 
+ 2= (Byte)(((dwCharIndex[3& 0xF<< 4+ (dwCharIndex[4>> 1));
                        abOutput[dwLength 
+ 3= (Byte)(((dwCharIndex[4& 0x1<< 7+ (dwCharIndex[5<< 2+ (dwCharIndex[6>> 3));
                        abOutput[dwLength 
+ 4= (Byte)((dwCharIndex[6& 0x7<< 5);
                        
break;

                    
default:
                        dwCharIndex[
0= GetCharIndex(acInput[dwLoop]);
                        dwCharIndex[
1= GetCharIndex(acInput[dwLoop + 1]);
                        dwCharIndex[
2= GetCharIndex(acInput[dwLoop + 2]);
                        dwCharIndex[
3= GetCharIndex(acInput[dwLoop + 3]);
                        dwCharIndex[
4= GetCharIndex(acInput[dwLoop + 4]);
                        dwCharIndex[
5= GetCharIndex(acInput[dwLoop + 5]);
                        dwCharIndex[
6= GetCharIndex(acInput[dwLoop + 6]);
                        dwCharIndex[
7= GetCharIndex(acInput[dwLoop + 7]);

                        abOutput[dwLength] 
= (Byte)((dwCharIndex[0<< 3+ (dwCharIndex[1>> 2));
                        abOutput[dwLength 
+ 1= (Byte)(((dwCharIndex[1& 0x3<< 6+ (dwCharIndex[2<< 1+ (dwCharIndex[3>> 4));
                        abOutput[dwLength 
+ 2= (Byte)(((dwCharIndex[3& 0xF<< 4+ (dwCharIndex[4>> 1));
                        abOutput[dwLength 
+ 3= (Byte)(((dwCharIndex[4& 0x1<< 7+ (dwCharIndex[5<< 2+ (dwCharIndex[6>> 3));
                        abOutput[dwLength 
+ 4= (Byte)(((dwCharIndex[6& 0x7<< 5+ dwCharIndex[7]);
                        
break;
                }
                dwLength 
+= 5;
            }

            
return abOutput;
        }

 

 完整的示例见附件(代码居然没法贴完整的类,郁闷)

 

 

相关文章推荐

Base64 | Base32 | Base16编码和解码小结

1.Base64简介 Base64是一种基于64个可打印字符来表示二进制数据的表示方法。base64编码一般有下面两个用途: a)所有的二进制文件,都可以因此转化为可打印的文本编码(都变成ASCI...

BASE64编码简介

BASE64是一种编码方式,通常用于把二进制数据编码为可写的字符形式的数据。 这是一种可逆的编码方式。 编码后的数据是一个字符串,其中包含的字符为:A-Z、a-z、0-9、+、/ 共64个字符...

BASE32编码--记录

BASE32编码 --记录 一、Base32数据编码简介     Base32这种数据编码机制,主要用来把二进制数据编码成可见的字符串,其编码规则是:任意给定一个二进制数据,以5个位(bit)为一...

Base32编码

分类: 开发心得2004-09-07 14:10 7741人阅读 评论(11) 收藏 举报 bytenullexceptionstringmodulesystem 0、写在前面...

无聊写的base32编码

  • 2011年02月23日 10:39
  • 2KB
  • 下载

磁力链接的BASE32编码向HEX编码的转换

动漫爱好者在“花园”使用磁链时,会发现有些网盘无法识别其提供的磁链。本文介绍了传统格式的磁链与花园使用的变种磁链的转换方法。...
  • sugar13
  • sugar13
  • 2016年03月02日 19:35
  • 1511

c语言实现base64编码解码

  • 2015年04月20日 18:26
  • 4KB
  • 下载

Base64编码实例详解

  • 2016年06月26日 00:28
  • 32KB
  • 下载
内容举报
返回顶部
收藏助手
不良信息举报
您举报文章:编码系列--Base32编码的完整示例
举报原因:
原因补充:

(最多只允许输入30个字)