Click here to Skip to main content
15,884,933 members
Articles / Programming Languages / C++

Product Keys Based on the Advanced Encryption Standard (AES)

Rate me:
Please Sign up or sign in to vote.
4.97/5 (69 votes)
12 Jun 2007CPOL11 min read 299.9K   25.3K   352  
A Compact Product Key System Based on AES and Crypto++
// BaseExp.cpp : Defines the entry point for the console application.
//
#include "stdafx.h"

// Runtime Includes
#include <iostream>
#include <string>

// Crypto++ Library
#ifdef _DEBUG
#  pragma comment ( lib, "cryptlibd" )
#else
#  pragma comment ( lib, "cryptlib" )
#endif

// Crypto++ Includes
#include "cryptlib.h"
#include "osrng.h"      // Random Number Generator
#include "base32.h"     // Encoding-Decoding
#include "base64.h"     // Encoding-Decoding

int main(int argc, char* argv[]) {

    try {

        const unsigned int BUFFER_SIZE = 1024 * 32;

        CryptoPP::AutoSeededRandomPool rng;
        byte cbBuffer[ BUFFER_SIZE ];

        rng.GenerateBlock( cbBuffer, BUFFER_SIZE );

        // Base 32 Encoding
        CryptoPP::Base32Encoder Encoder32;
        Encoder32.Put( cbBuffer, BUFFER_SIZE );
        Encoder32.MessageEnd();
        unsigned int Encoded32TextLength = Encoder32.MaxRetrievable();


        // Base 64 Encoding
        CryptoPP::Base64Encoder Encoder64;
        Encoder64.Put( cbBuffer, BUFFER_SIZE );
        Encoder64.MessageEnd();
        unsigned int Encoded64TextLength = Encoder64.MaxRetrievable();

        std::cout << "Statistics (" << BUFFER_SIZE << " bytes):" << std::endl;

        std::cout << "    Base 32: " << Encoded32TextLength << " bytes ";
        std::cout << static_cast<float>( Encoded32TextLength - BUFFER_SIZE ) * 100 / BUFFER_SIZE << "%" << std::endl;

        std::cout << "    Base 64: " << Encoded64TextLength << " bytes ";
        std::cout << static_cast<float>( Encoded64TextLength - BUFFER_SIZE ) * 100 / BUFFER_SIZE << "%" << std::endl;

    }

    catch( CryptoPP::Exception& e ) {
        std::cerr << "Crypto++ Error: " << e.what() << std::endl;
    }

    catch (...) {
        std::cerr << "Unknown Error" << std::endl;
    }

	return 0;
}

By viewing downloads associated with this article you agree to the Terms of Service and the article's licence.

If a file you wish to view isn't highlighted, and is a text file (not binary), please let us know and we'll add colourisation support for it.

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)


Written By
Systems / Hardware Administrator
United States United States
This member has not yet provided a Biography. Assume it's interesting and varied, and probably something to do with programming.

Comments and Discussions