Sha256: 92cfa3f9fa74d77553b3d8f31f0bf434dff98b87203326f2d95a062d94be32e4

Contents?: true

Size: 1.96 KB

Versions: 122

Compression:

Stored size: 1.96 KB

Contents

<?php
function encode($plainMessage, $rails)
{
    $cipherMessage = [];
    $position = ($rails * 2) - 2;
    for ($index = 0; $index < strlen($plainMessage); $index++) {
        for ($step = 0; $step < $rails; $step++) {
            if (!isset($cipherMessage[$step])) {
                $cipherMessage[$step] = '';
            }
            if ($index % $position == $step || $index % $position == $position-$step) {
                $cipherMessage[$step] .= $plainMessage[$index];
            } else {
                $cipherMessage[$step] .= ".";
            }
        }
    }
    return implode('', str_replace('.', '', $cipherMessage));
}

function decode($cipherMessage, $rails)
{
    $position = ($rails * 2) - 2;
    $textLength = strlen($cipherMessage);

    $minLength = floor($textLength / $position);
    $balance = $textLength % $position;
    $lengths = [];
    $strings = [];
    $totalLengths = 0;
    //find no of characters in each row
    for ($rowIndex = 0; $rowIndex < $rails; $rowIndex++) {
        $lengths[$rowIndex] = $minLength;
        if ($rowIndex != 0 && $rowIndex != ($rails - 1)) {
            $lengths[$rowIndex] += $minLength;
        }
        if ($balance > $rowIndex) {
            $lengths[$rowIndex]++;
        }
        if ($balance > ($rails + ($rails - $rowIndex) - 2)) {
            $lengths[$rowIndex]++;
        }
        $strings[] = substr($cipherMessage, $totalLengths, $lengths[$rowIndex]);
        $totalLengths += $lengths[$rowIndex];
    }

    //convert row of characters to plain message
    $plainText = '';
    while (strlen($plainText) < $textLength) {
        for ($charIndex = 0; $charIndex < $position; $charIndex++) {
            if (isset($strings[$charIndex])) {
                $index = $charIndex;
            } else {
                $index = $position - $charIndex;
            }
            $plainText .= substr($strings[$index], 0, 1);
            $strings[$index] = substr($strings[$index], 1);
        }
    }
    return $plainText;
}

Version data entries

122 entries across 122 versions & 1 rubygems

Version Path
trackler-2.2.1.78 tracks/php/exercises/rail-fence-cipher/example.php
trackler-2.2.1.77 tracks/php/exercises/rail-fence-cipher/example.php
trackler-2.2.1.76 tracks/php/exercises/rail-fence-cipher/example.php
trackler-2.2.1.75 tracks/php/exercises/rail-fence-cipher/example.php
trackler-2.2.1.74 tracks/php/exercises/rail-fence-cipher/example.php
trackler-2.2.1.73 tracks/php/exercises/rail-fence-cipher/example.php
trackler-2.2.1.72 tracks/php/exercises/rail-fence-cipher/example.php
trackler-2.2.1.71 tracks/php/exercises/rail-fence-cipher/example.php
trackler-2.2.1.70 tracks/php/exercises/rail-fence-cipher/example.php
trackler-2.2.1.69 tracks/php/exercises/rail-fence-cipher/example.php
trackler-2.2.1.68 tracks/php/exercises/rail-fence-cipher/example.php
trackler-2.2.1.67 tracks/php/exercises/rail-fence-cipher/example.php
trackler-2.2.1.66 tracks/php/exercises/rail-fence-cipher/example.php
trackler-2.2.1.65 tracks/php/exercises/rail-fence-cipher/example.php
trackler-2.2.1.64 tracks/php/exercises/rail-fence-cipher/example.php
trackler-2.2.1.63 tracks/php/exercises/rail-fence-cipher/example.php
trackler-2.2.1.62 tracks/php/exercises/rail-fence-cipher/example.php
trackler-2.2.1.61 tracks/php/exercises/rail-fence-cipher/example.php
trackler-2.2.1.60 tracks/php/exercises/rail-fence-cipher/example.php
trackler-2.2.1.59 tracks/php/exercises/rail-fence-cipher/example.php