<?xml version="1.0" encoding="UTF-8" ?>
<rss version="2.0">
<channel>
<title><![CDATA[向东博客 专注WEB应用 构架之美 --- 构架之美，在于尽态极妍 | 应用之美，在于药到病除]]></title> 
<link>http://www.jackxiang.com/index.php</link> 
<description><![CDATA[赢在IT，Playin' with IT,Focus on Killer Application,Marketing Meets Technology.]]></description> 
<language>zh-cn</language> 
<copyright><![CDATA[向东博客 专注WEB应用 构架之美 --- 构架之美，在于尽态极妍 | 应用之美，在于药到病除]]></copyright>
<item>
<link>http://www.jackxiang.com/post//</link>
<title><![CDATA[[聊天加密]前端crypto-js AES加密解密和PHP后端解密，加密。]]></title> 
<author>jack &lt;xdy108@126.com&gt;</author>
<category><![CDATA[Php/Js/Shell/Go]]></category>
<pubDate>Tue, 27 Aug 2013 02:19:02 +0000</pubDate> 
<guid>http://www.jackxiang.com/post//</guid> 
<description>
<![CDATA[ 
	实践中遇到的问题：<br/>在Js加密时出现，用Jquery进行$(&quot;#login_pwd&quot;)是不行的，包括用手写赋值也是不蚝的，还得（chatValidate是form的name名字）：<br/> document.chatValidate.login_pwd.value = encryptedLoginUserPwd;//加密后再传输&nbsp;&nbsp;<br/>通过console.log输出这个encryptedLoginUserPwd是一个对旬，但用:document.write是可以输出的，可能还是对这个加密函数不是太了解罢，还是对Js：<br/><textarea name="code" class="php" rows="15" cols="100">
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;var chatUserPwd&nbsp;&nbsp;= $(&quot;#login_pwd&quot;).val();//密码
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;var key_hash = CryptoJS.MD5(&quot;Message&quot;);
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;var key = CryptoJS.enc.Utf8.parse(key_hash);
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;var iv&nbsp;&nbsp;= CryptoJS.enc.Utf8.parse(&#039;1234567812345678&#039;);
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;var encryptedLoginUserPwd = CryptoJS.AES.encrypt(chatUserPwd, key, &#123; iv: iv,mode:CryptoJS.mode.CBC,padding:CryptoJS.pad.ZeroPadding&#125;);
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;document.chatValidate.login_pwd.value = encryptedLoginUserPwd;//加密后再传输 
</textarea><br/><br/>crypto-js提供了多种常用加密算法的JS库。这里不多解释。<br/><br/>这里主要讲 前端使用crypto-js AES加密后，php解密。<br/><br/>前端js<br/><br/>&lt;script src=&quot;http://crypto-js.googlecode.com/svn/tags/3.0.2/build/rollups/aes.js&quot;&gt;&lt;/script&gt;<br/>&lt;script src=&quot;http://crypto-js.googlecode.com/svn/tags/3.0.2/build/rollups/md5.js&quot;&gt;&lt;/script&gt;<br/>&lt;script src=&quot;http://crypto-js.googlecode.com/svn/tags/3.0.2/build/components/pad-zeropadding.js&quot;&gt;&lt;/script&gt;<br/>&lt;script&gt;<br/>&nbsp;&nbsp;&nbsp;&nbsp;var key_hash = CryptoJS.MD5(&quot;Message&quot;);<br/>&nbsp;&nbsp;&nbsp;&nbsp;var key = CryptoJS.enc.Utf8.parse(key_hash);<br/>&nbsp;&nbsp;&nbsp;&nbsp;var iv&nbsp;&nbsp;= CryptoJS.enc.Utf8.parse(&#039;1234567812345678&#039;);<br/>&nbsp;&nbsp;&nbsp;&nbsp;var encrypted = CryptoJS.AES.encrypt(&quot;Message&quot;, key, &#123; iv: iv,mode:CryptoJS.mode.CBC,padding:CryptoJS.pad.ZeroPadding&#125;);<br/>&nbsp;&nbsp;&nbsp;&nbsp;document.write(&quot;encode:&quot;+encrypted);<br/>&lt;/script&gt;<br/>php代码<br/><br/>&lt;?php<br/>$text = &quot;Message&quot;;<br/>$key = md5($text);&nbsp;&nbsp;//key的长度必须16，32位,这里直接MD5一个长度为32位的key<br/>$iv=&#039;1234567812345678&#039;;<br/>$crypttext = mcrypt_encrypt(MCRYPT_RIJNDAEL_128, $key, $text, MCRYPT_MODE_CBC, $iv);<br/>$decode = mcrypt_decrypt(MCRYPT_RIJNDAEL_128, $key, $crypttext, MCRYPT_MODE_CBC, $iv);<br/>echo base64_encode($crypttext);<br/>echo &quot;&lt;br/&gt;&quot;;<br/>echo $decode;<br/>echo &quot;&lt;br/&gt;&quot;;<br/>?&gt;<br/><br/>http://localhost/aes/aes.html<br/>encode:yMjizJCGQh+jxX4BXEtlNw==<br/>http://localhost/aes/aes.php<br/>yMjizJCGQh+jxX4BXEtlNw==<br/>Message<br/>实践是OK的，来自：<br/>http://www.madeby83.com/%E5%89%8D%E7%AB%AFcrypto-js-aes%E5%8A%A0%E5%AF%86-php%E5%90%8E%E7%AB%AF%E8%A7%A3%E5%AF%86.html<br/><br/><br/>前端解密：<br/><textarea name="code" class="JS" rows="15" cols="100">
&lt;!DOCTYPE html PUBLIC &quot;-//W3C//DTD XHTML 1.0 Transitional//EN&quot; &quot;http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd&quot;&gt;
&lt;html xmlns=&quot;http://www.w3.org/1999/xhtml&quot;&gt;
&lt;head&gt;
&lt;meta http-equiv=&quot;Content-Type&quot; content=&quot;text/html; charset=utf-8&quot; /&gt;
&lt;title&gt;无标题文档&lt;/title&gt;
&lt;/head&gt;
&lt;script src=&quot;./aes.js&quot;&gt;&lt;/script&gt;
&lt;script src=&quot;./md5.js&quot;&gt;&lt;/script&gt;
&lt;script src=&quot;./pad-zeropadding.js&quot;&gt;&lt;/script&gt; 
&lt;script&gt;
var data = &quot;message&quot;;
var key&nbsp;&nbsp;= CryptoJS.enc.Latin1.parse(&#039;1234567812345678&#039;);
var iv&nbsp;&nbsp; = CryptoJS.enc.Latin1.parse(&#039;1234567812345678&#039;);
 //加密
var encrypted = CryptoJS.AES.encrypt(data,key,&#123;iv:iv,mode:CryptoJS.mode.CBC,padding:CryptoJS.pad.ZeroPadding&#125;);
document.write(encrypted.ciphertext);
document.write(&#039;&lt;br/&gt;&#039;);
document.write(encrypted.key);
document.write(&#039;&lt;br/&gt;&#039;);
document.write(encrypted.iv);
document.write(&#039;&lt;br/&gt;&#039;);
document.write(encrypted.salt);
document.write(&#039;&lt;br/&gt;&#039;);
document.write(encrypted);
document.write(&#039;&lt;br/&gt;&#039;);
//解密
var decrypted = CryptoJS.AES.decrypt(encrypted,key,&#123;iv:iv,padding:CryptoJS.pad.ZeroPadding&#125;);
console.log(decrypted.toString(CryptoJS.enc.Utf8));
&lt;/script&gt;
&lt;body&gt;
&lt;/body&gt;
&lt;/html&gt;

</textarea><br/><br/>http://localhost/chat/JsJiamiJiemi.html<br/>dd52feee2ecea4ca159399e2dfb1d0bb<br/>31323334353637383132333435363738<br/>31323334353637383132333435363738<br/>undefined<br/>3VL+7i7OpMoVk5ni37HQuw==<br/>1234567890 <br/><br/><br/>PHP加密：<br/>$chat_msg_Html =&nbsp;&nbsp;nl2br($chat_msg);<br/>&nbsp;&nbsp;$outEncryOutText = &quot;Message&quot;;<br/>&nbsp;&nbsp;$key = md5($outEncryOutText);&nbsp;&nbsp;//key的长度必须16，32位,这里直接MD5一个长度为32位的key<br/>&nbsp;&nbsp;$iv=&#039;1234567812345678&#039;;<br/>&nbsp;&nbsp;$cryptEncryOutText = mcrypt_encrypt(MCRYPT_RIJNDAEL_128, $key, $outEncryOutText, MCRYPT_MODE_CBC, $iv);<br/>&nbsp;&nbsp;echo base64_encode($cryptEncryOutText);<br/>如果内容是message则echo会输出 ：<br/>yMjizJCGQh+jxX4BXEtlNw== <br/><br/><textarea name="code" class="php" rows="15" cols="100">
//显示聊天记录
if ($action==&quot;showAjax&quot;)
&#123;
&nbsp;&nbsp;&nbsp;&nbsp;isLoginCheck();//检查用户是否登录
&nbsp;&nbsp;&nbsp;&nbsp; if (file_exists(CHAT_NOTE)) &#123;
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;$chat_msg = @file_get_contents(CHAT_NOTE);
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;$des = new helper_des(KEY,IV);
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;$chat_msg = $des-&gt;decrypt($chat_msg);
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;$chat_msg_Html =&nbsp;&nbsp;nl2br($chat_msg);
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;if(!empty($chat_msg_Html))&#123;
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;$outEncryOutText = $chat_msg_Html;
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;$key = md5(&quot;Message&quot;);&nbsp;&nbsp;//key的长度必须16，32位,这里直接MD5一个长度为32位的key
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;$iv=&#039;1234567812345678&#039;;
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;$cryptEncryOutText = mcrypt_encrypt(MCRYPT_RIJNDAEL_128, $key, $outEncryOutText, MCRYPT_MODE_CBC, $iv);
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;echo base64_encode($cryptEncryOutText);
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&#125;else&#123;
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;echo &quot;0&quot;;//无记录
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&#125;
&nbsp;&nbsp;&nbsp;&nbsp; &#125; else &#123;
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;echo &quot;0&quot;;//文件不存在导致无记录
&nbsp;&nbsp;&nbsp;&nbsp; &#125;
&#125;

</textarea>
]]>
</description>
</item><item>
<link>http://www.jackxiang.com/post//#blogcomment</link>
<title><![CDATA[[评论] [聊天加密]前端crypto-js AES加密解密和PHP后端解密，加密。]]></title> 
<author> &lt;user@domain.com&gt;</author>
<category><![CDATA[评论]]></category>
<pubDate>Thu, 01 Jan 1970 00:00:00 +0000</pubDate> 
<guid>http://www.jackxiang.com/post//#blogcomment</guid> 
<description>
<![CDATA[ 
	
]]>
</description>
</item>
</channel>
</rss>