开发中惯用的工具类:加密类、非法字符过滤类

开发中常用的工具类:加密类、非法字符过滤类

 下面的类用于字符。有4个方法。

1、将字符转化为GBK编码

2、将字符转化为GB2312编码

3、md5加密字符串

4、md5加密文件

 

[java:showcolumns] view plain copy
·········10········20········30········40········50········60········70········80········90········100·······110·······120·······130·······140·······150
  1. import  java.io.File;  
  2. import  java.io.FileInputStream;  
  3. import  java.io.IOException;  
  4. import  java.nio.MappedByteBuffer;  
  5. import  java.nio.channels.FileChannel.MapMode;  
  6. import  java.security.MessageDigest;  
  7. import  java.security.NoSuchAlgorithmException;  
  8.   
  9.   
  10. public   final   class  CodingTool  
  11. {  
  12.     /**  
  13.      *   
  14.      * {将字符转化为GBK编码}  
  15.      *   
  16.      * @param str  
  17.      * @return  
  18.      * @see: {参照的方法}  
  19.      * @author:{pilove310}  
  20.      */   
  21.     public   static  String convertToGBK(String str)  
  22.     {  
  23.   
  24.         try   
  25.         {  
  26.   
  27.             byte [] bytes = str.getBytes( "ISO-8859-1" );  
  28.   
  29.             return   new  String(bytes,  "GBK" );  
  30.   
  31.         }  
  32.   
  33.         catch  (Exception e)  
  34.         {  
  35.   
  36.             return  str;  
  37.   
  38.         }  
  39.   
  40.     }  
  41.   
  42.     /**  
  43.      *   
  44.      * {将字符转化为GB2312编码}  
  45.      *   
  46.      * @param str  
  47.      * @return  
  48.      * @see: {参照的方法}  
  49.      * @author:{pilove310}  
  50.      */   
  51.     public   static  String convertToGB2312(String str)  
  52.     {  
  53.   
  54.         try   
  55.         {  
  56.   
  57.             byte [] bytes = str.getBytes( "ISO-8859-1" );  
  58.   
  59.             return   new  String(bytes,  "gb2312" );  
  60.   
  61.         }  
  62.   
  63.         catch  (Exception e)  
  64.         {  
  65.   
  66.             return  str;  
  67.   
  68.         }  
  69.   
  70.     }  
  71.   
  72.     /**  
  73.      *   
  74.      * {md5加密字符串}  
  75.      *   
  76.      * @param input  
  77.      * @return  
  78.      * @see: {参照的方法}  
  79.      * @author:{pilove310}  
  80.      */   
  81.     public   static   final  String toMD5(String input)  
  82.     {  
  83.         byte [] inputByte = input.getBytes();  
  84.         StringBuffer buf = new  StringBuffer();  
  85.         MessageDigest md;//MessageDigest包含md5、SHA等算法   
  86.         try   
  87.         {  
  88.             //返回实现指定md5算法的 MessageDigest 对象。   
  89.             md = MessageDigest.getInstance("md5" );  
  90.               
  91.             md.update(inputByte);  
  92.               
  93.             byte [] digest = md.digest(); //通过执行诸如填充之类的最终操作完成哈希计算。   
  94.   
  95.             for  ( int  i =  0 ; i < digest.length; i++)  
  96.             {  
  97.                 int  val = (( int ) digest[i]) &  0xff ;  
  98.                 if  (val <  16 )  
  99.                 {  
  100.                     buf.append("0" );  
  101.                 }  
  102.                 buf.append(Integer.toHexString(val));  
  103.             }  
  104.         }  
  105.         catch  (NoSuchAlgorithmException e)  
  106.         {  
  107.   
  108.             e.printStackTrace();  
  109.         }  
  110.   
  111.         return  buf.toString();  
  112.     }  
  113.       
  114.     /**  
  115.      *   
  116.      * {md5加密文件的}  
  117.      *   
  118.      * @param file  
  119.      * @return  
  120.      * @throws NoSuchAlgorithmException  
  121.      * @throws IOException  
  122.      * @see: {参照的方法}  
  123.      * @author:{pilove310}  
  124.      */   
  125.     public   final   static  String md5(File file)  throws  NoSuchAlgorithmException,   
  126.     IOException{  
  127.        MessageDigest md=MessageDigest.getInstance("md5" );  
  128.        FileInputStream fin=new  FileInputStream(file);  
  129.        MappedByteBuffer mappedByte = fin.getChannel().map(MapMode.READ_ONLY,   
  130.     0 , file.length());  
  131.        md.update(mappedByte);  
  132.        StringBuffer buf=new  StringBuffer();  
  133.        byte [] digest = md.digest();  
  134.        for ( int  i= 0 ;i<digest.length;i++){  
  135.         int  val=(( int )digest[i]) &  0xff ;  
  136.         if (val <  16 ){  
  137.          buf.append("0" );  
  138.         }  
  139.         buf.append(Integer.toHexString(val));  
  140.        }  
  141.        fin.close();  
  142.        return  buf.toString();  
  143.     }  
  144.   
  145. }  

 

