一、代码

1、php

//解密函数
function myDecode($txt, $key = 'YangYang')
{
$txt = urldecode($txt);
$chars = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789-=+";
$ch = $txt[0];
$nh = strpos($chars, $ch);
$mdKey = md5($key . $ch);
$mdKey = substr($mdKey, $nh % 8, $nh % 8 + 7);
$txt = substr($txt, 1);
$tmp = '';
$j = 0;
$k = 0;
for ($i = 0; $i < strlen($txt); $i++) {
$k = $k == strlen($mdKey) ? 0 : $k;
$j = strpos($chars, $txt[$i]) - $nh - ord($mdKey[$k++]);
while ($j < 0) $j += 64;
$tmp .= $chars[$j];
}
return base64_decode($tmp);
}

//加密函数
function myEncrypt($txt, $key = 'YangYang')
{
$chars = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789-=+";
$nh = rand(0, 64);
$ch = $chars[$nh];
$mdKey = md5($key . $ch);
$mdKey = substr($mdKey, $nh % 8, $nh % 8 + 7);
$txt = base64_encode($txt);

$tmp = '';
$j = 0;
$k = 0;
for ($i = 0; $i < strlen($txt); $i++) {
$k = $k == strlen($mdKey) ? 0 : $k;
$j = ($nh + strpos($chars, $txt[$i]) + ord($mdKey[$k++])) % 64;
$tmp .= $chars[$j];
}
return urlencode($ch . $tmp);
}

2、js

let Base64 = require('js-base64').Base64;
import md5 from 'js-md5'
var MyCode = {
encrypt(txt, key='YangYang') { //加密
var chars = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789-=+";
var nh = Math.floor(Math.random() * 65);
var ch = chars[nh];
var mdKey = md5(key + ch);
var mdKey = mdKey.substr(nh % 8, nh % 8 + 7);
txt = Base64.encode(txt);
var tmp = '',
j = 0,
k = 0;
for (var i = 0; i < txt.length; i++) {
k = k == mdKey.length ? 0 : k;
j = (nh + chars.indexOf(txt[i]) + mdKey[k++].charCodeAt(0)) % 64;
tmp += chars[j];
}
return encodeURI(ch + tmp);
},
decode(txt, key='YangYang') { //解密
txt = decodeURI(txt);
var chars = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789-=+";
var ch = txt[0];
var nh = chars.indexOf(ch);
var mdKey = md5(key + ch);
mdKey = mdKey.substr(nh % 8, nh % 8 + 7);
txt = txt.substr(1);
var tmp = '',
j = 0,
k = 0;
for (var i = 0; i < txt.length; i++) {
k = k == mdKey.length ? 0 : k;
j = chars.indexOf(txt[i]) - nh - mdKey[k++].charCodeAt(0);
while (j < 0) {
j += 64;
}
tmp += chars[j];
}
return Base64.decode(tmp);
}
}
export default MyCode