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.119 tracks/php/exercises/rail-fence-cipher/example.php
trackler-2.2.1.118 tracks/php/exercises/rail-fence-cipher/example.php
trackler-2.2.1.117 tracks/php/exercises/rail-fence-cipher/example.php
trackler-2.2.1.116 tracks/php/exercises/rail-fence-cipher/example.php
trackler-2.2.1.115 tracks/php/exercises/rail-fence-cipher/example.php
trackler-2.2.1.114 tracks/php/exercises/rail-fence-cipher/example.php
trackler-2.2.1.113 tracks/php/exercises/rail-fence-cipher/example.php
trackler-2.2.1.111 tracks/php/exercises/rail-fence-cipher/example.php
trackler-2.2.1.110 tracks/php/exercises/rail-fence-cipher/example.php
trackler-2.2.1.109 tracks/php/exercises/rail-fence-cipher/example.php
trackler-2.2.1.108 tracks/php/exercises/rail-fence-cipher/example.php
trackler-2.2.1.107 tracks/php/exercises/rail-fence-cipher/example.php
trackler-2.2.1.106 tracks/php/exercises/rail-fence-cipher/example.php
trackler-2.2.1.105 tracks/php/exercises/rail-fence-cipher/example.php
trackler-2.2.1.104 tracks/php/exercises/rail-fence-cipher/example.php
trackler-2.2.1.103 tracks/php/exercises/rail-fence-cipher/example.php
trackler-2.2.1.102 tracks/php/exercises/rail-fence-cipher/example.php
trackler-2.2.1.101 tracks/php/exercises/rail-fence-cipher/example.php
trackler-2.2.1.100 tracks/php/exercises/rail-fence-cipher/example.php
trackler-2.2.1.99 tracks/php/exercises/rail-fence-cipher/example.php