crypto-js的加解密使用方法
- rsa 公私钥加解密
- aes 随机字符加解密(mode:)
- 也可以参考[JavaScript前端和Java后端的AES加密和解密](/web-chuanfa/p/)
- 扫码体验 虚拟电话小程序
rsa 公私钥加解密
/* * 加密(需要先加载/aes.min.js文件)
RASkey=‘XXXXXXXXXXX’//这个是公钥 后端生成 或者前端自己用方法生成 前端只能纯公钥
function Encrypt(){
var encrypt=new JSEncrypt();
encrypt.setPrivateKey(RASkey);
let data =encrypt.encrypt(‘123456789ABCDE’)
return data;
}
aes 随机字符加解密(mode:)
/** 写法有很多 这里是博主目前自己用的第一个 真实有效
* 加密(需要先加载lib/aes/aes.min.js文件)
* key也可以使用随机数 方法如下
* 随机16位密钥
function getAesKey(len) {
len = len || 16;
var $chars = '12345678ABCDEFGH';//可以自己随便定义
var maxPos = $chars.length;
var keyStr = '';
for(let i = 0; i < len; i++) {
keyStr += $chars.charAt(Math.floor(Math.random() * maxPos));
}
return keyStr;
}
*/
案例1 ---start
function encrypt(word){
var key = CryptoJS.enc.Utf8.parse("abcdefgabcdefg12");
var srcs = CryptoJS.enc.Utf8.parse(word);
var encrypted = CryptoJS.AES.encrypt(srcs, key, {mode:CryptoJS.mode.ECB,padding: CryptoJS.pad.Pkcs7});
return encrypted.toString();
}
/**
* 解密 (后端若是返回乱码了 他需要base.encode的在返回 前端也可以直接使用 无需base64.decode)
*/
function decrypt(word){
var key = CryptoJS.enc.Utf8.parse("abcdefgabcdefg12");
var decrypt = CryptoJS.AES.decrypt(word, key, {mode:CryptoJS.mode.ECB,padding: CryptoJS.pad.Pkcs7});
return CryptoJS.enc.Utf8.stringify(decrypt).toString();
}
案例1----end
案例2-------start
const CryptoJS = require('crypto-js'); //引用AES源码js
const key = CryptoJS.enc.Utf8.parse("1234123412ABCDEF"); //十六位十六进制数作为密钥
const iv = CryptoJS.enc.Utf8.parse('ABCDEF1234123412'); //十六位十六进制数作为密钥偏移量
//解密方法
function Decrypt(word) {
let encryptedHexStr = CryptoJS.enc.Hex.parse(word);
let srcs = CryptoJS.enc.Base64.stringify(encryptedHexStr);
let decrypt = CryptoJS.AES.decrypt(srcs, key, { iv: iv, mode: CryptoJS.mode.CBC, padding: CryptoJS.pad.Pkcs7 });
let decryptedStr = decrypt.toString(CryptoJS.enc.Utf8);
return decryptedStr.toString();
}
//加密方法
function Encrypt(word) {
let srcs = CryptoJS.enc.Utf8.parse(word);
let encrypted = CryptoJS.AES.encrypt(srcs, key, { iv: iv, mode: CryptoJS.mode.CBC, padding: CryptoJS.pad.Pkcs7 });
return encrypted.ciphertext.toString().toUpperCase();
}
export default {
Decrypt ,
Encrypt
}
案例2----end
案例3------start
* AES加密
*/
const encryptAes = (word,key) => {
//console.log(CryptoJS)
const keys = CryptoJS.enc.Utf8.parse(key)
const ivs = CryptoJS.enc.Utf8.parse('tstlafljatwea')
let encryptedWord = CryptoJS.enc.Utf8.parse(word)
var encrypted = CryptoJS.AES.encrypt(encryptedWord, keys, { iv: ivs,mode:CryptoJS.mode.CBC, padding: CryptoJS.pad.Pkcs7});
return encrypted.toString()
}
/**
* AES解密
*/
const decryptAes = (encrypted,key) => {
const keys = CryptoJS.enc.Utf8.parse(key)
const ivs = CryptoJS.enc.Utf8.parse('tstlafljatwea')
var decrypted = CryptoJS.AES.decrypt(encrypted, keys, { iv: ivs,mode:CryptoJS.mode.CBC, padding: CryptoJS.pad.Pkcs7});
//console.log(decrypted)
//console.log(decrypted.toString(CryptoJS.enc.Utf8))
return decrypted
案例3-----end
以下是压缩后的文件
!function(t,n){"object"==typeof exports?=exports=n():"function"==typeof define&&?define([],n):=n()}(this,function(){var t=t||function(t,n){var i=||function(){function t(){}return function(n){var i;return =n,i=new t,=null,i}}(),e={},r=={},o==function(){return{extend:function(t){var n=i(this);return t&&(t),("init")&&!==||(=function(){n.$(this,arguments)}),=n,n.$super=this,n},create:function(){var t=();return (t,arguments),t},init:function(){},mixIn:function(t){for(var n in t)(n)&&(this[n]=t[n]);("toString")&&(=)},clone:function(){return (this)}}}(),s==({init:function(t,i){t==t||[],i!=n?=i:=4*},toString:function(t){return(t||c).stringify(this)},concat:function(t){var n=,i=,e=,r=;if((),e%4)for(var o=0;o<r;o++){var s=i[o>>>2]>>>24-o%4*8&255;n[e+o>>>2]|=s<<24-(e+o)%4*8}else for(var o=0;o<r;o+=4)n[e+o>>>2]=i[o>>>2];return +=r,this},clamp:function(){var n=,i=;n[i>>>2]&=4294967295<<32-i%4*8,=(i/4)},clone:function(){var t=(this);return =(0),t},random:function(n){for(var i,e=[],r=function(n){var n=n,i=987654321,e=4294967295;return function(){i=36969*(65535&i)+(i>>16)&e,n=18e3*(65535&n)+(n>>16)&e;var r=(i<<16)+n&e;return r/=4294967296,r+=.5,r*(()>.5?1:-1)}},o=0;o<n;o+=4){var a=r(4294967296*(i||()));i=987654071*a(),(4294967296*a()|0)}return new (e,n)}}),a=={},c=={stringify:function(t){for(var n=,i=,e=[],r=0;r<i;r++){var o=n[r>>>2]>>>24-r%4*8&255;((o>>>4).toString(16)),((15&o).toString(16))}return ("")},parse:function(t){for(var n=,i=[],e=0;e<n;e+=2)i[e>>>3]|=parseInt((e,2),16)<<24-e%8*4;return new (i,n/2)}},u=a.Latin1={stringify:function(t){for(var n=,i=,e=[],r=0;r<i;r++){var o=n[r>>>2]>>>24-r%4*8&255;((o))}return ("")},parse:function(t){for(var n=,i=[],e=0;e<n;e++)i[e>>>2]|=(255&(e))<<24-e%4*8;return new (i,n)}},f=a.Utf8={stringify:function(t){try{return decodeURIComponent(escape((t)))}catch(t){throw new Error("Malformed UTF-8 data")}},parse:function(t){return (unescape(encodeURIComponent(t)))}},h==({reset:function(){this._data=new ,this._nDataBytes=0},_append:function(t){"string"==typeof t&&(t=(t)),this._data.concat(t),this._nDataBytes+=},_process:function(n){var i=this._data,e=,r=,o=,a=4*o,c=r/a;c=n?(c):((0|c)-this._minBufferSize,0);var u=c*o,f=(4*u,r);if(u){for(var h=0;h<u;h+=o)this._doProcessBlock(e,h);var p=(0,u);-=f}return new (p,f)},clone:function(){var t=(this);return t._data=this._data.clone(),t},_minBufferSize:0}),p=(=({cfg:(),init:function(t){=(t),()},reset:function(){(this),this._doReset()},update:function(t){return this._append(t),this._process(),this},finalize:function(t){t&&this._append(t);var n=this._doFinalize();return n},blockSize:16,_createHelper:function(t){return function(n,i){return new (i).finalize(n)}},_createHmacHelper:function(t){return function(n,i){return new (t,i).finalize(n)}}}),={});return e}(Math);return t});
//# sourceMappingURL=
!function(e,t,i){"object"==typeof exports?=exports=t(require("./"),require("./"),require("./")):"function"==typeof define&&?define(["./","./","./"],t):t()}(this,function(e){return function(){var t=e,i=,r=,n=,o=,a=o.MD5,c==({cfg:({keySize:4,hasher:a,iterations:1}),init:function(e){=(e)},compute:function(e,t){for(var i=,r=(),o=(),a=,c=,f=;<c;){s&&(s);var s=(e).finalize(t);();for(var u=1;u<f;u++)s=(s),();(s)}return =4*c,o}});=function(e,t,i){return (i).compute(e,t)}}(),});
//# sourceMappingURL=
!function(r,e){"object"==typeof exports?=exports=e(require("./")):"function"==typeof define&&?define(["./"],e):e()}(this,function(r){return function(){function e(r,e,t){for(var n=[],i=0,o=0;o<e;o++)if(o%4){var f=t[(o-1)]<<o%4*2,c=t[(o)]>>>6-o%4*2;n[i>>>2]|=(f|c)<<24-i%4*8,i++}return (n,i)}var t=r,n=,a=,i=;i.Base64={stringify:function(r){var e=,t=,n=this._map;();for(var a=[],i=0;i<t;i+=3)for(var o=e[i>>>2]>>>24-i%4*8&255,f=e[i+1>>>2]>>>24-(i+1)%4*8&255,c=e[i+2>>>2]>>>24-(i+2)%4*8&255,s=o<<16|f<<8|c,h=0;h<4&&i+.75*h<t;h++)((s>>>6*(3-h)&63));var p=(64);if(p)for(;%4;)(p);return ("")},parse:function(r){var t=,n=this._map,a=this._reverseMap;if(!a){a=this._reverseMap=[];for(var i=0;i<;i++)a[(i)]=i}var o=(64);if(o){var f=(o);f!==-1&&(t=f)}return e(r,t,a)},_map:"ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/="}}(),.Base64});
//# sourceMappingURL=
!function(e,t,r){"object"==typeof exports?=exports=t(require("./"),require("./")):"function"==typeof define&&?define(["./","./"],t):t()}(this,function(e){||function(t){var r=e,i=,n=,c=,o=,s=,a=(s.Utf8,s.Base64),f=,p=,d==({cfg:(),createEncryptor:function(e,t){return (this._ENC_XFORM_MODE,e,t)},createDecryptor:function(e,t){return (this._DEC_XFORM_MODE,e,t)},init:function(e,t,r){=(r),this._xformMode=e,this._key=t,()},reset:function(){(this),this._doReset()},process:function(e){return this._append(e),this._process()},finalize:function(e){e&&this._append(e);var t=this._doFinalize();return t},keySize:4,ivSize:4,_ENC_XFORM_MODE:1,_DEC_XFORM_MODE:2,_createHelper:function(){function e(e){return"string"==typeof e?B:x}return function(t){return{encrypt:function(r,i,n){return e(i).encrypt(t,r,i,n)},decrypt:function(r,i,n){return e(i).decrypt(t,r,i,n)}}}}()}),h=(=({_doFinalize:function(){var e=this._process(!0);return e},blockSize:1}),={}),u==({createEncryptor:function(e,t){return (e,t)},createDecryptor:function(e,t){return (e,t)},init:function(e,t){this._cipher=e,this._iv=t}}),l==function(){function e(e,r,i){var n=this._iv;if(n){var c=n;this._iv=t}else var c=this._prevBlock;for(var o=0;o<i;o++)e[r+o]^=c[o]}var r=();return =({processBlock:function(t,r){var i=this._cipher,n=;(this,t,r,n),(t,r),this._prevBlock=(r,r+n)}}),=({processBlock:function(t,r){var i=this._cipher,n=,c=(r,r+n);(t,r),(this,t,r,n),this._prevBlock=c}}),r}(),_=={},v=_.Pkcs7={pad:function(e,t){for(var r=4*t,i=%r,n=i<<24|i<<16|i<<8|i,o=[],s=0;s<i;s+=4)(n);var a=(o,i);(a)},unpad:function(e){var t=255&[-1>>>2];-=t}},y=(=({cfg:({mode:l,padding:v}),reset:function(){(this);var e=,t=,r=;if(this._xformMode==this._ENC_XFORM_MODE)var i=;else{var i=;this._minBufferSize=1}this._mode&&this._mode.__creator==i?this._mode.init(this,t&&):(this._mode=(r,this,t&&),this._mode.__creator=i)},_doProcessBlock:function(e,t){this._mode.processBlock(e,t)},_doFinalize:function(){var e=;if(this._xformMode==this._ENC_XFORM_MODE){(this._data,);var t=this._process(!0)}else{var t=this._process(!0);(t)}return t},blockSize:4}),=({init:function(e){(e)},toString:function(e){return(e||).stringify(this)}})),m=={},k=={stringify:function(e){var t=,r=;if(r)var i=([1398893684,1701076831]).concat(r).concat(t);else var i=t;return (a)},parse:function(e){var t=(e),r=;if(1398893684==r[0]&&1701076831==r[1]){var i=((2,4));(0,4),-=16}return ({ciphertext:t,salt:i})}},x==({cfg:({format:k}),encrypt:function(e,t,r,i){i=(i);var n=(r,i),c=(t),o=;return ({ciphertext:c,key:r,iv:,algorithm:e,mode:,padding:,blockSize:,formatter:})},decrypt:function(e,t,r,i){i=(i),t=this._parse(t,);var n=(r,i).finalize();return n},_parse:function(e,t){return"string"==typeof e?(e,this):e}}),g=={},S=={execute:function(e,t,r,i){i||(i=(8));var n=({keySize:t+r}).compute(e,i),o=((t),4*r);return =4*t,({key:n,iv:o,salt:i})}},B==({cfg:({kdf:S}),encrypt:function(e,t,r,i){i=(i);var n=(r,,);=;var c=(this,e,t,,i);return (n),c},decrypt:function(e,t,r,i){i=(i),t=this._parse(t,);var n=(r,,,);=;var c=(this,e,t,,i);return c}})}()});
//# sourceMappingURL=
!function(e,i){"object"==typeof exports?=exports=i(require("./")):"function"==typeof define&&?define(["./"],i):i()}(this,function(e){!function(){var i=e,t=,n=,s=,r=s.Utf8,o=;=({init:function(e,i){e=this._hasher=new ,"string"==typeof i&&(i=(i));var t=,n=4*t;>n&&(i=(i)),();for(var s=this._oKey=(),o=this._iKey=(),a=,f=,c=0;c<t;c++)a[c]^=1549556828,f[c]^=909522486;==n,()},reset:function(){var e=this._hasher;(),(this._iKey)},update:function(e){return this._hasher.update(e),this},finalize:function(e){var i=this._hasher,t=(e);();var n=(this._oKey.clone().concat(t));return n}})}()});
//# sourceMappingURL=
!function(e,o,r){"object"==typeof exports?=exports=o(require("./"),require("./")):"function"==typeof define&&?define(["./","./"],o):o()}(this,function(e){return =function(){var o=();return =({processBlock:function(e,o){this._cipher.encryptBlock(e,o)}}),=({processBlock:function(e,o){this._cipher.decryptBlock(e,o)}}),o}(),});
//# sourceMappingURL=
!function(e,r,i){"object"==typeof exports?=exports=r(require("./"),require("./")):"function"==typeof define&&?define(["./","./"],r):r()}(this,function(e){return .Pkcs7});
//# sourceMappingURL=
!function(e,r,i){"object"==typeof exports?=exports=r(require("./"),require("./"),require("./"),require("./"),require("./")):"function"==typeof define&&?define(["./","./","./","./","./"],r):r()}(this,function(e){return function(){var r=e,i=,n=,o=,t=[],c=[],s=[],f=[],a=[],d=[],u=[],v=[],h=[],y=[];!function(){for(var e=[],r=0;r<256;r++)r<128?e[r]=r<<1:e[r]=r<<1^283;for(var i=0,n=0,r=0;r<256;r++){var o=n^n<<1^n<<2^n<<3^n<<4;o=o>>>8^255&o^99,t[i]=o,c[o]=i;var p=e[i],l=e[p],_=e[l],k=257*e[o]^16843008*o;s[i]=k<<24|k>>>8,f[i]=k<<16|k>>>16,a[i]=k<<8|k>>>24,d[i]=k;var k=16843009*_^65537*l^257*p^16843008*i;u[o]=k<<24|k>>>8,v[o]=k<<16|k>>>16,h[o]=k<<8|k>>>24,y[o]=k,i?(i=p^e[e[e[_^p]]],n^=e[e[n]]):i=n=1}}();var p=[0,1,2,4,8,16,32,64,128,27,54],l==({_doReset:function(){if(!this._nRounds||this._keyPriorReset!==this._key){for(var e=this._keyPriorReset=this._key,r=,i=/4,n=this._nRounds=i+6,o=4*(n+1),c=this._keySchedule=[],s=0;s<o;s++)if(s<i)c[s]=r[s];else{var f=c[s-1];s%i?i>6&&s%i==4&&(f=t[f>>>24]<<24|t[f>>>16&255]<<16|t[f>>>8&255]<<8|t[255&f]):(f=f<<8|f>>>24,f=t[f>>>24]<<24|t[f>>>16&255]<<16|t[f>>>8&255]<<8|t[255&f],f^=p[s/i|0]<<24),c[s]=c[s-i]^f}for(var a=this._invKeySchedule=[],d=0;d<o;d++){var s=o-d;if(d%4)var f=c[s];else var f=c[s-4];d<4||s<=4?a[d]=f:a[d]=u[t[f>>>24]]^v[t[f>>>16&255]]^h[t[f>>>8&255]]^y[t[255&f]]}}},encryptBlock:function(e,r){this._doCryptBlock(e,r,this._keySchedule,s,f,a,d,t)},decryptBlock:function(e,r){var i=e[r+1];e[r+1]=e[r+3],e[r+3]=i,this._doCryptBlock(e,r,this._invKeySchedule,u,v,h,y,c);var i=e[r+1];e[r+1]=e[r+3],e[r+3]=i},_doCryptBlock:function(e,r,i,n,o,t,c,s){for(var f=this._nRounds,a=e[r]^i[0],d=e[r+1]^i[1],u=e[r+2]^i[2],v=e[r+3]^i[3],h=4,y=1;y<f;y++){var p=n[a>>>24]^o[d>>>16&255]^t[u>>>8&255]^c[255&v]^i[h++],l=n[d>>>24]^o[u>>>16&255]^t[v>>>8&255]^c[255&a]^i[h++],_=n[u>>>24]^o[v>>>16&255]^t[a>>>8&255]^c[255&d]^i[h++],k=n[v>>>24]^o[a>>>16&255]^t[d>>>8&255]^c[255&u]^i[h++];a=p,d=l,u=_,v=k}var p=(s[a>>>24]<<24|s[d>>>16&255]<<16|s[u>>>8&255]<<8|s[255&v])^i[h++],l=(s[d>>>24]<<24|s[u>>>16&255]<<16|s[v>>>8&255]<<8|s[255&a])^i[h++],_=(s[u>>>24]<<24|s[v>>>16&255]<<16|s[a>>>8&255]<<8|s[255&d])^i[h++],k=(s[v>>>24]<<24|s[a>>>16&255]<<16|s[d>>>8&255]<<8|s[255&u])^i[h++];e[r]=p,e[r+1]=l,e[r+2]=_,e[r+3]=k},keySize:8});=n._createHelper(l)}(),});
//# sourceMappingURL=
!function(e,n){"object"==typeof exports?=exports=n(require("./")):"function"==typeof define&&?define(["./"],n):n()}(this,function(e){return .Utf8});
//# sourceMappingURL=