在PHP+MySQL应用中使用XOR运算加密算法
mysql|加密|算法|运算
【摘 要】 上一部分介绍了如何使用XOR运算进行加密/解密的原理,本节将使用其加密用户的登录信息。根据上一小节介绍的XOR加密算法的原理,不难写出以下的加密解密函数。首先列出加密算法。
本文将介绍一个简单易用的加密/解密算法:使用异或(XOR)运算。本算法原理简单,旨在使读者对信息的加密/解密有一个更加直观的印象。
XOR算法原理
从加密的主要方法看,换位法过于简单,特别是对于数据量少的情况很容易由密文猜出明文,而替换法不失为一种行之有效的简易算法。
从各种替换法运算的特点看,异或运算最适合用于简易加解密运算,这种方法的原理是:当一个数A和另一个数B进行异或运算会生成另一个数C,如果再将C和B进行异或运算则C又会还原为A。
相对于其他的简易加密算法,XOR算法的优点如下。
(1)算法简单,对于高级语言很容易能实现。
(2)速度快,可以在任何时候、任何地方使用。
(3)对任何字符都是有效的,不像有些简易加密算法,只对西文字符有效,对中文加密后再解密无法还原为原来的字符。
XOR算法实现
上一部分介绍了如何使用XOR运算进行加密/解密的原理,本节将使用其加密用户的登录信息。根据上一小节介绍的XOR加密算法的原理,不难写出以下的加密解密函数。首先列出加密算法。
1 <!--encrypy_xor:简单使用XOR运算的加密函数----------------------->
2 <?php
3 //加密函数
4 function myEncrypt($string, $key)
5 {
6 for($i=0; $i<STRLEN($STRING); p $i++)
7 {
8 for($j=0; $j<STRLEN($KEY); p $j++)
9 {
10 $string[$i] = $string[$i]^$key[$j];
11 }
12 }
13 return $string;
14 }
第4行定义了加密函数myEncrypt(),输入参数$string为明文,而$key为密钥;输出为使用$key作为密钥并使用XOR加密算法产生的密文。
第6~12行的外层for循环对明文字符串的每一个字符进行循环,而内层的for循环(第8~11行)对明文的每一字符循环与密钥的每一位做异或运算。其原理已经在上一小节中介绍,不再重述。
同样,与加密函数类似,可以写出下面的解密函数。
1 //解密函数
2 function myDecrypt($string, $key)
3 {
4 for($i=0; $i<STRLEN($STRING); p $i++)
5 {
6 for($j=0; $j<STRLEN($KEY); p $j++)
7 {
8 $string[$i] = $key[$j]^$string[$i];
9 }
10 }
11 return $string;
12 }
13 ?>
第4行定义了解密函数myDecrypt (),输入参数$string为密文,而$key为密钥;输出为使用$key作为密钥并使用XOR解密算法产生的明文。
下面,通过一个应用示例来进一步说明加密函数的功能。
1 //示例
2 $my_password="chair";
3 echo "my_password = $my_password";
4 $my_key="1234567890";
5 $my_password_en=myEncrypt($my_password,$my_key);
6 echo "my_password_en = $my_password_en";
7 $my_password_de=myDecrypt($my_password_en,$my_key);
8 echo "my_password_de = $my_password_de";
第3行首先定义了一个明文$my_password,然后在第4行定义密钥$my_key。
第5、6行分别调用加密函数生成密文并输出;反过来,又在第7、8行将密文解密。
上面示例的运行结果如下。
my_password = chair
my_password_en = RYPXC
my_password_de = chair
用XOR算法实现身份验证
上两部分分别介绍了使用XOR运算进行信息加密/解密的原理和实现,下面,将使用这一方法来对用户的登录密码进行加密。本例中,为了保护用户的密码,系统想要达到的目的如下。
·在用户注册时,用户需要添写用户密码表单。
·除用户本人之外,其他任何人都无法获取其密码信息,包括系统设计者和数据库管理员。
·系统能根据用户输入的密码验证用户的合法性。
为了达到以上目的,使用XOR算法时可以选择用户名作为明文,而密钥是用户自定义的密码,然后将加密后的用户名存储在数据库中。
另外,在用户登录的时候,有以下两种方式来验证合法用户。
(1)根据其提交的用户名(明文)和密码(密钥)信息重新加密,并使用加密后的信息与数据库中存储的密码信息进行比较,如果相等,则用户合法,否则,为非法用户。
(2)根据数据库中存储的密码信息(明文)和用户输入的密码(密钥)信息进行解密,并把加密后的信息与用户提交的用户名进行比较,如果相等,则用户合法,否则,为非法用户。
两种方式都可以实现第3个目的,本例,将采用第2种方式。本例的实现代码可在18.4.1节“用户登录”和18.4.2节“检查用户”的实现基础之上实现,其中“用户登录”页面无需变化,“检查用户”的实现参考如下。
1 <?php
2 session_start(); //装载Session库,一定要放在首行
3 $user_name=$_POST["user_name"];
4 session_register("user_name"); //注册$user_name变量,注意没有$符号
5
6 require_once("sys_conf.inc"); //系统配置文件,包含数据库配置信息
7 require_once("encrypy_xor.php"); //包含xor加密函数文件
8
9 //连接数据库
10 $link_id=mysql_connect($DBHOST,$DBUSER,$DBPWD);
11 mysql_select_db($DBNAME); //选择数据库my_chat
12
13 //查询是否存在登录用户信息
14 $str="select name,password from user where name ='$user_name'";
15 $result=mysql_query($str,$link_id); //执行查询
16 @$rows=mysql_num_rows($result); //取得查询结果的记录笔数
17 $user_name=$_SESSION["user_name"];
18 $password=$_POST["password"];
19 $password_en=myEncrypt($user_name,$password); //加密用户信息
20
21 //对于老用户
22 if($rows!=0)
23 {
24 list($name,$pwd)=mysql_fetch_row($result);
25 $password_de=myDecrypt($pwd,$password); //解密用户信息
26
27 //如果密码输入正确
28 if($user_name==$password_de)
29 {
30 $str="update user set is_online =1 where name ='$user_name' and password='$password_en'";
31 $result=mysql_query($str, $link_id); //执行查询
32 require("main.php"); //转到聊天页面
33 }
34 //密码输入错误
35 else
36 {
37 require("relogin.php");
38 }
39 }
40 //对于新用户,将其信息写入数据库
41 else
42 {
43 $str="insert into user (name,password,is_online) values('$user_name', '$password_en',1)";
44 $result=mysql_query($str, $link_id); //执行查询
45 require("main.php"); //转到聊天页面
46 }
47 //关闭数据库
48 mysql_close($link_id);
49 ?>
第7行引入了加密函数文件encrypy_xor.php,包括上一小节介绍的两个函数。
第19行,使用用户提交的用户名和密码得到加密后的密码值,并且对于新用户,在第44行将这个加密后的值存储在数据库中。
另外,对于老用户,在第24获取数据库中用户名和加密后的密码信息,并在25行利用这两个值进行解密,然后在第28行通过比较解密后的值与用户提交的用户名信息来检查用户的合法性。
自动生成密钥
上一部分介绍了如何使用XOR加密算法进行对用户信息的加密,其中,用户所输入的口令信息实际上成为了加密算法中的密钥,而用户名作为明文使用,虽然这能很好地完成功能,但是在逻辑上,这种方法似乎有些不合理。
本文将介绍一种自动生成密钥的技术,可以使用自动生成的密钥对用户提交的密码明文加密,使逻辑更加合理一些。
本例,假设生成的密钥为512位。代码如下。
1 <!--keygen.php:自动生成密钥------------------------------------>
2 <?php
3
4 //自动生成长度为$len的密钥
5 function generate_key($len)
6 {
7 $lowerbound = 35 ;
8 $upperbound = 96 ;
9 $strMyKey = "";
10
11 for($i=1;$i12 {
13 $rnd=rand(0,100); //产生随机数
14 $k = (($upperbound - $lowerbound) + 1) * $rnd + $lowerbound;
15 $strMyKey=$strMyKey.$k;
16 }
17 return $strMyKey;
18 }
19
20 //将密钥写入文件$file_name
21 function write_key($key,$file_name)
22 {
23 $filename="C:\key.txt";
24 $key=generate_key($key,512);
25
26 //使用添加模式打开$filename,文件指针将会在文件的末尾
27 if(!$handle=fopen($filename,'w'))
28 {
29 print"不能打开文件$filename";
30 exit;
31 }
32
33 //将$key写入到我们打开的文件中。
34 if(!fwrite($handle,$key))
35 {
36 print"不能写入到文件$filename";
37 exit;
38 }
39 fclose($handle);
40 }
41
42 //读取密钥文件中的密钥
43 function get_key($file_name)
44 {
45 //打开文件
46 $fp = fopen ($file_name, "r");
47 $result="";
48 //逐行读取
49 while (!feof($fp))
50 {
51 $buffer = fgets($fp, 4096);
52 $result=$result.$buffer;
53 }
54 return $result;
55 }
56
57 ///*
58 $KeyLocation = "C:\key.txt"; //保存密钥的文件
59 $key="123456";
60 write_key($key,$KeyLocation);
61 echo get_key($KeyLocation);
62 //*/
63 ?>
代码包括3个函数。
·generate_key($len):自动生成长度为$len的密钥
·write_key($key,$file_name):将密钥写入文件$file_name
·get_key($file_name):读取密钥文件$file_name中的密钥值
在使用时,当用户第一次登录系统时,自动为其生成密钥值,对于这个密钥值,可以有两种方式来处理。
(1)将其存入数据库的某个字段中,这种方法的缺点是密钥在数据库中的安全性无法得到保证;
(2)将这个密钥保存在用户本地的文件中,这样就可以避免密钥被别人获取,但这种方式的缺点是,当用户使用其他机器访问系统时,就无法登录。
本例中,将使用第2种方式。
具体地,上面代码第11~18行通过生成随机数的方式来不断生成密钥,并通过一个计算来增强其复杂性。其中的lowerbound和upperbound的数值其实就是你想使用来加密的ASCII字符范围。下面是生成的一个密钥文件示例。
208123915925183361116049369344372701567721435181102718332639307390344373445407
524316475863232913993383189547474747394154915312639841226741894189965623523913
011164730113445201935692839710274127251577929493941487145611337531549110895367
593586318332391170941272701152344371709270125776235313540032267139933835677407
617384135696111239130732949469623520815987524358635491542913374933524334454251
400327015367133759324537171709152357391089524342514685239122673135531363151191
833412771743139654…
最后,需要把密钥保存在服务器上一个安全的地方,然后就可以利用其和诸如XOR这样的加密算法来对用户信息进行加密/解密了。如何在上一部分介绍的XOR中使用这个密钥非常简单,不再详述。

Hot AI Tools

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Undress AI Tool
Undress images for free

Clothoff.io
AI clothes remover

AI Hentai Generator
Generate AI Hentai for free.

Hot Article

Hot Tools

Notepad++7.3.1
Easy-to-use and free code editor

SublimeText3 Chinese version
Chinese version, very easy to use

Zend Studio 13.0.1
Powerful PHP integrated development environment

Dreamweaver CS6
Visual web development tools

SublimeText3 Mac version
God-level code editing software (SublimeText3)

Hot Topics

Convert basic data types to strings using Java's String.valueOf() function In Java development, when we need to convert basic data types to strings, a common method is to use the valueOf() function of the String class. This function can accept parameters of basic data types and return the corresponding string representation. In this article, we will explore how to use the String.valueOf() function for basic data type conversions and provide some code examples to

What is Identity in SQL? Specific code examples are needed. In SQL, Identity is a special data type used to generate auto-incrementing numbers. It is often used to uniquely identify each row of data in a table. The Identity column is often used in conjunction with the primary key column to ensure that each record has a unique identifier. This article will detail how to use Identity and some practical code examples. The basic way to use Identity is to use Identit when creating a table.

Method of converting char array to string: It can be achieved by assignment. Use {char a[]=" abc d\0efg ";string s=a;} syntax to let the char array directly assign a value to string, and execute the code to complete the conversion.

1. Function Overview Keyspace notification allows clients to receive events that modify Rediskey changes in some way by subscribing to channels or patterns. All commands that modify key keys. All keys that received the LPUSHkeyvalue[value…] command. All expired keys in the db database. Events are distributed through Redis's subscription and publishing functions (pub/sub), so all clients that support subscription and publishing functions can directly use the keyspace notification function without any modifications. Because the current subscription and publishing functions of Redis adopt a fireandforget strategy, if your program

An unpatchable Yubico two-factor authentication key vulnerability has broken the security of most Yubikey 5, Security Key, and YubiHSM 2FA devices. The Feitian A22 JavaCard and other devices using Infineon SLB96xx series TPMs are also vulnerable.All

Hello everyone, today I will share with you the basic knowledge of Java: String. Needless to say the importance of the String class, it can be said to be the most used class in our back-end development, so it is necessary to talk about it.

Problems encountered: During the development process, you will encounter keys that need to be deleted in batches according to certain rules, such as login_logID (ID is a variable). Now you need to delete data such as "login_log*", but redis itself only has batch query. Command keys for class key values, but there is no command for batch deletion of a certain class. Solution: Query first, then delete, use xargs to pass parameters (xargs can convert pipe or standard input (stdin) data into command line parameters), execute the query statement first, and then remove the queried key value and the original del parameters. delete. redis-cliKEYSkey* (search condition)|xargsr

Replace characters (strings) in a string using Java's String.replace() function In Java, strings are immutable objects, which means that once a string object is created, its value cannot be modified. However, you may encounter situations where you need to replace certain characters or strings in a string. At this time, we can use the replace() method in Java's String class to implement string replacement. The replace() method of String class has two types:
