参考地址:vue DES加密(超详细)_u014678583的博客-CSDN博客_des vue
在与后台传输数据过程中,我们要避免采用明文传输密码相关的数据
后台对于密码的处理暂时采用的是des加密,为了保持与原来数据的一致性,也采用这种方式
按照参考地址的方式实现了以后,发现加密的数据与后台发过来的不一致,
又参考了地址
前端vue项目中cryptoJS AES加密_天山小黎的博客-CSDN博客
修改了填充方式,后来干脆修改了加密方式,但是仍然是不一致的
通过查找在线的des加密工具,来检验
参考地址:在线DES加密解密 - 加密解密
通过检验发现,是我的与加密的以后不符合
然后在检索改在线工具的加密的js
http://www.txttool.com/api/des/encrypt_des.js
观察以后发现,我觉得可能是我加密的数据发生了变化,直接传递数据,发现与后台加密后的一致了
存在的问题是参考的例子中对原始数据转化为了JSON格式,但是后台并没有采用这种方式,为了维持一致性去掉
而且后台采用的是固定的key值,因为暂时不需要用到生成random 的函数来生成key值
http://www.txttool.com/api/des/encrypt_des.js 源码
function encrypt(){let text=document.getElementById('area_text').value;if(text){let key=CryptoJS.enc.Utf8.parse(document.getElementById('input_key').value);try{let encrypted=CryptoJS.DES.encrypt(text,key,buildConfig());let encode=document.getElementById('select_output_encode').value;let cipherText=encrypted.toString();if(encode==='HEX'){cipherText=CryptoJS.enc.Hex.stringify(CryptoJS.enc.Base64.parse(cipherText));} document.getElementById('area_cipher_text').value=cipherText;}catch(e){alert(e)}}} function decrypt(){let text=document.getElementById('area_cipher_text').value;if(text){let encode=document.getElementById('select_output_encode').value;let cipherText;if(encode==='Base64'){cipherText=CryptoJS.enc.Base64.parse(text)}else if(encode==='HEX'){cipherText=CryptoJS.enc.Hex.parse(text)} let key=CryptoJS.enc.Utf8.parse(document.getElementById('input_key').value);try{let decrypted=CryptoJS.DES.decrypt({ciphertext:cipherText},key,buildConfig());document.getElementById('area_text').value=decrypted.toString(CryptoJS.enc.Utf8);}catch(e){alert(e)}}} function buildConfig(){let mode=document.getElementById('select_mode').value;let pad=document.getElementById('select_pad').value;let conf={};switch(mode){case 'CBC':conf.mode=CryptoJS.mode.CBC;break;case 'CFB':conf.mode=CryptoJS.mode.CFB;break;case 'CTR':conf.mode=CryptoJS.mode.CTR;break;case 'OFB':conf.mode=CryptoJS.mode.OFB;break;case 'ECB':conf.mode=CryptoJS.mode.ECB;break;} switch(pad){case 'Pkcs7':conf.padding=CryptoJS.pad.Pkcs7;break;case 'Iso97971':conf.padding=CryptoJS.pad.Iso97971;break;case 'AnsiX923':conf.padding=CryptoJS.pad.AnsiX923;break;case 'Iso10126':conf.padding=CryptoJS.pad.Iso10126;break;case 'ZeroPadding':conf.padding=CryptoJS.pad.ZeroPadding;break;case 'NoPadding':conf.padding=CryptoJS.pad.NoPadding;break;} conf.iv=CryptoJS.enc.Utf8.parse(document.getElementById('input_iv').value);return conf} function cleanup(){document.getElementById('area_text').value='';document.getElementById('area_cipher_text').value='';document.getElementById('input_key').value='';document.getElementById('input_iv').value='';}
解密的时候又参考了原来的地址,发现解密不出来,参考工具发现密文编码应该是base64,修改后可以解析出来原来的密码了