為情書加密的BASE64的java類

來源:互聯網
上載者:User

最近自己寫了個用BASE64編碼“加密”的小java類,覺得有點意思,發出來一下,希望大家不要見笑

 

/**//*author: livahu
 *
 *用於編碼BASE64t和解碼BASE64
 *
 */
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:livahu
@echo off
set CLASSPATH=你放BASE64.java的目錄;%CLASSPATH%
java BASE64 %1
del %1

 

只要把任意檔案往BASE64.bat一拖就好了,呵呵。

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.