问题描述
对URL参数加密之后再重定向跳转的时候就报错了!报错信息SecurityHelper类packagecom.huarun;importjava.security.*;importjavax.crypto.*;importjavax.crypto.spec.*;publicclassSecurityHelper{privatefinalstaticintITERATIONS=20;publicstaticStringencrypt(Stringkey,StringplainText)throwsException{try{byte[]salt=newbyte[8];MessageDigestmd=MessageDigest.getInstance("MD5");md.update(key.getBytes());byte[]digest=md.digest();for(inti=0;i<8;i++){salt[i]=digest[i];}PBEKeySpecpbeKeySpec=newPBEKeySpec(key.toCharArray());SecretKeyFactorykeyFactory=SecretKeyFactory.getInstance("PBEWithMD5AndDES");SecretKeyskey=keyFactory.generateSecret(pbeKeySpec);PBEParameterSpecparamSpec=newPBEParameterSpec(salt,ITERATIONS);Ciphercipher=Cipher.getInstance("PBEWithMD5AndDES");cipher.init(Cipher.ENCRYPT_MODE,skey,paramSpec);byte[]cipherText=cipher.doFinal(plainText.getBytes());StringsaltString=newString(Base64.encode(salt));StringciphertextString=newString(Base64.encode(cipherText));returnsaltString+ciphertextString;}catch(Exceptione){thrownewException("EncryptTextError:"+e.getMessage(),e);}}publicstaticStringdecrypt(Stringkey,StringencryptTxt)throwsException{intsaltLength=12;try{Stringsalt=encryptTxt.substring(0,saltLength);Stringciphertext=encryptTxt.substring(saltLength,encryptTxt.length());byte[]saltarray=Base64.decode(salt.getBytes());byte[]ciphertextArray=Base64.decode(ciphertext.getBytes());PBEKeySpeckeySpec=newPBEKeySpec(key.toCharArray());SecretKeyFactorykeyFactory=SecretKeyFactory.getInstance("PBEWithMD5AndDES");SecretKeyskey=keyFactory.generateSecret(keySpec);PBEParameterSpecparamSpec=newPBEParameterSpec(saltarray,ITERATIONS);Ciphercipher=Cipher.getInstance("PBEWithMD5AndDES");cipher.init(Cipher.DECRYPT_MODE,skey,paramSpec);byte[]plaintextArray=cipher.doFinal(ciphertextArray);returnnewString(plaintextArray);}catch(Exceptione){thrownewException(e);}}}
Base64类packagecom.huarun;importjava.io.*;importjavax.mail.internet.MimeUtility;publicclassBase64{publicstaticbyte[]encode(byte[]b)throwsException{ByteArrayOutputStreambaos=null;OutputStreamb64os=null;try{baos=newByteArrayOutputStream();b64os=MimeUtility.encode(baos,"base64");b64os.write(b);b64os.close();returnbaos.toByteArray();}catch(Exceptione){thrownewException(e);}finally{try{if(baos!=null){baos.close();baos=null;}}catch(Exceptione){}try{if(b64os!=null){b64os.close();b64os=null;}}catch(Exceptione){}}}publicstaticbyte[]decode(byte[]b)throwsException{ByteArrayInputStreambais=null;InputStreamb64is=null;try{bais=newByteArrayInputStream(b);b64is=MimeUtility.decode(bais,"base64");byte[]tmp=newbyte[b.length];intn=b64is.read(tmp);byte[]res=newbyte[n];System.arraycopy(tmp,0,res,0,n);returnres;}catch(Exceptione){thrownewException(e);}finally{try{if(bais!=null){bais.close();bais=null;}}catch(Exceptione){}try{if(b64is!=null){b64is.close();b64is=null;}}catch(Exceptione){}}}}
browseVideo.jsp页面<%@pagelanguage="java"import="java.util.*"pageEncoding="utf-8"%><%@pageimport="com.neusoft.seas.core.session.CSessionManager,com.neusoft.seas.core.session.CSession"%><%@pageimport="huarun.SecurityHelper"%><%@pageimport="java.net.URLEncoder"%><html><head><title>浏览影像原文</title></head><body><%//影像系统的IP地址StringvideoUrl="http://10.241.94.31:30028/imsp/SunIASTokenApplyServlet.do";//StringvideoUrl="http://www.baidu.com";CSessionManagersessionMgr=CSessionManager.getSessionManager();CSessionseasSession=sessionMgr.getSession(request);//拼接的urlStringurl="";//个贷档案申请流水号StringapplyNumber=request.getParameter("applyNumber");//影像系统接入用户(固定)StringUID="admin";//影像系统接入用户密码(固定)StringPWD="111";//个贷系统应用ID(固定,等待个贷系统确认)StringAppID="DD";//当前档案登录用户IDStringUserID=seasSession.getUserId();//当前档案登录用户名称StringUserName=seasSession.getUserName();//(是不是登录用户的部门ID)StringOrgID="100001";//华润银行(信贷)StringOrgName="华润银行(信贷)";//BUSI_SERIAL_NO:个贷档案申请流水号;RIGHT:1101100(进入影像系统的人员权限;固定:查看、下载、打印)Stringinfo1="BUSI_SERIAL_NO:"+applyNumber+";OBJECT_NAME:SUN_CUST_DOC;QUERY_TIME:20140929;FILELEVEL:3;PERIOD:1;RIGHT:1101100";System.out.println("info1="+info1);StringnewUserName=URLEncoder.encode(UserName,"utf-8");StringnewOrgName=URLEncoder.encode(OrgName,"utf-8");//调用影像系统的加密包进行加密StringnewInfo1=URLEncoder.encode(SecurityHelper.encrypt("crbank",info1),"utf-8");url="?UID="+UID+"&PWD="+PWD+"&AppID="+AppID+"&UserID="+UserID+"&UserName="+newUserName+"&OrgID="+OrgID+"&OrgName="+newOrgName+"&info1="+newInfo1;//形成最终访问链接videoUrl+=url;System.out.println(videoUrl);//重定向到最终的链接地址response.sendRedirect(videoUrl);%></body></html>
解决方案
本帖最后由 czh520czh 于 2014-12-04 14:37:50 编辑