如何使用Base64进行加密和解密
Posted
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了如何使用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];
//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;
参考技术A
这里有详细的代码解释保存c元,java,php,javascript
以上是关于如何使用Base64进行加密和解密的主要内容,如果未能解决你的问题,请参考以下文章
JAVA如何解密用PHP加密的base64编码和RIJNDAEL 256的数据?
BASE64Encoder,BASE64Decoder加密后再解密不成功