Question

J'ai un texte crypté que je dois déchiffrer. Il est crypté avec AES-256-CBC. J'ai le texte crypté, la clé et iv. Cependant, peu importe ce que j'essaie, je n'arrive pas à le faire fonctionner.

Internet a suggéré que le chiffrement Rijndael de mcrypt devrait pouvoir le faire. Voici donc ce que j'ai maintenant:

function decrypt_data($data, $iv, $key) {
    $cypher = mcrypt_module_open(MCRYPT_RIJNDAEL_128, '', MCRYPT_MODE_CBC, '');

    // initialize encryption handle
    if (mcrypt_generic_init($cypher, $key, $iv) != -1) {
        // decrypt
        $decrypted = mdecrypt_generic($cypher, $data);

        // clean up
        mcrypt_generic_deinit($cypher);
        mcrypt_module_close($cypher);

        return $decrypted;
    }

    return false;
}

Dans l'état actuel des choses, je reçois deux avertissements et le résultat est du charabia:

Warning: mcrypt_generic_init() [function.mcrypt-generic-init]: Key size too large; supplied length: 64, max: 32 in /var/www/includes/function.decrypt_data.php on line 8
Warning: mcrypt_generic_init() [function.mcrypt-generic-init]: Iv size incorrect; supplied length: 32, needed: 16 in /var/www/includes/function.decrypt_data.php on line 8

Toute aide serait appréciée.

Était-ce utile?

La solution

Je ne connais pas très bien cela, mais il semble que d'essayer MCRYPT_RIJNDAEL_256 à la place de MCRYPT_RIJNDAEL_128 serait une étape évidente à venir ...

Modifier: Vous avez raison. Ce n'est pas ce dont vous avez besoin. MCRYPT_RIJNDAEL_128 est en fait le bon choix. Selon le lien que vous avez fourni, votre clé et votre IV sont deux fois plus longs qu'ils devraient l'être:

// How do you do 256-bit AES encryption in PHP vs. 128-bit AES encryption???
// The answer is:  Give it a key that's 32 bytes long as opposed to 16 bytes long.
// For example:
$key256 = '12345678901234561234567890123456';
$key128 = '1234567890123456';

// Here's our 128-bit IV which is used for both 256-bit and 128-bit keys.
$iv =  '1234567890123456';

Autres conseils

Je vous envoie un exemple, S'il vous plaît, vérifiez le code, ok

$data_to_encrypt = "2~1~000024~0910~20130723092446~T~00002000~USD~F~375019001012120~0~0~00000000000~";
$key128 = "abcdef0123456789abcdef0123456789";
$iv = "0000000000000000";

$cc = $data_to_encrypt;
$key = $key128;
$iv =  $iv;
$length = strlen($cc);

$cipher = mcrypt_module_open(MCRYPT_RIJNDAEL_128,'','cbc','');

mcrypt_generic_init($cipher, $key, $iv);
$encrypted = base64_encode(mcrypt_generic($cipher,$cc));
mcrypt_generic_deinit($cipher);

mcrypt_generic_init($cipher, $key, $iv);
$decrypted = mdecrypt_generic($cipher,base64_decode($encrypted));
mcrypt_generic_deinit($cipher);

echo "encrypted: " . $encrypted;
echo "<br/>";
echo "length:".strlen($encrypted);
echo "<br/>";
echo "decrypted: " . substr($decrypted, 0, $length);
Licencié sous: CC-BY-SA avec attribution
Non affilié à StackOverflow
scroll top