0

I'm trying to figure out how to generate and use an RSA key with Crypto++. To be precise, I am being asked to:

  1. You will implement secure communications between two parties, Alice and Bob. For simplicity, the sending of a message from the sender to the receiver will be simulated
    through writing the message into a file by the sender and reading the message from the file by the receiver. This assignment is designed to practice key distribution, encryption/decryption,
    and integrity protection with secret key cryptography and public key cryptography.

  2. Communication scenario: Alice (as a Client) needs to send messages to Bob (as a Server). Alice and Bob each have a pair of under the RSA cryptosystem (their key pairs are different), and they know each other’s public key beforehand (the public keys can be hard coded into the program or read from a file). Step 1: Set up a shared secret key: Alice and Bob set up a shared secret key using the following
    method: Alice generates a random key k, encrypts it using Bob’s public key with the RSA algorithm, and sends the ciphertext to Bob. Bob receives the ciphertext and then decrypts it to get the key k.

So, I've spent a few hours trying to set this up properly, and it seems like I've finally gotten Eclipse to accept the Crypto++ library itself, as it has compiled up to this point. Here is the complete code I have so far:

#include <iostream>
#include "cryptlib.h"
#include <rsa.h>
#include <osrng.h>
#include <base64.h>
#include <files.h>
using namespace CryptoPP;


int main()
{
    std::cout << "!!!Hello World!!!" << std::endl; // prints !!!Hello World!!!

     // InvertibleRSAFunction is used directly only because the private key
     // won't actually be used to perform any cryptographic operation;
     // otherwise, an appropriate typedef'ed type from rsa.h would have been used.
     AutoSeededRandomPool rng;
     InvertibleRSAFunction privkey;
     privkey.Initialize(rng, 1024);  <--

     // With the current version of Crypto++, MessageEnd() needs to be called
     // explicitly because Base64Encoder doesn't flush its buffer on destruction.
     Base64Encoder privkeysink(new FileSink("c:\\privkey.txt"));
     privkey.DEREncode(privkeysink);
     privkeysink.MessageEnd();

     // Suppose we want to store the public key separately,
     // possibly because we will be sending the public key to a third party.
     RSAFunction pubkey(privkey);

     Base64Encoder pubkeysink(new FileSink("c:\\pubkey.txt"));
     pubkey.DEREncode(pubkeysink); <--
     pubkeysink.MessageEnd();

    return 0;
}

Some of this I have pulled from here in an attempt to understand exactly how this library works: https://www.cryptopp.com/wiki/User_Guide:_rsa.h

(We were given no sort of primer, and I find the site a little confusing)

The current issue I'm having at the moment is that the two lines I've marked with '<--' do not compile.

Specifically:

undefined reference to CryptoPP::InvertibleRSAFunction::Initialize(CryptoPP::RandomNumberGenerator&, unsigned int, CryptoPP::Integer const&)

undefined reference to CryptoPP::X509PublicKey::DEREncode(CryptoPP::BufferedTransformation&) const

I would ask what I would need to do to make these two compile, but I'm not even sure this sample code even does what I want it to. How would I generate the keys I need for this?

jww
  • 97,681
  • 90
  • 411
  • 885
Razmode
  • 119
  • 11
  • Which version of Crypto++ are you using ? – Blacktempel Apr 19 '18 at 04:44
  • 1
    @Blacktempel Crypto++ 7.0.0 – Razmode Apr 19 '18 at 04:46
  • 3
    Sounds like the code is compiling but not linking. Have you added crypto++ to your link command? – Alan Birtles Apr 19 '18 at 07:06
  • @Raznarok I have no problem compiling this code with v7.0.0. Have you tried compiling it with something else than Eclipse ? Also, how are you compiling Crypto++ ? As library, DLL, exe with your main... ? – Blacktempel Apr 19 '18 at 10:00
  • @Blacktempel only Eclipse so far. I'm compiling crypto as a library. – Razmode Apr 19 '18 at 12:55
  • I think @AlanBirtles is probably right. Also see [How to configure Crypto++ in Eclipse?](https://stackoverflow.com/q/7847835/608639), [Adding standard libraries to C++ eclipse project](https://stackoverflow.com/q/6869570/608639), [Cooperation between Eclipse and Crypto++](https://stackoverflow.com/q/18968579/608639), [Configuring Eclipse to use Crypto++](https://groups.google.com/d/msg/cryptopp-users/xct66aqkvfo/nvsy46qKzbcJ), etc. – jww Apr 20 '18 at 01:24
  • 1
    Also see [RSA Cryptography](https://www.cryptopp.com/wiki/RSA_Cryptography) and [RSA Encryption Schemes](https://www.cryptopp.com/wiki/RSA_Encryption_Schemes) on the Cryto++ wiki. The User Guide you are using is a little old. It is from the Crypto++ 3.x and 4.x days. – jww Apr 20 '18 at 01:52
  • @jww those last two you linked has one of code snippets I also tried. – Razmode Apr 20 '18 at 03:03
  • @AlanBirtles I'm not sure I follow. – Razmode Apr 20 '18 at 03:17
  • @Raznarok Please check the links jww has posted. Did you try these yet ? You will have to add the Crypto++ library to your linker. – Blacktempel Apr 20 '18 at 04:29
  • @Blacktempel Linker? – Razmode Apr 20 '18 at 14:22
  • @Raznarok Are you able to upload a [MCVE](https://stackoverflow.com/help/mcve) ? Zip the project and upload it. You can do it without Crypto++, just preserve the paths. – Blacktempel Apr 23 '18 at 05:18

0 Answers0