<?
$text 
stripslashes($_REQUEST["text"]);
$ciphered_text stripslashes($_REQUEST["ciphered_text"]);

$keyword stripslashes($_REQUEST["keyword"]);

if(
$_REQUEST["action"] == "Encrypt")
    
$ciphered_text vigenere_encrypt($text$keyword);

if(
$_REQUEST["action"] == "Decrypt")
    
$text vigenere_decrypt($ciphered_text$keyword);








/*



i have hid your prize
it is protected by the tentacled alien in the wiring closet
it sits inside a protective plastic egg

i dens tmd usmf bvivi
ah uw pnslsoxez fq hti tarloopez edwqr ij xzs imrery qxssax
ah emto mfguhe w tjcficpmns bpaoxaq qkg

*/

/*
ahp em tspaw sbp hraeeg
etraev cgx ijxg cbin xmlg
zivav lc ni raekgqqbhiv oseij
*/

?>
<form method="post">
Your original message<br>
<textarea name="text" cols="55" rows="6">
<?=$text;?>
</textarea>
<br>
A keyword to use as a cipher<br>
<input name="keyword" type="text" value="awesome">
<br>
<input name="action" type="submit" value="Encrypt">
<br>
</form>

<br><br><br>

<form method="post">
Encrypted Message<br>
<textarea name="ciphered_text" cols="55" rows="6">
<?=$ciphered_text;?>
</textarea>
<br>
A keyword to use as a cipher<br>
<input name="keyword" type="text" value="awesome">
<br>
<input name="action" type="submit" value="Decrypt">
<br>
</form>



















<?
function vigenere_encrypt($text$keyword)
{
    
$alphabet "abcdefghijklmnopqrstuvwxyz";
    
$alphabet preg_split('//'$alphabet, -1PREG_SPLIT_NO_EMPTY);
    
$alphabet_flip array_flip($alphabet);

    
$keyword $keyword;
    
$keyword preg_split('//'$keyword, -1PREG_SPLIT_NO_EMPTY);
    
$keyword_flip array_flip($keyword);

    
$ciphered_text "";

    
$y 0;
    
    for(
$x 0$x strlen($text); $x++)
    {
        if(
in_array($text[$x], $alphabet))
        {
            
$current_character $text[$x];
            
$current_number $alphabet_flip[$current_character];

            
$key_character $keyword[$y];
            
$shift_number $alphabet_flip[$key_character];

            
$ciphered_number = ($current_number $shift_number);
            if(
$ciphered_number >= 26)
                
$ciphered_number $ciphered_number 26;

            
$ciphered_character $alphabet[$ciphered_number];

            
//now that we have our scrambled letter, we add it to our output
            
$ciphered_text .= $ciphered_character;

            
//this will reset y so that we keep looping through the keyword letters
            
$y++;
            if(
$y >= count($keyword))
                
$y 0;
        }
        else
            
$ciphered_text .= $text[$x];
    }
    return 
$ciphered_text;
}





function 
vigenere_decrypt($ciphered_text$keyword)
{
    
$alphabet "abcdefghijklmnopqrstuvwxyz";
    
$alphabet preg_split('//'$alphabet, -1PREG_SPLIT_NO_EMPTY);
    
$alphabet_flip array_flip($alphabet);

    
$keyword $keyword;
    
$keyword preg_split('//'$keyword, -1PREG_SPLIT_NO_EMPTY);
    
$keyword_flip array_flip($keyword);

    
$text "";

    
$y 0;
    
    for(
$x 0$x strlen($ciphered_text); $x++)
    {
        if(
in_array($ciphered_text[$x], $alphabet))
        {
            
$ciphered_character $ciphered_text[$x];
            
$ciphered_number $alphabet_flip[$ciphered_character];

            
$key_character $keyword[$y];
            
$shift_number $alphabet_flip[$key_character];

            
$plaintext_number = ($ciphered_number $shift_number);

            if(
$plaintext_number 0)
                
$plaintext_number = (26 $plaintext_number);

            
$plaintext_character $alphabet[$plaintext_number];

            
//now that we have our scrambled letter, we add it to our output
            
$text .= $plaintext_character;

            
//this will reset y so that we keep looping through the keyword letters
            
$y++;
            if(
$y >= count($keyword))
                
$y 0;
        }
        else
            
$text .= $ciphred_text[$x];
    }
    return 
$text;
}

/*

        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   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
    B   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 
    C   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
    D   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 A B C D 
    F   F G H I J K L M N O P Q R S T U V W X Y Z A B C D E 
    G   G H I J K L M N O P Q R S T U V W X Y Z A B C D E F 
    H   H I J K L M N O P Q R S T U V W X Y Z A B C D E F G 
    I   I J K L M N O P Q R S T U V W X Y Z A B C D E F G H 
    J   J K L M N O P Q R S T U V W X Y Z A B C D E F G H I 
    K   K L M N O P Q R S T U V W X Y Z A B C D E F G H I J 
    L   L M N O P Q R S T U V W X Y Z A B C D E F G H I J K 
    M   M N O P Q R S T U V W X Y Z A B C D E F G H I J K L 
    N   N O P Q R S T U V W X Y Z A B C D E F G H I J K L M 
    O   O P Q R S T U V W X Y Z A B C D E F G H I J K L M N 
    P   P Q R S T U V W X Y Z A B C D E F G H I J K L M N O 
    Q   Q R S T U V W X Y Z A B C D E F G H I J K L M N O P 
    R   R S T U V W X Y Z A B C D E F G H I J K L M N O P Q 
    S   S T U V W X Y Z A B C D E F G H I J K L M N O P Q R  
    T   T U V W X Y Z A B C D E F G H I J K L M N O P Q R S 
    U   U V W X Y Z A B C D E F G H I J K L M N O P Q R S T 
    V   V W X Y Z A B C D E F G H I J K L M N O P Q R S T U
    W   W X Y Z A B C D E F G H I J K L M N O P Q R S T U V 
    X   X Y Z A B C D E F G H I J K L M N O P Q R S T U V W 
    Y   Y Z A B C D E F G H I J K L M N O P Q R S T U V W X 
    Z   Z 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 

*/
?>