为情书加密的BASE64的java类

最近自己写了个用BASE64编码“加密”的小java类,觉得有点意思,发出来一下,希望大家不要见笑

/*author: hujinpu

*用于编码BASE64t和解码BASE64 
*由于用了Scanner类,所以要求JDK1.5或更高版本 
*/
 
import  java.io.PrintWriter; 
import  java.io.FileInputStream; 
import  java.io.IOException; 
import  java.io.UnsupportedEncodingException; 
import  java.io.FileNotFoundException; 
import  java.util.Scanner; 
import  sun.misc.BASE64Decoder; 
import  sun.misc.BASE64Encoder; 
  
public   class  BASE64  

    
// 将s进行BASE64编码 
    public static String getBASE64EncoderStr(String s) throws UnsupportedEncodingException 
    

        
if (null == s) 
            
return null
        
else 
            
return new BASE64Encoder().encode(s.getBytes("GB2312")); 
    }
 
    
// 将s进行BASE64解码 
    public static byte[] getBASE64DecoderBt(String s) throws IOException 
    

        
if (null == s) 
            
return null
        
else 
        

            BASE64Decoder decoder 
= new BASE64Decoder(); 
            
return decoder.decodeBuffer(s); 
        }
 
  
    }
 
    
// 将指定文件用BASE64编码 
    public static void changeToBASE64(String filePath) throws IOException 
    

        Scanner sc 
= new Scanner(new FileInputStream(filePath)).useDelimiter(" "); 
        PrintWriter pw 
= new PrintWriter(filePath + "_BASE64_"); 
        
while (sc.hasNext()) 
        

            pw.println(getBASE64EncoderStr(sc.next())); 
        }
 
        sc.close(); 
        pw.close(); 
    }
 
    
// 将指定文件从BASE64解码 
    public static void recoverFromBASE64(String filePath) throws IOException, FileNotFoundException 
    

        Scanner sc 
= new Scanner(new FileInputStream(filePath)).useDelimiter(" "); 
        PrintWriter out 
= new PrintWriter(filePath.substring(0, filePath.indexOf("_BASE64_"))); 
        
while (sc.hasNext()) 
        

            out.println(
new String(getBASE64DecoderBt(sc.next()), "GB2312")); 
        }
 
        sc.close(); 
        out.close(); 
    }
 
    
// main方法 
    public static void main(String[] args) 
    

        
try 
        

            
if (args[0].indexOf("_BASE64_"> 0
            

                recoverFromBASE64(args[
0]); 
            }
 
            
else 
            

                changeToBASE64(args[
0]); 
            }
 
        }
 
        
catch (IOException e) 
        

            System.out.println(
"Catch some IOException!"); 
        }
 
    }
 
}

 

 下面是批处理文件
rem   author:hujinpu
@echo   off  
set  CLASSPATH = 你放BASE64 . java的目录 ; %CLASSPATH% 
java BASE64 
%1  
del   %1
只要把任意文件往BASE64.bat一拖就好了,呵呵。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值