search
HomeWeb Front-endJS TutorialBase64 implements encryption and decryption functions
Base64 implements encryption and decryption functionsApr 19, 2018 pm 03:51 PM
base64Functionencrypt and decode

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:


  <title></title>
  <meta>
  <script></script>
  <script></script>


<input>
<input>
<br>
<input>
<br>
加密后:<input>
<br>
<input>
<br>
<br>
<hr>
<input>
<br>
加密后:<input>
<br>
<input>
<br>
<br>
<input>
<br>
<textarea></textarea>

<script>
varpath=document.getElementById("path").value;
functionapp(info){
  $("#txt").val($("#txt").val()+&#39;\n&#39;+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==&#39;undefined&#39;||times==null||times==""){
    num=1;
  }else{
    varvt=times+"";
    num=parseInt(vt);
  }
  if(typeofmingwen==&#39;undefined&#39;||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==&#39;undefined&#39;||times==null||times==""){
    num=1;
  }else{
    varvt=times+"";
    num=parseInt(vt);
  }
  if(typeofmi==&#39;undefined&#39;||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>
Is encryption and decryption in the background the same as in the frontend?

Let’s test it:

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
Judging from the results, jquery.base64.js encryption and decryption are the same as java's base64 encryption and decryption. <p style="text-align: left;"></p> I believe you have mastered the method after reading the case in this article. For more exciting information, please pay attention to other related articles on the php Chinese website! <p></p>Recommended reading: <p></p><p>jQuery to create page mask layer effect<a href="http://www.php.cn/js-tutorial-393449.html" target="_blank"></a><br></p><p>How to use keyboard events in jquery<a href="http://www.php.cn/js-tutorial-393438.html" target="_blank"></a><br></p>

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!

Statement
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
使用JavaScript实现自动登录功能使用JavaScript实现自动登录功能Jun 15, 2023 pm 11:52 PM

随着互联网的发展,人们越来越依赖网络,大部分时间都在使用各种各样的网站和应用程序,这也使得我们需要记住很多账号和密码。为了方便用户的使用,很多网站提供了自动登录功能,让用户免除频繁输入账号和密码的烦恼。本文将介绍使用JavaScript实现自动登录功能的方法。一、登录流程分析在开始实现自动登录功能之前,我们需要了解整个登录流程。一般情况下,一个网站的登录流程

如何在 Windows 11 上安全禁用 Windows Modules Installer Worker如何在 Windows 11 上安全禁用 Windows Modules Installer WorkerApr 13, 2023 pm 03:43 PM

无论您使用的是旧计算机还是需要您的 PC 同时运行许多任务,您都可能希望禁用 Windows 模块安装程序工作程序。原因是 Windows 模块安装程序工作人员对您的磁盘、CPU 和内存施加了很高的负载。您可能会使用最好的软件来修复高 CPU 使用率,但一些报告显示它甚至可能会占用 100% 的 CPU 使用率。虽然它可以帮助您维护一个更安全和可靠的系统,但它会付出代价。因此,您可以决定保留或禁用它以避免性能问题。在本文中,我们将详细探讨什么是 Windows 模块安装程序工作人员以及如何启用或

如何使用PHP实现天气预报功能如何使用PHP实现天气预报功能Jun 27, 2023 pm 05:54 PM

PHP作为一款流行的后端编程语言,在Web开发领域广受欢迎。天气预报功能是一种常见的Web应用场景,基于PHP实现天气预报功能相对简单易懂。本文将介绍如何使用PHP实现天气预报功能。一、获取天气数据API要实现天气预报功能,首先需要获取天气数据。我们可以使用第三方天气API来获取实时、准确的天气数据。目前,国内主流的天气API供应商包括免费的“心知天气”和收

linux base64加密解密怎么实现linux base64加密解密怎么实现May 14, 2023 am 11:58 AM

1、给文件file进行base64编码,并打印到标准输出[root@pps~]#base64filec25haWx3YXJyaW9yCg==也可以这样:[root@pps~]#catfile|base64c25haWx3YXJyaW9yCg==2、从标准输入读取文件内容,base64编码并打印到标准输出[root@pps~]#base64snailwarriorc25haWx3YXJyaW9yCg==3、对字符串"snailwarrior"编码,并打印到标准输出[root@p

Win11加密功能怎么添加到右键菜单? Win11加密解密右键快捷添加方法Win11加密功能怎么添加到右键菜单? Win11加密解密右键快捷添加方法Jan 07, 2024 am 08:45 AM

这篇文章是本站给大家分享的在不使用第三方加解密工具情况下对文件进行加解密方法,Win11右键菜单添加加密解密方法教程,由于需要修改注册表,大家一定要做好备份再进行操作。1、首先,按键盘上的【Win+R】组合键,打开运行,然后输入【regedit】命令,按【确定或回车】可以打开注册表编辑器;2、用户账户控制窗口,你要允许此应用对你的设备进行更改吗?点击【是】;3、注册表编辑器窗口,依次展开到以下路径:HKEY_CURRENT_USER\Software\Microsoft\Windows\Curr

未来功能抢先用 Safari 技术预览 173 版本释出未来功能抢先用 Safari 技术预览 173 版本释出Jul 02, 2023 pm 01:37 PM

Apple今日释出了Safari技术预览173版本,涵盖部分可能于Safari17推出的功能。该版本适用于macOSSonoma测试版以及macOSVentura系统,有兴趣的用户可于官方网页下载。Safari技术预览173版于设定中新增了功能标志区块,取代原先开发菜单的实验功能。该区块可让开发者快速地搜索特定功能,并以不同形式将「稳定」、「可供测试」、「预览」或「开发人员」等状态标示出来。重新设计的开发菜单可以帮助创作者更容易找到关键工具,以便建立网页、网页应用程序、其他应用程序中的网页内容、

使用Yii框架中间件加密和解密敏感数据使用Yii框架中间件加密和解密敏感数据Jul 28, 2023 pm 07:12 PM

使用Yii框架中间件加密和解密敏感数据引言:在现代的互联网应用中,隐私和数据安全是非常重要的问题。为了确保用户的敏感数据不被未经授权的访问者获取,我们需要对这些数据进行加密。Yii框架为我们提供了一种简单且有效的方法来实现加密和解密敏感数据的功能。在本文中,我们将介绍如何使用Yii框架的中间件来实现这一目标。Yii框架简介Yii框架是一个高性能的PHP框架,

Google Colab将很快使用Codey进行AI编码Google Colab将很快使用Codey进行AI编码Jun 09, 2023 am 10:43 AM

GoogleColab是一个自2017年以来一直在促进Python编程的平台,它将利用Google的高级代码模型Codey引入AI编码功能。Codey基于PaLM2模型构建,对来自外部来源的大型高质量代码数据集进行了精心微调,以提高其在编码任务方面的性能。Colab即将推出的功能包括代码补全、自然语言到代码生成以及代码辅助聊天机器人。最初的重点将放在代码生成上,该功能旨在使用户能够生成更大的代码块并从注释或提示编写整个函数。这旨在减少编写重复代码的需求,允许用户专注于编程和数据科学的更复杂的方面

See all articles

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

AI Hentai Generator

AI Hentai Generator

Generate AI Hentai for free.

Hot Article

R.E.P.O. Energy Crystals Explained and What They Do (Yellow Crystal)
2 weeks agoBy尊渡假赌尊渡假赌尊渡假赌
Repo: How To Revive Teammates
1 months agoBy尊渡假赌尊渡假赌尊渡假赌
Hello Kitty Island Adventure: How To Get Giant Seeds
4 weeks agoBy尊渡假赌尊渡假赌尊渡假赌

Hot Tools

mPDF

mPDF

mPDF is a PHP library that can generate PDF files from UTF-8 encoded HTML. The original author, Ian Back, wrote mPDF to output PDF files "on the fly" from his website and handle different languages. It is slower than original scripts like HTML2FPDF and produces larger files when using Unicode fonts, but supports CSS styles etc. and has a lot of enhancements. Supports almost all languages, including RTL (Arabic and Hebrew) and CJK (Chinese, Japanese and Korean). Supports nested block-level elements (such as P, DIV),

SublimeText3 Linux new version

SublimeText3 Linux new version

SublimeText3 Linux latest version

Notepad++7.3.1

Notepad++7.3.1

Easy-to-use and free code editor

PhpStorm Mac version

PhpStorm Mac version

The latest (2018.2.1) professional PHP integrated development tool

Dreamweaver CS6

Dreamweaver CS6

Visual web development tools