Пример #1
0
function encode($plaintext, $key)
{
    $encryptedText = "";
    $cleanText = clean($plaintext);
    $paddedText = padText($cleanText, $key);
    $counter = 0;
    for ($i = 0; $i < $key; $i++) {
        $encryptedText .= $paddedText[$i];
        for ($j = $i + 1; $j < strlen($paddedText); $j++) {
            $counter++;
            if ($counter % $key == 0) {
                $encryptedText .= $paddedText[$j];
                $counter = 0;
            }
        }
        $counter = 0;
    }
    return $encryptedText;
}
Пример #2
0
function encode($plaintext, $key, $sizeOfKey)
{
    $encryptedText = "";
    $cleanText = clean($plaintext);
    $paddedText = padText($cleanText, $key, $sizeOfKey);
    $paddedTextArray = explode(",", $paddedText);
    $keyArray = explode(",", $key);
    array_splice($keyArray, 0, 1);
    // Moves through text one block at a time, converts it to the 0-25 value system,
    // multiples key row against block as a column, mods by 26, then adds 97 to convert
    // back to ascii letter.
    for ($x = 0; $x < count($paddedTextArray); $x += $sizeOfKey) {
        $plainBlock = array();
        // Collects a block and converts to the 0-25 value.
        for ($y = 0; $y < $sizeOfKey; $y++) {
            array_push($plainBlock, $paddedTextArray[$x + $y]);
        }
        // Traverses the whole key, one row after the next.
        for ($p = 0; $p < $sizeOfKey * $sizeOfKey; $p += $sizeOfKey) {
            $letterTotal = 0;
            // Collects the sum of products of a key row and plaintext block column.
            for ($z = 0; $z < $sizeOfKey; $z++) {
                $letterTotal += $keyArray[$p + $z] * $plainBlock[$z];
            }
            // Converts 0-25 value of encrypted letter back into ascii equivalent.
            if ($letterTotal >= 0) {
                $encryptedText .= NumToLetter($letterTotal % 26);
            } else {
                $result = $letterTotal;
                while ($result < 0) {
                    $result += 26;
                }
                $encryptedText .= NumToLetter($result);
            }
        }
    }
    return $encryptedText;
}