-1

I'm trying to implement RSA Algorithm using Crypto++ under Ubuntu 12.04 I managed to implement both encryption and decryption in one program. Is there any way so that I can separate the encryption and decryption? What I want is when I call the encryption part, it will create a ciphertext as an output, and then when I call the decryption part, it will take the ciphertext from encryption as an input, and If I call the decryption part first, then it will create an error message.

This is the code for encryption and decryption:

#include <iostream>
using std::cout;
using std::endl;

#include <iomanip>
using std::hex;

#include <string>
using std::string;

#include "rsa.h"
using CryptoPP::RSA;

#include "integer.h"
using CryptoPP::Integer;

#include "osrng.h"
using CryptoPP::AutoSeededRandomPool;

int main(int argc, char** argv)
{

    // Pseudo Random Number Generator
    AutoSeededRandomPool rng;

    ///////////////////////////////////////
    // Generate Parameters
    CryptoPP::InvertibleRSAFunction params;
    params.GenerateRandomWithKeySize(rng, 3072);

    ///////////////////////////////////////
    // Generated Parameters
    const Integer& n = params.GetModulus();
    const Integer& p = params.GetPrime1();
    const Integer& q = params.GetPrime2();
    const Integer& d = params.GetPrivateExponent();
    const Integer& e = params.GetPublicExponent();

    cout << endl;

    ///////////////////////////////////////
    // Create Keys
    RSA::PrivateKey privateKey(params);
    RSA::PublicKey publicKey(params);
    /////////////////////////////////////////////////////////

    string message, recovered;
    Integer m, c, r;

    cout << endl;
    cout << "RSA Algorithm" << endl;
    cout << "message: " ;
    std::cin >> message;

    // Treat the message as a big endian array
    m = Integer((const byte *)message.data(), message.size());
    cout << "plaintext: " << hex << m << endl << endl;

    cout << "ENCRYPTION" << endl;
    // Encrypt
    c = publicKey.ApplyFunction(m);
    cout << "cipherthext: " << hex << c << endl << endl;

    cout << "DECRYPTION" << endl;
    // Decrypt
    r = privateKey.CalculateInverse(rng, c);
    cout << "plaintext: " << hex << r << endl;

    // Round trip the message
    size_t req = r.MinEncodedSize();
    recovered.resize(req);
    r.Encode((byte *)recovered.data(), recovered.size());

    cout << "recovered: " << recovered << endl; 

    return 0;
}

I appreciate any help. Thank you.

dulipat
  • 171
  • 2
  • 4
  • 12
  • So, are you asking how to write two different programs and pipe them together? – Drew McGowen Jul 24 '13 at 15:44
  • Not that it shouldn' already be apparent, but your separate decryption will require the private key be persisted and retrieved for that purpose. Currently you're generating the key-pair on the fly. I would suggest you consider a secured key-store. Or better still, use symmetric encryption (I'm not sure there is a reason you're using RSA for this, but pipelined encryption is not really what it is made for). – WhozCraig Jul 24 '13 at 15:46
  • @Drew yes, I want to know how to write two different program and pipe them together, but just like what WhozCraig said, I need to somehow store the encryption result and the private key for decryption, which is I don't know how. – dulipat Jul 25 '13 at 09:44

1 Answers1

0

You can either

  1. Write two programs, each with their own main.
  2. Use the argvyou send into main to tell it what you want it to do.

For option 2, there is another question here. In essence, after checking the arg count, argc, and remembering argv[0] is your program, you could say

if(strcmp(argv[1], "ENCRYPTION")==0)
{
//... do ENCRYPTION
}...
Community
  • 1
  • 1
doctorlove
  • 18,872
  • 2
  • 46
  • 62