Click here to Skip to main content
13,589,560 members
Click here to Skip to main content

Stats

204.9K views
12.1K downloads
179 bookmarked
Posted 22 Apr 2008
Licenced CPOL

Cryptographic Interoperability: Keys

, 5 Jun 2008
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)

Share

About the Author

Jeffrey Walton
Systems / Hardware Administrator
United States United States
No Biography provided

You may also be interested in...

Permalink | Advertise | Privacy | Cookies | Terms of Use | Mobile
Web02 | 2.8.180615.1 | Last Updated 5 Jun 2008
Article Copyright 2008 by Jeffrey Walton
Everything else Copyright © CodeProject, 1999-2018
Layout: fixed | fluid