PHP 实现Affine 简单仿射密码 [信息安全]

PHP 实现简单仿射密码 [2007]

欧几里德算法/扩展欧几里德算法的PHP实现

 

 affine_form.php

 

affine.php

getMessage(); } } } else if($_POST['cmdDecrypt'] == 'Decrypt') { //如果提交解密按键 $key1 = settype(trim($_POST['txtKey1']), 'int'); $key2 = settype(trim($_POST['txtKey2']), 'int'); $content = $_POST['txtContent']; if(!filled_out($_POST)) { //输出出错页面 do_html_header('Error~'); display_error_form(100); do_html_footer(); } else { try { $result = Decrypt($key1, $key2, $content); do_html_header('Result~'); diplay_affine_result($result); do_html_footer(); } catch (Exception $e) { echo @$e->getMessage(); } } } ?> 

affine.inc.php

96 && $tmp <123) { $c = ($a*($tmp-97)+$b)%26; $cryptograph .= chr($c + 97); } elseif ($tmp > 64 && $tmp < 91) { $c = ($a*($tmp-65)+$b)%26; $cryptograph .= chr($c + 65); } else { echo " Unknow Error occured...Program Exit..."; exit(); } } } return $cryptograph; } function Decrypt ($a, $b, $str) { $a = ExtendEuclid($a); if ($a == 0) { echo "该数没有乘法逆元!!"; } else { $a = ExtendEuclid($a); $length = strlen($str); for ($i = 0; $i < $length; $i++) { $tmp = ord($str[$i]); //获取字符串中每个字母的ascii码 int ord() //Check whether the $tmp is a valid number; valid_text($tmp); if ($tmp > 96 && $tmp <123) { $c = ($a*($tmp-97-$b))%26; $expressData .= chr($c + 97); } elseif ($tmp > 64 && $tmp < 91) { $c = ($a*($tmp-65-$b))%26; $expressData .= chr($c + 65); } else { echo " Unknow Error occured...Program Exit..."; exit(); } } } return $expressData; } /** *检查表单项是否完整 * * @param unknown_type $form_vars * @return true or false */ function filled_out ($form_vars) { // test that each variable has a value foreach ($form_vars as $key => $value) { if (!isset($key) || ($value == '')) { return false; } } return true; } /** * 检查明文/密文是否符合格式... * * @param int $num * @return true or error form... */ function valid_text ($num) { if (($num > 96 && $num < 123)||($num >64 && $num <91)) { return TRUE; } else { @do_html_header(); display_error_form(200); do_html_footer(); } } ?> 

extendEuclid.inc.php

 

output_fns.php

<?php echo $title;?>



{$heading}"; } /** * Display the sidebar include the calendar and the categories... * */ function display_html_sidebar () { ?>

AFFINE INPUT!

请输入密钥 AND

请输入要加密/解密的文本: