 The Vigenere cipher is poly-alphabetic and is a sequence of Caesar ciphers with different shift values. For example, the first character is encoded with a shift of 3, the second - by 5, the third - by 8, etc. The numerical sequence of shift values ​​is memorized using a code word, where the position of the corresponding letter in the original alphabet means the desired shift value. So, for the code word “AVERS”, the first character of our text will be encrypted without a shift (“A” -0), the second - with a shift by 2 (“B” -2) ... the fifth one - with a shift by 18 (“C” -18 ), the sixth - again without a shift (“A”), etc. As a result, for the code word “AVERS” we get “PARUS” -> “PVCDG”.
 The Vigenere cipher is poly-alphabetic and is a sequence of Caesar ciphers with different shift values. For example, the first character is encoded with a shift of 3, the second - by 5, the third - by 8, etc. The numerical sequence of shift values ​​is memorized using a code word, where the position of the corresponding letter in the original alphabet means the desired shift value. So, for the code word “AVERS”, the first character of our text will be encrypted without a shift (“A” -0), the second - with a shift by 2 (“B” -2) ... the fifth one - with a shift by 18 (“C” -18 ), the sixth - again without a shift (“A”), etc. As a result, for the code word “AVERS” we get “PARUS” -> “PVCDG”.function vizhener_encode($text,$kod) // ,   { $kod=strtoupper($kod); $string=strtoupper($text); $enc = array(); $dec = array(); $str="0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZ"; for($i=0;$i<strlen($str);$i++) { for($j=0;$j<strlen($str);$j++) { $ij=$i+$j; if($ij>=strlen($str)) { $ij=$ij-strlen($str); } $enc[$str{$i}][$str{$j}]=$str{$ij}; $dec[$str{$i}][$str{$ij}]=$str{$j}; } } $pos=0; $result=""; $string=eregi_replace(" ","_",$string); for($i=0;$i<strlen($string);$i++) { if(!eregi($string{$i},$str)) { $result=$result.$string{$i}; } else { $result=$result.$enc[$kod{$pos}][$string{$i}]; $pos=$pos+1; if($pos>=strlen($kod)) { $pos=$pos-strlen($kod); } } } return $result; } function vizhener_encode_mod($text,$kod) // ,   { $kod=strtoupper($kod); $string=strtoupper($text); $enc = array(); $dec = array(); $str="0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZ"; $add=mt_rand(1,9); $string=$string; for($i=1;$i<=$add;$i++) { $string=$string.$str{mt_rand(0,strlen($str))}; } for($i=0;$i<strlen($str);$i++) { for($j=0;$j<strlen($str);$j++) { $ij=$i+$j; if($ij>=strlen($str)) { $ij=$ij-strlen($str); } $enc[$str{$i}][$str{$j}]=$str{$ij}; $dec[$str{$i}][$str{$ij}]=$str{$j}; } } $pos=0; $result=""; $string=eregi_replace(" ","_",$string); $pos=$pos+$add; while($pos>=strlen($kod)) { $pos=$pos-strlen($kod); } if($pos<0) { $pos=$pos+strlen($kod); } for($i=0;$i<strlen($string);$i++) { if(!eregi($string{$i},$str)) { $result=$result.$string{$i}; } else { $result=$result.$enc[$kod{$pos}][$string{$i}]; $pos=$pos+1; if($pos>=strlen($kod)) { $pos=$pos-strlen($kod); } } } $result=$enc[$kod{0}][$add].$result; return $result; } function vizhener_decode($text,$kod) // ,   { $kod=strtoupper($kod); $string=strtoupper($text); $enc = array(); $dec = array(); $str="0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZ"; for($i=0;$i<strlen($str);$i++) { for($j=0;$j<strlen($str);$j++) { $ij=$i+$j; if($ij>=strlen($str)) { $ij=$ij-strlen($str); } $enc[$str{$i}][$str{$j}]=$str{$ij}; $dec[$str{$i}][$str{$ij}]=$str{$j}; } } $pos=0; $result=""; $string=eregi_replace(" ","_",$string); for($i=0;$i<strlen($string);$i++) { if(!eregi($string{$i},$str)) { $result=$result.$string{$i}; } else { $result=$result.$dec[$kod{$pos}][$string{$i}]; $pos=$pos+1; if($pos>=strlen($kod)) { $pos=$pos-strlen($kod); } } } return $result; } function vizhener_decode_mod($text,$kod) // ,   { $kod=strtoupper($kod); $string=strtoupper($text); $enc = array(); $dec = array(); $str="0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZ"; for($i=0;$i<strlen($str);$i++) { for($j=0;$j<strlen($str);$j++) { $ij=$i+$j; if($ij>=strlen($str)) { $ij=$ij-strlen($str); } $enc[$str{$i}][$str{$j}]=$str{$ij}; $dec[$str{$i}][$str{$ij}]=$str{$j}; } } $pos=0; $result=""; $string=eregi_replace(" ","_",$string); $add=$dec[$kod{0}][$string{0}]; $pos=$pos+$add; while($pos>=strlen($kod)) { $pos=$pos-strlen($kod); } if($pos<0) { $pos=$pos+strlen($kod); } for($i=1;$i<(strlen($string)-$add);$i++) { if(!eregi($string{$i},$str)) { $result=$result.$string{$i}; } else { $result=$result.$dec[$kod{$pos}][$string{$i}]; $pos=$pos+1; if($pos>=strlen($kod)) { $pos=$pos-strlen($kod); } } } return $result; } Source: https://habr.com/ru/post/109904/
All Articles