下面方法用于过滤sql注入和Xss攻击(方法过滤考虑的攻击方式有限,待完善)

 

[java] view plain copy
  1. /**  
  2.  *   
  3.  * <p>application name:{系统安全工具类}</p>  
  4.  * <p>application describing:{处理字符串,把sql注入、xss跨站等字段去除}</p>  
  5.  * <p>Copyright:Copyright  </p>  
  6.  * <p>company:neusoft</p>  
  7.  * <p>time:{时间,如2007.11.16}</p>  
  8.  * @author {pilove310}  
  9.  * @version {v1.0}  
  10.  */   
  11. public   final   class  SecTool  
  12. {  
  13. /**  
  14.  *   
  15.  * {去除字符串中的sql注入字段}  
  16.  *   
  17.  * @param str 传入一个String  
  18.  * @return  返回一个不包含sql注入字段的String  
  19.  * @see: {参 的方法}  
  20.  * @author:{pilove310}  
  21.  */   
  22.     public   static  String filterSQLInjection(String str)  
  23.     {  
  24.         String flt ="'|and|exec|insert|select|delete|update|count|*|%|chr|mid|master|truncate|char |declare|;|or|-  
  25.   
  26. |+|,|drop";  
  27.         String filter[] = flt.split("|" );  
  28.         for ( int  i= 0 ;i<filter.length ; i++)  
  29.         {  
  30.           str.replace(filter[i], "" );  
  31.         }  
  32.         return  str;  
  33.     }  
  34.     /**  
  35.      *   
  36.      * {去除字符串中的xss跨站字段}  
  37.      *   
  38.      * @param str 传入一个String  
  39.      * @return 返回一个不包含XSS跨站字段的String  
  40.      * @see: {参照的方法}  
  41.      * @author:{pilove310}  
  42.      */   
  43.     public   static  String filterXSS(String str)  
  44.     {  
  45.         String flt="<|>|script|&|%23|/n|/0|<scirpt|<scirpt>" ;  
  46.         String filter[] = flt.split("|" );  
  47.          
  48.           
  49.        //-------说明:两次循环处理是防止使用拼接进行,实现跨站代码传入-------   
  50.           
  51.         for ( int  i= 0 ;i<filter.length ; i++)  
  52.         {  
  53.           str.replace(filter[i], "" );  
  54.           System.out.println(filter[i]+"..." );  
  55.            
  56.         }  
  57. //        for(int i=0;i<filter.length ; i++)   
  58. //        {   
  59. //          str.replace(filter[i], "");   
  60. //        }   
  61.         return  str;  
  62.     }  

原文转自:http://blog.csdn.net/pilove310/article/details/4302130