Jak przekonwertować hex na ciąg znaków lub tekst w php


Chcę zaszyfrować wiadomość w formacie string (tekst), ale nie znam funkcji, która może konwertować Hex na ciąg:
oto moja strona:
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title></title>
</head>
<body>
<?php// on commence par définir la fonction Cryptage que l'on utilisera ensuite
function Cryptage($TEXT, $Clef) {
$LClef = strlen($Clef);
$LTEXT = strlen($TEXT);
if ($LClef < $LTEXT) {
$Clef = str_pad($Clef, $LTEXT, $Clef, STR_PAD_RIGHT);
} elseif ($LClef > $LTEXT) {
$diff = $LClef - $LTEXT;
$_Clef = substr($Clef, 0, -$diff);
}
return bin2hex($TEXT ^ $Clef);
}/* On vérifie l’existence de $_POST['TEXT'] et de $_POST['Clef'].
Ça revient au même que isset($_POST['TEXT']) AND isset($_POST['Clef']) */
if (isset($_POST['TEXT'], $_POST['Clef'])) {
$resultat = Cryptage($_POST['TEXT'], $_POST['Clef']);
}// on a fini les traitement en PHP, on passe à l'affichage : if (isset($resultat)) {
echo "Chaîne cryptée/décryptée : " . $resultat;
}
?> <!-- on affiche le formulaire pour que l'utilisateur puisse directement refaire un cryptage/décryptage -->
<form method="post">
<input type="text" name="TEXT" style="width:500px" value="Cliquez ici pour ajouter un texte." onFocus="javascript:this.value=''"/>
<input type="text" name="Clef" style="width:500px" value="Cliquez ici pour ajouter un masque." onFocus="javascript:this.value=''"/>
<input type="submit" value="Crypter/Décrypter"/>
</form> </body>
</html>

Testowałem tę funkcję, ale nic nie zwraca (zwraca pusty ciąg)
function hextostr($hex)
{
$str='';
for ($i=0; $i < strlen($hex)-1; $i+=2)
{
$str .= chr(hexdec($hex[$i].$hex[$i+1]));
}
return $str;
}

czy masz jakieś pomysły dzięki
Zaproszony:
Anonimowy użytkownik

Anonimowy użytkownik

Potwierdzenie od:

function hex2str($hex) {
for($i=0;$i<strlen($hex);$i+=2)
$str .= chr(hexdec(substr($hex,$i,2))); return $str;
}

Da rade
Anonimowy użytkownik

Anonimowy użytkownik

Potwierdzenie od:

Wypróbuj tę funkcję
function hex2str($func_string) {
$func_retVal = '';
$func_length = strlen($func_string);
for($func_index = 0; $func_index < $func_length; ++$func_index) $func_retVal .= chr(hexdec($func_string{$func_index} . $func_string{++$func_index}));return $func_retVal;
}

Używam go dużo osobiście, więc powinno działać.
Anonimowy użytkownik

Anonimowy użytkownik

Potwierdzenie od:

Możesz spróbować
hex2bin()
Spowoduje to przekonwertowanie szesnastkowego na format ciągu.

Aby odpowiedzieć na pytania, Zaloguj się lub Zarejestruj się