Advertisement
Guest User

Untitled

a guest
May 30th, 2017
112
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.76 KB | None | 0 0
  1. # spunto per creare client per la connessione a database remoti, comunicando solo dati criptati
  2.  
  3. use strict;
  4. use warnings;
  5.  
  6. use DBI;
  7. use Crypt::FNA;
  8.  
  9. use feature 'say';
  10.  
  11. system('cls');
  12.  
  13. # istanza oggetto FNA
  14.  
  15. my $krypto=Crypt::FNA->new(
  16. {
  17. r=> '7',
  18. angle => [37,60,-61,53],
  19. square => 4096,
  20. background => [255,255,255],
  21. foreground => [0,0,0],
  22. magic => 2
  23. }
  24. );
  25.  
  26. # fine istanza oggetto FNA
  27.  
  28. # dati per l'accesso al database
  29.  
  30. my $user="anakadmin";
  31. my $pass="thewolf";
  32.  
  33. my $dbname="dataCrypted";
  34. my $host="anak_mob";
  35.  
  36. my $dsn="DBI:mysql:database=$dbname;host=$host";
  37. my $dbh=DBI->connect($dsn,$user,$pass,{RaiseError => 1 }) or die ("Couldn't connect to database: ".DBI->errstr);
  38. $dbh->{AutoCommit} = 0; # disabilita il commit
  39.  
  40. # fine dati per l'accesso al database
  41.  
  42. #if ($ARGV[0] eq "inserisci") {
  43. # &inserimennto
  44. #} else {
  45. &lettura_scrittura;
  46. #}
  47.  
  48. $dbh->disconnect;
  49.  
  50. exit;
  51.  
  52. # FASE 1 INSERIMENTO DATI CRIPTATI DA BASE IN CHIARO
  53.  
  54. sub inserimento {
  55.  
  56. # preparo l'inserimento dei dati
  57.  
  58. my $sth = $dbh->prepare(q
  59. {
  60. INSERT INTO customers (name,surname,email) VALUES (?,?,?)
  61. }
  62. ) or die $dbh->errstr;
  63.  
  64. # fine preparo l'inserimento dei dati
  65.  
  66. # inserisci i valori in tabella, questi devono essere criptati mediante fna
  67.  
  68. # supponiamo siano stipati in un csv
  69.  
  70. my $fh_csv;
  71. my $csv_filename="customers.csv";
  72.  
  73. open $fh_csv,'<',$csv_filename or die "Unable to open $csv_filename: $!";
  74. while (!eof($fh_csv)) {
  75. my ($name,$surname,$email)=split(/,/,<$fh_csv>);
  76.  
  77. say $name;
  78. say $surname;
  79. say $email;
  80.  
  81.  
  82. my @encrypted_name=$krypto->encrypt_scalar($name);
  83. my $encrypted_name=join('\n',@encrypted_name);
  84.  
  85. my @encrypted_surname=$krypto->encrypt_scalar($surname);
  86. my $encrypted_surname=join('\n',@encrypted_surname);
  87.  
  88. my @encrypted_email=$krypto->encrypt_scalar($email);
  89. my $encrypted_email=join('\n',@encrypted_email);
  90.  
  91. say $encrypted_name;
  92. say "";
  93. say $encrypted_surname;
  94. say "";
  95. say $encrypted_email;
  96.  
  97. say "";
  98. say "------------------------------------";
  99. say "";
  100.  
  101. # qui li vado a inserire nel database
  102. $sth->execute($encrypted_name,$encrypted_surname,$encrypted_email);
  103. # fine qui li vado a inserire nel database
  104. }
  105. close $fh_csv;
  106.  
  107. # salvo le modifiche nel database e chiudo la connessione
  108. $dbh->commit;
  109. # fine salvo le modifiche nel database e chiudo la connessione
  110.  
  111. # fine supponiamo siano stipati in un csv
  112.  
  113. # fine inserisci i valori in tabella
  114. }
  115.  
  116. sub lettura_scrittura {
  117.  
  118. # apro database criptato e ne leggo i dati decriptati, li modifico e risalvo criptato
  119.  
  120. my $sth = $dbh->prepare(q
  121. {
  122. SELECT * FROM customers
  123. }
  124. ) or die $dbh->errstr;
  125.  
  126. $sth->execute;
  127.  
  128. while (my $row=$sth->fetchrow_hashref()) {
  129.  
  130. my $name=ricostruzione_stringa($row->{name});
  131. my $surname=ricostruzione_stringa($row->{surname});
  132. my $email=ricostruzione_stringa($row->{email});
  133.  
  134. say "$name, $surname, $email";
  135. say "------------------------------------";
  136. say ""
  137. }
  138.  
  139. # a questo punto faccio quello che devo con i dati e quando li modifico li salvo criptati così come nella sub inserimento
  140. }
  141.  
  142. sub ricostruzione_stringa {
  143. my $encrypted_field_value=shift;
  144. my @encrypted_scalar=split(/\\n/,$encrypted_field_value);
  145.  
  146. # ricostruzione stringa
  147. my ($fh_testo_criptato,$file_criptato);
  148. open $fh_testo_criptato, '>',\$file_criptato or die "err. scrittura file in memoria\n";
  149. for (@encrypted_scalar) {print $fh_testo_criptato $_."\n"}
  150. close $fh_testo_criptato;
  151. my ($fh_testo_decriptato,$stringa_decriptata);
  152. $krypto->decrypt_file(\$file_criptato,\$stringa_decriptata);
  153. return ($stringa_decriptata)
  154. # end ricostruzione stringa
  155.  
  156. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement