Crittazione/decrittazione

Altutto

Utente Attivo
30 Set 2013
262
0
16
stubborn.altervista.org
Con il seguente codice
PHP:
<?php
include("config.php");
function PasswordCasuale($lunghezza=100){
	$caratteri_disponibili ="ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz1234567890";
	$password = "";
	for($i = 0; $i<$lunghezza; $i++){
		$password = $password.substr($caratteri_disponibili,rand(0,strlen($caratteri_disponibili)-1),1);
	}
	return $password;
}
$stringa = addslashes($_POST['nota']);
$chiave  = PasswordCasuale(500);
for ($i = 0; $i < strlen($stringa); $i++ ) {
   $temp = $stringa[$i] ^ $chiave[$i % strlen($chiave)];
   $crypt .= str_pad( dechex( ord( $temp ) ), 2, 0, STR_PAD_LEFT);
}
$val[0] = $chiave;
for($a = 1; $a<10; $a++){$stringa = $crypt;
$chiave  = PasswordCasuale(500);
$crypt = "";
for ($i = 0; $i < strlen($stringa); $i++ ) {
   $temp = $stringa[$i] ^ $chiave[$i % strlen($chiave)];
   $crypt .= str_pad( dechex( ord( $temp ) ), 2, 0, STR_PAD_LEFT);
}
$val[$a] = $chiave;
}
$utente = $_SESSION["utente"];
$query = "INSERT INTO  `notes` (
`username` ,
`nota` ,
`letto` ,
`1` ,
`2` ,
`3` ,
`4` ,
`5` ,
`6` ,
`7` ,
`8` ,
`9` ,
`10`
)
VALUES (
 '$utente', '$crypt', '0', '$val[0]', '$val[1]', '$val[2]', '$val[3]', '$val[4]', '$val[5]', '$val[6]', '$val[7]', '$val[8]', '$val[9]'
)";
mysqli_query($connessione,$query);
?>
Dovrei crittare una stringa inviata tramite form per dieci volte, con dieci chiavi diverse generate in modo casuale.
Sembra funzionare tutto alla meraviglia. Il problema si pone quando devo tornare alla stringa di partenza.
Ho provato ad utilizzare questo codice:
PHP:
$note = mysqli_fetch_assoc(mysqli_query($connessione,"SELECT * FROM notes"));
$crypt = $note['nota'];
for($b=1;$b<11;$b++){
$num = 11-$b;
$chiave  = $note[$num];
$cnt = 0;
for ($i = 0; $i < strlen( $crypt ); $i+=2){
    $temp = chr( hexdec( substr( $crypt, $i, 2) ) );
    $stringa .= $temp ^ $chiave[$cnt % strlen($chiave)];
    $cnt++;
}
$crypt = $stringa;
}
echo $stringa;
?>
Ma $stringa non corrisponde alla stringa inserita :( sapreste dirmi cosa sbaglio?
 

marino51

Utente Attivo
28 Feb 2013
3.204
207
63
Lombardia
non so se vinco il peluche ... ma tu scrivi con la insert .... '$val[8]', '$val[9]'

poi

hai un 11 ... $num = 11-$b;

non ho fatto nessuna verifica ma controlla tu .... a prima vista c'è incongruenza di indici

ciao
Marino
 

marino51

Utente Attivo
28 Feb 2013
3.204
207
63
Lombardia
ci sono diverse incongruenze negli script che hai postato
renditi conto che ogni passo di crittografia moltiplica x 2 la lunghezza della stringa originale
quindi come prima cosa devi dimensionare adeguatamente i campi del tuo database
tieni conto che anche le chiavi generate devono essere della stessa lunghezza della stringa da crittografare,
lunghezza aggiornata ad ogni passaggio
poi non eseguivi l'ultima decrit ... quel 1 e 11 proprio non vanno !
non mettere mai funzioni o calcoli negli cicli for, passi da decine di secondi con ingolfamento del server a ... nulla
poi divertiti a vedere come funziona
ciao
Marino

ps voglio una scatola di peluches come premio !

PHP:
<?php 

# http://localhost/test_site/php/test/cript_decript.php

  function PasswordCasuale($lunghezza){
    $caratteri_disponibili = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz1234567890"; 
    $password = ""; 
    for($i = 0; $i<$lunghezza; $i++){ 
        $password .= substr($caratteri_disponibili,rand(0,strlen($caratteri_disponibili)-1),1); 
    } 
    return $password; 
  }



  echo "step 1<br /><br />"; 

  $stringa = "ciaooooo te la do io la brioschhhh";
  $lenstr = strlen($stringa);
  echo "stringa : $stringa<br />"; 
  echo "len     : $lenstr<br />"; 

  $chiave = PasswordCasuale($lenstr);
  $lenkey = strlen($chiave);
  $note[0] = $chiave;
  echo "note[0] : $note[0]<br />";
  echo "len     : $lenkey<br />"; 

  $crypt = "";
  for ($i = 0; $i < $lenstr; $i++ ) { 
    $temp = $stringa[$i] ^ $chiave[$i % $lenkey]; 
    $crypt .= str_pad( dechex( ord( $temp ) ), 2, 0, STR_PAD_LEFT);
  }
  echo "crypt   : $crypt<br />";
  echo "len     : ".strlen($crypt)."<br /><br />"; 



  echo "step 2<br /><br />"; 

  for($a = 1; $a<10; $a++){

    $stringa = $crypt;
    $lenstr = strlen($stringa);
    echo "stringa : $stringa<br />"; 
    echo "len     : $lenstr<br />"; 

    $chiave  = PasswordCasuale($lenstr); 
    $lenkey = strlen($chiave);
    $note[$a] = $chiave;
    echo "note[$a] : $note[$a]<br />";
    echo "len     : $lenkey<br />"; 

    $crypt = "";
    for ($i = 0; $i < $lenstr; $i++ ) { 
      $temp = $stringa[$i] ^ $chiave[$i % $lenkey]; 
      $crypt .= str_pad( dechex( ord( $temp ) ), 2, 0, STR_PAD_LEFT); 
    }
    echo "crypt   : $crypt<br />";
    echo "len     : ".strlen($crypt)."<br /><br />"; 
  }

  $nota = $crypt;



  echo "step 3<br /><br />"; 

  $stringa = $nota;
  for($b=9;$b>=0;$b--){

    $crypt = $stringa; 
    $lencry = strlen($crypt);
    echo "cript   : $crypt<br />"; 
    echo "len     : $lencry<br />"; 

    $chiave  = $note[$b]; 
    $lenkey = strlen($chiave);
    echo "note[$b] : $note[$b]<br />";
    echo "len     : $lenkey<br />"; 

    $cnt = 0; 
    $stringa = "";
    for ($i = 0; $i < $lencry; $i+=2){
      $temp = chr( hexdec( substr( $crypt, $i, 2) ) ); 
      $stringa .= $temp ^ $chiave[$cnt % $lenkey]; 
      $cnt++;
    }
    echo "stringa : $stringa<br />";
    echo "len     : ".strlen($stringa)."<br /><br />"; 
  } 
  echo "stringa : $stringa<br />"; 
?>
 
Ultima modifica:

marino51

Utente Attivo
28 Feb 2013
3.204
207
63
Lombardia
l'ultima versione, meglio utilizzabile con le due funzioni crittazione/decrittazione
codice inutile eliminato
chiavi come global
più facilmente integrabile
ciao
Marino

ps intendevo ... non mettere calcoli o richiamo di funzioni nell'istruzione del ciclo for,
soprattutto quando di fatto stai usando una costante

PHP:
<?php 

# http://localhost/test_site/php/test/cript_decript.php


  $note    = array();
  $stringa = "ciaooooo te la do io la brioschhhh";
  $nota    = Crittazione($stringa);
  $stringa = Decrittazione($nota);;



  function PasswordCasuale($lunghezza){
    $caratteri_disponibili = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz1234567890"; 
    $password = ""; 
    for($i = 0; $i<$lunghezza; $i++){ 
        $password .= substr($caratteri_disponibili,rand(0,strlen($caratteri_disponibili)-1),1); 
    } 
    return $password; 
  }

  function Crittazione($var){
    global $note;
    $crypt = $var;
    echo "step crittazione<br /><br />"; 

    for($a = 0; $a<10; $a++){

      $stringa = $crypt;
      $lenstr = strlen($stringa);
      echo "stringa : $stringa<br />"; 
      echo "len     : $lenstr<br />"; 

      $chiave  = PasswordCasuale($lenstr); 
      $lenkey = strlen($chiave);
      $note[$a] = $chiave;
      echo "note[$a] : $note[$a]<br />";
      echo "len     : $lenkey<br />"; 

      $crypt = "";
      for ($i = 0; $i < $lenstr; $i++ ) { 
        $temp = $stringa[$i] ^ $chiave[$i % $lenkey]; 
        $crypt .= str_pad( dechex( ord( $temp ) ), 2, 0, STR_PAD_LEFT); 
      }
      echo "crypt   : $crypt<br />";
      echo "len     : ".strlen($crypt)."<br /><br />"; 
    }
    return $crypt;
  }

  function Decrittazione($var){
    global $note;
    $stringa = $var;
    echo "step decrittografa<br /><br />"; 

    for($b=9;$b>=0;$b--){

      $crypt = $stringa; 
      $lencry = strlen($crypt);
      echo "cript   : $crypt<br />"; 
      echo "len     : $lencry<br />"; 

      $chiave  = $note[$b]; 
      $lenkey = strlen($chiave);
      echo "note[$b] : $note[$b]<br />";
      echo "len     : $lenkey<br />"; 

      $cnt = 0; 
      $stringa = "";
      for ($i = 0; $i < $lencry; $i+=2){
        $temp = chr( hexdec( substr( $crypt, $i, 2) ) ); 
        $stringa .= $temp ^ $chiave[$cnt % $lenkey]; 
        $cnt++;
      }
      echo "stringa : $stringa<br />";
      echo "len     : ".strlen($stringa)."<br /><br />"; 
    }
    return $stringa;
  }
?>