Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 7 of 7
  1. #1
    New Coder
    Join Date
    Mar 2006
    Posts
    51
    Thanks
    1
    Thanked 0 Times in 0 Posts

    Question Vegenere Cipher using PHP

    Hi everyone...
    May somebody could write an example code to encrypt strings using vegenere cyper according to vegenere table (36x36)? I have search the example with Google but just found nothing.

    Thank you so much...

    Best regards.

  • #2
    Super Moderator
    Join Date
    May 2002
    Location
    Perth Australia
    Posts
    4,040
    Thanks
    10
    Thanked 92 Times in 90 Posts
    you get better results if spelt 'vigenere' (not that I pretend to know which is correct) , an example @ http://www.marcobax.nl/?p=27
    resistance is...

    MVC is the current buzz in web application architectures. It comes from event-driven desktop application design and doesn't fit into web application design very well. But luckily nobody really knows what MVC means, so we can call our presentation layer separation mechanism MVC and move on. (Rasmus Lerdorf)

  • #3
    New Coder
    Join Date
    May 2006
    Posts
    62
    Thanks
    0
    Thanked 0 Times in 0 Posts
    Quote Originally Posted by redfroc View Post
    Hi everyone...
    May somebody could write an example code to encrypt strings using vegenere cyper according to vegenere table (36x36)? I have search the example with Google but just found nothing.

    Thank you so much...

    Best regards.
    Here's a class I just wrote based on the description at wikipedia: http://en.wikipedia.org/wiki/Vigen%C3%A8re_cipher

    PHP Code:
    /**
    * Example Implementation of the Vigenere Cypher in PHP
    */
    class Vigenere {

        
    /* @param String $table Vigenere table characters */
        
    var $table;
        
    /* @param String $key Vigenere key */
        
    var $key;
        
    /* @param Int $mod Modulus (length of Vigenere table string above) */
        
    var $mod;

        
    /**
        * Constructor
        * @param String $key Vigenere Key
        * @param String $table Optional Character Table (Vigenere table)
        */
        
    function Vigenere($key false$table false) {
            
    $this->table $table $table 'abcdefghijklmnopqrstuvwxyz';
            
    $this->mod strlen($this->table);
            
    $this->key $key $key $this->generateKey();
        }
        
        
    /**
        * Generate a random Vigenere Key (one-time pad)
        */
        
    function generateKey() {
            
    $this->key '';
            for (
    $i 0$i $this->mod$i++) {
                
    $this->key .= $this->table{rand(0$this->mod)};
            }
            return 
    $this->key;
        }
        
        
    /**
        * Get the Vigenere Key being Used
        */
        
    function getKey() {
            return 
    $this->key;
        }
        
        
    /**
        * Encode a String with Vigenere Cipher
        * @param String $str String
        */
        
    function encrypt($str) {
            
    $enc_str '';
            
    $len strlen($str);
            for(
    $i 0$i $len$i++) {
                
    $shift $this->P($this->charAt($str$i)) + $this->P($this->charAt($this->key$i));
                
    $pos $this->modulo($shift$this->mod);
                
    $enc_str .= $this->A($pos);
            }
            return 
    $enc_str;
        }
        
        
    /**
        * Decode a String encoded with Vigenere Cipher
        * @param String $str String
        */
        
    function decrypt($str) {
            
    $txt_str '';
            
    $len strlen($str);
            for(
    $i 0$i $len$i++) {
                
    $shift $this->P($this->charAt($str$i)) - $this->P($this->charAt($this->key$i));
                
    $pos $this->modulo($shift$this->mod);
                
    $txt_str .= $this->A($pos);
            }
            return 
    $txt_str;
        }

        
    /*
        * Position in Table
        */
        
    function P($a) {
            return 
    strpos($this->table$a); // todo: catch chars not in table
        
    }
        
        
    /**
        * Alphabet at Position in Table
        */
        
    function A($p) {
            
    $p $p >= $p strlen($this->table) + $p// include negative positions
            
    return $this->table{$p};
        }
        
        
    /**
        * Alphabet at Position in a String with the string length as Modulus
        */
        
    function charAt($str$i) {
            
    $i $i%strlen($str);
            return 
    $str{$i};
        }
        
        
    /**
        * Modulo
        */
        
    function modulo($n$mod) {
            return 
    $n%$mod;
        }


    Simple Usage:

    PHP Code:
    // test 1
    $text 'thisisatestz';
    $key 'vigenere';

    $Vigenere = new Vigenere($key);
    $enctext =  $Vigenere->encrypt($text);
    $plaintext $Vigenere->decrypt($enctext);

    echo 
    $text.' = '.$enctext.' = '.$plaintext.'<br />'
    Here's an example where you define your own Vigenere table (http://en.wikipedia.org/wiki/Tabula_recta), and have the class generate a random one-time pad (http://en.wikipedia.org/wiki/One-time_pad) key.

    PHP Code:
    // test 2
    $text 'this is a test, number 2.';
    $table 'abcdefghijklmnopqrstuvwxyz0123456789 .,';

    $Vigenere = new Vigenere(false$table);
    $enctext =  $Vigenere->encrypt($text);
    $plaintext $Vigenere->decrypt($enctext);

    echo 
    $text.' = '.$enctext.' = '.$plaintext;
    echo 
    '<hr />Key: ('.$Vigenere->getKey().')<hr />'
    How it works is easily explained below:

    PHP Code:
    <?php

    // test 1
    $text 'thisisatestz';
    $key 'vigenere';
    $table 'abcdefghijklmnopqrstuvwxyz';

    // example on how to get the first corresponding vegenere cypher for the first character, t

    $position strpos($table$text{0}) + strpos($table$key{0});
    $char_pos $position%strlen($table);
    $char $table{$char_pos};
    echo 
    $char;

    ?>
    The following line gets the position of the first character or the text to be encrypted and the first character position of the key in the vigenere table. Then it adds them together.
    PHP Code:
    $position strpos($table$text{0}) + strpos($table$key{0}); 
    $text{0} is the first character in the string $text.

    strpos($table, $text{0}) finds that char in $table, which is a string containing the characters in the vigenere table.

    This position will likely lie outside the table, so the modulo is taken using the number of characters in the table as the modulus.

    PHP Code:
    $char_pos $position%strlen($table); 
    Then we get the character whose position we have from the vigenere table:

    PHP Code:
    $char $table{$char_pos}; 
    Hope that helps you out.
    Fiji Web Design - where i do Joomla Web Design
    Bucabay.com - My blog

  • #4
    Senior Coder
    Join Date
    Aug 2003
    Location
    One step ahead of you.
    Posts
    2,815
    Thanks
    0
    Thanked 3 Times in 3 Posts
    I thought I'll try too.
    PHP Code:
    <?php
    function enc($str$key)
    {
        
    $slen strlen($str);
        
    $klen strlen($key);
        for (
    $i 0$i $slen; ++$i)
        {
            
    $str[$i] = chr(ord($str[$i]) + ord($key[$i $klen]) % 255);
        }
        return 
    base64_encode($str);
    }

    function 
    dec($str$key)
    {
        
    $str base64_decode($str);
        
    $slen strlen($str);
        
    $klen strlen($key);
        for (
    $i 0$i $slen; ++$i)
        {
            
    $tmp = (ord($str[$i]) - ord($key[$i $klen])) % 255;
            
    $str[$i] = ($tmp 0) ? chr(255 $tmp) : chr($tmp);
        }
        return 
    $str;
    }

    $text 'Vigenere cipher test!!!';
    $key 'Who needs a key?';

    print 
    "Key:\t\t$key\n";
    print 
    "Plaintext:\t$text\n";
    $enc enc($text$key);
    print 
    "Encrypted:\t$enc\n";
    $dec dec($enc$key);
    print 
    "Decrypted:\t$dec\n";
    var_dump($text == $dec);
    ?>
    I'm not sure if this was any help, but I hope it didn't make you stupider.

    Experience is something you get just after you really need it.
    PHP Installation Guide Feedback welcome.

  • #5
    New Coder
    Join Date
    Mar 2006
    Posts
    51
    Thanks
    1
    Thanked 0 Times in 0 Posts
    thank you so much...

  • #6
    New to the CF scene
    Join Date
    Dec 2010
    Location
    Singapore
    Posts
    2
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Vigenere Cipher

    I have just written a vigenere cipher class based on digital's codes. I made the new class more modular and added in some stuff.

    You can view them here:
    http://chernkuan.blogspot.com/2010/1...re-cipher.html

    You can refer to the links in the blog post to download the 2 PHP files to test the program.

  • #7
    New to the CF scene
    Join Date
    Apr 2014
    Posts
    1
    Thanks
    0
    Thanked 0 Times in 0 Posts
    Quote Originally Posted by firepages View Post
    you get better results if spelt 'vigenere' (not that I pretend to know which is correct) , an example @ Vignere encryption - marcobax.nl
    Since my blog has gotten more and more traffic from this (old) url, I've put up the old script! You can go to Vignere encryption - marcobax.nl or via the new one: Vignere encryption - marcobax.nl. Since it's been over 7 years that I last edited this script (or checked) it's wise to test it out before you use it. Enjoy!


  •  

    Posting Permissions

    • You may not post new threads
    • You may not post replies
    • You may not post attachments
    • You may not edit your posts
    •