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.