[愚翁专栏]如何使用Base64进行加密和解密

转载 2006年05月18日 20:06:00
 如何使用Base64进行加密和解密

其实Base64的加密和解密的算法不是很复杂,首先是定义自己64位的密钥,64个字符是固定,但是顺序是可以随自己的设计而变化。例如:

char[] BaseTable=new char[64]{

'A','B','C','D','E','F','G','H','I','J','K','L','M','N','O','P','Q','R','S','T','U','V','W','X','Y','Z',

       'a','b','c','d','e','f','g','h','i','j','k','l','m','n','o','p','q','r','s','t','u','v','w','x','y','z',

       '0','1','2','3','4','5','6','7','8','9','+','/'};

 

接下来,在加密的时候就是对于需要加密的每个字符,通过一定的算法,转换成上面64字符的一种;而在解密的时候则是把64字符中任意一个字符反算出加密前的字符。对于每个字符的操作有很多方法,这里就不一一介绍了。需要注意的是“=”字符,在Base64加密算法中,是很重要的,它是起到补零作用。

 

以下是完整代码:

//----------------------------- Base64 class --------------------------------------

//---------------------------------------------------------------------------------

//---File:clsBase64

//---Description:The class file to encode string or decode string in base algorith

//---Author:Knight

//---Date:Oct.8, 2005

//---------------------------------------------------------------------------------

//----------------------------{ Base64 class }-------------------------------------

using System;

 

namespace Base64

{

       /// <summary>

       /// Summary description for clsBase64.

       /// </summary>

       public class clsBase64

       {

              private char[] source;

              private char[] lookupTable;

              private int length, length2, length3;

              private int blockCount;

              private int paddingCount;

 

              public clsBase64()

              {

                     //

                     // TODO: Add constructor logic here

                     //

                     source = null;

                     length = length2 = length3 =0;

                     blockCount = 0;

                     paddingCount = 0;

              }

 

              /// <summary>

              /// Create base64 char array using default base64 char array

              /// </summary>

              /// <param name="CreatePara"></param>

              /// <returns>return the new base64 char array</returns>

              private char[] CreateBase64Char( ref char[] CreatePara )

