PHP 实现Affine 简单仿射密码 [信息安全]
2009-03-10 20:59
621 查看
PHP 实现简单仿射密码 [2007]
欧几里德算法/扩展欧几里德算法的PHP实现
affine_form.php
affine.php
affine.inc.php
extendEuclid.inc.php
output_fns.php
欧几里德算法/扩展欧几里德算法的PHP实现
affine_form.php
<?php ///仿射密码主程序界面 require_once 'output_fns.php'; require_once 'affine.inc.php'; do_html_header('AffineTesting'); display_affine_form(); do_html_footer(); ?>
affine.php
<?php require_once 'affine.inc.php'; if (trim($_POST['cmdEncrypt']) == 'Encrypt') { //如果提交加密按键 $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 = Encrypt($key1, $key2, $content); do_html_header('Result~'); diplay_affine_result($result); do_html_footer(); } catch (Exception $e) { echo @$e->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
<?php // 仿射密码主程序处理部分 require_once 'extendEuclid.inc.php'; require_once 'output_fns.php'; /** * 加密算法...Encrypt the data * * @param int $a * @param int $b * @param string $str * @return string $cryptograph */ function Encrypt ($a, $b, $str) { $a = ExtendEuclid($a); if ($a == 0) { echo "该数没有乘法逆元!!"; } else { $length = strlen($str); echo $length; 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; $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
<?php /** * 扩展欧几里得算法,返回0或者$d 的乘法逆元... * Extend_Euclid Algorithm * * @param int $d * @return 0 or the Multiplicative inverse */ function ExtendEuclid ($d) { $a1 = 1; $a2 = 0; $a3 = 26; $b1 = 0; $b2 = 1; $b3 = $d; while ($b3 != 1 && $b3 != 0) { $q = $a3 % $b3; //注: 取模 $a % $b 在 $a 为负值时的结果也是负值, 但实际数学定义为正值的可能也有,此处不考虑负值 $t1 = $a1 - $q * $b1; $t2 = $a2 - $q * $b2; $t3 = $a3 - $q * $b3; $a1 = $b1; $a2 = $b2; $a3 = $b3; $b1 = $t1; $b2 = $t2; $b3 = $t3; } if ($b3 == 0) { return 0; } if ($b3 == 1) { if ($b2 < 0) { $b2 += 26; } return $b2; } } ?>
output_fns.php
<?php // 以HTML形式格式化输出函数 /** * Print an HTML header * * @param string $title */ function do_html_header ($title) { ?> <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd"> <!-- Design by Free CSS Templates http://www.psvi.net Released for free under a Creative Commons Attribution 2.5 License Name : StarGazer Description: Fixed-width, two-column design suitable for small sites and blogs. Version : 1.0 Released : 20071222 --> <html xmlns="http://www.w3.org/1999/xhtml"> <head> <meta http-equiv="content-type" content="text/html; charset=GB2312" /> <title><?php echo $title;?></title> <meta name="keywords" content="affine" /> <meta name="description" content="this is a affine encrypt/decript Demo web page" /> <link href="style.css" mce_href="style.css" rel="stylesheet" type="text/css" media="screen" /> </head> <body> <div id="header"> <h1><a href="#" mce_href="#">AFFINE TESTING</a></h1> <h2> </h2> </div> <div id="menu"> <ul> <li><a href="#" mce_href="#">Homepage</a></li> <li><a href="#" mce_href="#">Code</a></li> <li><a href="#" mce_href="#">Image</a></li> <li><a href="#" mce_href="#">About Me</a></li> <li><a href="#" mce_href="#">Contact Me</a></li> </ul> </div> <hr /> <?php if ($title) { //do_html_heading($title); } } /** * Print an HTML footer * */ function do_html_footer () { ?> <hr /> <div id="footer"> <p>(c) 2008 ZUN.com.. Design by <a href="mailto:dengzonghuan@yahoo.com.cn/" mce_href="mailto:dengzonghuan@yahoo.com.cn/">ZUN</a>.</p> </div> </body> </html> <?php } /** * Print heading * * @param unknown_type $heading */ function do_html_heading ($heading) { //echo "<h2>{$heading}</h2>"; } /** * Display the sidebar include the calendar and the categories... * */ function display_html_sidebar () { ?> <!-- begin #sidebar --> <div id="sidebar"> <ul> <li id="search"> <form id="searchform" method="get" action=""> <div> <input type="text" name="s" id="s" size="15" /> <br /> <input name="submit" type="submit" value="Search" /> </div> </form> </li> <li id="calendar"> <h2>Calendar</h2> <div id="calendar_wrap"> <table summary="Calendar"> <caption> November 2007 </caption> <thead> <tr> <th abbr="Monday" scope="col" title="Monday">M</th> <th abbr="Tuesday" scope="col" title="Tuesday">T</th> <th abbr="Wednesday" scope="col" title="Wednesday">W</th> <th abbr="Thursday" scope="col" title="Thursday">T</th> <th abbr="Friday" scope="col" title="Friday">F</th> <th abbr="Saturday" scope="col" title="Saturday">S</th> <th abbr="Sunday" scope="col" title="Sunday">S</th> </tr> </thead> <tfoot> <tr> <td abbr="October" colspan="3" id="prev"><a href="#" mce_href="#" title="View posts for October 2007">« Oct</a></td> <td class="pad"> </td> <td abbr="December" colspan="3" id="next"><a href="3" mce_href="3" title="View posts for October 2007">Dec »</a></td> </tr> </tfoot> <tbody> <tr> <td colspan="3" class="pad"> </td> <td>1</td> <td>2</td> <td>3</td> <td>4</td> </tr> <tr> <td>5</td> <td>6</td> <td>7</td> <td>8</td> <td>9</td> <td>10</td> <td>11</td> </tr> <tr> <td><a href="#" mce_href="#">12</a></td> <td>13</td> <td>14</td> <td>15</td> <td>16</td> <td>17</td> <td>18</td> </tr> <tr> <td>19</td> <td>20</td> <td>21</td> <td>22</td> <td>23</td> <td id="today">24</td> <td>25</td> </tr> <tr> <td>26</td> <td>27</td> <td>28</td> <td>29</td> <td>30</td> <td class="pad" colspan="2"> </td> </tr> </tbody> </table> </div> </li> <li> <h2>Categories</h2> <ul> <li><a href="#" mce_href="#" title="View all posts filed under Nothing">Nothing</a> (0) </li> <li><a href="#" mce_href="#" title="View all posts filed under Nothing">Nothing</a> (0) </li> <li><a href="#" mce_href="#" title="View all posts filed under Nothing">Nothing</a> (0) </li> <li><a href="#" mce_href="#" title="View all posts filed under Nothing">Nothing</a> (0) </li> <li><a href="#" mce_href="#" title="View all posts filed under Nothing">Nothing</a> (0) </li> </ul> </li> </ul> <div style="clear: both; height: 40px;"> </div> </div> <!-- end #sidebar --> <?php } /** * Display the affine form... * */ function display_affine_form () { ?> <div id="page"> <div id="content"> <div class="post"> <h2 class="title"><a href="#" mce_href="#">AFFINE INPUT!</a></h2> <div class="entry"> <form id="form1" method="post" action="affine.php"> <strong>请输入密钥</strong>: <label> <input name="txtKey1" type="text" id="txtKey1" size="12" /> </label> <strong>AND</strong> <label> <input name="txtKey2" type="text" id="txtKey2" size="12" /> </label><br/><br/> <strong>请输入要加密/解密的文本:</strong><br/> <textarea name="txtContent" cols="45" id="txtContent">
相关文章推荐
- 信息安全 - 任意单表代替密码之仿射密码的代码实现和破解 e(x)=ax+b (mod 26)
- PHP+JS密码加密实现HTTP上安全地传输密码
- PHP和JS实现HTTP上安全地传输密码
- PHP会员找回密码功能的简单实现
- 给MD5加上salt随机盐值加密算法实现密码安全的php实现
- 简单的加密与解密的实现---仿射密码
- 密码技术应用设计实践-安全信息传输系统(SITS)(用Java实现DES、RSA、MD5算法)
- Android实现信息安全中维吉尼亚密码技术
- 前端JS RSA加密,PHP后端解密实现密码安全传输
- PHP实现根据密码长度显示安全条
- PHP配合JS实现HTTP安全地传输密码
- 信息安全——RSA密码系统的实现
- 【信息安全技术】DES算法的详细设计(简单实现)
- PHP入门手记-简单实现网页密码验正
- 简单的加密与解密的实现---仿射密码(c++使用string)
- php+mysql实现简单登录注册修改密码网页
- 信息安全——RSA密码系统的实现
- php 调用ffmpeg获取视频信息的简单实现
- php实现简单的用户注册,登录,修改个人信息接口
- PHP实现简单的学生信息管理系统