javascript Base64 加密解密方法
<p>引用:</p><div class="cnblogs_code">
<pre><script type="text/javascript" src="/public/js/cryptoJs/core-min.js"></script>
<script type="text/javascript" src="/public/js/cryptoJs/enc-base64-min.js"></script></pre>
</div>
<div class="cnblogs_code">
<pre><span style="color: rgba(0, 128, 0, 1)"> //</span><span style="color: rgba(0, 128, 0, 1)"> base64 加密</span>
<span style="color: rgba(0, 0, 255, 1)">function</span><span style="color: rgba(0, 0, 0, 1)"> base64_encode(val){
</span><span style="color: rgba(0, 0, 255, 1)">var</span> str=<span style="color: rgba(0, 0, 0, 1)">CryptoJS.enc.Utf8.parse(val);
</span><span style="color: rgba(0, 0, 255, 1)">var</span> base64=<span style="color: rgba(0, 0, 0, 1)">CryptoJS.enc.Base64.stringify(str);
</span><span style="color: rgba(0, 0, 255, 1)">return</span><span style="color: rgba(0, 0, 0, 1)"> base64
}
</span><span style="color: rgba(0, 128, 0, 1)">//</span><span style="color: rgba(0, 128, 0, 1)"> base64 解密</span>
<span style="color: rgba(0, 0, 255, 1)">function</span><span style="color: rgba(0, 0, 0, 1)"> base64_decode(val){
</span><span style="color: rgba(0, 0, 255, 1)">var</span> words=<span style="color: rgba(0, 0, 0, 1)"> CryptoJS.enc.Base64.parse(val);
</span><span style="color: rgba(0, 0, 255, 1)">var</span> deBase64 =<span style="color: rgba(0, 0, 0, 1)"> words.toString(CryptoJS.enc.Utf8)
</span><span style="color: rgba(0, 0, 255, 1)">return</span><span style="color: rgba(0, 0, 0, 1)"> deBase64
}<br></span></pre>
</div>
<div class="cnblogs_code">
<pre><span style="color: rgba(0, 0, 255, 1)">// 引用方式<br>var</span> baseContent = base64_encode(content) <span style="color: rgba(0, 128, 0, 1)">//</span><span style="color: rgba(0, 128, 0, 1)"> base64 加密</span></pre>
</div>
<p>vue 版本 :</p>
<div class="cnblogs_code">
<pre>import { Base64 } from 'js-base64';</pre>
</div>
<div class="cnblogs_code">
<pre> Base64.encode(str),<span style="color: rgba(0, 128, 0, 1)">//</span><span style="color: rgba(0, 128, 0, 1)"> 加密</span></pre>
</div>
<div class="cnblogs_code">
<pre>export const base64 = (input) =><span style="color: rgba(0, 0, 0, 1)"> {
input </span>+= ''<span style="color: rgba(0, 0, 0, 1)">;
</span><span style="color: rgba(0, 128, 0, 1)">//</span><span style="color: rgba(0, 128, 0, 1)"> base64加密开始</span>
const keyStr =
'ABCDEFGHIJKLMNOP' +
'QRSTUVWXYZabcdef' +
'ghijklmnopqrstuv' +
'wxyz0123456789+/' +
'='<span style="color: rgba(0, 0, 0, 1)">;
let output </span>= ''<span style="color: rgba(0, 0, 0, 1)">;
let chr1,
chr2,
chr3 </span>= ''<span style="color: rgba(0, 0, 0, 1)">;
let enc1,
enc2,
enc3,
enc4 </span>= ''<span style="color: rgba(0, 0, 0, 1)">;
let i </span>= 0<span style="color: rgba(0, 0, 0, 1)">;
</span><span style="color: rgba(0, 0, 255, 1)">do</span><span style="color: rgba(0, 0, 0, 1)"> {
chr1 </span>= input.charCodeAt(i++<span style="color: rgba(0, 0, 0, 1)">);
chr2 </span>= input.charCodeAt(i++<span style="color: rgba(0, 0, 0, 1)">);
chr3 </span>= input.charCodeAt(i++<span style="color: rgba(0, 0, 0, 1)">);
enc1 </span>= chr1 >> 2<span style="color: rgba(0, 0, 0, 1)">;
enc2 </span>= ((chr1 & 3) << 4) | (chr2 >> 4<span style="color: rgba(0, 0, 0, 1)">);
enc3 </span>= ((chr2 & 15) << 2) | (chr3 >> 6<span style="color: rgba(0, 0, 0, 1)">);
enc4 </span>= chr3 & 63<span style="color: rgba(0, 0, 0, 1)">;
</span><span style="color: rgba(0, 0, 255, 1)">if</span><span style="color: rgba(0, 0, 0, 1)"> (isNaN(chr2)) {
enc3 </span>= enc4 = 64<span style="color: rgba(0, 0, 0, 1)">;
} </span><span style="color: rgba(0, 0, 255, 1)">else</span> <span style="color: rgba(0, 0, 255, 1)">if</span><span style="color: rgba(0, 0, 0, 1)"> (isNaN(chr3)) {
enc4 </span>= 64<span style="color: rgba(0, 0, 0, 1)">;
}
output </span>=<span style="color: rgba(0, 0, 0, 1)">
output </span>+<span style="color: rgba(0, 0, 0, 1)">
keyStr.charAt(enc1) </span>+<span style="color: rgba(0, 0, 0, 1)">
keyStr.charAt(enc2) </span>+<span style="color: rgba(0, 0, 0, 1)">
keyStr.charAt(enc3) </span>+<span style="color: rgba(0, 0, 0, 1)">
keyStr.charAt(enc4);
chr1 </span>= chr2 = chr3 = ''<span style="color: rgba(0, 0, 0, 1)">;
enc1 </span>= enc2 = enc3 = enc4 = ''<span style="color: rgba(0, 0, 0, 1)">;
} </span><span style="color: rgba(0, 0, 255, 1)">while</span> (i <<span style="color: rgba(0, 0, 0, 1)"> input.length);
</span><span style="color: rgba(0, 0, 255, 1)">return</span><span style="color: rgba(0, 0, 0, 1)"> output;
};</span></pre>
</div>
<p> </p>
</div>
<div id="MySignature" role="contentinfo">
记录平时的一些小问题~
或转载一些小知识点
学习学习<br><br>
来源:https://www.cnblogs.com/arealy/p/14214942.html
頁:
[1]