王朝网络
分享
 
 
 

如何使用Base64进行加密和解密(续)

王朝other·作者佚名  2007-08-24
宽屏版  字体: |||超大  

由于以前文章在介绍Base64方法的时候,所提供的调用方法过于麻烦,而且接口不太直观,因此我最近在原有的基础上进行了修改。

具体代码如下:

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

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

//---File: clsBase64

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

//---Author: Knight

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

//---Modified Date: Jul.4, 2006

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

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

using System;

namespace Base64

{

using System.Text;

/// <summary>

/// Summary description for clsBase64.

/// </summary>

public class clsBase64

{

protected clsBase64()

{

//Avoid to be inited

}

#region Base64_Algorithm_Implement

/// <summary>

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

/// </summary>

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

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

private static 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>

private static char[] GetEncoded( char[] Para, byte[] input )

{

char[] lookupTable;

int length, length2;

int blockCount;

int paddingCount;

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( lookupTable, 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 static char sixbit2char(char[] lookupTable, 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>

private static byte[] GetDecoded( char[] Para, char[] input )

{

char[] source;

char[] lookupTable;

int length, length2, length3;

int blockCount;

int paddingCount;

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( lookupTable, 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 static byte char2sixbit( char[] lookupTable, 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;

}

}

#endregion

/// <summary>

/// Encrypt data based on specific key

/// </summary>

/// <param name="Data">the data to be encrypted</param>

/// <param name="Key">key data</param>

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

public static string EncryptData( string Data, string Key )

{

if( Data == null || Data == "" ) return null;

if( Key == null || Key == "" ) return null;

char[] chrEncrypted = GetEncoded( Key.ToCharArray(),

Encoding.Unicode.GetBytes( Data ) );

if( chrEncrypted != null )

return new string( chrEncrypted );

else

return null;

}

/// <summary>

/// Decrypt data based on specific key

/// </summary>

/// <param name="Data">the data to be decrypted</param>

/// <param name="Key">key data</param>

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

public static string DecryptData( string Data, string Key )

{

if( Data == null || Data == "" ) return null;

if( Key == null || Key == "" ) return null;

byte[] bDecrypted = GetDecoded( Key.ToCharArray(),

Data.ToCharArray() );

if( bDecrypted != null )

return Encoding.Unicode.GetString( bDecrypted );

else

return null;

}

}

}

调用如下:

//Encrypt data

string strEncryptedData = Base64.clsBase64.EncryptData(

yourData, yourKey );

if( strEncryptedData != null )

MessageBox.Show( strEncryptedData );

//Decrypt data

string strDecryptedData = Base64.clsBase64.DecryptData(

yourData, yourKey );

if( strDecryptedData != null )

MessageBox.Show( strDecryptedData );

 
 
 
免责声明:本文为网络用户发布,其观点仅代表作者个人观点,与本站无关,本站仅提供信息存储服务。文中陈述内容未经本站证实,其真实性、完整性、及时性本站不作任何保证或承诺,请读者仅作参考,并请自行核实相关内容。
2023年上半年GDP全球前十五强
 百态   2023-10-24
美众议院议长启动对拜登的弹劾调查
 百态   2023-09-13
上海、济南、武汉等多地出现不明坠落物
 探索   2023-09-06
印度或要将国名改为“巴拉特”
 百态   2023-09-06
男子为女友送行,买票不登机被捕
 百态   2023-08-20
手机地震预警功能怎么开?
 干货   2023-08-06
女子4年卖2套房花700多万做美容:不但没变美脸,面部还出现变形
 百态   2023-08-04
住户一楼被水淹 还冲来8头猪
 百态   2023-07-31
女子体内爬出大量瓜子状活虫
 百态   2023-07-25
地球连续35年收到神秘规律性信号,网友:不要回答!
 探索   2023-07-21
全球镓价格本周大涨27%
 探索   2023-07-09
钱都流向了那些不缺钱的人,苦都留给了能吃苦的人
 探索   2023-07-02
倩女手游刀客魅者强控制(强混乱强眩晕强睡眠)和对应控制抗性的关系
 百态   2020-08-20
美国5月9日最新疫情:美国确诊人数突破131万
 百态   2020-05-09
荷兰政府宣布将集体辞职
 干货   2020-04-30
倩女幽魂手游师徒任务情义春秋猜成语答案逍遥观:鹏程万里
 干货   2019-11-12
倩女幽魂手游师徒任务情义春秋猜成语答案神机营:射石饮羽
 干货   2019-11-12
倩女幽魂手游师徒任务情义春秋猜成语答案昆仑山:拔刀相助
 干货   2019-11-12
倩女幽魂手游师徒任务情义春秋猜成语答案天工阁:鬼斧神工
 干货   2019-11-12
倩女幽魂手游师徒任务情义春秋猜成语答案丝路古道:单枪匹马
 干货   2019-11-12
倩女幽魂手游师徒任务情义春秋猜成语答案镇郊荒野:与虎谋皮
 干货   2019-11-12
倩女幽魂手游师徒任务情义春秋猜成语答案镇郊荒野:李代桃僵
 干货   2019-11-12
倩女幽魂手游师徒任务情义春秋猜成语答案镇郊荒野:指鹿为马
 干货   2019-11-12
倩女幽魂手游师徒任务情义春秋猜成语答案金陵:小鸟依人
 干货   2019-11-12
倩女幽魂手游师徒任务情义春秋猜成语答案金陵:千金买邻
 干货   2019-11-12
 
>>返回首页<<
推荐阅读
 
 
频道精选
 
静静地坐在废墟上,四周的荒凉一望无际,忽然觉得,凄凉也很美
© 2005- 王朝网络 版权所有