Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- # spunto per creare client per la connessione a database remoti, comunicando solo dati criptati
- use strict;
- use warnings;
- use DBI;
- use Crypt::FNA;
- use feature 'say';
- system('cls');
- # istanza oggetto FNA
- my $krypto=Crypt::FNA->new(
- {
- r=> '7',
- angle => [37,60,-61,53],
- square => 4096,
- background => [255,255,255],
- foreground => [0,0,0],
- magic => 2
- }
- );
- # fine istanza oggetto FNA
- # dati per l'accesso al database
- my $user="anakadmin";
- my $pass="thewolf";
- my $dbname="dataCrypted";
- my $host="anak_mob";
- my $dsn="DBI:mysql:database=$dbname;host=$host";
- my $dbh=DBI->connect($dsn,$user,$pass,{RaiseError => 1 }) or die ("Couldn't connect to database: ".DBI->errstr);
- $dbh->{AutoCommit} = 0; # disabilita il commit
- # fine dati per l'accesso al database
- #if ($ARGV[0] eq "inserisci") {
- # &inserimennto
- #} else {
- &lettura_scrittura;
- #}
- $dbh->disconnect;
- exit;
- # FASE 1 INSERIMENTO DATI CRIPTATI DA BASE IN CHIARO
- sub inserimento {
- # preparo l'inserimento dei dati
- my $sth = $dbh->prepare(q
- {
- INSERT INTO customers (name,surname,email) VALUES (?,?,?)
- }
- ) or die $dbh->errstr;
- # fine preparo l'inserimento dei dati
- # inserisci i valori in tabella, questi devono essere criptati mediante fna
- # supponiamo siano stipati in un csv
- my $fh_csv;
- my $csv_filename="customers.csv";
- open $fh_csv,'<',$csv_filename or die "Unable to open $csv_filename: $!";
- while (!eof($fh_csv)) {
- my ($name,$surname,$email)=split(/,/,<$fh_csv>);
- say $name;
- say $surname;
- say $email;
- my @encrypted_name=$krypto->encrypt_scalar($name);
- my $encrypted_name=join('\n',@encrypted_name);
- my @encrypted_surname=$krypto->encrypt_scalar($surname);
- my $encrypted_surname=join('\n',@encrypted_surname);
- my @encrypted_email=$krypto->encrypt_scalar($email);
- my $encrypted_email=join('\n',@encrypted_email);
- say $encrypted_name;
- say "";
- say $encrypted_surname;
- say "";
- say $encrypted_email;
- say "";
- say "------------------------------------";
- say "";
- # qui li vado a inserire nel database
- $sth->execute($encrypted_name,$encrypted_surname,$encrypted_email);
- # fine qui li vado a inserire nel database
- }
- close $fh_csv;
- # salvo le modifiche nel database e chiudo la connessione
- $dbh->commit;
- # fine salvo le modifiche nel database e chiudo la connessione
- # fine supponiamo siano stipati in un csv
- # fine inserisci i valori in tabella
- }
- sub lettura_scrittura {
- # apro database criptato e ne leggo i dati decriptati, li modifico e risalvo criptato
- my $sth = $dbh->prepare(q
- {
- SELECT * FROM customers
- }
- ) or die $dbh->errstr;
- $sth->execute;
- while (my $row=$sth->fetchrow_hashref()) {
- my $name=ricostruzione_stringa($row->{name});
- my $surname=ricostruzione_stringa($row->{surname});
- my $email=ricostruzione_stringa($row->{email});
- say "$name, $surname, $email";
- say "------------------------------------";
- say ""
- }
- # a questo punto faccio quello che devo con i dati e quando li modifico li salvo criptati così come nella sub inserimento
- }
- sub ricostruzione_stringa {
- my $encrypted_field_value=shift;
- my @encrypted_scalar=split(/\\n/,$encrypted_field_value);
- # ricostruzione stringa
- my ($fh_testo_criptato,$file_criptato);
- open $fh_testo_criptato, '>',\$file_criptato or die "err. scrittura file in memoria\n";
- for (@encrypted_scalar) {print $fh_testo_criptato $_."\n"}
- close $fh_testo_criptato;
- my ($fh_testo_decriptato,$stringa_decriptata);
- $krypto->decrypt_file(\$file_criptato,\$stringa_decriptata);
- return ($stringa_decriptata)
- # end ricostruzione stringa
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement