ecco il listato.
Ti preciso che il programma interagisce con GPG
codice:
file h
#ifndef TESINA_GREGORIO_H
#define TESINA_GREGORIO_H
#include <iostream>
#include <cstdlib>
/************* Librerie importate *************/
#include <fstream>
#include <stdio.h>
#include <stdlib.h>
#include <string>
#include <algorithm>
/************* per progetto *************/
using namespace std;
class tesina_gregorio{
public:
tesina_gregorio(); //Costruttore
~tesina_gregorio(); //Distruttore
//void menu();
void create_key(); //Funzione che crea una nuova coppia di chiavi
void select_and_sign();
void authenticate();
void verifica_file();
void menu();
//void leggi_file();
/* Proprietà/Variabili */
//int selection;
int length_app;
int i;
int choice;
int length;
//int menu;
string s;
string testo;
string row;
string t;
string sign;
//fstream filestream;
char * file, *path, *path1, *path2, *create_signature, *keyid, *send_key,* buffer_app, *toserver, *buffer_file, *buffer,*buffertot, *keyid1;
//string s;
private:
};
#endif
;
codice:
file cpp
#ifdef HAVE_CONFIG_H
#include <config.h>
#endif
#include "tesina_gregorio.h"
using namespace std;
tesina_gregorio::tesina_gregorio()
{
}
tesina_gregorio::~tesina_gregorio()
{
}
void tesina_gregorio::menu()
{
cout <<" MENU "<<endl;
cout <<"------------------------------------"<<endl;
cout << "A) Generare una nuova coppia di chiavi" << endl;
cout << "B) Firmare file" << endl;
cout << "C) Verifica firma digitale" << endl;
cout << "D) Esci dal programma" << endl << endl;
string opzione;
bool flag =1 ; //flag controllo ciclo while
while (flag)
{
cout<<"Inserire opzione: ";
getline(cin, opzione);
cout<< "\n";
if(opzione.length() >1)
opzione = "z"; // caso di default
switch (toupper(*opzione.c_str()) )
{
case 'A': create_key(); break; // Richiamo alla funzione crea firma
case 'B': select_and_sign(); break;//Richiamo alla funzione creazione firma
case 'C': authenticate(); break; // Richiamo alla funzione verifica firma
case 'D': cout << "Programma Terminato\n"; exit(0); break;
default: cout << "ERRORE,";
}
}
while(getchar() != '\n'); // Richiede l'inserimento dell'opzione ad ogni fine funzione
}
// METODO CREAZIONE NUOVA COPPIA DI CHIAVI
void tesina_gregorio::create_key()
{
system("gpg --gen-key");
cout << endl << endl;
cout << "***********Chiavi generate correttamente*********" << endl << endl;
/*
send_key = new char[100];
sprintf(send_key,"gpg --keyserver keyserver.ubuntu.com --send-key %s",keyid);
system(send_key);*/
cout << "Chiave pubblica inviata al keyserver.ubuntu.com" << endl << endl;
menu();
}
// METODO CREAZIONE FIRMA DIGITALE
void tesina_gregorio::select_and_sign()
{
//Inserimento file da firmare
char *file = new char[100];
cout << "Inserire il path file da firmare: " ;
cin >> file;
cout << endl;
// Verifica che il file esista
FILE *fp;
fp=fopen(file,"r");
//Richiesta di inserimento nuovo path file poichè quello inserito precedentemente era inisistente o non supportato.
if (fp == NULL)
{
do {
cout << "Errore apertura file! Inserire il path file: ";
cin >> file;
cout << endl;
fp=fopen(file,"r");
} while ( fp==NULL);
}
//Copia del private keyring sul file privatekeyring.txt
system("gpg --list-keys > /root/Tesina_Gregorio/privatekeyring.txt");
//Inserimento Key ID per effetture la firma con la chiave privata associata
keyid = new char[8];
cout << "Inserisci il Key Id della chiave che vuoi utilizzare: ";
cin >> keyid;
cout<<endl;
//Controllo dell'esistenza del Key ID associato alla chiave privata in privatekeyring.txt
t="";
ifstream in("/root/Tesina_Gregorio/privatekeyring.txt");
while(in>>row)
t += row;
bool keyFound;
keyFound = false;
//Ricerca del key ID a blocchi di 8 caratteri su privatekeyring.txt se
for(int i=0;i<t.size()-7;i++){
if(t.substr(i,8) == keyid) keyFound = true;
}
//Reinserire Key Id se non trovato in keyring.txt
while (keyFound == false)
{
cout << "KeyID non trovato,inserisci il KeyID corretto: ";
cin >> keyid;
//Ricerca Key ID in keyring.txt facendo un controllo a gruppi di 8 caratteri e spos
for(int i=0;i<t.size()-7;i++){
if(t.substr(i,8) == keyid) keyFound = true;
}
}
// CREAZIONE FIRMA DIGITALE DELL'APPLICAZIONE
create_signature = new char[strlen(file)+50];
sprintf(create_signature,"gpg -u %s --sign %s",keyid,file);
system(create_signature);
//INVIO KEY ID AL KEYSERVER
toserver = new char[1];
cout << "Voui inviare la chiave al server? digita y per inviare : ";
cin >> toserver;
// la scelta è diversa da y e n richiede nuovamente cosa fare
while (toserver[0] != 'y' and toserver[0] != 'n' ){
cout << "Voui inviare la chiave al server? digita Y o N"<<endl<<endl;
cin >> toserver;
}
// Se la scelta è y la chiave pubblica viene inviata al Keyserver
if(toserver[0] == 'y'){
send_key = new char[100];
sprintf(send_key,"gpg --keyserver keyserver.ubuntu.com --send-key %s",keyid);
system(send_key);
}
cout << endl <<endl;
cout << "*****************************FIRMA ESEGUITA CORRETTAMENTE ************" << endl << endl;
//CREAZIONE FILE CONTENENTE KEY ID E FIRMA E APPLICAZIONE
testo = file; // associazione di "file" alla stringa "testo"
sign = testo + ".gpg";//percorso file contenente firma e applicazione
// apertura stream del file firmato
ifstream is;
is.open (sign.c_str(), ios::binary );
//calcolo lunghezza file firmato
is.seekg (0, ios::end);
length_app = is.tellg();
is.seekg (0, ios::beg);
// alloca memoria buffer con dimensioni pari alla lunghezza del file firmato:
buffer_app = new char [length_app];
// leggi i dati dallo stream is e li salva su buffer_app:
is.read (buffer_app,length_app);
is.close();
length=length_app+8; //lunghezza file firmato + keyid
//allocazione memoria per file completo
buffer= new char [length];
//copia il key id sull'array buffer
memcpy(buffer,keyid,8);
//copia la parte relativa alla firma partendo dall'ottavo posto
memcpy(buffer+8, buffer_app, length_app);
// crea un file in modalità binaria e scrive il contenuto dello stream su buffer
ofstream os("/root/fileverifica",ios::binary);
os.write (buffer,length);
os.close();
//deallocazione memoria
delete [] buffer_app;
menu();
}
void tesina_gregorio::authenticate()
{
char *file = new char[100];
keyid = new char [8];
FILE *fp;
//verifica che il file esiste
cout << "Inserire il path file per verificare la firma: " ;
cin >> file;
fp=fopen(file,"r");//apertura del puntatore a file in lettura
if (fp == NULL)
{
//richiede l'inserimento di un nuovo file finchè non si inserisce quello corretto
do {
cout << "Errore apertura file! Inserire il path file corretto: ";
cin >> file;
cout << endl;
fp=fopen(file,"r");
}while (fp==NULL);
}
// CREAZIONE FILE ORIGINARIO E ESTRAZIONE KEYID
testo = file; //associazione array file a stringa testo
ifstream is;
// apro il file il cui percorso è salvato sulla stringa testo
is.open (testo.c_str(), ios::binary );
//calcolo lunghezza del file caricato
is.seekg (0, ios::end);
length_app = is.tellg();
is.seekg (0, ios::beg);
// alloca memoria al char buffer con dimensioni pari alla lunghezza del fiel
buffer_app = new char [length_app];
// leggi i dati dallo stream e li salva su buffer_app:
is.read (buffer_app,length_app);
is.close(); // chiude lo stream
//alloca memoria per array che conterra solo il contenuto del file senza il keyid
buffer= new char [length_app-8];
//copia i primi 8 caratteri di buffer_app su keyid
memcpy(keyid, buffer_app,8);
//copia sull'array buffer i caratteri del file caricato dal'ottavo posto in poi
memcpy(buffer, buffer_app+8, length_app-8);
//Riscrive il file contenente solo il file da verificare
ofstream os("/root/mia_applicazione",ios::binary);
os.write (buffer,length_app-8);
os.close();
//dealloca la memoria
delete [] buffer;
//RICHIESTA AL SERVER DELLA CHIAVE PUBBLICA
path2 = new char[100];
sprintf(path2,"gpg --keyserver keyserver.ubuntu.com --recv-key 0x%s",keyid);
system(path2);
// VERIFICA DELLA FIRMA DEL FILE INSERITO
path1 = new char[100];
sprintf(path1,"gpg --verify /root/mia_applicazione");
system(path1);
cout << endl << endl;
menu();
}
codice:
file main
#ifdef HAVE_CONFIG_H
#include <config.h>
#endif
#include "tesina_gregorio.h"
using namespace std;
int main()
{
cout << "Tesina di Criptografia e Sicurezza delle Reti di : Indelicato Gregorio"<<endl<<endl;
tesina_gregorio* crypto;
crypto = new tesina_gregorio();
crypto->menu();
}