各位大侠:
     我想把JSP URL传递的参数进行Base64加密解密。
     例如 http://zhidao.baidu.com/index.jsp?id=4
     对4进行Base64加密。
     加密后
     http://zhidao.baidu.com/index.jsp?id=NA==
     然后在后台进行解密。
   
     请教如何在页面中加密这个URL。
     谢谢!

解决方案 »

  1.   

    <%
    String str=加密的数据
    %>js上这样取
    var s="<%=str%>";
    form.action="http://zhidao.baidu.com/index.jsp?id="+s
      

  2.   

    /**  
        
      *加密解密类  
      *  
      *  
      *  
      */  
      package com.abacus.common ;   
      import java.security. *;   
      import javax.crypto. *;   
      /**  
      *加密解密类  
        
      */  
      public class Eryptogram    
      {   
          private static String Algorithm ="DES";   
          //定义加密算法,可用DES,DESede,Blowfish   
           static boolean debug =false ;   
          /**  
          *构造子注解.  
          */  
          public Eryptogram ()   
          {   
           }   
          /**  
          *生成密钥  
          *@returnbyte[]返回生成的密钥  
          *@throwsexception扔出异常.  
          */  
          public static byte []getSecretKey ()throws Exception    
          {   
              KeyGenerator keygen =KeyGenerator.getInstance (Algorithm );   
              SecretKey deskey =keygen.generateKey ();   
              if (debug )System.out.println ("生成密钥:"+byte2hex (deskey.getEncoded ()));   
              return deskey.getEncoded ();   
           }   
          /**  
          *将指定的数据根据提供的密钥进行加密  
          *@paraminput需要加密的数据  
          *@paramkey密钥  
          *@returnbyte[]加密后的数据  
          *@throwsException  
          */  
          public static byte []encryptData (byte []input ,byte []key )throws Exception    
          {   
              SecretKey deskey =new javax.crypto.spec.SecretKeySpec (key ,Algorithm );   
              if (debug )   
              {   
                  System.out.println ("加密前的二进串:"+byte2hex (input ));   
                  System.out.println ("加密前的字符串:"+new String (input ));   
               }   
              Cipher c1 =Cipher.getInstance (Algorithm );   
              c1.init (Cipher.ENCRYPT_MODE ,deskey );   
              byte []cipherByte =c1.doFinal (input );   
              if (debug )System.out.println ("加密后的二进串:"+byte2hex (cipherByte ));   
              return cipherByte ;   
           }   
          /**  
          *将给定的已加密的数据通过指定的密钥进行解密  
          *@paraminput待解密的数据  
          *@paramkey密钥  
          *@returnbyte[]解密后的数据  
          *@throwsException  
          */  
          public static byte []decryptData (byte []input ,byte []key )throws Exception    
          {   
              SecretKey deskey =new javax.crypto.spec.SecretKeySpec (key ,Algorithm );   
              if (debug )System.out.println ("解密前的信息:"+byte2hex (input ));   
              Cipher c1 =Cipher.getInstance (Algorithm );   
              c1.init (Cipher.DECRYPT_MODE ,deskey );   
              byte []clearByte =c1.doFinal (input );   
              if (debug )   
              {   
                  System.out.println ("解密后的二进串:"+byte2hex (clearByte ));   
                  System.out.println ("解密后的字符串:"+(new String (clearByte )));   
               }   
              return clearByte ;   
           }   
          /**  
          *字节码转换成16进制字符串  
          *@parambyte[]b输入要转换的字节码  
          *@returnString返回转换后的16进制字符串  
          */  
          public static String byte2hex (byte []b )   
          {   
              String hs ="";   
              String stmp ="";   
              for (int n =0 ;n <b.length ;n ++)   
              {   
                  stmp =(java.lang.Integer.toHexString (b [n ]&0XFF ));   
                  if (stmp.length ()==1 )hs =hs +"0"+stmp ;   
                  else hs =hs +stmp ;   
                  if (n <b.length -1 )hs =hs +":";   
               }   
              return hs.toUpperCase ();   
           }   
          public static void main (String []args )   
          {   
              try    
              {   
                  debug =false ;   
                  Eryptogram etg =new Eryptogram ();   
                  byte []key =etg.getSecretKey ();   
                  System.out.println ("key="+key );   
                  String aa ="1234567";   
                  byte []data =aa.getBytes ();   
                  System.out.println (data );   
                  byte []en =etg.encryptData (data ,key );   
                  System.out.println ("encryptData="+new String (en ));   
                  byte []de =etg.decryptData (en ,key );   
                  System.out.println ("decryptData="+new String (de ));   
               }   
              catch (Exception e )   
              {   
                  e.printStackTrace ();   
               }   
           }   
      } 
      

  3.   

    请问怎么能把获得的输入的值,在JSP页面上用base64的加密方式加密呢?
      

  4.   

    2楼的,人家要base64编码,你发个des上来,文不对题啊。先给你一段javascript编码的(解码的也顺带送给你了,呵呵。)
    //Base64 の符号化と復号化
    var base64EncodeChars = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/";
    var base64DecodeChars = new Array(
        -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1,
        -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1,
        -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, 62, -1, -1, -1, 63,
        52, 53, 54, 55, 56, 57, 58, 59, 60, 61, -1, -1, -1, -1, -1, -1,
        -1,  0,  1,  2,  3,  4,  5,  6,  7,  8,  9, 10, 11, 12, 13, 14,
        15, 16, 17, 18, 19, 20, 21, 22, 23, 24, 25, -1, -1, -1, -1, -1,
        -1, 26, 27, 28, 29, 30, 31, 32, 33, 34, 35, 36, 37, 38, 39, 40,
        41, 42, 43, 44, 45, 46, 47, 48, 49, 50, 51, -1, -1, -1, -1, -1);function base64encode(str) {
        var out, i, len;
        var c1, c2, c3;    len = str.length;
        i = 0;
        out = "";
        while(i < len) {
        c1 = str.charCodeAt(i++) & 0xff;
        if(i == len)
        {
            out += base64EncodeChars.charAt(c1 >> 2);
            out += base64EncodeChars.charAt((c1 & 0x3) << 4);
            out += "==";
            break;
        }
        c2 = str.charCodeAt(i++);
        if(i == len)
        {
            out += base64EncodeChars.charAt(c1 >> 2);
            out += base64EncodeChars.charAt(((c1 & 0x3)<< 4) | ((c2 & 0xF0) >> 4));
            out += base64EncodeChars.charAt((c2 & 0xF) << 2);
            out += "=";
            break;
        }
        c3 = str.charCodeAt(i++);
        out += base64EncodeChars.charAt(c1 >> 2);
        out += base64EncodeChars.charAt(((c1 & 0x3)<< 4) | ((c2 & 0xF0) >> 4));
        out += base64EncodeChars.charAt(((c2 & 0xF) << 2) | ((c3 & 0xC0) >>6));
        out += base64EncodeChars.charAt(c3 & 0x3F);
        }
        return out;
    }function base64decode(str) {
        var c1, c2, c3, c4;
        var i, len, out;    len = str.length;
        i = 0;
        out = "";
        while(i < len) {
        /* c1 */
        do {
            c1 = base64DecodeChars[str.charCodeAt(i++) & 0xff];
        } while(i < len && c1 == -1);
        if(c1 == -1)
            break;    /* c2 */
        do {
            c2 = base64DecodeChars[str.charCodeAt(i++) & 0xff];
        } while(i < len && c2 == -1);
        if(c2 == -1)
            break;    out += String.fromCharCode((c1 << 2) | ((c2 & 0x30) >> 4));    /* c3 */
        do {
            c3 = str.charCodeAt(i++) & 0xff;
            if(c3 == 61)
            return out;
            c3 = base64DecodeChars[c3];
        } while(i < len && c3 == -1);
        if(c3 == -1)
            break;    out += String.fromCharCode(((c2 & 0XF) << 4) | ((c3 & 0x3C) >> 2));    /* c4 */
        do {
            c4 = str.charCodeAt(i++) & 0xff;
            if(c4 == 61)
            return out;
            c4 = base64DecodeChars[c4];
        } while(i < len && c4 == -1);
        if(c4 == -1)
            break;
        out += String.fromCharCode(((c3 & 0x03) << 6) | c4);
        }
        return out;
    }
    用的时候,在onsubmit的时候,把需要的value用一下上面的函数就ok。
    要注意,根据base64的规定,编码后的长度,mod 4 不等于0的话,要补等号("="),
    但是如果URL参数里面有了多余的等号容易出问题。上面的base64编码时,如果补了等号,你要自己去掉它(在java端再补就ok)
    java的Base64在org.apache.commons.codec.binary.Base64里面。
    import org.apache.commons.codec.binary.Base64;
    LZ自己去down apache的code包。
        /**
         * 暗号化処理.
         *
         * @param userName 暗号化される文字列
         * @return 暗号化した文字列
         */
        public static String encodeBase64(String userName) {        if (userName == null || userName.length() == 0) {
                return userName;
            }        // Base64で暗号化
            StringBuffer sb;
            try {
                sb = new StringBuffer(new String(Base64.encodeBase64(userName.getBytes("UTF-8"))));
                while (sb.charAt(sb.length() - 1) == '=') {
                    sb.deleteCharAt(sb.length() - 1);
                }
            } catch (UnsupportedEncodingException e) {
                throw new CwsApplicationException(e);
            }        return sb.toString();
        }    /**
         * 複合化処理.
         *
         * @param userName 複合化される文字列
         * @return 複合化した文字列
         */
        public static String decodeBase64(String userName) {        if (userName == null || userName.length() == 0) {
                return userName;
            }        // 4文字に満たない文字列に「=」を付加
            int k = userName.length() % 4;
            if (k > 0) {
                StringBuffer sb = new StringBuffer(userName);
                for (int i = 0; i < 4 - k; i++) {
                    sb.append("=");
                }
                userName = sb.toString();
            }        // Base64で複合化
            try {
                userName = new String(Base64.decodeBase64(userName.getBytes("UTF-8")), "UTF-8");
            } catch (UnsupportedEncodingException e) {
                throw new CwsApplicationException(e);
            }        return userName;
        }
    上面的程序都是我现在做的项目里面的,java端编码,javascript解码,经过验证没有问题。LZ要求的javascript编码,java解码,正好也帮俺测试一下,当然,单个函数都已经测过没有问题的。了。