#include<sys/types.h>
#include<sys/stat.h>
#include<fstream>
#include<sstream>
#include<iostream>
#include<string>
#include<cstring>
#include<stdio.h>
#include<stdlib.h>
using namespace std;
int main ( int argc, char *argv[] ){
string a=argv[1];
string op=argv[2];
string key=argv[3];
string file1=argv[4];
string file2;
string file_name;
string sys;
string filev;
string files;
string new_file_name;
string new_file_name2;
int pos = file1.find(".");
file_name = file1.substr(0, pos);
if(a=="1"){
if(op=="e"){
sys="openssl rsautl -encrypt -inkey "+ key +" -pubin -in " + file1+" -out "+file_name+".enc";
system(sys.c_str());
}
else if(op=="d")
{
sys="openssl rsautl -decrypt -inkey " + key + " -in " + file1+" -out "+file_name+".dec";
system(sys.c_str());
}
}
else if(a=="2")
{
if(op=="s"){
sys = "openssl dgst -sha512 -r -out " + file_name+".sha512 "+file1;
system(sys.c_str());
sys = "openssl rsautl -inkey " +key+" -sign -in "+ file_name+".sha512 -out "+file_name+".sign";
system(sys.c_str());
}
else if(op=="v"){
sys = "openssl dgst -sha512 -r -out " + file_name+".sha512 "+file1;
system(sys.c_str());
filev=new_file_name+".sha512";
sys = "openssl rsautl -inkey " + key + " -pubin -verify -in " + file2 + " -out " + file_name+".verify";
system(sys.c_str());
files=new_file_name2+".verify";
string namefile1=file_name+".sh512";
string namefile2=file_name+".verify";
string new_content1;
string new_content2;
int result;
stringstream strStream1;
stringstream strStream2;
ifstream inFile1;
ifstream inFile2;
strStream1 << inFile1.rdbuf();
string str1 = strStream1.str();
strStream2 << inFile2.rdbuf();
string str2 = strStream2.str();
inFile1.open(namefile1.c_str());
inFile2.open(namefile2.c_str());
int pos = str1.find("(");
new_content1 = str1.substr(0, pos);
int poss = str2.find("(");
new_content2 = str2.substr(0, poss);
if (new_content1.compare(new_content2) != 0){cout <<"verification failed"<<endl; }
else {cout <<" verification succeeded"<<endl; }
}
}
}