javascript实现DES加密(笔记)
场景:登录密码需要前端加密,后端同学采⽤des解密,所以前端需要des加密,需两者共同定义⼀个私钥key。
参考:⽹上搜了下 需要进⼊三个js
<script type="text/javascript" src="js/jquery.min.js" ></script>
<script type="text/javascript" src="js/tripledes.js" ></script>
<script type="text/javascript" src="js/mode-ecb.js" ></script>
但是最后觉得引⼊三个js不太好,同时我们项⽬⾥⾯已经引⼊了jquery,所以就决定⾃⼰整合下写到我们⾃⼰得⼯具库⾥,这样⽤得时候就直接引⽤。
将tripledes.js 和mode-dec.js合成⼀个js⽂件,引⼊
import {
CryptoJS }
from
'../../../utils/des'
js页⾯代码写上如下代码,当点击登录时需要调⽤此⽅法加密, message 就是这⾥得密码,key就是私钥
encryptByDES = (
message,
key)
=> {
let
keyHex =
CryptoJS.
enc.
Utf8.
parse(
key);
let
encrypted =
CryptoJS.
DES.
encrypt(
message,
keyHex, {
mode:
CryptoJS.
mode.
ECB,
padding:
CryptoJS.
pad.
Pkcs7
});
return
encrypted.
可以将下⾯得代码放到⼀个js⽂件⾥,前端直接引⽤:
/
*
CryptoJS v3.1.2
(c) 2009-2013 by Jeff Mott. All rights reserved. le/p/crypto-js/wiki/License
*/
var
CryptoJS =
CryptoJS ||
function (
u,
l) {
var
d = {},
n =
d.
lib = {},
p =
function () { },
s =
n.
Base = {
extend:
function (
a) {
p.
prototype =
this;
var
c =
new
p;
a &&
c.
mixIn(
a);
c.
hasOwnProperty(
"init") || (
c.
init =
function () {
c.
$super.
prototype =
c;
c.
$super =
this;
return
c },
create: function () {
var
a =
this.
extend();
a.
init.
apply(
a, arguments); return
a },
init:
function () { }, mixIn:
function (
a) {
for (
var
c
in
a)
a. hasOwnProperty( c) && (
this[
c] =
a[
c]);
a. hasOwnProperty( "toString") && ( this.
toString =
a.
toString) }, clone:
function () { return
js代码加密软件this.
init. prototype. extend(
this) } },
q =
n.
WordArray = s.
extend({
init:
function (
a,
c) {
a =
this.
a. length }, toString: function ( a) { return ( a || v). stringify( this) }, concat: function ( a) {
var
c =
this. words, m =
a. words, f = this. sigBytes;
a =
a. sigBytes; this. clamp(); if (
f %
4)
for (
var
t =
0;
t <
a;
t++)
c[
f +
t >>> 2] |= (
m[
t >>> 2] >>> 24 -
8 * (
t %
4) & 255) << 24 -
8 * ((
f +
t) % 4);
else
if (
65535 < m. length) for (
t =
0;
t <
a;
t +=
4)
c.
push.
apply(
c,
m);
this.
sigBytes += a;
return
this },
clamp: function () {
var
a =
this.
words,
c =
this. sigBytes;
a[
c >>>
2] &= 4294967295 << 32 -
8 * (
c %
4);
a.
length =
u.
ceil(
c /
4)
},
clone: function () {
var
a =
s.
clone.
call(
this);
a.
words = this.
words.
slice(
0);
return
a },
random: function (
a) {
for (
var
c = [],
m =
0;
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系QQ:729038198,我们将在24小时内删除。
发表评论