This time I will bring you base64 to implement encryption and decryption functions. What are the precautions for base64 to implement encryption and decryption functions? The following is a practical case, let’s take a look.
Regarding encryption, many people think of encodeURI and escape. This is useful for encrypting URLs, especially URLs with Chinese parameters. If you just want to do encryption and decryption, similar to Java's DES, jQuery on the Internet has jquery.base64.js. (For md5 encryption of js, you can use jquery.md5.js. If you are interested, you can find it and test it). Here is the test:<html> <head> <title></title> <meta http-equiv="Content-Type"content="text/html; charset=UTF-8"> <script language="javascript"src="jquery-1.7.1.js"></script> <script language="javascript"src="jquery.base64.js"></script> </head> <body> <input id="path"name="path"type="hidden"value="haha"></input> <input id="putcardno01"name="putcardno01"type="text"size="65"value=""></input> <br> <input onclick="subfunc();"class="btn1"value="提交加密" type="button"></input> <br> 加密后:<input id="putcardno02"name="putcardno02"type="text"size="65"value=""></input> <br> <input onclick="subfunc02();"class="btn1"value="提交解密" type="button"></input> <br> <br> <hr> <input onclick="subfunc03();"class="btn1"value="提交N次加密" type="button"></input> <br> 加密后:<input id="putcardno03"name="putcardno03"type="text"size="65"value=""></input> <br> <input onclick="subfunc04();"class="btn1"value="提交N次解密" type="button"></input> <br> <br> <input onclick="clearrr();"class="btn1"value="清除" type="button"></input> <br> <textarea id='txt'cols="75"rows="19"></textarea> </body> <script language="javascript"> varpath=document.getElementById("path").value; functionapp(info){ $("#txt").val($("#txt").val()+'\n'+info); } functionsubfunc(){ varput1=$.trim($("#putcardno01").val()); // var estxt=$.base64.encode(put1); //var estxt=$.base64.btoa(put1); varestxt=encodeBase64(put1); $("#putcardno02").val(estxt); app("加密后["+estxt+"]"); } functionsubfunc02(){ varput1=$.trim($("#putcardno02").val()); //var estxt=$.base64.decode(put1); //var estxt=$.base64.atob(put1); varestxt=decodeBase64(put1); app("解密后["+estxt+"]"); } ////////////////////////////////////////// varnumTimes=5; functionsubfunc03(){ varput1=$.trim($("#putcardno01").val()); // var estxt=$.base64.encode(put1); //var estxt=$.base64.btoa(put1); //estxt=$.base64.btoa(estxt); estxt=encodeBase64(put1,numTimes); $("#putcardno03").val(estxt); app(numTimes+"次加密后["+estxt+"]"); } functionsubfunc04(){ varput1=$.trim($("#putcardno03").val()); //var estxt=$.base64.decode(put1); //var estxt=$.base64.atob(put1); //estxt=$.base64.atob(estxt); estxt=decodeBase64(put1,numTimes); app(numTimes+"次解密后["+estxt+"]"); } functionclearrr(){ $("#putcardno02").val(""); $("#putcardno03").val(""); $("#putcardno04").val(""); $("#txt").val(""); } //加密方法。没有过滤首尾空格,即没有trim. //加密可以加密N次,对应解密N次就可以获取明文 functionencodeBase64(mingwen,times){ varcode=""; varnum=1; if(typeoftimes=='undefined'||times==null||times==""){ num=1; }else{ varvt=times+""; num=parseInt(vt); } if(typeofmingwen=='undefined'||mingwen==null||mingwen==""){ }else{ $.base64.utf8encode =true; code=mingwen; for(vari=0;i<num;i++){ code=$.base64.btoa(code); } } returncode; } //解密方法。没有过滤首尾空格,即没有trim //加密可以加密N次,对应解密N次就可以获取明文 functiondecodeBase64(mi,times){ varmingwen=""; varnum=1; if(typeoftimes=='undefined'||times==null||times==""){ num=1; }else{ varvt=times+""; num=parseInt(vt); } if(typeofmi=='undefined'||mi==null||mi==""){ }else{ $.base64.utf8encode =true; mingwen=mi; for(vari=0;i<num;i++){ mingwen=$.base64.atob(mingwen); } } returnmingwen; } /* 测试 输入 suolong2014version 加密后[c3VvbG9uZzIwMTR2ZXJzaW9u] 解密后[suolong2014version] 5次加密后[VjFod1QxWXlVblJUYTJoUVYwWmFhRnBYZEhOTk1WSlhWV3hPVG1KSVFscFZNalYzWVVaYU5tSkVSVDA9] 5次解密后[suolong2014version] */ </script>
packagecom.code; importsun.misc.BASE64Decoder; importsun.misc.BASE64Encoder; /** * * Base64加密--解密 * * @author lushuaiyin * */ publicclassBase64Util { /** * @param args */ publicstaticvoidmain(String[] args) { // TODO Auto-generated method stub String str="suolong2014version"; System.out.println("测试明文["+str+"]"); String basecode =Base64Util.encodeBase64(str); System.out.println("加密后["+basecode+"]"); if(basecode!=null){ String res =Base64Util.decodeBase64(basecode); System.out.println("解密后["+res+"]"); } ///////////////////////////////////////// System.out.println(""); System.out.println("N次加密测试--------"); String basecodeN=Base64Util.encodeBase64(str,2); String resN=Base64Util.decodeBase64(basecodeN,2); String basecodeN3=Base64Util.encodeBase64(str,5); String resN3=Base64Util.decodeBase64(basecodeN3,5); } //提供加密N次 publicstaticString encodeBase64(String mingwen,inttimes){ intnum=(times<=0)?1:times; String code=""; if(mingwen==null||mingwen.equals("")){ }else{ code=mingwen; for(inti=0;i<num;i++){ code=encodeBase64(code); } System.out.println("加密"+num+"次后["+code+"]"); } returncode; } //对应提供解密N次 publicstaticString decodeBase64(String mi,inttimes){ intnum=(times<=0)?1:times; String mingwen=""; if(mi==null||mi.equals("")){ }else{ mingwen=mi; for(inti=0;i<num;i++){ mingwen=decodeBase64(mingwen); } System.out.println("解密"+num+"次后["+mingwen+"]"); } returnmingwen; } /////////////////////////////////////////////////////////////////// publicstaticString encodeBase64(String mingwen){ String code=""; if(mingwen==null||mingwen.equals("")){ }else{ BASE64Encoder encoder =newBASE64Encoder(); try{ code=encoder.encode(mingwen.getBytes()); }catch(Exception e) { e.printStackTrace(); } // System.out.println("加密后["+code+"]"); } returncode; } publicstaticString decodeBase64(String mi){ String mingwen=""; if(mi==null||mi.equals("")){ }else{ BASE64Decoder decoder =newBASE64Decoder(); try{ byte[] by = decoder.decodeBuffer(mi); mingwen =newString(by); }catch(Exception e) { e.printStackTrace(); } // System.out.println("解密后["+mingwen+"]"); } returnmingwen; } } /* 打印: 测试明文[suolong2014version] 加密后[c3VvbG9uZzIwMTR2ZXJzaW9u] 解密后[suolong2014version] N次加密测试-------- 加密2次后[YzNWdmJHOXVaekl3TVRSMlpYSnphVzl1] 解密2次后[suolong2014version] 加密5次后[VjFod1QxWXlVblJUYTJoUVYwWmFhRnBYZEhOTk1WSlhWV3hPVG1KSVFscFZNalYzWVVaYU5tSkVS VDA9] 解密5次后[suolong2014version] */
jQuery to create page mask layer effect
How to use keyboard events in jquery
The above is the detailed content of Base64 implements encryption and decryption functions. For more information, please follow other related articles on the PHP Chinese website!