intelliproject logo

Location: Desktop development - C/C++    License: The Intelliproject Open License (IPOL)

Encoding and decoding base64 with C++

Posted by Silviu Caragea

Describes how Base64 encoding Works

Skill: Intermediate

Posted: 21/11/2008

Views: 775

Rating: 4.00 /5

Popularity: 0.00

Sign Up to vote for this article

Introduction

The Base 64 encoding is designed to represent arbitrary sequences of octets in a form that requires case sensitivity but need not be humanly readable.

A 65-character subset of US-ASCII is used, enabling 6 bits to be represented per printable character.(The extra 65th character, "=",is used to signify a special processing function.)

Here we have the Base64 characters map:

64-character subset : ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/ 
Extra character : =

base64.jpg

How does it work?

Base64 encoding takes three bytes (each byte consisting of eight bits) and represents them as four printable characters in the ASCII standard.

These transformations are making in two steps:

  1. Convert three bytes to four numbers of six bits. Each character in the ASCII code is formed from seven bits. Base64 only uses 6 bits (2^6 = 64 characters) to ensure encoded data is printable and humanly readable. None of the special characters available in ASCII are used.
  2. These numbers are converted to ASCII characters using the Base64 encoding map.

This two-step process is applied to the whole sequence of bytes that are encoded.

If the size of the original buffer is not multiple of three we should add one or two octets. For proper encoding the buffer length should be multiple of 3.

These bytes (one or two) will be equal with ‘0’. In this way we have created a 3-byte group. These artificial values ‘0’ cannot be encoded using the encoded table . They are represented by a 65th character. This character is ‘=’ and it can only appear at the end of encoded data.

The source code

Class definition

Base64 map initialization

The encode function

The decode function

Using this class

Conclusion

For more information about Base64, I suggest consulting the various RFCs. Base 64 is described in RFC 1521, http://www.ietf.org/rfc/rfc1521.txt?number=1521.

License

This article, along with any associated source code and files, is licensed under The Intelliproject Open License (IPOL)

About the author

Silviu Caragea

Silviu Caragea is the Founder, Administrator and Chief Editor who wrote and runs The IntelliProject.

He's been programming since 2000 and now he's student at The Faculty of Economic Cybernetics, Statistics and Informatics from Bucharest. In the same time he's working as software developer at Cratima Software, a Romanian software and web design company that activates both on the local and foreign market, providing its customers with software development services, internet and intranet solutions, web design, graphic design and IT consultancy.

His programming experience includes:
- C,C++, Visual C++(Win32 API, MFC, ADO, STL, DAO, ODBC, ATL, COM, DirectShow, DirectDraw, WTL)
- Open Source libraries :CURL & Boost
- HTML, CSS
- Java (SE,ME)
- JavaScript, Ajax, Google Web Toolkit (GWT)
- Php, MySQL
-Oracle, PL SQL
- C# .NET
-Objective C, IPhone SDK, Cocoa

Location: Romania
Ocupation: Software Engineer
Home page: http://www.intelliproject.net

Sign up to post message on the article message board!