🔒 Locked
BASE64 encryption
Advertisement
Original Post
First to get the semantics correct: Base64 does not encrypt, it encodes.
[edited by - dalleboy on December 25, 2002 5:41:06 PM]
#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]
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();
}
}
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.
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.
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:
Update system time campaign:
''date ddmmHHMMYYYY''
Topic Locked
This topic has been locked by a moderator. New replies are not allowed.
Sign in to reply to this topic.
Advertisement