/*---------------------------------------------------------------*/
|
/*
|
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é
|
Date mise a jour : 18 Fev 2026
|
|
Rapport de la maj:
|
- refactoring du code en PHP 7
|
- amélioration du code
|
- fonctionnement du code vérifié en PHP 8
|
*/
|
/*---------------------------------------------------------------*/
|
|
class Crypter
|
{
|
private string $key;
|
|
public function __construct(string $clave)
|
{
|
$this->key = $clave;
|
}
|
|
public function setKey(string $clave): void
|
{
|
$this->key = $clave;
|
}
|
|
private function keyED(string $txt): string
|
{
|
$encrypt_key = md5($this->key);
|
$ctr = 0;
|
$tmp = '';
|
$len = strlen($txt);
|
$keyLen = strlen($encrypt_key);
|
|
for ($i = 0; $i < $len; $i++) {
|
if ($ctr == $keyLen) $ctr = 0;
|
$tmp .= $txt[$i] ^ $encrypt_key[$ctr];
|
$ctr++;
|
}
|
|
return $tmp;
|
}
|
|
public function encrypt(string $txt): string
|
{
|
$encrypt_key = md5(random_int(0, 32000));
|
$ctr = 0;
|
$tmp = '';
|
$len = strlen($txt);
|
$keyLen = strlen($encrypt_key);
|
|
for ($i = 0; $i < $len; $i++) {
|
if ($ctr == $keyLen) $ctr = 0;
|
$tmp .= $encrypt_key[$ctr] . ($txt[$i] ^ $encrypt_key[$ctr]);
|
$ctr++;
|
}
|
|
return base64_encode($this->keyED($tmp));
|
}
|
|
public function decrypt(string $txt): string
|
{
|
$txt = $this->keyED(base64_decode($txt));
|
$tmp = '';
|
$len = strlen($txt);
|
|
for ($i = 0; $i < $len; $i++) {
|
$md5 = $txt[$i];
|
$i++;
|
$tmp .= ($txt[$i] ?? '') ^ $md5;
|
}
|
|
return $tmp;
|
}
|
}
|
|
| ?> |