Base64 Java class encrypted for Love Letters

Source: Internet
Author: User

Recently I wrote a small Java class that uses base64 encoding for "encryption". I think it is a bit interesting. I hope you will not laugh at it.

 

/** // * Author: livahu
*
* Used to encode base64t and decode base64
*
*/
Import java. Io. printwriter;
Import java. Io. fileinputstream;
Import java. Io. ioexception;
Import java. Io. unsupportedencodingexception;
Import java. Io. filenotfoundexception;
Import java. util. collections;
Import sun. Misc. base64decoder;
Import sun. Misc. base64encoder;

Public class base64
...{
// Encode s with base64
Public static string getbase64encoderstr (string s) throws unsupportedencodingexception
...{
If (null = s)
Return NULL;
Else
Return new base64encoder (). encode (S. getbytes ("gb2312 "));
}
// Decodes s from base64
Public static byte [] getbase64decoderbt (string s) throws ioexception
...{
If (null = s)
Return NULL;
Else
...{
Base64decoder decoder = new base64decoder ();
Return decoder. decodebuffer (s );
}

}
// Encode the specified file with base64
Public static void changetobase64 (string filepath) throws ioexception
...{
Required SC = new partition (New fileinputstream (filepath). usedelimiter ("");
Printwriter PW = new printwriter (filepath + "_ base64 _");
While (SC. hasnext ())
...{
PW. println (getbase64encoderstr (SC. Next ()));
}
SC. Close ();
PW. Close ();
}
// Decodes a specified object from base64
Public static void recoverfrombase64 (string filepath) throws ioexception, filenotfoundexception
...{
Required SC = new partition (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 method
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! ");
}
}
}

 

The following are batch files.

 

Rem Author: livahu
@ Echo off
Set classpath = directory where base64.java is stored; % classpath %
Java base64 % 1
Del % 1

 

You just need to drag any file to base64.bat.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

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.