ciphers continued--Vegerne Cipher and its application in php
>
2.Vegerne Cipher-
The Vigenère cipher is a method of encrypting alphabetic text by using a series of different Caesar ciphers based on the letters of a keyword. It is a simple form of polyalphabetic substitution. The Vigenère cipher has been reinvented many times.
lets look at the image for vegerne ciphers logic
now i will make a function for encription by logic of vegerne cipher
lets look at the image for vegerne ciphers logic
function vegerne_cipher($str,$str_passwd)
{
if(preg_match("/[^a-zA-Z]/", $str_passwd))
{
return null;
}
else
{
$str_password=strtolower($str_passwd);
$pass_arr=array();
$pass_arr_num=array();
$alpha=array('a'=>'1','b'=>'2','c'=>'3','d'=>'4','e'=>'5','f'=>'6','g'=>'7','h'=>'8','i'=>'9','j'=>'10','k'=>'11','l'=>'12','m'=>'13','n'=>'14','o'=>'15','p'=>'16','q'=>'17','r'=>'18','s'=>'19','t'=>'20','u'=>'21','v'=>'22','w'=>'23','x'=>'24','y'=>'25','z'=>'26');
$one_str_pass=strlen($str_password)-1;
$one_str=strlen($str)-1;
$alpha_num=array_keys($alpha);
$match_num=array();
$nums=array_flip($alpha);
$encoded=array();
$string=strtolower($str);
if(is_array($string)==true)
{
throw new Exception("Can't use array as string");
}
if(strlen($str_password)>0)
{
for ($b=0; $b < strlen($str) ; $b++)
{
$p=$b;
if($p>$str_password)
{
$p=$b%strlen($str_password);
}
$pass_arr[$b]=$str_password[$p];
$pass_arr_num[$b]=$alpha[$pass_arr[$b]];
}
for ($i=0; $i < count($pass_arr_num) ; $i++)
{
for ($j=0; $j < 26 ; $j++)
{
$match=preg_match("/".$alpha_num[$j]."/",$string[$i]);
if($match==1)
{
$match_num[$i]=$alpha[$alpha_num[$j]];
$match_num[$i]+=$pass_arr_num[$i];
if($match_num[$i]>26)
{
$match_num[$i]=$match_num[$i]-26;
}
$encoded[$i]=$nums[$match_num[$i]];
}
}
$matchtwo=preg_match('/[^a-z]/',$string[$i]);
if($matchtwo==1)
{
$encoded[$i]=$string[$i];
}
}
}
$enc=implode("",$encoded);
return $enc;
}
}
Comments
Post a Comment