473,752 Members | 11,124 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Need Help with base64

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_decodes tring(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(c har)*160);
if (!*ppcdest)
{
return 1;
}
memset(*ppcdest ,0,sizeof(char) *160);

char* pcstring = malloc( sizeof(char) * SIZE);
if (!pcstring)
{
aps_log("APS_lo g.txt","\nbae64 .c:base64encode :malloc 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(c har* 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_lo g.txt","\nbae64 .c:base64encode :malloc 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.encodest ring and base64.decodest ring respectively .

I need some help on this matter . I need some pointers on
where is the mistake for the above file .

Jul 23 '07
10 4054
rp*****@yahoo.c om (Roland Pibinger) writes:
On Mon, 23 Jul 2007 16:58:46 +0100, Mark Bluemel wrote:
>>A little examination shows that 3548 and 1113 are very similar. See
http://www.faqs.org/rfcs/rfc<number>.htm l

So, assuming they are different, which is the right one? A format
primarily created for exchange of data can hardly be used in different
variants.
That's a good question (assuming that they really are significantly
different), but it's not a C question.

--
Keith Thompson (The_Other_Keit h) ks***@mib.org <http://www.ghoti.net/~kst>
San Diego Supercomputer Center <* <http://users.sdsc.edu/~kst>
"We must do something. This is something. Therefore, we must do this."
-- Antony Jay and Jonathan Lynn, "Yes Minister"
Jul 23 '07 #11

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

2
2566
by: Karl Pech | last post by:
Hi all, I'm trying to write a program which can read in files in the following format: sos_encoded.txt: --- begin-base64 644 sos.txt UGxlYXNlLCBoZWxwIG1lIQ== ---
0
3557
by: simon | last post by:
Hi I am trying to install MIME::Base64 and MIME::QuotedPrint using CPAN on Suse 9.1. Suse comes with Perl v5.8.3 already installed. The error message I receive is as follows: MOOSE:/usr/local/cpanSGH # perl -MCPAN -e "install 'MIME::Base64'" CPAN: Storable loaded ok Going to read /usr/local/cpanSGH/Metadata Database was generated on Sat, 04 Dec 2004 02:50:14 GMT
4
5364
by: John | last post by:
Hi all, I've been going through google and yahoo looking for a certain base64 decoder in C without success. What I'm after is something that you can pass a base64 encoded string into and get back a decoded String. Any help is very much appreciated. Thanks Philip.
3
11069
by: AHanso | last post by:
Hey I am new to C# (My background is in Java), I am writing a C# application (that uses the Compact Framework) that communicates to a Java server. To login the server is expecting the password to be MD5 encrypted, and then base64 encoded. The Java client is doing this as follows, and it works with the server: private static String encodePassword(String password) { try {
4
5547
by: Russell Warren | last post by:
I've got a case where I want to convert binary blocks of data (various ctypes objects) to base64 strings. The conversion calls in the base64 module expect strings as input, so right now I'm converting the binary blocks to strings first, then converting the resulting string to base64. This seems highly inefficient and I'd like to just go straight from binary to a base64 string. Here is the conversion we're using from object to...
1
3882
by: Roland Rickborn | last post by:
Hallo zusammen, in meine Anwendung ist ein Bild eingebettet und oben in der Leiste soll ein Icon erscheinen. Ausserdem will ich nur _eine_ Datei ausgeben, also ohne zusärtliche Bild-Dateien etc. Dazu habe ich das Bild in base64 codiert und als String im Skript gespeichert, siehe unten. Beim Ausführen des Skripts wird dieser String decodiert, in ein Image umgewandelt und als Bitmap dargestellt.
1
4979
by: mirandacascade | last post by:
I am attempting to implement a process, and I'm pretty sure that a major roadblock is that I do not understand the nomenclature. The specs indicate that the goal is to calculate a message digest using an SHA-256 algorithm. There are 2 examples included with the specs. The label on the 2 examples are: 'HMAC samples'. In both examples, the message on which the digest is to be calculated is (the 33 chars within the quotes): 'This is a...
4
3801
by: pycraze | last post by:
Hi , I am working on Fedora core 5 and my OS version is 2.6.15-1.2054_FC5 . My GCC version is 4.1.1 20070105 (Red Hat 4.1.1-51) . I am currently using the openssl libraries to write a simple base64 encode routine and base64 decoding routine . My openssl version is 0.9.8a 11 Oct 2005 .
3
2082
by: pycraze | last post by:
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 .
0
9031
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
9624
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
9429
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
1
9383
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
4738
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
0
4921
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
3354
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
2
2839
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
3
2243
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.