javascript加密PHP解密---jsencrypt

   今天偶然發現jsencrypt這玩意,以前作"直播室聊天"時 數據傳輸明文問題沒解決; 一直苦苦尋找技術解決方案今天勉強找了個;
   原理:javascript加密PHP解密; 徹底依賴openssl;

    一. openssl 是幹嗎的
         它集成了衆多密碼算法及實用工具
         
         rsa加密流程;(今天只講衆多加密方式中的一種)
           1. 在當前文件夾下生成一個包含 "公鑰""私鑰" 兩部份內容的文本文件; 命名test.key
           [root@iZ28pw7sv4qZ openssl]#openssl genrsa -out test.key 1024
           
           2.將這個文本文件中的「公鑰」提取出來: 命名test_pub.key
           [root@iZ28pw7sv4qZ openssl]#openssl rsa -in test.key -pubout -out test_pub.key

           3.建立一個hello.txt的文本文件,而後利用此前生成的公鑰加密文件;
           [root@iZ28pw7sv4qZ openssl]#echo "1234561122" > ./hello.txt
           [root@iZ28pw7sv4qZ openssl]#openssl rsautl -encrypt -in hello.txt -inkey test_pub.key -pubin -out hello.en.txt 

           4.解密文件
            [root@iZ28pw7sv4qZ openssl]#openssl rsautl -decrypt -in hello.en.txt -inkey test.key -out hello.de.txt
           
            5.解析後的結果
            [root@iZ28pw7sv4qZ openssl]# cat hello.de.txt 

     二. jsencrypt.js這個文件定義了一個JSEncrypt方法  能夠去git搜一下 對於此篇文檔就不展開太多
           使用步驟
             var res = new JSEncrypt  
            res.setPublicKey('---這裏就填寫test_pub.key文件中的字符串內容---'); //設置公有key
            var temp = res.encrypt("123456789羅源縣中華失聯飛機安撫拉斯加 大是的發生兩地");//利用剛設好的key 對明文進行加密;
            var data = encodeURI(temp).replace(/\+/g, '%2B'),  //+號的處理:  加密完成後就能夠ajax傳送了  

下載地址: http://files.cnblogs.com/files/sixiong/openssl.zip
相關文章
相關標籤/搜索