              {

                     char[] BaseTable=new char[64]{

                                                                              'A','B','C','D','E','F','G','H','I','J','K','L','M','N','O','P','Q','R','S','T','U','V','W','X','Y','Z',

                                                                              'a','b','c','d','e','f','g','h','i','j','k','l','m','n','o','p','q','r','s','t','u','v','w','x','y','z',

                                                                              '0','1','2','3','4','5','6','7','8','9','+','/'};

                    

                     const int CREATE_TYPE = 8;

                     byte bParaValue = 0;

                     byte bTemp;

                     for( int i = 0; i < CreatePara.Length; i++ )

                     {

                            bTemp = ( byte )( CreatePara[i] );

                            switch( bTemp % CREATE_TYPE )

                            {

                                   case 1:

                                          // 00000001

                                          bTemp =( byte )( bTemp ^ 0x01 );

                                          break;

                                   case 2:

                                          // 00000010

                                          bTemp =( byte )( bTemp ^ 0x02 );

                                          break;

                                   case 3:

                                          // 00000100

                                          bTemp =( byte )( bTemp ^ 0x04 );

                                          break;

                                   case 4:

                                          // 00001000

                                          bTemp =( byte )( bTemp ^ 0x08 );

                                          break;

                                   case 5:

                                          // 00010000

                                          bTemp =( byte )( bTemp ^ 0x10 );

                                          break;

                                   case 6:

                                          // 00100000

                                          bTemp =( byte )( bTemp ^ 0x20 );

                                          break;

                                   case 7:

                                          // 01000000

                                          bTemp =( byte )( bTemp ^ 0x40 );

                                          break;

                                   default:

                                          // 10000000

                                          bTemp =( byte )( bTemp ^ 0x80 );

                                          break;

                            }

                            bParaValue =( byte )( bParaValue ^ bTemp );

                     }

                    

                     char chrTemp;

                     int nIndex;

                     switch( bParaValue % CREATE_TYPE )

                     {

                            case 1:

                                   // Exechange 0 <--> 1, 2 <--> 3, 4 <--> 5, 6 <--> 7

                                   for( int i = 0; i < BaseTable.Length / CREATE_TYPE; i++ )

                                   {

                                          nIndex = i * CREATE_TYPE;

                                          chrTemp = BaseTable[nIndex];

                                          BaseTable[nIndex] = BaseTable[nIndex + 1];

                                          BaseTable[nIndex + 1] = chrTemp;

                                          chrTemp = BaseTable[nIndex + 2];

                                          BaseTable[nIndex + 2] = BaseTable[nIndex + 3];

                                          BaseTable[nIndex + 3] = chrTemp;

                                          chrTemp = BaseTable[nIndex + 4];

                                          BaseTable[nIndex + 4] = BaseTable[nIndex + 5];

                                          BaseTable[nIndex + 5] = chrTemp;

                                          chrTemp = BaseTable[nIndex + 6];

                                          BaseTable[nIndex + 6] = BaseTable[nIndex + 7];

                                          BaseTable[nIndex + 7] = chrTemp;

                                   }

                                   break;

                            case 2:

                                   // Exechange 0 <--> 2, 1 <--> 3, 4 <--> 6, 5 <--> 7

                                   for( int i = 0; i < BaseTable.Length / CREATE_TYPE; i++ )

                                   {

                                          nIndex = i * CREATE_TYPE;

                                          chrTemp = BaseTable[nIndex];

                                          BaseTable[nIndex] = BaseTable[nIndex + 2];

                                          BaseTable[nIndex + 2] = chrTemp;

                                          chrTemp = BaseTable[nIndex + 1];

                                          BaseTable[nIndex + 1] = BaseTable[nIndex + 3];

                                          BaseTable[nIndex + 3] = chrTemp;

                                          chrTemp = BaseTable[nIndex + 4];

                                          BaseTable[nIndex + 4] = BaseTable[nIndex + 6];

                                          BaseTable[nIndex + 6] = chrTemp;

                                          chrTemp = BaseTable[nIndex + 5];

                                          BaseTable[nIndex + 5] = BaseTable[nIndex + 7];

                                          BaseTable[nIndex + 7] = chrTemp;

                                   }

                                   break;

                            case 3:

                                   // Exechange 0 <--> 3, 1 <--> 2, 4 <--> 7, 5 <--> 6

                                   for( int i = 0; i < BaseTable.Length / CREATE_TYPE; i++ )

                                   {

                                          nIndex = i * CREATE_TYPE;

                                          chrTemp = BaseTable[nIndex];

                                          BaseTable[nIndex] = BaseTable[nIndex + 3];

                                          BaseTable[nIndex + 3] = chrTemp;

                                          chrTemp = BaseTable[nIndex + 1];

                                          BaseTable[nIndex + 1] = BaseTable[nIndex + 2];

                                          BaseTable[nIndex + 2] = chrTemp;

                                          chrTemp = BaseTable[nIndex + 4];

                                          BaseTable[nIndex + 4] = BaseTable[nIndex + 7];

                                          BaseTable[nIndex + 7] = chrTemp;

                                          chrTemp = BaseTable[nIndex + 5];

                                          BaseTable[nIndex + 5] = BaseTable[nIndex + 6];

                                          BaseTable[nIndex + 6] = chrTemp;

                                   }

                                   break;

                            case 4:

                                   // Mirror exechange

                                   for( int i = 0; i < BaseTable.Length / CREATE_TYPE; i++ )

                                   {

                                          nIndex = i * CREATE_TYPE;

                                          chrTemp = BaseTable[nIndex];

                                          BaseTable[nIndex] = BaseTable[nIndex + CREATE_TYPE - 1];

                                          BaseTable[nIndex + CREATE_TYPE - 1] = chrTemp;

                                          chrTemp = BaseTable[nIndex + 1];

                                          BaseTable[nIndex + 1] = BaseTable[nIndex + CREATE_TYPE - 2];

                                          BaseTable[nIndex + CREATE_TYPE - 2] = chrTemp;

                                          chrTemp = BaseTable[nIndex + 2];

                                          BaseTable[nIndex + 2] = BaseTable[nIndex + CREATE_TYPE - 3];

                                          BaseTable[nIndex + CREATE_TYPE - 3] = chrTemp;

                                          chrTemp = BaseTable[nIndex + 3];

                                          BaseTable[nIndex + 3] = BaseTable[nIndex + CREATE_TYPE - 4];

                                          BaseTable[nIndex + CREATE_TYPE - 4] = chrTemp;

                                   }

                                   break;

                            case 5:

                                   // Exechange 0 <--> 4, 1 <--> 5, 2 <--> 6, 3 <--> 7

                                   for( int i = 0; i < BaseTable.Length / CREATE_TYPE; i++ )

                                   {

                                          nIndex = i * CREATE_TYPE;

                                          chrTemp = BaseTable[nIndex];

                                          BaseTable[nIndex] = BaseTable[nIndex + 4];

                                          BaseTable[nIndex + 4] = chrTemp;

                                          chrTemp = BaseTable[nIndex + 1];

                                          BaseTable[nIndex + 1] = BaseTable[nIndex + 5];

                                          BaseTable[nIndex + 5] = chrTemp;

                                          chrTemp = BaseTable[nIndex + 2];

                                          BaseTable[nIndex + 2] = BaseTable[nIndex + 6];

                                          BaseTable[nIndex + 6] = chrTemp;

                                          chrTemp = BaseTable[nIndex + 3];

                                          BaseTable[nIndex + 3] = BaseTable[nIndex + 7];

                                          BaseTable[nIndex + 7] = chrTemp;

                                   }

                                   break;

                            case 6:

                                   // Exechange 0 <--> 5, 1 <--> 6, 2 <--> 7, 3 <--> 4

                                   for( int i = 0; i < BaseTable.Length / CREATE_TYPE; i++ )

                                   {

                                          nIndex = i * CREATE_TYPE;

                                          chrTemp = BaseTable[nIndex];

                                          BaseTable[nIndex] = BaseTable[nIndex + 5];

                                          BaseTable[nIndex + 5] = chrTemp;

                                          chrTemp = BaseTable[nIndex + 1];

                                          BaseTable[nIndex + 1] = BaseTable[nIndex + 6];

                                          BaseTable[nIndex + 6] = chrTemp;

                                          chrTemp = BaseTable[nIndex + 2];

                                          BaseTable[nIndex + 2] = BaseTable[nIndex + 7];

                                          BaseTable[nIndex + 7] = chrTemp;

                                          chrTemp = BaseTable[nIndex + 3];

                                          BaseTable[nIndex + 3] = BaseTable[nIndex + 4];

                                          BaseTable[nIndex + 4] = chrTemp;

                                   }

                                   break;

                            case 7:

                                   // Exechange 0 <--> 6, 1 <--> 7, 2 <--> 4, 3 <--> 5

                                   for( int i = 0; i < BaseTable.Length / CREATE_TYPE; i++ )

                                   {

                                          nIndex = i * CREATE_TYPE;

                                          chrTemp = BaseTable[nIndex];

                                          BaseTable[nIndex] = BaseTable[nIndex + 6];

                                          BaseTable[nIndex + 6] = chrTemp;

                                          chrTemp = BaseTable[nIndex + 1];

                                          BaseTable[nIndex + 1] = BaseTable[nIndex + 7];

                                          BaseTable[nIndex + 7] = chrTemp;

                                          chrTemp = BaseTable[nIndex + 2];

                                          BaseTable[nIndex + 2] = BaseTable[nIndex + 4];

                                          BaseTable[nIndex + 4] = chrTemp;

                                          chrTemp = BaseTable[nIndex + 3];

                                          BaseTable[nIndex + 3] = BaseTable[nIndex + 5];

                                          BaseTable[nIndex + 5] = chrTemp;

                                   }

                                   break;

                            default:

                                   break;

                     }

                     return BaseTable;

              }

 

