Click here to Skip to main content
Click here to Skip to main content
Add your own
alternative version

Cryptographic Interoperability: Keys

, 5 Jun 2008 CPOL
Import and export Cryptographic Keys in PKCS#8 and X.509 formats, using Crypto++, C#, and Java.
cryptoppinteropkeys.zip
CryptoPPInteropKeys
CryptoPPInteropKeys
csinteropkeys.zip
CSInteropKeys
CSInteropKeys
Properties
javainteropkeys.zip
JavaInteropKeys
manifest.mf
nbproject
genfiles.properties
private
private.properties
project.properties
src
JavaInteropKeys
test
// 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

| Advertise | Privacy | Terms of Use | Mobile
Web01 | 2.8.1411028.1 | Last Updated 5 Jun 2008
Article Copyright 2008 by Jeffrey Walton
Everything else Copyright © CodeProject, 1999-2014
Layout: fixed | fluid