Autor Zpráva
danzpet
Profil
Ahoj mám takový problem vytvořil jsem si script pro převádění textu do bin. soustavy, to funguje, ale když chci převést z bin. soustavy do textu, tak se mi to nepřevede, přitom když v poli $sifr nebudu mit kod v bin. soustave '10011101',...ale normalne treba 'b', 'c', 'u', 'r' jako je v $abeceda tak to funguje normalne... prosim o pomoc, nevim si radz cim to je
<?
$text=$_POST['text'];
$dvojka=$_POST['dvojka'];

if ($text!=="" && $_POST["test"]=="Preved")
{
$abeceda=array('a','b','c','d','e','f','g','h','i','j','k','l','m','n','o','p','q','r','s','t','u','v','w','x','y','z','A','B','C','E','E','F','G','H','I','J','K','L','M','N','O','P','Q','R','S','T','U','V','W','X','Y','Z');

$sifr=array('01100001','01100010','01100011','01100100','01100101','01100110','01100111','01101000','01101001','01101010','01101011','01101100','01101101','01101110','01101111','01110000','01110001','01110010','01110011','01110100','01110101','01110110','01110111','01111000','01111001','01111010',
'01000001','01000010','01000011','01000100','01000101','01000110','01000111','01001000','01001001','01001010','01001011','01001100','01001101','01001110','01001111','01010000','01010001','01010010','01010011','01010100','01010101','01010110','01010111','01011000','01011001','01011010');

for($i=0;$i<strlen($text);$i++)
{
for($y=0;$y<count($abeceda);$y++)
{
if ($text[$i]==$abeceda[$y])
{
$sifr_text.= $sifr[$y];
}
}
}



for ($i=0;$i<strlen($sifr_text);$i++)
{
for($y=0;$y<count($sifr);$y++)
{
if ($sifr_text[$i]==$sifr[$y])
{
$norm_text.= $abeceda[$y];
}
}
}

}


if ($dvojka!=="" && $_POST["test"]=="Preved")
{
$abeceda=array('a','b','c','d','e','f','g','h','i','j','k','l','m','n','o','p','q','r','s','t','u','v','w','x','y','z','A','B','C','E','E','F','G','H','I','J','K','L','M','N','O','P','Q','R','S','T','U','V','W','X','Y','Z');

$sifr=array('01100001','01100010','01100011','01100100','01100101','01100110','01100111','01101000','01101001','01101010','01101011','01101100','01101101','01101110','01101111','01110000','01110001','01110010','01110011','01110100','01110101','01110110','01110111','01111000','01111001','01111010',
'01000001','01000010','01000011','01000100','01000101','01000110','01000111','01001000','01001001','01001010','01001011','01001100','01001101','01001110','01001111','01010000','01010001','01010010','01010011','01010100','01010101','01010110','01010111','01011000','01011001','01011010');

for($i=0;$i<strlen($dvojka);$i++)
{
for($y=0;$y<count($sifr);$y++)
{
if ($dvojka[$i]==$sifr[$y])
{
$sifr_dvojka.= $abeceda[$y];
}
}
}



for ($i=0;$i<strlen($sifr_dvojka);$i++)
{
for($y=0;$y<count($abeceda);$y++)
{
if ($sifr_dvojka[$i]==$abeceda[$y])
{
$norm_dvojka.= $sifr[$y];
}
}
}

}
?>
<form method="POST" action="ara.php">
<table><tr><td>Text do dvojkové:</td><td> Z Dvojkové do textu:</td></tr>
<tr><td><textarea name="text" rows="5" cols="20" value="demeence" ><? echo $sifr_dvojka ?></textarea></td>
<td><textarea name="dvojka" rows="5" cols="20" value="demence" ><? echo $sifr_text ?></textarea></td></tr>
<tr><td><input type="submit" value="Preved" name="test"></td></tr></table>
</form>
ShiraNai7
Profil
edit2: pro šifrování zkus toto:

function binXOR($data, $key)
{
    $key_len = strlen($key);
    if($key_len < 1) throw new UnderflowException(__method__.': Key is empty!');
    if($key_len > 255) throw new OverflowException(__method__.': Key is too long! Max 255 bytes.');
    for($i = 0; isset($data[$i]); ++$i) $data[$i] = chr(ord($data[$i]) ^ ord($key[$i % $key_len]) ^ ((($i % 16) + 1) * 16 - 1));
    return $data;
}


$test = "Ahoj toto je test :)"; // testovaci text
$heslo = 'supertajneheslo'; // testovaci heslo

echo $test."\n"; // vypiseme nezasifrovany text
$test = binXOR($test, $heslo); // zasifrujeme text
echo bin2hex($test)."\n"; // vypiseme zasifrovany text (musime pouzit bin2hex protoze $test je ted v binarnim formatu a zasifrovan)
$test = binXOR($test, $heslo); // desifrujeme text
echo $test."\n"; // vypiseme desifrovany text


Výsledek příkladu je:

Ahoj toto je test :)
3d0230301d5f61618edaadbf9cc7e5ff0e4f7064
Ahoj toto je test :)


Pokud bys chtěl dekódovat text zakódovaný a ještě převedený přes bin2hex, tak ho musíš převést zpět do binární podoby touto funkcí:
function binFromHex($hex_string)
    {
        $len = strlen($hex_string);
        if(($len % 2) !== 0 || preg_match('%^[a-fA-F0-9]+$%', $hex_string) !== 1) return false;
        $output = '';
        for($i = 0; $i !== $len; $i += 2) $output .= chr(hexdec($hex_string[$i].$hex_string[$i + 1]));
        return $output;
    }




Původní příklad, kde jsem jsi nevšíml že se (asi) pokoušíš o jakousi šifru...

Nevím, proč ti to nefunguje a zkoumat to nebudu, protože to řešíš zbytečně složitě. Zde máš funkce napsané za krátkou chvilku, které navíc pracujou se všemi znaky.

// tato funkce prevadi text do "binarniho formatu"
function str2bin($str)
{
    $output = '';
    for($i = 0; isset($str[$i]); ++$i) $output .= str_pad(decbin(ord($str[$i])), 8, '0', STR_PAD_LEFT);
    return $output;
}

// tato funkce prevadi "binarni format" zpet na text
function bin2str($bin)
{
    $output = '';
    if((strlen($bin) % 8) !== 0) return false;
    $bin = str_split($bin, 8);
    for($i = 0; isset($bin[$i]); ++$i) $output .= chr(bindec($bin[$i]));
    return $output;
}


$test = "Ahoj toto je test :)";
echo $test."\n";
$test = str2bin($test); // zakodujeme
echo $test."\n";
$test = bin2str($test); // dekodujeme
echo $test."\n";


Výsledek příkladu je:

Ahoj toto je test :)
0100000101101000011011110110101000100000011101000110111101110100011011110010000001101010011001010010000001110100011001010111001101110100001000000011101000101001
Ahoj toto je test :)



edit: aha, ty se snažíš cosi šifrovat :D

Vaše odpověď

Mohlo by se hodit


Prosím používejte diakritiku a interpunkci.

Ochrana proti spamu. Napište prosím číslo dvě-sta čtyřicet-sedm:

0