...

View Full Version : Vegenere Cipher using PHP



redfroc
06-07-2007, 01:25 PM
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.

firepages
06-07-2007, 02:52 PM
you get better results if spelt 'vigenere' (not that I pretend to know which is correct) , an example @ http://www.marcobax.nl/?p=27

digital-ether
06-08-2007, 12:05 AM
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


/**
* 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 >= 0 ? $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:


// 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.


// 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

// 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.


$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.


$char_pos = $position%strlen($table);

Then we get the character whose position we have from the vigenere table:


$char = $table{$char_pos};

Hope that helps you out.

marek_mar
06-08-2007, 01:35 AM
I thought I'll try too.


<?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);
?>

redfroc
06-10-2007, 01:24 PM
thank you so much... :)

chernkuan
12-20-2010, 02:02 AM
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/12/vigenere-cipher.html

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

marcobax
04-10-2014, 05:29 PM
you get better results if spelt 'vigenere' (not that I pretend to know which is correct) , an example @ Vignere encryption - marcobax.nl (http://www.marcobax.nl/?p=27)

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 (http://www.marcobax.nl/?p=27) or via the new one: Vignere encryption - marcobax.nl (http://www.marcobax.nl/vignere-encryption/). 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! :)



EZ Archive Ads Plugin for vBulletin Copyright 2006 Computer Help Forum