<?xml version="1.0" encoding="UTF-8"?><rss version="2.0"
	xmlns:content="http://purl.org/rss/1.0/modules/content/"
	xmlns:dc="http://purl.org/dc/elements/1.1/"
	xmlns:atom="http://www.w3.org/2005/Atom"
	xmlns:sy="http://purl.org/rss/1.0/modules/syndication/"
		>
<channel>
	<title>Comments on: Howto base64 decode with C/C++ and OpenSSL</title>
	<atom:link href="http://www.ioncannon.net/programming/122/howto-base64-decode-with-cc-and-openssl/feed/" rel="self" type="application/rss+xml" />
	<link>http://www.ioncannon.net/programming/122/howto-base64-decode-with-cc-and-openssl/</link>
	<description>Thoughts on Software Development and Engineering</description>
	<lastBuildDate>Tue, 16 Mar 2010 00:32:58 +0000</lastBuildDate>
	
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
		<item>
		<title>By: Heavy</title>
		<link>http://www.ioncannon.net/programming/122/howto-base64-decode-with-cc-and-openssl/comment-page-1/#comment-153337</link>
		<dc:creator>Heavy</dc:creator>
		<pubDate>Thu, 01 Oct 2009 16:32:44 +0000</pubDate>
		<guid isPermaLink="false">http://www.ioncannon.net/cc/122/howto-base64-decode-with-cc-and-openssl/#comment-153337</guid>
		<description>I like to add that doing that :

// Calculate line size
std::string strData(ptrOut);
size_t lineLen = strData.find(&quot;\n&quot;);
if(lineLen == (size_t) MPSStringType::npos)

is a verry dangerous thing which will probably crash under a lot of circumstances. So it is a lot safer to just input the line len into the function.It can differ between 64 = PEM newline padding,and 72 for some other standard i cant recall right now X). Although there are some type mißmatches i forgot to clean out.  
The input types i used where:
typedef std::string MPSStringType and
typedef char MPSCharType