              /// <summary>

              /// Encode string at specific parameter

              /// </summary>

              /// <param name="Para"></param>

              /// <param name="input"></param>

              /// <returns>return encoded string</returns>

              public char[] GetEncoded( char[] Para, byte[] input )

              {

                     length=input.Length;

                     if((length % 3)==0)

                     {

                            paddingCount=0;

                            blockCount=length/3;

                     }

                     else

                     {

                            paddingCount=3-(length % 3);//need to add padding

                            blockCount=(length+paddingCount) / 3;

                     }

                     length2=length+paddingCount;//or blockCount *3

 

                     byte[] source2;

                     source2=new byte[length2];

                     //copy data over insert padding

                     for (int x=0; x<length2;x++)

                     {

                            if (x<length)

                            {

                                   source2[x]=input[x];

                            }

                            else

                            {

                                   source2[x]=0;

                            }

                     }

                    

                     byte b1, b2, b3;

                     byte temp, temp1, temp2, temp3, temp4;

                     byte[] buffer=new byte[blockCount*4];

                     char[] result=new char[blockCount*4];

                     for (int x=0;x<blockCount;x++)

                     {

                            b1=source2[x*3];

                            b2=source2[x*3+1];

                            b3=source2[x*3+2];

 

                            temp1=(byte)((b1 & 252)>>2);//first

 

                            temp=(byte)((b1 & 3)<<4);

                            temp2=(byte)((b2 & 240)>>4);

                            temp2+=temp; //second

 

                            temp=(byte)((b2 & 15)<<2);

                            temp3=(byte)((b3 & 192)>>6);

                            temp3+=temp; //third

 

                            temp4=(byte)(b3 & 63); //fourth

 

                            buffer[x*4]=temp1;

                            buffer[x*4+1]=temp2;

                            buffer[x*4+2]=temp3;

                            buffer[x*4+3]=temp4;

 

                     }

 

                     lookupTable = CreateBase64Char( ref Para );

                     for (int x=0; x<blockCount*4;x++)

                     {

                            result[x]=sixbit2char(buffer[x]);

                     }

 

                     //covert last "A"s to "=", based on paddingCount

                     switch (paddingCount)

                     {

                            case 0:break;

                            case 1:result[blockCount*4-1]='=';break;

                            case 2:result[blockCount*4-1]='=';

                                   result[blockCount*4-2]='=';

                                   break;

                            default:break;

                     }

                     return result;

              }

 

