CryptoJS遇到的小坑
时间:2016-01-06 14:07:25
收藏:1
阅读:9903
CryptoJs是前端加密用的,具体介绍:http://code.google.com/p/crypto-js/ 对应国内免费CDN 服务:http://www.bootcdn.cn/crypto-js/
我是工作上要爬虫电信的信息,要先登录呀,于是我查看了他的代码
1 <script type="text/javascript"> 2 $("#loginbtn").click(function() { 3 var t = $("#txtAccount").val(), 4 n = $("#hidUType").val(), 5 i = $("#txtCityNo").val(), 6 o = $("#txtShowPwd").attr("data-preval"), 7 u = $("#txtPassword").val(), 8 r = $("#txtCaptcha").val(), 9 f, 10 e; 11 if (t == "" || t == txtAccount_Default && n != "204" || t == txtAccount_Default_YWMob && n == "204") return $("#txtAccount").focus(), 12 showErrMsg("请输入用户名"), 13 !1; 14 if (n != "201") if (n == "202" || n == "203") { 15 if (i == "" || i == txtCityNo_Default) return $("#txtCityNo").focus(), 16 showErrMsg("请输入地市"), 17 !1; 18 if (i = $("#hidCityNo").val(), i == "") return $("#txtCityNo").focus(), 19 showErrMsg("请重新输入地市"), 20 !1 21 } else if (n == "204") { 22 if (!checkIsCellphone(t)) return showErrMsg("请输入正确的手机号码"), 23 !1 24 } else if ($("#txtAccount").blur(), n = $("#hidUType").val(), n == "") return showErrMsg("请重新输入登录信息"), 25 !1; 26 if (u == "") return $("#txtShowPwd").focus(), 27 showErrMsg("请输入" + o), 28 !1; 29 if (f = $("#hidRandomFlag").val(), e = ValidatePwd(n, t, u, f), e.Code != "0") return $("#txtShowPwd").focus(), 30 showErrMsg(‘密码过于简单,请通过“<a style="color: #ff8200" onclick="wjmmShow();">忘记密码<\/a>”进行重置。‘), 31 !1; 32 if (r == txtCaptcha_Default && (r = "", $("#txtCaptcha").val(r)), $("#liCaptcha").is(":visible") && r == "") return $("#txtCaptcha").focus(), 33 showErrMsg("请输入" + txtCaptcha_Default), 34 !1; 35 reqInfo_Cookie_W(); 36 $("#txtPassword").valAesEncryptSet(); 37 $("#loginForm").submit() 38 }); 39 40 41 $.fn.valAesEncryptSet = function() { 42 var i = this.val(), 43 n, 44 t; 45 try { 46 n = this.aesDecrypt(i); 47 n != "" && (t = this.aesEncrypt(n), t != i && (n = "")) 48 } catch(r) { 49 n = "" 50 } 51 return n == "" && (t = this.aesEncrypt(i)), 52 this.val(t), 53 this.val() 54 } 55 56 $.fn.aesEncrypt = function(n) { 57 var t = CryptoJS.MD5("login.189.cn"), 58 i = CryptoJS.enc.Utf8.parse(t), 59 r = CryptoJS.enc.Utf8.parse("1234567812345678"), //31323334353637383132333435363738 60 u = CryptoJS.AES.encrypt(n, i, { 61 iv: r 62 }); 63 return u + "" 64 }; 65 66 67 $.fn.aesDecrypt = function(n) { 68 var t = CryptoJS.MD5("login.189.cn"), 69 i = CryptoJS.enc.Utf8.parse(t), 70 r = CryptoJS.enc.Utf8.parse("1234567812345678"); 71 return CryptoJS.AES.decrypt(n, i, { 72 iv: r 73 }).toString(CryptoJS.enc.Utf8) 74 }; 75 </script >
代码看的少看不太懂呀,于是我就自己试了一下其实就是把代码复制自己断点运行看看。
<script type="text/javascript"> $.fn.valAesEncryptSet = function() { var i = "123", n, t; var hash = CryptoJS.MD5("Message"); var encrypted = CryptoJS.AES.encrypt("Message", "Secret Passphrase"); var decrypted = CryptoJS.AES.decrypt(encrypted, "Secret Passphrase"); try { n = this.aesDecrypt(i); n != "" && (t = this.aesEncrypt(n), t != i && (n = "")) var c = n != ""; alert( c && (t = this.aesEncrypt(n), t != i && (n = ""))); } catch(r) { n = "" } alert(n == "" && (t = this.aesEncrypt(i)), this.val(t), this.val()); return n == "" && (t = this.aesEncrypt(i)), this.val(t), this.val() } //加密 $.fn.aesEncrypt = function(n) { //U2FsdGVkX1+vmOX1CZKawT1bU4PKfohd/2EJZKD25l0= var t = CryptoJS.MD5("login.189.cn"), //33b21adee1b8620a7ba81aea1a80c724 i = CryptoJS.enc.Utf8.parse(t), //3333623231616465653162383632306137626138316165613161383063373234 r = CryptoJS.enc.Utf8.parse("1234567812345678"), //31323334353637383132333435363738 u = CryptoJS.AES.encrypt(n, i, { iv: r }); //U2FsdGVkX1+vmOX1CZKawT1bU4PKfohd/2EJZKD25l0= return u + "" }; //E+kuJYooy4s85LxPhwh5kw== //解密 $.fn.aesDecrypt = function(n) { var t = CryptoJS.MD5("login.189.cn"), //33b21adee1b8620a7ba81aea1a80c724 (传入的字符串(还是原来字符串)进行md5)// 6c6f67696e2e3138392e636e(utf8转码获取十六进制编码) i = CryptoJS.enc.Utf8.parse(t), //3333623231616465653162383632306137626138316165613161383063373234 (utf8转码获取十六进制编码byte[]) r = CryptoJS.enc.Utf8.parse("1234567812345678"); //31323334353637383132333435363738 return CryptoJS.AES.decrypt(n, i, { iv: r }).toString(CryptoJS.enc.Utf8) }; </script> <!-- <script src="http://crypto-js.googlecode.com/svn/tags/3.1.2/build/rollups/md5.js"></script> --> <!-- <script src="http://crypto-js.googlecode.com/svn/tags/3.1.2/build/rollups/aes.js"></script> --> <script src="//cdn.bootcss.com/crypto-js/3.1.2/components/core.js"></script> <script src="//cdn.bootcss.com/crypto-js/3.1.2/rollups/aes.js"></script> <!-- <script src="//cdn.bootcss.com/crypto-js/3.1.2/components/aes.js"></script> --> <script src="//cdn.bootcss.com/crypto-js/3.1.2/components/md5.js"></script> <script src="//cdn.bootcss.com/crypto-js/3.1.2/components/enc-base64.js"></script>
第一个坑(引用js错误)
首先core一定要引用的,其次是注意引用js是否正确这里我把aes引用错了,(这两个js都是存在的)
第二个坑(如何查看编码后的字符串)
在你想要查看变量时候直接查看
最开始我是弹框看对应的值,但是这样看着不太方便,后来我在看他的代码发现竟然是这样获取的。
对于 加密的变量 直接在后面 +"" 即可查看到加密后字符串
字符串编码后(Utf8.parse)文字要用查看( toString(CryptoJS.enc.Utf8) )
CtyptoJS下载
评论(4)
mmmthgh
2016-07-27 14:36:21
你好 我最近也爬取电信的信息能否赐教下 是怎么登陆的? 谢谢!!!!
mmmthgh
2016-07-27 14:36:48
你好 我最近也爬取电信的信息能否赐教下 是怎么登陆的? 谢谢!!!!
opina
2016-10-17 15:09:27
你好 我最近也爬取电信的信息能否赐教下 是怎么登陆的? 谢谢!!!!
opina
2016-10-17 17:15:29
帅哥 求教啊
共4条