Click here to Skip to main content
15,881,380 members
Articles / Desktop Programming / Win32

Cryptographic Interoperability: Keys

Rate me:
Please Sign up or sign in to vote.
5.00/5 (63 votes)
5 Jun 2008CPOL33 min read 300.4K   14.2K   192  
Import and export Cryptographic Keys in PKCS#8 and X.509 formats, using Crypto++, C#, and Java.
// CryptoPP.cpp
//
#include "stdafx.h"

#include <osrng.h>
using CryptoPP::AutoSeededRandomPool;

#include <rsa.h>
using CryptoPP::RSAFunction;
typedef CryptoPP::InvertibleRSAFunction RSAPrivateKey;

using CryptoPP::InvertibleRSAFunction;
typedef CryptoPP::RSAFunction RSAPublicKey;

#include <files.h>
using CryptoPP::FileSource;
using CryptoPP::FileSink;

bool GenerateAndSaveKeys();
bool LoadKeys();

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

    LoadKeys();

    return 0;
}

bool GenerateAndSaveKeys()
{
    try
    {
        AutoSeededRandomPool prng;

        // Private Key
        RSAPrivateKey privateKey;
        privateKey.Initialize( prng, 1024 /*, e=17*/);

        // Save as PKCS #8 (using ASN.1 DER Encoding )
        privateKey.Save( FileSink("private.rsa.cpp.key") );

        // Public Key
        RSAPublicKey publicKey( privateKey );

        // Save as X.509 (using ASN.1 DER Encoding )
        publicKey.Save( FileSink("public.rsa.cpp.key") );

        // Incorrect...
        // publicKey.DEREncodePublicKey ( FileSink("public.rsa.cpp.key") );

        cout << "Saved key with modulus: " << endl;
        cout << privateKey.GetModulus() << endl;
        cout << endl;
    }

    catch( CryptoPP::Exception& ex )
    {
        cout << "Caught Error:" << endl;
        cout << " " << ex.what() << endl;

        return false;
    }

    return true;
}

bool LoadKeys()
{
    try
    {
        // Private Key
        RSAPrivateKey privateKey;
        // Load as PKCS #8 (using ASN.1 BER Decoding )
        privateKey.Load( FileSource( "private.rsa.cpp.key", true ) );

        // Public Key
        RSAPublicKey publicKey;
        // Load as X.509 (using ASN.1 BER Decoding )
        publicKey.Load( FileSource( "public.rsa.cpp.key", true ) );

        // Incorrect...
        // The 2nd and 3rd parameters to BERDecodePublicKey are not used,
        //  so they can be anything. See RSAFunction::BERDecodePublicKey
        // publicKey.BERDecodePublicKey(FileSource( "public.rsa.cpp.key", true ), false, 0);

        cout << "Loaded key with modulus: " << endl;
        cout << privateKey.GetModulus() << endl;
        cout << endl;
    }

    catch( CryptoPP::Exception& ex )
    {
        cout << "Caught Error:" << endl;
        cout << " " << ex.what() << endl;

        return false;
    }

    return true;
}

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