使用说明:
XSXSXSblubro.comSXSXSX
1.将红色字体内容复制到主函数前
2.将绿色字体内容复制到主函数之后
参数说明
1.pClerText指向需要加密的明文
2.ClerTextLen明文的长度(单位字节)
3.ppCipherText用于存放密文的地址
4.pCipherTextLen用于存放密文长度的地址
//头文件及函数声明
#include <stdlib.h>
#include <malloc.h>
#include <memory.h>
unsigned int Base64Encrypt(unsigned char *pClerText,unsigned int ClerTextLen,unsigned char **ppCipherText,unsigned int *pCipherTextLen);
//函数实现
unsigned int Base64Encrypt ( unsigned char *pClerText,unsigned int ClerTextLen,unsigned char **ppCipherText,unsigned int *pCipherTextLen )
{
unsigned char Base64Table[64]={65,66,67,68,69,70,71,72,73,74,75,76,77,78,79,80,81,82,83,84,85,86,87,88,89,90,
97,98,99,100,101,102,103,104,105,106,107,108,109,110,111,112,113,114,115,116,117,118,119,120,121,122,
48,49,50,51,52,53,54,55,56,57,
43,47};
unsigned int ClerTextBytes,LoopA;
unsigned char *pClerTextBytes=(unsigned char *)&ClerTextBytes;
unsigned char *pCipherText;
unsigned int CipherTextLen, EquaSignCount,Remainder;
Remainder=ClerTextLen%3;
if(Remainder==0)
{
CipherTextLen=ClerTextLen/3*4;
EquaSignCount=0;
}
else
{
CipherTextLen=(ClerTextLen-Remainder)/3*4+4;
if(Remainder==1)
EquaSignCount=2;
else
EquaSignCount=1;
}
pCipherText=(unsigned char *)malloc(CipherTextLen);
if(pCipherText==NULL) return 1;
memset((void *)pCipherText,0,CipherTextLen);
for(LoopA=0;LoopA<(CipherTextLen/4);LoopA++)
{
pClerTextBytes[3]=pClerText[LoopA*3];
if(LoopA*3+1>=ClerTextLen)
pClerTextBytes[2]=0;
else
pClerTextBytes[2]=pClerText[LoopA*3+1];
if(LoopA*3+2>=ClerTextLen)
pClerTextBytes[1]=0;
else
pClerTextBytes[1]=pClerText[LoopA*3+2];
pClerTextBytes[0]=0;
ClerTextBytes=ClerTextBytes>>2;
pCipherText[LoopA*4]=pClerTextBytes[3];
ClerTextBytes=ClerTextBytes>>2;
pCipherText[LoopA*4+1]=pClerTextBytes[2]&63;
ClerTextBytes=ClerTextBytes>>2;
pCipherText[LoopA*4+2]=pClerTextBytes[1]&63;
ClerTextBytes=ClerTextBytes>>2;
pCipherText[LoopA*4+3]=pClerTextBytes[0]&63;
}
for(LoopA=0;LoopA<CipherTextLen;LoopA++)
{
pCipherText[LoopA]=Base64Table[pCipherText[LoopA]];
}
if(EquaSignCount==1)
{
pCipherText[CipherTextLen-1]=61;
}
if(EquaSignCount==2)
{
pCipherText[CipherTextLen-2]=61;
pCipherText[CipherTextLen-1]=61;
}
*ppCipherText=pCipherText;
*pCipherTextLen=CipherTextLen;
return 0;
}