Domanda

Usando PHP, sto cercando di convertire il testo cifrato generato da McRypt in binario, ma quando provo a convertirlo nel testo di Cipher non viene convertito correttamente, quindi non può essere decrittografato. Suppongo che le mie codifiche si stiano facendo incasinare da qualche parte, ma non so da dove cominciare a capirlo. Ecco un link al sito http://dev.hersha.me/str2bin.php Ed ecco il mio codice.

<?php

class phpSteg {

function bin2bstr($input) {
    if (!is_string($input)) return null;
    return pack('H*', base_convert($input, 2, 16)); 
}

function bstr2bin($input) {
    if (!is_string($input)) return null;
    $value = unpack('H*', $input);
    return base_convert($value[1], 16, 2);
}


};

$steg = new phpSteg();

//echo $steg->bstr2bin('OMG') . "\n <br \>";
//echo $steg->bin2bstr('010011110100110101000111') . "\n <br \>";
$hash =  hash('md5',"OMGZWTF");
echo $hash . "\n <br \>";
$message = "OMG WTF BBQ";

$text = $message;
$key = $hash;

$iv = mcrypt_create_iv(mcrypt_get_iv_size(MCRYPT_RIJNDAEL_256, MCRYPT_MODE_CFB), MCRYPT_RAND);
echo $iv . "\n <br \>";
$encrypted = mcrypt_encrypt(MCRYPT_RIJNDAEL_256, $key, $text, MCRYPT_MODE_CFB, $iv);

$binenc = $steg->bstr2bin($encrypted);

$bstrenc = $steg->bin2bstr($binenc);


$decrypted = mcrypt_decrypt(MCRYPT_RIJNDAEL_256, $key, $bstrenc, MCRYPT_MODE_CFB, $iv);
echo "Cipher Text : " . $encrypted . "\n <br \>";

echo "Cipher Text (binary) : " . $binenc . "\n <br \>";

echo "Cipher Text (back from binary) : " . $bstrenc . "\n <br \>";

echo "Decryption : " . $decrypted; // The quick brown fox jumps over the lazy dog

?>

Qualche idea?

È stato utile?

Soluzione 3

Tutti i problemi risolti.

Primo: stavo sovraccaricando il base_convert funzione. Set di dati più piccolo necessario.

Secondo: piuttosto semplice e ovvio davvero, base_convert Ignora gli zeri principali, quindi quando la conversione ha dovuto aggiungerli.

Altri suggerimenti

Non preoccuparti di usare bin2bstr e bstr2bin. Se è necessario archiviare la stringa da qualche parte (come un database) solo base64 codifica/decodifica la stringa dopo la crittografia.

$encrypted = mcrypt_encrypt(MCRYPT_RIJNDAEL_256, $key, $text, MCRYPT_MODE_CFB, $iv);

$binenc = base64_encode($encrypted);

$bstrenc = base64_decode($binenc);

$decrypted = mcrypt_decrypt(MCRYPT_RIJNDAEL_256, $key, $bstrenc, MCRYPT_MODE_CFB, $iv);

Provare base64_encode sulla tua stringa binaria prima di crittografare e utilizzare base64_decode dopo che è stato decrittografato. McRypt potrebbe soffocare i dati altrimenti, senza farti sapere.

Autorizzato sotto: CC-BY-SA insieme a attribuzione
Non affiliato a StackOverflow
scroll top