如何使用Base64进行加密和解密
答案:2 悬赏:40 手机版
解决时间 2021-02-28 13:08
- 提问者网友:我没有何以琛的痴心不悔
- 2021-02-27 22:47
如何使用Base64进行加密和解密
最佳答案
- 五星知识达人网友:从此江山别
- 2021-02-27 23:50
其实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 description for clsBase64.
///
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;
}
///
/// Create base64 char array using default base64 char array
///
///
///return the new base64 char array
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];
}
//remove paddings
length3=length2-paddingCount;
byte[] result=new byte[length3];
for(int x=0;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;
}
}
}
}
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 description for clsBase64.
///
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;
}
///
/// Create base64 char array using default base64 char array
///
///
///
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];
}
//remove paddings
length3=length2-paddingCount;
byte[] result=new byte[length3];
for(int x=0;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;
}
}
}
}
全部回答
- 1楼网友:蓝房子
- 2021-02-28 01:20
这里有详细的代码解释保存c元,java,php,javascript
我要举报
如以上问答信息为低俗、色情、不良、暴力、侵权、涉及违法等信息,可以点下面链接进行举报!
大家都在看
推荐资讯