Archived

This topic is now archived and is closed to further replies.

H4ck3r

BASE64 encryption

Recommended Posts

Im searching for a function which can encrypt data to BASE64. Ive found many samples, but none of them where functions. So where can I found a function that I can use, for example: Encrypted = Base64(ToEncrypt); ? 10x...

Share this post


Link to post
Share on other sites
First to get the semantics correct: Base64 does not encrypt, it encodes.

  

#include <exception>

#include <sstream>

#include <cctype>

#include <string>

static const char* szToBase64 = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/=";
namespace Base64
{
std::string Encode(std::string strData, size_t uiLineLength = 72, std::string strEndOfLine = "\n")
{
std::ostringstream stream;
unsigned char pbBuffer[3];
size_t uiBufferPos = 0;
size_t uiCharPos = 0;

for (std::string::const_iterator pIter = strData.begin(), pIterEnd = strData.end(); pIter != pIterEnd; ++pIter)
{
pbBuffer[uiBufferPos++] = *pIter;
if (uiBufferPos == 3)
{
stream << szToBase64[(pbBuffer[0] >> 2) & 0x3F];
if (++uiCharPos >= uiLineLength)
{
uiCharPos = 0;
stream << strEndOfLine;
}

stream << szToBase64[((pbBuffer[0] << 4) & 0x30) | ((pbBuffer[1] >> 4) & 0x0F)];
if (++uiCharPos >= uiLineLength)
{
uiCharPos = 0;
stream << strEndOfLine;
}

stream << szToBase64[((pbBuffer[1] << 2) & 0x3C) | ((pbBuffer[2] >> 6) & 0x03)];
if (++uiCharPos >= uiLineLength)
{
uiCharPos = 0;
stream << strEndOfLine;
}

stream << szToBase64[pbBuffer[2] & 0x3F];
if (++uiCharPos >= uiLineLength)
{
uiCharPos = 0;
stream << strEndOfLine;
}

uiBufferPos = 0;
}
}

switch (uiBufferPos)
{
case 0:
{
}
break;

case 1:
{
stream << szToBase64[(pbBuffer[0] >> 2) & 0x3F];
if (++uiCharPos >= uiLineLength)
{
uiCharPos = 0;
stream << strEndOfLine;
}

stream << szToBase64[(pbBuffer[0] << 4) & 0x30];
if (++uiCharPos >= uiLineLength)
{
uiCharPos = 0;
stream << strEndOfLine;
}

stream << szToBase64[64];
if (++uiCharPos >= uiLineLength)
{
uiCharPos = 0;
stream << strEndOfLine;
}

stream << szToBase64[64];
if (++uiCharPos >= uiLineLength)
{
uiCharPos = 0;
stream << strEndOfLine;
}
}
break;

case 2:
{
stream << szToBase64[(pbBuffer[0] >> 2) & 0x3F];
if (++uiCharPos >= uiLineLength)
{
uiCharPos = 0;
stream << strEndOfLine;
}

stream << szToBase64[((pbBuffer[0] << 4) & 0x30) | ((pbBuffer[1] >> 4) & 0x0F)];
if (++uiCharPos >= uiLineLength)
{
uiCharPos = 0;
stream << strEndOfLine;
}

stream << szToBase64[(pbBuffer[1] << 2) & 0x3C];
if (++uiCharPos >= uiLineLength)
{
uiCharPos = 0;
stream << strEndOfLine;
}

stream << szToBase64[64];
if (++uiCharPos >= uiLineLength)
{
uiCharPos = 0;
stream << strEndOfLine;
}
}
break;

case 3:
default:
{
throw std::exception("Base64::Encode - Internal error");
}
break;
}

return stream.str();
}
}


[edited by - dalleboy on December 25, 2002 5:41:06 PM]

Share this post


Link to post
Share on other sites
Decoding would also be nice:

  

namespace Base64
{
std::string Decode(std::string strData)
{
std::ostringstream stream;
unsigned char pbBuffer[4];
size_t uiBufferPos = 0;
size_t uiEndOfMessage = 0;

for (std::string::const_iterator pIter = strData.begin(), pIterEnd = strData.end(); pIter != pIterEnd; ++pIter)
{
if (!isspace((unsigned char) *pIter))
{
// This could be optimized.

const char* pChar = strchr(szToBase64, *pIter);
if (pChar == NULL)
{
throw std::exception("Base64::Decode - Internal error");
}
else if (pChar - szToBase64 == 64)
{
++uiEndOfMessage;
if (uiEndOfMessage > 2)
{
throw std::exception("Base64::Decode - Internal error");
}
pbBuffer[uiBufferPos] = 0;
}
else if (uiEndOfMessage > 0)
{
throw std::exception("Base64::Decode - Internal error");
}
else
{
pbBuffer[uiBufferPos++] = static_cast<unsigned char>(pChar - szToBase64);
if (uiBufferPos == 4)
{
uiBufferPos = 0;
stream << (char) (((pbBuffer[0] << 2) & 0xFC) | ((pbBuffer[1] >> 4) & 0x03));
stream << (char) (((pbBuffer[1] << 4) & 0xF0) | ((pbBuffer[2] >> 2) & 0x0F));
stream << (char) (((pbBuffer[2] << 6) & 0xC0) | ((pbBuffer[3]) & 0x3F));
}
}
}
}

if ((uiBufferPos > 0) && (uiBufferPos + uiEndOfMessage != 4))
{
throw std::exception("Base64::Decode - Internal error");
}
else if (uiBufferPos > 0)
{
switch (uiEndOfMessage)
{
case 1:
{
stream << (char) (((pbBuffer[0] << 2) & 0xFC) | ((pbBuffer[1] >> 4) & 0x03));
stream << (char) (((pbBuffer[1] << 4) & 0xF0) | ((pbBuffer[2] >> 2) & 0x0F));
}
break;

case 2:
{
stream << (char) (((pbBuffer[0] << 2) & 0xFC) | ((pbBuffer[1] >> 4) & 0x03));
}
break;

case 0:
default:
{
throw std::exception("Base64::Encode - Internal error");
}
break;
}
}

return stream.str();
}
}

Share this post


Link to post
Share on other sites
By encrypting something you will make the original data more secure by adding a key (pair) and thus transforming the data into cipher text. Only those with the same cipher algorithm and the same key (pair) can decrypt the encrypted data.

By encoding something you only reorder the data and you don''t use a key, it is as secure as sending a plain text message. Anyone with the Base64 algorithm can decode the encoded message.

Share this post


Link to post
Share on other sites
OK 10x!

But one more thing. I thought std::string took a lot of space on your harddisk. Am I right? If so - is the encrypting possible without std::string?

[edited by - h4ck3r on December 26, 2002 6:21:47 AM]

Share this post


Link to post
Share on other sites
openssl has some functions for base64 encoding and a couple different ways to use them.
http://www.openssl.org

I may be wrong but I think it is mainly used for sending data over email.

kdIXfA.gamedev.10.coreyh@xoxy.net
www.ipeg.com/~rlfc

Share this post


Link to post
Share on other sites
You don''t *need* std::string, but its very useful. It doesn''t take up any space on your hard disk (because you don''t access the hard disk ), and it takes up very little memory. Its so much easier to work with than C-style strings.

Share this post


Link to post
Share on other sites
The implementation that I posted is only one of many. There are others that do not take a std::string. But you don''t need to worry about std::string taking up your hard disk, it though takes a tiny tiny amount of more memory than a null-terminated-C-string as it holds some more information.

Update system time campaign: ''date ddmmHHMMYYYY''

Share this post


Link to post
Share on other sites