#include <cstdio>
#include <iostream>
#include <exception>

#include "files.h"
#include "hex.h"
#include "rsa.h"
#include "integer.h"

using namespace CryptoPP;
using namespace std;

int main(int argc, char *argv[])
{
	try {
		FileSource a(argv[1], true, new HexDecoder);

		// Read PKCS#8 file and get Integers n, e, d

		InvertibleRSAFunction irsa;
		irsa.BERDecode(a);
		Integer n = irsa.GetModulus();
		Integer e = irsa.GetPublicExponent();
		Integer d = irsa.GetPrivateExponent();

		// Initialise an RSAES-PKCS1-v1_5-DECRYPT object from a PKCS#8 file.

		FileSource b(argv[1], true, new HexDecoder);
		cout << endl << "Constructing decryptor with pkcs8 FileSource: " << argv[1] << "\t:\t";
		RSAES_PKCS1v15_Decryptor Decryptor1(b);
		cout << "Success" << endl;
	
		// Initialise an RSAES-PKCS1-v1_5-DECRYPT object using the Integers from the same PKCS#8 file.

		cout << "Initializing decryptor with Integer values from: " << argv[1] << "\t:\t";
		RSAES_PKCS1v15_Decryptor Decryptor2;
		Decryptor2.AccessKey().Initialize(n, e, d);
		cout << "Success" << endl;
	}
	catch(CryptoPP::Exception &e) {
		cout << "CryptoPP::Exception caught: " << e.what() << endl;
	}
}
