需要帮助base64 [英] Need Help with base64
问题描述
我目前正在尝试在C中实现base64编码和解码
方案。 Python有一个模块base64,它可以轻松地进行
编码和解码。我知道OpenSSL支持
用于base64编码和解码,但我现在必须在C中实现
而不使用openssl库。
我能够下载代码wrt base 64编码和
解码。我附上下面的代码。
void encodeblock(unsigned char cin [3],unsigned char cout [4],int
nlen)
{
cout [0] = cb64 [cin [0]> 2];
cout [1] = cb64 [( (cin [0]& 0x03)<< 4)| ((cin [1]& 0xf0)> 4)];
cout [2] =(无符号字符)(nlen 1?cb64 [((cin [1]& 0x0f)< ;< 2)
|((cin [2]& 0xc0)> 6)]:''='');
cout [3] = (unsigned char)(nlen 2?cb64 [cin [2]& 0x3f]:
''='');
}
void decodeblock(unsigned char cin [4],unsigned char cout [3])
{
cout [0] =(unsigned char)(cin [0]<< 2 | cin [1]> 4);
cout [1] =(unsigned char)(cin [1]<< 4> cin [2]> 2);
cout [2] =(unsigned char)(((cin [2]<<<<<<<<<<<<<<<< 6>& 0xc0)| cin [3]);
}
int base641_decodestring(char * pcstr,int size,char ** ppcdest)
{
unsigned char cin [4] = {""};
unsigned char cout [3] = {""};
unsigned char cv = 0;
int ni = 0;
int nlen = 0;
char * cptr = pcstr;
* ppcdest = malloc(sizeof(char) * 160);
if(!* ppcdest)
{
返回1;
}
memset(* ppcdest,0,sizeof(char)* 160);
char * pcstring = malloc(sizeof(char)* SIZE);
if(!pcstring)
{
aps_log(" APS_log.txt"," \ nbae64.c:base64encode:mall oc failed
\ n");
返回1;
}
memset(pcstring,''\ 0' ',SIZE);
for(nlen = 0,ni = 0; ni< 4; ni ++)
{
cv = 0;
而(cv == 0)
{
cv =(unsigned char)* cptr;
cv =(unsigned char)((cv< 43 || cv 122)?0:
cd64 [cv - 43]);
if(cv)
{
cv =(unsigned char)((cv =='' $'')?0:cv - 61);
}
}
if(cptr ++)
{
nlen ++;
if(cv)
{
cin [ni] =(unsigned char)( cv - 1);
}
}
其他
{
cin [ni] = 0;
}
}
if(nlen)
{
decodeblock(cin,cout);
}
memcpy(* ppcdest,cout,160);
返回0;
} / * base64_decode结束* /
char * base64_encode(char * pcstr)
{
unsigned char cin [3] = {""};
unsigned char cout [4] = {""};
int ni = 0;
int nlen = 0;
int flag = 1;
char * cptr = pcstr;
char * pcstring = malloc(sizeof(char)* SIZE);
if(!pcstring)
{
aps_log(" APS_log.txt"," \ nbae64.c:base64encode:mall oc failed
\ n");
return(void *)0;
}
memset(pcstring,''\ 0'',SIZE);
while(flag)
{
for(ni = 0; ni< 3; ni ++)
{
cin [ni] =(unsigned char)* cptr;
if(* ++ cptr!=''\\ \\ 0'')
nlen ++;
else
{
cin [ni] = 0;
flag = 0;
休息;
}
}
encodeblock(cin, cout,nlen);
strcat(pcstring,cout);
}
返回pcstring;
} / * base64_encode结束* /
但是这个代码与
python base64.encodestring相比给出了不同的十六进制值base64.decodestring分别。
我需要一些关于此事的帮助。我需要一些指示
这里是上述文件的错误。
Hi ,
I am currently trying to implement base64 encoding and decoding
scheme in C . Python has a module , base64 , that will do the
encoding and decoding with ease . I am aware of OpenSSL having support
for base64 encoding and decoding , but i will have to now implement
both in C without using the openssl libraries .
I was able to download a code w.r.t. base 64 encoding and
decoding . I am attaching the code below .
void encodeblock( unsigned char cin[3], unsigned char cout[4], int
nlen )
{
cout[0] = cb64[ cin[0] >2 ];
cout[1] = cb64[ ((cin[0] & 0x03) << 4) | ((cin[1] & 0xf0) >4) ];
cout[2] = (unsigned char) (nlen 1 ? cb64[ ((cin[1] & 0x0f) << 2)
| ((cin[2] & 0xc0) >6) ] : ''='');
cout[3] = (unsigned char) (nlen 2 ? cb64[ cin[2] & 0x3f ] :
''='');
}
void decodeblock( unsigned char cin[4], unsigned char cout[3] )
{
cout[ 0 ] = (unsigned char ) (cin[0] << 2 | cin[1] >4);
cout[ 1 ] = (unsigned char ) (cin[1] << 4 | cin[2] >2);
cout[ 2 ] = (unsigned char ) (((cin[2] << 6) & 0xc0) | cin[3]);
}
int base641_decodestring(char* pcstr,int size,char** ppcdest)
{
unsigned char cin[4] = {""};
unsigned char cout[3] = {""};
unsigned char cv = 0;
int ni = 0;
int nlen = 0;
char* cptr = pcstr;
*ppcdest = malloc(sizeof(char)*160);
if (!*ppcdest)
{
return 1;
}
memset(*ppcdest,0,sizeof(char)*160);
char* pcstring = malloc( sizeof(char) * SIZE);
if (!pcstring)
{
aps_log("APS_log.txt","\nbae64.c:base64encode:mall oc failed
\n");
return 1;
}
memset(pcstring,''\0'',SIZE);
for( nlen = 0, ni = 0; ni < 4; ni++ )
{
cv = 0;
while( cv == 0 )
{
cv = (unsigned char) *cptr;
cv = (unsigned char) ((cv < 43 || cv 122) ? 0 :
cd64[ cv - 43 ]);
if( cv )
{
cv = (unsigned char) ((cv == ''$'') ? 0 : cv - 61);
}
}
if( cptr++ )
{
nlen++;
if( cv )
{
cin[ ni ] = (unsigned char) (cv - 1);
}
}
else
{
cin[ni] = 0;
}
}
if( nlen )
{
decodeblock( cin, cout );
}
memcpy(*ppcdest,cout,160);
return 0;
}/*end of base64_decode */
char* base64_encode(char* pcstr)
{
unsigned char cin[3] = {""};
unsigned char cout[4] = {""};
int ni = 0;
int nlen = 0;
int flag = 1;
char* cptr = pcstr;
char* pcstring = malloc( sizeof(char) * SIZE);
if (!pcstring)
{
aps_log("APS_log.txt","\nbae64.c:base64encode:mall oc failed
\n");
return (void*)0;
}
memset(pcstring,''\0'',SIZE);
while( flag )
{
for( ni = 0; ni < 3; ni++ )
{
cin[ni] = (unsigned char)*cptr;
if( *++cptr != ''\0'' )
nlen++;
else
{
cin[ni] = 0;
flag = 0;
break;
}
}
encodeblock(cin, cout,nlen);
strcat(pcstring,cout);
}
return pcstring;
}/* end of base64_encode */
But this code gives different hex values as compared to the
python base64.encodestring and base64.decodestring respectively .
I need some help on this matter . I need some pointers on
where is the mistake for the above file .
推荐答案
'') ? 0:cv - 61);
}
}
if(cptr ++)
{
nlen ++;
if(cv)
{
cin [ni] =(unsigned char)(cv - 1);
}
}
其他
{
cin [ni] = 0 ;
}
}
if(nlen)
{
decodeblock( cin,cout);
}
memcpy(* ppcdest,cout,160);
返回0;
} / * base64_decode结束* /
char * base64_encode(char * pcstr)
{
unsigned char cin [3] = {""};
unsigned char cout [4] = {""};
int ni = 0;
int nlen = 0;
int flag = 1;
char * cptr = pcstr;
char * pcstring = malloc(sizeof(char)* SIZE);
if(!pcstring)
{
aps_log(" APS_log .txt"," \ nbae64.c:base64encode:mall oc failed
\ n");
return(void *)0;
}
memset(pcstring,''\'',SIZE);
while(flag)
{
for(ni = 0; ni< 3; ni ++)
{
cin [ni] =(unsigned char)* cptr;
if(* ++ cptr!=''\\ \\ 0'')
nlen ++;
else
{
cin [ni] = 0;
flag = 0;
休息;
}
}
encodeblock(cin, cout,nlen);
strcat(pcstring,cout);
}
返回pcstring;
} / * base64_encode结束* /
但是这个代码与
python base64.encodestring相比给出了不同的十六进制值base64.decodestring分别。
我需要一些关于此事的帮助。我需要一些指示
这是上述文件的错误。
'') ? 0 : cv - 61);
}
}
if( cptr++ )
{
nlen++;
if( cv )
{
cin[ ni ] = (unsigned char) (cv - 1);
}
}
else
{
cin[ni] = 0;
}
}
if( nlen )
{
decodeblock( cin, cout );
}
memcpy(*ppcdest,cout,160);
return 0;
}/*end of base64_decode */
char* base64_encode(char* pcstr)
{
unsigned char cin[3] = {""};
unsigned char cout[4] = {""};
int ni = 0;
int nlen = 0;
int flag = 1;
char* cptr = pcstr;
char* pcstring = malloc( sizeof(char) * SIZE);
if (!pcstring)
{
aps_log("APS_log.txt","\nbae64.c:base64encode:mall oc failed
\n");
return (void*)0;
}
memset(pcstring,''\0'',SIZE);
while( flag )
{
for( ni = 0; ni < 3; ni++ )
{
cin[ni] = (unsigned char)*cptr;
if( *++cptr != ''\0'' )
nlen++;
else
{
cin[ni] = 0;
flag = 0;
break;
}
}
encodeblock(cin, cout,nlen);
strcat(pcstring,cout);
}
return pcstring;
}/* end of base64_encode */
But this code gives different hex values as compared to the
python base64.encodestring and base64.decodestring respectively .
I need some help on this matter . I need some pointers on
where is the mistake for the above file .
On Mon,2007年7月23日05:37 :40 -0700,pycraze写道:
On Mon, 23 Jul 2007 05:37:40 -0700, pycraze wrote:
但是这个代码分别给出了与
python base64.encodestring和base64.decodestring相比的不同十六进制值。 br />
我需要一些帮助。我需要一些指示
上面文件的错误在哪里。
But this code gives different hex values as compared to the
python base64.encodestring and base64.decodestring respectively .
I need some help on this matter . I need some pointers on
where is the mistake for the above file .
重新使用现有代码通常比从头实现(和
调试)更容易。几个Base64实现是可用的,例如
,例如 http://www.synesis.com.au/software/b64。 HTML
-
Roland Pibinger
最好的软件简单,优雅,充满戏剧性 - Grady Booch
It''s usually easier to re-use existing code than to implement (and
debug) something from scratch. Several Base64 implementations are
available, e.g. http://www.synesis.com.au/software/b64.html
--
Roland Pibinger
"The best software is simple, elegant, and full of drama" - Grady Booch
pycraze写道:
pycraze wrote:
我目前正在尝试在C中实现base64编码和解码
方案。 Python有一个模块base64,它可以轻松地进行
编码和解码。我知道OpenSSL支持
用于base64编码和解码,但我现在必须在C中实现
而不使用openssl库。
我能够下载代码wrt base 64编码和
解码。我附上下面的代码。
Hi ,
I am currently trying to implement base64 encoding and decoding
scheme in C . Python has a module , base64 , that will do the
encoding and decoding with ease . I am aware of OpenSSL having support
for base64 encoding and decoding , but i will have to now implement
both in C without using the openssl libraries .
I was able to download a code w.r.t. base 64 encoding and
decoding . I am attaching the code below .
[Snip]
[Snip]
但是这段代码给出了不同的十六进制值分别与
python base64.encodestring和base64.decodestring进行比较。
我需要一些帮助来解决这个问题。我需要一些指示
这里是上述文件的错误。
But this code gives different hex values as compared to the
python base64.encodestring and base64.decodestring respectively .
I need some help on this matter . I need some pointers on
where is the mistake for the above file .
在一个简短的测试中,你的代码似乎与捆绑到的代码兼容我必须提供的Java环境中的
MIME支持。也许Python
支持基于不同的标准/ RFC?你可能希望在Python主题的地方查询
。
On a brief test your code seemed compatible with that bundled into the
MIME support in a Java environment I had to hand. Perhaps the Python
support is based on a different standard/RFC? You might wish to enquire
somewhere where Python is on topic.
这篇关于需要帮助base64的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!