VIGENERE Encryption

  • wasEncryptVIGENERE takes as parameter a plain-text and an encryption key and returns cypher-text.
  • wasDecryptVIGENERE takes as parameter a cypher-text and an encryption key and returns plain-text.
VIGENERE.php
#!/usr/bin/php -e
<?php
 
///////////////////////////////////////////////////////////////////////////
//  Copyright (C) Wizardry and Steamworks 2015 - License: GNU GPLv3      //
///////////////////////////////////////////////////////////////////////////
function wasReversePermuteArrayElements($input, $times) {
    if ($times == 0) return $input;
    return wasReversePermuteArrayElements(
        array_merge(
            array_slice(
                $input,
                1
            ),
            array_slice(
                $input,
                0,
                1
            )
        ),
    --$times
    );
}
 
///////////////////////////////////////////////////////////////////////////
//  Copyright (C) Wizardry and Steamworks 2015 - License: GNU GPLv3      //
///////////////////////////////////////////////////////////////////////////
function wasVIGENEREExpandKey($input, $enc_key) {
    $exp_key = "";
    $i = 0;
    $j = 0;
    do {
        $p = substr($input, $i, 1);
        if (!ctype_alpha($p)) {
            $exp_key .= $p;
            ++$i;
            continue;
        }
        $m = $j % strlen($enc_key);
        $exp_key .= substr($enc_key, $m, 1);
        ++$j;
        ++$i;
    } while ($i < strlen($input));
    return $exp_key;
}
 
///////////////////////////////////////////////////////////////////////////
//  Copyright (C) Wizardry and Steamworks 2015 - License: GNU GPLv3      //
///////////////////////////////////////////////////////////////////////////
function wasEncryptVIGENERE($input, $enc_key) {
    $a = 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"
         );
 
    // expand the key
    $enc_key = wasVIGENEREExpandKey($input, $enc_key);
 
    $result = "";
    $i = 0;
    do {
        $p = substr($input, $i, 1);
        if (!ctype_alpha($p)) {
            $result .= $p;
            ++$i;
            continue;
        }
        $q = wasReversePermuteArrayElements(
            $a,
            array_search(
                substr(
                    $enc_key,
                    $i,
                    1
                    ),
                $a
            )
        )[
            array_search(
                strtolower($p),
                $a
            )
        ];
        if (ctype_upper($p)) {
            $q = strtoupper($q);
        }
        $result .= $q;
        ++$i;
    } while ($i < strlen($input));
 
    return $result;
}
 
///////////////////////////////////////////////////////////////////////////
//  Copyright (C) Wizardry and Steamworks 2015 - License: GNU GPLv3      //
///////////////////////////////////////////////////////////////////////////
function wasDecryptVIGENERE($input, $enc_key) {
    $a =
    [
        "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"
    ];
 
    // expand the key
    $enc_key = wasVIGENEREExpandKey($input, $enc_key);
 
    $result = "";
    $i = 0;
    do {
        $p = substr($input, $i, 1);
        if (!ctype_alpha($p)) {
            $result .= $p;
            ++$i;
            continue;
        }
        $q = $a[
            array_search(
                strtolower($p),
                wasReversePermuteArrayElements(
                    $a,
                    array_search(
                        substr(
                            $enc_key,
                            $i,
                            1
                        ),
                    $a
                    )
                )
            )
        ];
        if (ctype_upper($p)) {
            $q = strtoupper($q);
        }
        $result .= $q;
        ++$i;
    } while ($i < strlen($input));
 
    return $result;
}
 
?>

Symmetry

A consistency test call that first encrypts the string It is a good day! and then decrypts the string would be:

// Encrypt the text "It is a good day!!" using the key "vig" and decrypt result
// using the same key.
print wasDecryptVIGENERE(
    wasEncryptVIGENERE(
        "It is a good day!!", 
        "vig"
    ), 
    "vig"
);
 
print "\n";

which should return It is a good day!.


fuss/php/cryptography/cyphers/vigenere.txt ยท Last modified: 2017/02/22 18:30 (external edit)

Access website using Tor Access website using i2p


For the copyright, license, warranty and privacy terms for the usage of this website please see the license, privacy and plagiarism pages.