Problème avec mcrypt de php

Résolu
Utilisateur anonyme -  
 Utilisateur anonyme -
Bonjour, j'ai actuellement un problème avec la fonction mcrypt.
Voici le code :
$key = pack('H*', "bcb04b7e103a0cd8b54763051cef08bc55abe029fdebae5e1d417e2ffb2a00a3");
    # Montre la taille de la clé utilisée ; soit des clés sur 16, 24 ou 32 octets pour
    # AES-128, 192 et 256 respectivement.
    $key_size =  strlen($key);
    $plaintext = "Cette chaîne de caractère a été chiffrée en AES-256 / CBC / ZeroBytePadding.";
    $iv_size = mcrypt_get_iv_size(MCRYPT_RIJNDAEL_128, MCRYPT_MODE_CBC);
    $iv = mcrypt_create_iv($iv_size, MCRYPT_RAND);
    $ciphertext = mcrypt_encrypt(MCRYPT_RIJNDAEL_128, $key,$plaintext, MCRYPT_MODE_CBC, $iv);
    $ciphertext = $iv . $ciphertext;
    $ciphertext_base64 = base64_encode($ciphertext);
    
    # --- DECHIFFREMENT ---
    
    $ciphertext_dec = base64_decode($ciphertext_base64);
    $iv_dec = substr($ciphertext_dec, 0, $iv_size);
    $ciphertext_dec = substr($ciphertext_dec, $iv_size);

    # On doit supprimer les caractères de valeur 00h de la fin du texte plein
    $plaintext_dec = mcrypt_decrypt(MCRYPT_RIJNDAEL_128, $key,$ciphertext_dec, MCRYPT_MODE_CBC, $iv_dec);


echo $plaintext_dec;
echo '<br />'.$plaintext;

if($plaintext == $plaintext_dec){
	echo '<br />La décryption a fonctionné correctement';
} else {
	echo '<br />La décryption n\'a pas fonctionné correctement';
}


Et voici le résultat que j'obtiens :
Cette chaîne de caractère a été chiffrée en AES-256 / CBC / ZeroBytePadding.
Cette chaîne de caractère a été chiffrée en AES-256 / CBC / ZeroBytePadding.
La décryption n'a pas fonctionné correctement

Quand je regarde le code source de la page, j'obtiens :
Cette chaîne de caractère a été chiffrée en AES-256 / CBC / ZeroBytePadding. avec 15 caractères inconnus derrières :/

Merci de bien vouloir m'aider
Nils0

2 réponses

  1. Utilisateur anonyme
     
    Pour ceux qui veulent le code finale, le voici :
    function generate_key($size){
        if($size > 0){
            return bin2hex(openssl_random_pseudo_bytes($size));
        }
    }
    
    function crypter($chaine, $key){
        if($chaine != null){
            $iv_size = mcrypt_get_iv_size(MCRYPT_RIJNDAEL_128, MCRYPT_MODE_CBC);
            $iv = mcrypt_create_iv($iv_size, MCRYPT_RAND);
            $ciphertext = mcrypt_encrypt(MCRYPT_RIJNDAEL_128, $key,
            $chaine, MCRYPT_MODE_CBC, $iv);
            $ciphertext = $iv . $ciphertext;
            $ciphertext_base64 = base64_encode($ciphertext);
            return $ciphertext_base64; 
        }
    }
    
    function decrypter($chaine_crypter, $key){
        if($chaine_crypter != null){
            $iv_size = mcrypt_get_iv_size(MCRYPT_RIJNDAEL_128, MCRYPT_MODE_CBC);
            $ciphertext_dec = base64_decode($chaine_crypter);
            $iv_dec = substr($ciphertext_dec, 0, $iv_size);
            $ciphertext_dec = substr($ciphertext_dec, $iv_size);
            $plaintext_dec = mcrypt_decrypt(MCRYPT_RIJNDAEL_128, $key,
            $ciphertext_dec, MCRYPT_MODE_CBC, $iv_dec);
            return trim($plaintext_dec);
        }
    }
    
    1
  2. yg_be Messages postés 23437 Date d'inscription   Statut Contributeur Dernière intervention   Ambassadeur 1 588
     
    bonjour, je vois deux éléments, y aurait-il un lien entre les deux?
    1) il y a 4 caractères de valeur 00h à la fin de $plaintext_dec, comparé à $plaintext
    2) ton commentaire "On doit supprimer les caractères de valeur 00h de la fin du texte plein"

    d'où vient le commentaire?

    suggestion pour bien voir les différences:
    echo '<br />'.strlen($plaintext_dec);
    echo '<br />'.strlen($plaintext);
    echo '<br />'.bin2hex($plaintext_dec);
    echo '<br />'.bin2hex($plaintext);

    tiens, la chaîne décryptée à une longueur multiple de 8 octets!
    il est, je pense, nécessaire de mémoriser la longueur de la chaîne avant encryption. moi je rajouterais cette information dans le texte avant encryption.
    0
    1. Utilisateur anonyme
       
      Finalement, j'ai trouvé :
      $plaintext_dec = trim($plaintext_dec);
      

      Merci beaucoup de m'avoir aidé :)
      0