              private char sixbit2char(byte b)

              {

                     if((b>=0) &&(b<=63))

                     {

                            return lookupTable[(int)b];

                     }

                     else

                     {

                            //should not happen;

                            return ' ';

                     }

              }

 

              /// <summary>

              /// Decode string using specific parameter

              /// </summary>

              /// <param name="Para"></param>

              /// <param name="input"></param>

              /// <returns>If decoded successfully, return the decoded string; else return NULL</returns>

              public byte[] GetDecoded( char[] Para, char[] input )

              {

                     int temp=0;

                     source=input;

                     length=input.Length;

 

                     if( ( length % 4 ) != 0 ) return null; // The string is not encoded with my base64;

 

                     //find how many padding there are

                     while( input[length - temp - 1] == '=' && temp < 3 )

                     {

                            temp++;

                     }

                     if( temp == 3 ) return null; // The string is not encoded with my base64;

 

                     paddingCount=temp;

                     //calculate the blockCount;

                     //assuming all whitespace and carriage returns/newline were removed.

                     blockCount=length/4;

                     length2=blockCount*3;

 

                     byte[] buffer=new byte[length];//first conversion result

                     byte[] buffer2=new byte[length2];//decoded array with padding

 

                     lookupTable = CreateBase64Char( ref Para );

                     for(int x=0;x<length;x++)

                     {

                            buffer[x]=char2sixbit(source[x]);

                     }

 

                     byte b, b1,b2,b3;

                     byte temp1, temp2, temp3, temp4;

 

                     for(int x=0;x<blockCount;x++)

                     {

                            temp1=buffer[x*4];

                            temp2=buffer[x*4+1];

                            temp3=buffer[x*4+2];

                            temp4=buffer[x*4+3];                       

 

                            b=(byte)(temp1<<2);

                            b1=(byte)((temp2 & 48)>>4);

                            b1+=b;

 

                            b=(byte)((temp2 & 15)<<4);

                            b2=(byte)((temp3 & 60)>>2);

                            b2+=b;

 

                            b=(byte)((temp3 & 3)<<6);

                            b3=temp4;

                            b3+=b;

 

                            buffer2[x*3]=b1;

                            buffer2[x*3+1]=b2;

                            buffer2[x*3+2]=b3;

                     }

                     //remove paddings

                     length3=length2-paddingCount;

                     byte[] result=new byte[length3];

 

                     for(int x=0;x<length3;x++)

                     {

                            result[x]=buffer2[x];

                     }

 

                     return result;

              }

 

