编码系列--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...
  • wangjianno2
  • wangjianno2
  • 2016年09月07日 23:22
  • 10291

如何用python解码base32/base64

base32/base64是一种常用的加密方式,拿到base64的密文后,我们虽然可以在某些网站上解码。但在没网络下的情况,我们可以运用python进行base32/base64解码 ”’pytho...
  • just_h
  • just_h
  • 2017年05月28日 22:15
  • 1688

base32解码器

  • 2017年11月02日 19:02
  • 8KB
  • 下载

开源-base64、base32、base16 的编码与解码

  • 2012年06月30日 07:41
  • 22KB
  • 下载

BASE32编码 --记录

Base32
  • instruder
  • instruder
  • 2010年10月25日 08:36
  • 9825

Base32加解密算法与实战

通过孤水绕城大牛的推荐,小菜我在玩某一黑客游戏时,其中一关碰到一个密文,看样子很像base64的编码,但通过base64解码出现乱码。后来考虑是Md5(base64),于是又通过base64解码再转1...
  • qindehong
  • qindehong
  • 2014年04月13日 20:43
  • 2145

使用 Base 32 数字编码(DES加密解密)

  • 2008年11月07日 15:28
  • 17KB
  • 下载

Base32 解码以及加密

此原创: 仅供参考。    向网上大神,共享成果,致敬。 理论: 编码: 将输入字符串的每个字符转成ASCII码;以5个bit为一组进行二进制分割,最后一个字符如果不足5位的话就补零...
  • qq244337987
  • qq244337987
  • 2013年06月03日 11:46
  • 2883

Base32编码

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

编码系列--Base32编码

0、写在前面这里讨论的编码主要的目的是将不可显示的二进制数组转变为可显示的字符串,包括其逆运算。通过特定的协议传输数据,或者加密解密的时候都会用到类似的方法。在这类运算中用的比较多的是Base64,比...
  • Mittermeyer
  • Mittermeyer
  • 2004年09月07日 14:10
  • 22449
内容举报
返回顶部
收藏助手
不良信息举报
您举报文章:编码系列--Base32编码的完整示例
举报原因:
原因补充:

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