/************************************************************************/
/* ?車?邦﹞?那?㏒oAES128 ECB?㏒那?㏒?Base64??那??邦???㏒
那米??2?3?1y64℅??迆米?℅?﹞?∩??車?邦㏒?辰?Base64??那?那?3??㏒
那米??2?3?1y88??℅??迆Base64??那?℅?﹞?∩??a?邦?㏒
?‾??2?℅?128bit米?2?﹞?㏒?辰?0x0021??
?邦??2?3?1y16B,3?3?o車?芍3?∩赤*/
/************************************************************************/
#include "AES128Base64.h"
//void output(unsigned char * p, int len )
//{
// int i = 0 ;
// printf("0000~0015:");
// for(i = 0 ; i<len ; i ++)//p[i]
// {
// if(i && i % 16 == 0 )
// {
// printf("\n");
// printf("%04d~%04d:",i,i+15);
// }
// printf(" 0x%02X ", p[i]);
// }
// printf(" \n");
//}
/************************************************************************/
/* 那?豕?㏒o ?車?邦?邦??㏒??‾??℅?﹞?∩? ?車?邦o車米?Base64℅?﹞?∩??邦?? ?‾??2?3?1y64??℅?﹞?㏒?3?3??芍3?∩赤?㏒
﹞米???米㏒o0 ?∩DD3谷1|
???? ?∩DD3?∩赤*/
/************************************************************************/
int Aes128Encry( const u8 *Key, const u8*SrcString, u8 *CipherText)
{
struct aes_ctx aes;
int len ,rnd, tail, iCnt;
u8 szOrgiText[65]={0}; //?-??
u8 iCiphBin[64] = {0}; //?邦?? HEX
u8 *szCiphTestB64 = CipherText; //?邦?? Base64?車?邦
u8 Key16B[17] = {0}; //16B?邦??
strcpy((char*)Key16B, (char*)Key);//
gen_tabs();
if(aes_set_key(&aes, Key16B , 16) != 0) {
#if AES_DEBUG
// printf("Can't set key : %s \n",Key16B);
#endif //AES_DEBUG
return -1; //?邦??3?∩赤
}
len = strlen((char*)SrcString);
if( len > 64 )
{
#if AES_DEBUG
// printf("Sorce String too long\n");
#endif
return -2;
}
strcpy((char*)szOrgiText, (char*)SrcString);
len = strlen((char*)szOrgiText);
rnd = len/16;//?車?邦??那y
tail = len%16;//℅?o車辰???2?℅?16℅??迆那㊣㏒?℅??迆那y芍?
if (tail)//車D?2那y
rnd+=1;
for (iCnt = 0; iCnt < rnd; iCnt++)
{
aes_encrypt(&aes,iCiphBin + iCnt*16, szOrgiText + iCnt*16);
}
#if AES_DEBUG
// printf("Encryp Result is : \n");
// output(iCiphBin, rnd *16 );//
#endif
base64_in(iCiphBin,szCiphTestB64, rnd *16);
return 0;//?車?邦赤那㊣?
}
/************************************************************************/
/* 那?豕?㏒o ?a?邦?邦??㏒? ?邦??㏒??a?邦o車米?℅?﹞?∩? ?邦??2?3?1y88??℅?﹞?㏒?3?3??芍3?∩赤
﹞米???米㏒o+ ?∩DD3谷1| ﹞米???a?邦℅??迆那y
???? ?∩DD3?∩赤*/
/************************************************************************/
int Aes128Decry(const u8 *Key, const u8 *CipherText, u8 *DestString)//
{
struct aes_ctx aes;
int len ,rnd;
u8 iCiphBin[64] = {0};//?邦?? HEX
u8 szCiphTestB64[89] = {0}; //?邦?? Base64?車?邦
u8 *szDecoText = DestString; //?a?邦o車
int iCnt;
u8 Key16B[17] = {0}; //16B?邦??
strcpy((char *)Key16B, (char*)Key);//
gen_tabs();
if(aes_set_key(&aes, Key16B , 16) != 0) {
#if AES_DEBUG
// printf("Can't set key : %s \n",Key16B);
#endif //AES_DEBUG
return -1; //?邦??3?∩赤
}
if (strlen((char*)CipherText) >89 || strlen((char*)CipherText) <= 0)
{
#if AES_DEBUG
// printf("CiperTest Error\n");
#endif
return -2;
}
strcpy((char*)szCiphTestB64,(char*)CipherText);
len = base64_out(szCiphTestB64, iCiphBin);//iCiphBin,szCiphTestB64,(rnd) *16
#if AES_DEBUG
// printf("Base64 To bin Result %d Bytes : \n",len);
// output(iCiphBin,len );
#endif
memset(szDecoText,0,len+1);//℅?o車1??米ㄓ?a∩?∩⊿/0
if ( len % 16 )//2?那?16℅??迆??那y㊣?
{
#if AES_DEBUG
// printf("Buffer For AES Deciph Not Fit ",strlen(szCiphTestB64), szCiphTestB64);
#endif
return -2;//?邦??那y芍?2???
}
rnd = len / 16;
for ( iCnt = 0; iCnt < rnd; iCnt++)
{
aes_decrypt(&aes, szDecoText + iCnt*16,iCiphBin + iCnt*16);
}
#if AES_DEBUG
// printf("Descrpy Result %d characters,which is : %s \n", strlen(szDecoText), szDecoText);
#endif
return len ; //?a?邦赤那㊣? ﹞米???a?邦米?℅??迆那y
}
/************************************************************************/
/* 那?豕?㏒o ?車?邦?邦??㏒??‾??BYTE[],?‾??3∟?豕㏒? ?車?邦o車米?Base64℅?﹞?∩??邦?? ?‾??2?3?1y64??℅?﹞?㏒?3?3??芍3?∩赤?㏒
﹞米???米㏒o0 ?∩DD3谷1|
???? ?∩DD3?∩赤*/
/************************************************************************/
int Aes128EncryBin( const u8 *Key, const u8 *SrcBytes, unsigned int SrcLen, u8 *CipherText)
{
struct aes_ctx aes;
int rnd, tail, iCnt;
u8 szOrgiText[65]={0}; //?-??
u8 iCiphBin[64] = {0}; //?邦?? HEX
u8 *szCiphTestB64 = CipherText; //?邦?? Base64?車?邦
u8 Key16B[17] = {0}; //16B?邦??
strcpy((char*)Key16B, (char*)Key);//
gen_tabs();
if(aes_set_key(&aes, Key16B , 16) != 0) {
#if AES_DEBUG
// printf("Can't set key : %s \n",Key16B);
#endif //AES_DEBUG
return -1; //?邦??3?∩赤
}
if( SrcLen > 64 )
{
#if AES_DEBUG
// printf("Sorce Buffer too long\n");
#endif
return -2;
}
memcpy(szOrgiText, SrcBytes, SrcLen);
rnd = SrcLen/16;//?車?邦??那y
tail = SrcLen%16;//℅?o車辰???2?℅?16℅??迆那㊣㏒?℅??迆那y芍?
if (tail)//車D?2那y
rnd+=1;
for (iCnt = 0; iCnt < rnd; iCnt++)
{
aes_encrypt(&aes,iCiphBin + iCnt*16, szOrgiText + iCnt*16);
}
#if AES_DEBUG
// printf("Encryp Result is : \n");
// output(iCiphBin, rnd *16 );//
#endif
base64_in(iCiphBin,szCiphTestB64, rnd *16);
return 0;//?車?邦赤那㊣?
}
/************************************************************************/
/* 那?豕?㏒o ?a?邦?邦??㏒? ?邦??㏒??a?邦o車米?℅?﹞?∩? ?邦??2?3?1y88??℅?﹞?㏒?3?3??芍3?∩赤
﹞米???米㏒o?a?邦o車米??‾??℅??迆那y
???? ?∩DD3?∩赤*/
/************************************************************************/
int Aes128DecryBin(u8 *Key, const u8 *CipherText, u8 *DestBytes)//
{
struct aes_ctx aes;
int len ,rnd;
u8 iCiphBin[64] = {0};//?邦?? HEX
u8 szCiphTestB64[89] = {0}; //?邦?? Base64?車?邦
u8 *szDecoText = DestBytes; //?a?邦o車
int iCnt;
u8 Key16B[17] = {0}; //16B?邦??
strcpy((char*)Key16B, (char*)Key);//
gen_tabs();
if(aes_set_key(&aes, Key16B , 16) != 0) {
#if AES_DEBUG
// printf("Can't set key : %s \n",Key16B);
#endif //AES_DEBUG
return -1; //?邦??3?∩赤
}
if (strlen((char*)CipherText) >89 || strlen((char*)CipherText) <= 0)
{
#if AES_DEBUG
// printf("CiperTest Error\n");
#endif
return -2;
}
strcpy((char*)szCiphTestB64,(char*)CipherText);
len = base64_out(szCiphTestB64, iCiphBin);//iCiphBin,szCiphTestB64,(rnd) *16
#if AES_DEBUG
// printf("Base64 To bin Result %d Bytes : \n",len);
// output(iCiphBin,len );
#endif
memset(szDecoText,0,len);//
if ( len % 16 )//2?那?16℅??迆??那y㊣?
{
#if AES_DEBUG
// printf("Buffer For AES Deciph Not Fit ",strlen(szCiphTestB64), szCiphTestB64);
#endif
return -2;//?邦??那y芍?2???
}
rnd = len / 16;
for ( iCnt = 0; iCnt < rnd; iCnt++)
{
aes_decrypt(&aes, szDecoText + iCnt*16,iCiphBin + iCnt*16);
}
#if AES_DEBUG
// printf("Descrpy Result %d characters,which is : %s \n", strlen(szDecoText), szDecoText);
#endif
return len ; //?a?邦赤那㊣?
}