Cryptage et décryptage md5

  Information

Une classe qui crypte une chaine de caractère en md5 et ce de manière aléatoire et au final la décrypte. Exemple fourni.

  code source classé dans  Classes

 
 01    
 02    
 03    
 04    
 05    
 06    
 07    
 08    
 09    
 10    
 11    
 12    
 13    
 14    
 15    
 16    
 17    
 18    
 19    
 20    
 21    
 22    
 23    
 24    
 25    
 26    
 27    
 28    
 29    
 30    
 31    
 32    
 33    
 34    
 35    
 36    
 37    
 38    
 39    
 40    
 41    
 42    
 43    
 44    
 45    
 46    
 47    
 48    
 49    
 50    
 51    
 52    
 53    
 54    
 55    
 56    
 57    
 58    
 59    
 60    
 61    
 62    
 63    
 64    
 65    
 66    
 67    
 68    
                               
/*---------------------------------------------------------------*/
/*
Titre : Cryptage et décryptage md5

URL : https://phpsources.net/code_s.php?id=481
Auteur : freemh
Date édition : 06 Jan 2009
Date mise a jour : 17 Oct 2019

Rapport de la maj:
- fonctionnement du code vérifié
*/
/*---------------------------------------------------------------*/

class Crypter{

var $key;

/*--------------------------------
le constructeur de la classe.
--------------------------------*/
function __construct($clave){
$this->key = $clave;
}

function setKey($clave){
$this->key = $clave;
}

function keyED($txt) {
$encrypt_key = md5($this->key);
$ctr=0;
$tmp = "";
for ($i=0;$i<strlen($txt);$i++) {
if ($ctr==strlen($encrypt_key)) $ctr=0;
$tmp.= substr($txt,$i,1) ^ substr($encrypt_key,$ctr,1);
$ctr++;
}
return $tmp;
}

function encrypt($txt){
srand((double)microtime()*1000000);
$encrypt_key = md5(rand(0,32000));
$ctr=0;
$tmp = "";
for ($i=0;$i<strlen($txt);$i++){
if ($ctr==strlen($encrypt_key)) $ctr=0;
$tmp.= substr($encrypt_key,$ctr,1) .
(substr($txt,$i,1) ^ substr($encrypt_key,$ctr,1));
$ctr++;
}
return base64_encode($this->keyED($tmp));
}

function decrypt($txt) {
$txt = $this->keyED(base64_decode($txt));
$tmp = "";
for ($i=0;$i<strlen($txt);$i++){
$md5 = substr($txt,$i,1);
$i++;
$tmp.= (substr($txt,$i,1) ^ $md5);
}
return $tmp;
}

}
?>

Exemple :

 
 01    
 02    
 03    
 04    
 05    
 06    
 07    
 08    
 09    
 10    
 11    
 12    
 13    
                                

$chaine = 'chaine a crypter';

$crypter = new Crypter($chaine);
$chaine_crypter = $crypter->encrypt($chaine);
echo $chaine_crypter;
// Affiche une chaine comme:
// UjFVPgI3A2kDblhtDy0HZAZ3ATAHfQonUScLdFJnVHQ

$chaine_decrypter = $crypter->decrypt($chaine_crypter);
echo $chaine_decrypter
// Affiche : chaine a crypter
?>

      Fonctions du code - Doc officielle PHP

Détail    php.net  
Description
Versions PHP
   base64_decode
Décode une chaîne en MIME base64
PHP 4, 5, 7 et 8
   base64_encode
Encode une chaîne en MIME base64
PHP 4, 5, 7 et 8
   echo
Affiche une chaîne de caractères
PHP 4, 5, 7 et 8
   md5
Calcule le md5 d'une chaîne (PHP 4, PHP 5, PHP 7, PECL hash:1.11.3)
PHP 4, 5, 7 et 8
   microtime
Retourne le timestamp UNIX actuel avec les microsecondes
PHP 4, 5, 7 et 8
   rand
Génère une valeur aléatoire
PHP 4, 5, 7 et 8
   return
Retourne le controle du programme au module appelant.
PHP 4, 5, 7 et 8
   srand
Initialise le générateur de nombres aléatoires
PHP 4, 5, 7 et 8
   strlen
Calcule la taille d'une chaîne
PHP 4, 5, 7 et 8
   substr
Retourne un segment de chaîne
PHP 4, 5, 7 et 8

   Un petit merci aux auteurs pour leur travail, ça ne coûte rien et ça fait toujours plaisir wink
avatar

Freemh

  06 Jan 2009

  SOURCE   Télécharger

Information sur les mises à jour

Dernière mise à jour :

    17 Oct 2019
    fonctionnement du code vérifié

19 192 Vues
Compatibilité du code
PHP 5, 7 et 8+