养生 装修 购物 美食 感冒 便秘 营销 加盟 小吃 火锅 管理 创业 搭配 减肥 培训 旅游

base64对密码进行加密和解密

时间:2024-10-22 18:42:36

网站敏感字段信息在传输过程中没有加密,可能导致数据被非法窃取。所以需要对密码,用户名进行前端加密传送到后端进行解密。

方法/步骤

1、先找到登录页面,然后再找到登录的方法。可以看到Service.asExcute('ISignOnByDWR','login',[{verifyCode:verifyCode,staffCode:username,password:password}]这里就是把前端的用户名密码传到后端的。不能进行密码明文传输,就要在数据交换之前进行加密。

base64对密码进行加密和解密

2、这个是jsp页面,要想使用base64,要先引入jquery.base64.js,这个要自己下载。

base64对密码进行加密和解密

3、引入之后,就要对用户名,密码进行加密。varpassword=$.base64.encode(document.getElementById("password").value);$.base64.encode这个就是在进行加密。

base64对密码进行加密和解密

4、这个是定义的base瀵鸦铙邮64加密和解密的方法。;(function($){varb64="A蚱澄堆别BCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/",a256='',r64=[256],r256=[256],i=0;varUTF8={encode:function(strUni){varstrUtf=strUni.replace(/[\u0080-\u07ff]/g,//U+0080-U+07FF=>2bytes110yyyyy,10zzzzzzfunction(c){varcc=c.charCodeAt(0);returnString.fromCharCode(0xc0|cc>>6,0x80|cc&0x3f);}).replace(/[\u0800-\uffff]/g,//U+0800-U+FFFF=>3bytes1110xxxx,10yyyyyy,10zzzzzzfunction(c){varcc=c.charCodeAt(0);returnString.fromCharCode(0xe0|cc>>12,0x80|cc>>6&0x3F,0x80|cc&0x3f);});returnstrUtf;},decode:function(strUtf){varstrUni=strUtf.replace(/[\u00e0-\u00ef][\u0080-\u00bf][\u0080-\u00bf]/g,//3-bytecharsfunction(c){//(noteparenthesesforprecence)varcc=((c.charCodeAt(0)&0x0f)<<12)|((c.charCodeAt(1)&0x3f)<<6)|(c.charCodeAt(2)&0x3f);returnString.fromCharCode(cc);}).replace(/[\u00c0-\u00df][\u0080-\u00bf]/g,//2-bytecharsfunction(c){//(noteparenthesesforprecence)varcc=(c.charCodeAt(0)&0x1f)<<6|c.charCodeAt(1)&0x3f;returnString.fromCharCode(cc);});returnstrUni;}};while(i<256){varc=String.fromCharCode(i);a256+=c;r256[i]=i;r64[i]=b64.indexOf(c);++i;}functioncode(s,discard,alpha,beta,w1,w2){s=String(s);varbuffer=0,i=0,length=s.length,result='',bitsInBuffer=0;while(i<length){varc=s.charCodeAt(i);c=c<256?alpha[c]:-1;buffer=(buffer<<w1)+c;bitsInBuffer+=w1;while(bitsInBuffer>=w2){bitsInBuffer-=w2;vartmp=buffer>>bitsInBuffer;result+=beta.charAt(tmp);buffer^=tmp<<bitsInBuffer;}++i;}if(!discard&&bitsInBuffer>0)result+=beta.charAt(buffer<<(w2-bitsInBuffer));returnresult;}varPlugin=$.base64=function(dir,input,encode){returninput?Plugin[dir](input,encode):dir?null:this;};Plugin.btoa=Plugin.encode=function(plain,utf8encode){plain=Plugin.raw===false||Plugin.utf8encode||utf8encode?UTF8.encode(plain):plain;plain=code(plain,false,r256,b64,8,6);returnplain+'===='.slice((plain.length%4)||4);};Plugin.atob=Plugin.decode=function(coded,utf8decode){coded=String(coded).split('=');vari=coded.length;do{--i;coded[i]=code(coded[i],true,r64,a256,6,8);}while(i>0);coded=coded.join('');returnPlugin.raw===false||Plugin.utf8decode||utf8decode?UTF8.decode(coded):coded;};}(jQuery));

5、数据传输到后端也要进行解密。java后端进行解密。tringpass=decode(newpwd);decode就是解密的方法,需要自己定义方法。

base64对密码进行加密和解密

6、decod娣定撰钠e自定义的方法。//base64解密方法 publicstaticStringdecode烫喇霰嘴(Strings){ byte[]b=null; Stringresult=null; if(s!=null){ BASE64Decoderdecoder=newBASE64Decoder(); try{ b=decoder.decodeBuffer(s); result=newString(b,"utf-8"); }catch(Exceptione){ e.printStackTrace(); } } returnresult; }

base64对密码进行加密和解密

7、java端还需要导入一个包,importsun.misc.BASE64Decoder;不导入这个包,解密无法使用。

base64对密码进行加密和解密

8、到这里加密和解密就结束了,该进行debug验证了。先看传入的用户名密码是否转码。可以看到,用户名密码都已经加密了。就这样完成了加密和解密。

base64对密码进行加密和解密

© 一点知识