Have fun ;)</description>
		<content:encoded><![CDATA[<p>I like to add that doing that :</p>
<p>// Calculate line size<br />
std::string strData(ptrOut);<br />
size_t lineLen = strData.find(&#034;\n&#034;);<br />
if(lineLen == (size_t) MPSStringType::npos)</p>
<p>is a verry dangerous thing which will probably crash under a lot of circumstances. So it is a lot safer to just input the line len into the function.It can differ between 64 = PEM newline padding,and 72 for some other standard i cant recall right now X). Although there are some type mißmatches i forgot to clean out.<br />
The input types i used where:<br />
typedef std::string MPSStringType and<br />
typedef char MPSCharType</p>
<p>Have fun ;)</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: Heavy</title>
		<link>http://www.ioncannon.net/programming/122/howto-base64-decode-with-cc-and-openssl/comment-page-1/#comment-153321</link>
		<dc:creator>Heavy</dc:creator>
		<pubDate>Thu, 01 Oct 2009 13:03:18 +0000</pubDate>
		<guid isPermaLink="false">http://www.ioncannon.net/cc/122/howto-base64-decode-with-cc-and-openssl/#comment-153321</guid>
		<description>Hi 
I had some trouble with the openssl base 64 de/encoding too. This was because openssl does no &#039;pure&#039; base64 encoding by default but uses the pem standard base64 encoding scheme ( this is why adding the lines from roxas will make it work in the most cases because the newlines are ignored ) but if you want to decode base64 that is used inside a certificate or mail it will be encoded after the pem standard which requires a newline after 64 bytes of base64 data. This makes encoding a bit tricky because you have to calculate the lenght of the encoded data yourself ( or i yust did not find the propper openssl routine to do that ;) 

However here is some code i wrote that does the job. Feel free to use it ..

size_t MPSCalculateBase64Size(size_t inLen,int iLineSize)
{
		double dInLen = inLen;
		double dLineSize = iLineSize;
		double dAdd = 0.0;
		double dLen = 0.0;
		size_t iResult = 0;

		double dModIn = fmod( dInLen,3.0);

		if( dModIn )
			dLen = ( ((( dInLen + 3.0 ) - fmod( dInLen, 3.0 )) / 3.0 ) * 4.0 );
		else
			dLen = ( ( dInLen  / 3.0 ) * 4.0 ) ;

		
		dAdd = dLen;
		
		// If line size is != 0 the we must add the newlines to our resulting
		// data length
		if( iLineSize != 0)
		{
			// Add the newlines required after the pem/base64 standard
			// (after 64 chars)
			dAdd += ( dLen / dLineSize );
			
			// Check for incomplete line which is although terminated with a 
			// \n (PEM Base64).
			double dModLine = fmod(dLen,dLineSize);
			if( dModLine )
			{
				dAdd += 1.0;
			}
		}

		iResult = (size_t) dAdd;
		return iResult;
	}

	int MPSBase64EncodeStr(const MPSCharType* ptrDataIn,size_t inLen,MPSStringType&amp; strResult,size_t&amp; stResultSize)
	{
		if(! ptrDataIn)
			return MPS_ERR_PARAM;

		BIO *bio = NULL;
		BIO	*b64 = NULL;
		
		b64 = BIO_new(BIO_f_base64());
		bio = BIO_new( BIO_s_mem() );
		bio = BIO_push(b64, bio);
		
		size_t rLen = BIO_write(bio, ptrDataIn, (int) inLen);
		BIO_flush(bio);

		char* ptrOut = NULL;
		BIO_get_mem_data(bio, &amp;ptrOut);

		if(ptrOut)
		{
			// Calculate line size
			std::string strData(ptrOut);
			size_t lineLen = strData.find(&quot;\n&quot;);
			if(lineLen == (size_t) MPSStringType::npos)
			{
				lineLen = 0;
			}

			int size = (int) MPSCalculateBase64Size( inLen, (int)lineLen ) ;
			
			strResult.insert(0,ptrOut,size);
			stResultSize = size;
		}

		BIO_free_all(bio);
		
		return MPS_OK;
	}

	int MPSBase64DecodeStr(const MPSCharType* ptrDataIn,size_t inLen,MPSStringType&amp; strResult,size_t&amp; stResultSize)
	{
		if(! ptrDataIn)
			return MPS_ERR_PARAM;

		BIO* b64 = NULL; 
		BIO* bmem = NULL;
		char *buffer = (char *) malloc(inLen);
		if(buffer)
		{
			memset(buffer, 0, inLen);
			b64 = BIO_new(BIO_f_base64());
			bmem = BIO_new_mem_buf((void*) ptrDataIn, (int) inLen);
			bmem = BIO_push(b64, bmem);

			size_t rLen = BIO_read(bmem, buffer,(int) inLen);
			
			strResult.insert(0, (const char*) buffer, rLen);
			stResultSize = rLen;

			BIO_free_all(bmem);
			delete [] buffer,buffer = NULL;
		
			return MPS_OK;
		}
		return MPS_ERR;

	}</description>
		<content:encoded><![CDATA[<p>Hi<br />
I had some trouble with the openssl base 64 de/encoding too. This was because openssl does no &#039;pure&#039; base64 encoding by default but uses the pem standard base64 encoding scheme ( this is why adding the lines from roxas will make it work in the most cases because the newlines are ignored ) but if you want to decode base64 that is used inside a certificate or mail it will be encoded after the pem standard which requires a newline after 64 bytes of base64 data. This makes encoding a bit tricky because you have to calculate the lenght of the encoded data yourself ( or i yust did not find the propper openssl routine to do that ;) </p>
<p>However here is some code i wrote that does the job. Feel free to use it ..</p>
<p>size_t MPSCalculateBase64Size(size_t inLen,int iLineSize)<br />
{<br />
		double dInLen = inLen;<br />
		double dLineSize = iLineSize;<br />
		double dAdd = 0.0;<br />
		double dLen = 0.0;<br />
		size_t iResult = 0;</p>
<p>		double dModIn = fmod( dInLen,3.0);</p>
<p>		if( dModIn )<br />
			dLen = ( ((( dInLen + 3.0 ) &#8211; fmod( dInLen, 3.0 )) / 3.0 ) * 4.0 );<br />
		else<br />
			dLen = ( ( dInLen  / 3.0 ) * 4.0 ) ;</p>
<p>		dAdd = dLen;</p>
<p>		// If line size is != 0 the we must add the newlines to our resulting<br />
		// data length<br />
		if( iLineSize != 0)<br />
		{<br />
			// Add the newlines required after the pem/base64 standard<br />
			// (after 64 chars)<br />
			dAdd += ( dLen / dLineSize );</p>
<p>			// Check for incomplete line which is although terminated with a<br />
			// \n (PEM Base64).<br />
			double dModLine = fmod(dLen,dLineSize);<br />
			if( dModLine )<br />
			{<br />
				dAdd += 1.0;<br />
			}<br />
		}</p>
<p>		iResult = (size_t) dAdd;<br />
		return iResult;<br />
	}</p>
<p>	int MPSBase64EncodeStr(const MPSCharType* ptrDataIn,size_t inLen,MPSStringType&amp; strResult,size_t&amp; stResultSize)<br />
	{<br />
		if(! ptrDataIn)<br />
			return MPS_ERR_PARAM;</p>
<p>		BIO *bio = NULL;<br />
		BIO	*b64 = NULL;</p>
<p>		b64 = BIO_new(BIO_f_base64());<br />
		bio = BIO_new( BIO_s_mem() );<br />
		bio = BIO_push(b64, bio);</p>
<p>		size_t rLen = BIO_write(bio, ptrDataIn, (int) inLen);<br />
		BIO_flush(bio);</p>
<p>		char* ptrOut = NULL;<br />
		BIO_get_mem_data(bio, &amp;ptrOut);</p>
<p>		if(ptrOut)<br />
		{<br />
			// Calculate line size<br />
			std::string strData(ptrOut);<br />
			size_t lineLen = strData.find(&#034;\n&#034;);<br />
			if(lineLen == (size_t) MPSStringType::npos)<br />
			{<br />
				lineLen = 0;<br />
			}</p>
<p>			int size = (int) MPSCalculateBase64Size( inLen, (int)lineLen ) ;</p>
<p>			strResult.insert(0,ptrOut,size);<br />
			stResultSize = size;<br />
		}</p>
<p>		BIO_free_all(bio);</p>
<p>		return MPS_OK;<br />
	}</p>
<p>	int MPSBase64DecodeStr(const MPSCharType* ptrDataIn,size_t inLen,MPSStringType&amp; strResult,size_t&amp; stResultSize)<br />
	{<br />
		if(! ptrDataIn)<br />
			return MPS_ERR_PARAM;</p>
<p>		BIO* b64 = NULL;<br />
		BIO* bmem = NULL;<br />
		char *buffer = (char *) malloc(inLen);<br />
		if(buffer)<br />
		{<br />
			memset(buffer, 0, inLen);<br />
			b64 = BIO_new(BIO_f_base64());<br />
			bmem = BIO_new_mem_buf((void*) ptrDataIn, (int) inLen);<br />
			bmem = BIO_push(b64, bmem);</p>
<p>			size_t rLen = BIO_read(bmem, buffer,(int) inLen);</p>
<p>			strResult.insert(0, (const char*) buffer, rLen);<br />
			stResultSize = rLen;</p>
<p>			BIO_free_all(bmem);<br />
			delete [] buffer,buffer = NULL;</p>
<p>			return MPS_OK;<br />
		}<br />
		return MPS_ERR;</p>
<p>	}</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: roxaz</title>
		<link>http://www.ioncannon.net/programming/122/howto-base64-decode-with-cc-and-openssl/comment-page-1/#comment-151972</link>
		<dc:creator>roxaz</dc:creator>
		<pubDate>Sat, 19 Sep 2009 12:41:11 +0000</pubDate>
		<guid isPermaLink="false">http://www.ioncannon.net/cc/122/howto-base64-decode-with-cc-and-openssl/#comment-151972</guid>
		<description>add:
	BIO_set_flags(b64, BIO_FLAGS_BASE64_NO_NL);
after:
	b64 = BIO_new(BIO_f_base64());

and this will work.</description>
		<content:encoded><![CDATA[<p>add:<br />
	BIO_set_flags(b64, BIO_FLAGS_BASE64_NO_NL);<br />
after:<br />
	b64 = BIO_new(BIO_f_base64());</p>
<p>and this will work.</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: Lucas</title>
		<link>http://www.ioncannon.net/programming/122/howto-base64-decode-with-cc-and-openssl/comment-page-1/#comment-142417</link>
		<dc:creator>Lucas</dc:creator>
		<pubDate>Wed, 24 Jun 2009 14:58:55 +0000</pubDate>
		<guid isPermaLink="false">http://www.ioncannon.net/cc/122/howto-base64-decode-with-cc-and-openssl/#comment-142417</guid>
		<description>Same Problem here! nothing readed!</description>
		<content:encoded><![CDATA[<p>Same Problem here! nothing readed!</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: carson</title>
		<link>http://www.ioncannon.net/programming/122/howto-base64-decode-with-cc-and-openssl/comment-page-1/#comment-142326</link>
		<dc:creator>carson</dc:creator>
		<pubDate>Mon, 22 Jun 2009 13:39:11 +0000</pubDate>
		<guid isPermaLink="false">http://www.ioncannon.net/cc/122/howto-base64-decode-with-cc-and-openssl/#comment-142326</guid>
		<description>@Carlo

Can you provide more information? I&#039;ve tested to make sure it works with the latest version of OpenSSL.</description>
		<content:encoded><![CDATA[<p>@Carlo</p>
<p>Can you provide more information? I&#039;ve tested to make sure it works with the latest version of OpenSSL.</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: Carlo Pires</title>
		<link>http://www.ioncannon.net/programming/122/howto-base64-decode-with-cc-and-openssl/comment-page-1/#comment-142111</link>
		<dc:creator>Carlo Pires</dc:creator>
		<pubDate>Fri, 19 Jun 2009 01:11:41 +0000</pubDate>
		<guid isPermaLink="false">http://www.ioncannon.net/cc/122/howto-base64-decode-with-cc-and-openssl/#comment-142111</guid>
		<description>This doesn&#039;t work to me. BIO_read always returns 0 bytes readed.</description>
		<content:encoded><![CDATA[<p>This doesn&#039;t work to me. BIO_read always returns 0 bytes readed.</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: G</title>
		<link>http://www.ioncannon.net/programming/122/howto-base64-decode-with-cc-and-openssl/comment-page-1/#comment-112854</link>
		<dc:creator>G</dc:creator>
		<pubDate>Wed, 31 Dec 2008 10:06:05 +0000</pubDate>
		<guid isPermaLink="false">http://www.ioncannon.net/cc/122/howto-base64-decode-with-cc-and-openssl/#comment-112854</guid>
		<description>Used this for a project of mine and had a bit of trouble getting it to work.  If anyone is tearing their hair out wondering why no errors are being reported but no data is being delivered, try changing

bmem = BIO_push(b64, bmem);

to

BIO_push(b64, bmem);</description>
		<content:encoded><![CDATA[<p>Used this for a project of mine and had a bit of trouble getting it to work.  If anyone is tearing their hair out wondering why no errors are being reported but no data is being delivered, try changing</p>
<p>bmem = BIO_push(b64, bmem);</p>
<p>to</p>
<p>BIO_push(b64, bmem);</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: saleem</title>
		<link>http://www.ioncannon.net/programming/122/howto-base64-decode-with-cc-and-openssl/comment-page-1/#comment-63379</link>
		<dc:creator>saleem</dc:creator>
		<pubDate>Wed, 16 Apr 2008 05:05:48 +0000</pubDate>
		<guid isPermaLink="false">http://www.ioncannon.net/cc/122/howto-base64-decode-with-cc-and-openssl/#comment-63379</guid>
		<description>Hi,
Thanks for the code.

Could you please share some code, on how to decode base64 encode DER format to X509.

Any help will be great.

Thanks,
Saleem</description>
		<content:encoded><![CDATA[<p>Hi,<br />
Thanks for the code.</p>
<p>Could you please share some code, on how to decode base64 encode DER format to X509.</p>
<p>Any help will be great.</p>
<p>Thanks,<br />
Saleem</p>
]]></content:encoded>
	</item>
</channel>
</rss>
