Click here to Skip to main content
11,641,387 members (63,716 online)
Click here to Skip to main content
Add your own
alternative version

Cryptographic Interoperability: Digital Signatures

, 20 Oct 2009 CPOL 130K 8.9K 170
Sign and verify messages using Crypto++, Java, and C#.
cryptoppinteropkeys.zip
CryptoPPInteropKeys
CryptoPPInteropKeys
cryptoppinteropsign.zip
CryptoPPInteropSign
CryptoPPInteropSign
csinteropkeys.zip
CSInteropKeys
CSInteropKeys
Properties
csinteropsign.zip
CSInteropSign
CSInteropSign
Properties
javainteropkeys.zip
JavaInteropKeys
manifest.mf
nbproject
genfiles.properties
private
private.properties
project.properties
src
JavaInteropKeys
test
javainteropsign.zip
JavaInteropSign
build
classes
JavaInteropSign
test
classes
results
manifest.mf
nbproject
genfiles.properties
private
private.properties
project.properties
src
JavaInteropSign
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

You may also be interested in...

| Advertise | Privacy | Terms of Use | Mobile
Web04 | 2.8.150731.1 | Last Updated 20 Oct 2009
Article Copyright 2008 by Jeffrey Walton
Everything else Copyright © CodeProject, 1999-2015
Layout: fixed | fluid