              private byte char2sixbit(char c)

              {

                     if( c=='=' )

                            return 0;

                     else

                     {

                            for (int x=0;x<64;x++)

                            {

                                   if (lookupTable[x]==c)

                                          return (byte)x;

                            }

                            //should not reach here

                            return 0;

                     }

              }

 

       }

}



Trackback: http://tb.blog.csdn.net/TrackBack.aspx?PostId=627040

相关文章推荐

java 用BASE64 进行加密 解密

使用java语言实现base64加密解密操作

.NET中进行Base64加密解密

方法一:   ///   /// Base64加密   ///   ///   ///   public string Base64Code(string Messa...

将Ansi编码的字符串进行Base64编码对迅雷地址加密解密

function UnicodeChr(){ return '00A4,00A7,00A8,00B0,00B1,00B7,00D7,00E0,00E1,00E8,00E9,00EA,00EC,00ED...

java中Base64转码与解码(加密与解密)原理与使用

Base64是网络上最常见的用于传输8Bit字节代码的编码方式之一,大家可以查看RFC2045~RFC2049,上面有MIME的详细规范。Base64编码可用于在HTTP环境下传递较长的标识信息。例如...

AES加密解密|及Base64的使用

AES加解密算法,使用Base64做转码以及辅助加密: package com.wintv.common; import javax.crypto.Cipher; import javax....

【java】AES加密解密|及Base64的使用

package com.common; import javax.crypto.Cipher; import javax.crypto.spec.IvParameterSpec; imp...

java httpServer端接受post请求数据并使用3DES+base64加密解密demo希望能够对您有帮助

技术真实难者不会,会者不难。希望这篇文章对大家有帮助

AES加密解密|及Base64的使用

转自:http://www.cnblogs.com/arix04/archive/2009/10/15/1511839.html AES加解密算法,使用Base64做转码以及辅助加密: ...

Java代码Base64加密解密使用

Java代码Base64加密解密使用       程序中有时候需要把用户名和密码保存到本地,下次登录时自动获取,如果是直接使用字符串保存,肯定容易泄露密码,这就需要经过加密,但是登录时还要获取密码,这...
内容举报
返回顶部
收藏助手
不良信息举报
您举报文章:深度学习:神经网络中的前向传播和反向传播算法推导
举报原因:
原因补充:

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