


About jQuery implementing base64 front-end encryption and decryption function
This article mainly introduces jQuery's implementation of base64 front-end encryption and decryption functions. It analyzes the implementation method of jquery.base64.js to implement front-end base64 encryption and decryption functions in the form of examples, and gives a comparison of operation examples of java's implementation of back-end base64 encryption and decryption. To verify the encryption effect, friends who need it can refer to it. I hope it can help everyone.
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).
The following 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"> var path=document.getElementById("path").value; function app(info){ $("#txt").val($("#txt").val()+'\n'+info); } function subfunc(){ var put1=$.trim($("#putcardno01").val()); // var estxt=$.base64.encode(put1); //var estxt=$.base64.btoa(put1); var estxt=encodeBase64(put1); $("#putcardno02").val(estxt); app("加密后["+estxt+"]"); } function subfunc02(){ var put1=$.trim($("#putcardno02").val()); //var estxt=$.base64.decode(put1); //var estxt=$.base64.atob(put1); var estxt=decodeBase64(put1); app("解密后["+estxt+"]"); } ////////////////////////////////////////// var numTimes=5; function subfunc03(){ var put1=$.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+"]"); } function subfunc04(){ var put1=$.trim($("#putcardno03").val()); //var estxt=$.base64.decode(put1); //var estxt=$.base64.atob(put1); //estxt=$.base64.atob(estxt); estxt=decodeBase64(put1,numTimes); app(numTimes+"次解密后["+estxt+"]"); } function clearrr(){ $("#putcardno02").val(""); $("#putcardno03").val(""); $("#putcardno04").val(""); $("#txt").val(""); } //加密方法。没有过滤首尾空格,即没有trim. //加密可以加密N次,对应解密N次就可以获取明文 function encodeBase64(mingwen,times){ var code=""; var num=1; if(typeof times=='undefined'||times==null||times==""){ num=1; }else{ var vt=times+""; num=parseInt(vt); } if(typeof mingwen=='undefined'||mingwen==null||mingwen==""){ }else{ $.base64.utf8encode = true; code=mingwen; for(var i=0;i<num;i++){ code=$.base64.btoa(code); } } return code; } //解密方法。没有过滤首尾空格,即没有trim //加密可以加密N次,对应解密N次就可以获取明文 function decodeBase64(mi,times){ var mingwen=""; var num=1; if(typeof times=='undefined'||times==null||times==""){ num=1; }else{ var vt=times+""; num=parseInt(vt); } if(typeof mi=='undefined'||mi==null||mi==""){ }else{ $.base64.utf8encode = true; mingwen=mi; for(var i=0;i<num;i++){ mingwen=$.base64.atob(mingwen); } } return mingwen; } /* 测试 输入 suolong2014version 加密后[c3VvbG9uZzIwMTR2ZXJzaW9u] 解密后[suolong2014version] 5次加密后[VjFod1QxWXlVblJUYTJoUVYwWmFhRnBYZEhOTk1WSlhWV3hPVG1KSVFscFZNalYzWVVaYU5tSkVSVDA9] 5次解密后[suolong2014version] */ </script>
Is the encryption and decryption in the background the same as in the front desk?
Let’s test it:
package com.code; import sun.misc.BASE64Decoder; import sun.misc.BASE64Encoder; /** * * Base64加密--解密 * * @author lushuaiyin * */ public class Base64Util { /** * @param args */ public static void main(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次 public static String encodeBase64(String mingwen,int times){ int num=(times<=0)?1:times; String code=""; if(mingwen==null||mingwen.equals("")){ }else{ code=mingwen; for(int i=0;i<num;i++){ code=encodeBase64(code); } System.out.println("加密"+num+"次后["+code+"]"); } return code; } //对应提供解密N次 public static String decodeBase64(String mi,int times){ int num=(times<=0)?1:times; String mingwen=""; if(mi==null||mi.equals("")){ }else{ mingwen=mi; for(int i=0;i<num;i++){ mingwen=decodeBase64(mingwen); } System.out.println("解密"+num+"次后["+mingwen+"]"); } return mingwen; } /////////////////////////////////////////////////////////////////// public static String encodeBase64(String mingwen){ String code=""; if(mingwen==null||mingwen.equals("")){ }else{ BASE64Encoder encoder = new BASE64Encoder(); try { code=encoder.encode(mingwen.getBytes()); } catch (Exception e) { e.printStackTrace(); } // System.out.println("加密后["+code+"]"); } return code; } public static String decodeBase64(String mi){ String mingwen=""; if(mi==null||mi.equals("")){ }else{ BASE64Decoder decoder = new BASE64Decoder(); try { byte[] by = decoder.decodeBuffer(mi); mingwen = new String(by); } catch (Exception e) { e.printStackTrace(); } // System.out.println("解密后["+mingwen+"]"); } return mingwen; } } /* 打印: 测试明文[suolong2014version] 加密后[c3VvbG9uZzIwMTR2ZXJzaW9u] 解密后[suolong2014version] N次加密测试-------- 加密2次后[YzNWdmJHOXVaekl3TVRSMlpYSnphVzl1] 解密2次后[suolong2014version] 加密5次后[VjFod1QxWXlVblJUYTJoUVYwWmFhRnBYZEhOTk1WSlhWV3hPVG1KSVFscFZNalYzWVVaYU5tSkVS VDA9] 解密5次后[suolong2014version] */
From the results, jquery.base64.js encryption and decryption are the same as java’s base64 encryption and decryption.
Related recommendations:
How does PHP use a custom key to encrypt and decrypt data?
Java encryption and decryption and Digital signature complete code example
php data encryption related introduction
The above is the detailed content of About jQuery implementing base64 front-end encryption and decryption function. For more information, please follow other related articles on the PHP Chinese website!

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

Video Face Swap
Swap faces in any video effortlessly with our completely free AI face swap tool!

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

Detailed explanation of jQuery reference method: Quick start guide jQuery is a popular JavaScript library that is widely used in website development. It simplifies JavaScript programming and provides developers with rich functions and features. This article will introduce jQuery's reference method in detail and provide specific code examples to help readers get started quickly. Introducing jQuery First, we need to introduce the jQuery library into the HTML file. It can be introduced through a CDN link or downloaded

How to use PUT request method in jQuery? In jQuery, the method of sending a PUT request is similar to sending other types of requests, but you need to pay attention to some details and parameter settings. PUT requests are typically used to update resources, such as updating data in a database or updating files on the server. The following is a specific code example using the PUT request method in jQuery. First, make sure you include the jQuery library file, then you can send a PUT request via: $.ajax({u

jQuery is a fast, small, feature-rich JavaScript library widely used in front-end development. Since its release in 2006, jQuery has become one of the tools of choice for many developers, but in practical applications, it also has some advantages and disadvantages. This article will deeply analyze the advantages and disadvantages of jQuery and illustrate it with specific code examples. Advantages: 1. Concise syntax jQuery's syntax design is concise and clear, which can greatly improve the readability and writing efficiency of the code. for example,

Title: jQuery Tips: Quickly modify the text of all a tags on the page In web development, we often need to modify and operate elements on the page. When using jQuery, sometimes you need to modify the text content of all a tags in the page at once, which can save time and energy. The following will introduce how to use jQuery to quickly modify the text of all a tags on the page, and give specific code examples. First, we need to introduce the jQuery library file and ensure that the following code is introduced into the page: <

How to remove the height attribute of an element with jQuery? In front-end development, we often encounter the need to manipulate the height attributes of elements. Sometimes, we may need to dynamically change the height of an element, and sometimes we need to remove the height attribute of an element. This article will introduce how to use jQuery to remove the height attribute of an element and provide specific code examples. Before using jQuery to operate the height attribute, we first need to understand the height attribute in CSS. The height attribute is used to set the height of an element

Title: Use jQuery to modify the text content of all a tags. jQuery is a popular JavaScript library that is widely used to handle DOM operations. In web development, we often encounter the need to modify the text content of the link tag (a tag) on the page. This article will explain how to use jQuery to achieve this goal, and provide specific code examples. First, we need to introduce the jQuery library into the page. Add the following code in the HTML file:

jQuery is a popular JavaScript library that is widely used to handle DOM manipulation and event handling in web pages. In jQuery, the eq() method is used to select elements at a specified index position. The specific usage and application scenarios are as follows. In jQuery, the eq() method selects the element at a specified index position. Index positions start counting from 0, i.e. the index of the first element is 0, the index of the second element is 1, and so on. The syntax of the eq() method is as follows: $("s

How to tell if a jQuery element has a specific attribute? When using jQuery to operate DOM elements, you often encounter situations where you need to determine whether an element has a specific attribute. In this case, we can easily implement this function with the help of the methods provided by jQuery. The following will introduce two commonly used methods to determine whether a jQuery element has specific attributes, and attach specific code examples. Method 1: Use the attr() method and typeof operator // to determine whether the element has a specific attribute
