Home > Web Front-end > JS Tutorial > Share an example of a problem based on crypto-js encryption

Share an example of a problem based on crypto-js encryption

零下一度
Release: 2017-05-16 09:58:00
Original
2645 people have browsed it

Recently, I used PHP to build an HTML5 website for online streaming of movies/TV series. I used PHP’s curl for real-time crawling. I encountered a problem with JS encryption, so Google solved it, and I came up with this article. Summarize.

一波Advertising

Personal homepage: www.linganmin.cn
Film station address: www.ifilm.ltd

Let’s talk about js based on crypto-js Encryption

CryptoJS is an encryption class library written purely in javascript. Its GitHub warehouse address is github.com/brix/crypt...

We need it when using it QuoteThis js file, for the convenience I quoted the link on the CDN

The code

<script src="cdn.bootcss.com/crypto-js/3.1.9/crypto-js.js"></script>
<script>
var data = "en2JprK0nMyYgbd6dQO0O0OO0O0O" // 需要加密的字符串
var key_base="contentWindowHig"; // 加密秘钥的基值
var iv_base="contentDocuments"; // 加密所需iv基值
/**
 * 定义加密函数
 * @param  {[type]} a [形参,需要加密的值]
 * @return {[type]}   [加密后的值]
 */
 var get=function(a){
     var key_hash=CryptoJS.MD5(key_base); 
     var key=CryptoJS.enc.Utf8.parse(key_hash);
     var iv=CryptoJS.enc.Utf8.parse(iv_base);
     var res=CryptoJS.AES.encrypt(a,key,{iv:iv,mode:CryptoJS.mode.CBC,padding:CryptoJS.pad.ZeroPadding});
     return res.toString()
 }
    console.log(get(data)) // tPYJv39iEbdFD/UqNejyvkLG8ATdifyfE+BDeld2jWk=
</script>
Copy after login

Let’s talk about the same encryption and decryption of PHP

For The elegance of the article, php will use the same variable naming as js

Up code

// 定义变量
$data = "en2JprK0nMyYgbd6dQO0O0OO0O0O";
$key_base = "contentWindowHig";
$iv_base = "contentDocuments";
// 加密前处理
$key = md5(&#39;contentWindowHig&#39;);
$iv = $iv_base;
// 加密
$cryptText = mcrypt_encrypt(MCRYPT_RIJNDAEL_128, $key, $data, MCRYPT_MODE_CBC, $iv);
$res = base64_encode($cryptText);
// 解密
$cryptText = base64_decode($res); 
$decode = mcrypt_decrypt(MCRYPT_RIJNDAEL_128, $key, $cryptText, MCRYPT_MODE_CBC, $iv);
Copy after login

[Related recommendations]

1. Special Recommended : "php Programmer Toolbox" V0.1 version download

2. Free js online video tutorial

3. php.cn Dugu Jiujian (3) - JavaScript video tutorial

The above is the detailed content of Share an example of a problem based on crypto-js encryption. For more information, please follow other related articles on the PHP Chinese website!

Related labels:
source:php.cn
Statement of this Website
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
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template