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. toString();
}

可以将下面得代码放到一个js文件里,前端直接引用:
/*
CryptoJS v3.1.2
code.google.com/p/crypto-js
(c) 2009-2013 by Jeff Mott. All rights reserved.
code.google.com/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. init. apply( this, arguments) }); c. init. 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 this. init. prototype. extend( this) } },
q = n. WordArray = s. extend({
init: function ( a, c) { a = this. words = a || []; this. sigBytes = c != l ? c : 4 * 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[ f + t >>> 2] = m[ t >>> 2]; else 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; m < a; m += 4) c. push( 4294967296 * u. random() | 0); return new q. init( c, a) }
}), w = d. enc = {}, v = w. Hex = {
stringify: function ( a) { var c = a. words; a = a. sigBytes; for ( var m = [], f = 0; f < a; f++) { var t = c[ f >>> 2] >>> 24 - 8 * ( f % 4) & 255; m. push(( t >>> 4). toString( 16)); m. push(( t & 15). toString( 16)) } return m. join( "") }, parse: function ( a) {
for ( var c = a. length, m = [], f = 0; f < c; f += 2) m[ f >>> 3] |= parseInt( a. substr( f,
2), 16) << 24 - 4 * ( f % 8); return new q. init( m, c / 2)
}
}, b = w. Latin1 = { stringify: function ( a) { var c = a. words; a = a. sigBytes; for ( var m = [], f = 0; f < a; f++) m. push( String. fromCharCode( c[ f >>> 2] >>> 24 - 8 * ( f % 4) & 255)); return m. join( "") }, parse: function ( a) { for ( var c = a. length, m = [], f = 0; f < c; f++) m[ f >>> 2] |= ( a. charCodeAt( f) & 255) << 24 - 8 * ( f % 4); return new q. init( m, c) } }, x = w. Utf8 = { stringify: function ( a) { try { return decodeURIComponent( escape( b. stringify( a))) } catch ( c) { throw Error( "Malformed UTF-8 data"); } }, parse: function ( a) { return b. parse( unescape( encodeURIComponent( a))) } },
r = n. BufferedBlockAlgorithm = s. extend({
reset: function () { this. _data = new q. init; this. _nDataBytes = 0 }, _append: function ( a) { "string" == typeof a && ( a = x. parse( a)); this. _data. concat( a); this. _nDataBytes += a. sigBytes }, _process: function ( a) { var c = this. _data, m = c. words, f = c. sigBytes, t = this. blockSize, b = f / ( 4 * t), b = a ? u. ceil( b) : u. max(( b | 0) - this. _minBufferSize, 0); a = b * t; f = u. min( 4 * a, f); if ( a) { for ( var e = 0; e < a; e += t) this. _doProcessBlock( m, e); e = m. splice( 0, a); c. sigBytes -= f } return new q. init( e, f) }, clone: function () {
var a = s. clone. call( this);
a. _data = this. _data. clone(); return a
}, _minBufferSize: 0
}); n. Hasher = r. extend({
cfg: s. extend(), init: function ( a) { this. cfg = this. cfg. extend( a); this. reset() }, reset: function () { r. reset. call( this); this. _doReset() }, update: function ( a) { this. _append( a); this. _process(); return this }, finalize: function ( a) { a && this. _append( a); return this. _doFinalize() }, blockSize: 16, _createHelper: function ( a) { return function ( c, m) { return ( new a. init( m)). finalize( c) } }, _createHmacHelper: function ( a) {
return function ( c, m) {
return ( new e. HMAC. init( a,
m)). finalize( c)
}
}
}); var e = d. algo = {}; return d
}( Math);
( function () {
var u = CryptoJS, l = u. lib. WordArray; u. enc. Base64 = {
stringify: function ( d) { var n = d. words, l = d. sigBytes, s = this. _map; d. clamp(); d = []; for ( var q = 0; q < l; q += 3) for ( var w = ( n[ q >>> 2] >>> 24 - 8 * ( q % 4) & 255) << 16 | ( n[ q + 1 >>> 2] >>> 24 - 8 * (( q + 1) % 4) & 255) << 8 | n[ q + 2 >>> 2] >>> 24 - 8 * (( q + 2) % 4) & 255, v = 0; 4 > v && q + 0.75 * v < l; v++) d. push( s. charAt( w >>> 6 * ( 3 - v) & 63)); if ( n = s. charAt( 64)) for (; d. length % 4;) d. push( n); return d. join( "") }, parse: function ( d) {
var n = d. length, p = this. _map, s = p. charAt( 64); s && ( s = d. indexOf( s), - 1 != s && ( n = s)); for ( var s = [], q = 0, w = 0; w <
n; w++) if ( w % 4) { var v = p. indexOf( d. charAt( w - 1)) << 2 * ( w % 4), b = p. indexOf( d. charAt( w)) >>> 6 - 2 * ( w % 4); s[ q >>> 2] |= ( v | b) << 24 - 8 * ( q % 4); q++ } return l. create( s, q)
}, _map: "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/="
}
})();
( function ( u) {
function l( b, e, a, c, m, f, t) { b = b + ( e & a | ~ e & c) + m + t; return ( b << f | b >>> 32 - f) + e } function d( b, e, a, c, m, f, t) { b = b + ( e & c | a & ~ c) + m + t; return ( b << f | b >>> 32 - f) + e } function n( b, e, a, c</
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值