How to perform Base64 in Java, an interesting discovery MHT HTML HTM Base64 Java
Original program from James, I just added a main method, and note package. File name: Base64.java usage: java base64 ENC good person output: USPIYWAA Java Base64 Dec USPIYWAA Output: Good people modified: hajavaor
If you want to know the knowledge of Base64's related coding and decoding, high recommendation: http://www.luocong.com/Articles/show_article.asp? Article_ID = 17 Article Title: The contest of the spear and shield (4) - Wonderful base64 coding
After reading it, I totally know what Base64 is going.
For more exciting, please see the article in the end, my discovery.
Nothing to gossip, see the Java source program.
File: // Program start / * ============================================= =================================================================================003 The Apache Software Foundation. All Rights * reserved * * Redistribution and use in source and binary forms, with or without * modification, are permitted provided that the following conditions * are met:. * * 1. Redistributions of source code must retain the above copyright * notice, this list of conditions and the following disclaimer. * * 2. Redistributions in binary form must reproduce the above copyright * notice, this list of conditions and the following disclaimer in * the documentation and / or other materials provided with the * distribution. * * 3. The end-user documentation included with the redistribution, * if any, must include the following acknowledgment: * ". This product includes software developed by the * Apache Software (http://www.apache.org/) Foundation" * Alternately, this Acknowledg ment may appear in the software itself, * if and wherever such third-party acknowledgments normally appear. * * 4. The names "Apache", "Jakarta", "JAMES" and "Apache Software Foundation" * must not be used to endorse Or Promote Products Derived from this * Software without prior written permission. for Written * Permission, please contact apache @
apache.org. * * 5. Products derived from this software may not be called "Apache", * nor may "Apache" appear in their name, without prior written * permission of the Apache Software Foundation. * * THIS SOFTWARE IS PROVIDED ` `AS IS '' AND ANY EXPRESSED OR IMPLIED * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE * dISCLAIMED. IN NO EVENT SHALL THE APACHE SOFTWARE FOUNDATION OR * ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF * USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT * OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF * SUCH DAMAGE. * === ===================================== ================================================================================================================================================================================================================================================================================00 For more * information on the apache Software Foundation, please see *
. * * Portions of this software are based upon public domain software * originally written at the National Center for Supercomputing Applications, * University of Illinois, Urbana-Champaign * / file:. // package org.apache.james.util;
import javax.mail.internet.MimeUtility; import java.io.BufferedReader; import java.io.ByteArrayInputStream; import java.io.ByteArrayOutputStream; import java.io.InputStreamReader;
/ ** * Simple Base64 string decoding function * @Author jason borden
Public class base64 {
public static BufferedReader decode (String b64string) throws Exception {return new BufferedReader (new InputStreamReader (MimeUtility.decode (new ByteArrayInputStream (b64string.getBytes ()), "base64")));} file: // 2003-10-29 10 : 28 Hajavaor Added Public Static Void Main (String Args []) THROWS Exception {if (args.length! = 2) {System.err.Println ("Usage: Java Base64 Encordec Yourstrings); System.exit (0); } String str1 = args [0] .trim (); if (str1.equalsignorecase ("ENC")) {system.out.println (encodesstring (args [1]));} else if (str1.equalsignorecase ")) {System.out.println (decodesstring (args [1]));} else {system.err.println (" USAGE: Java Base64 Encordec Yourstrings); System.exit (0);}} //
public static String decodeAsString (String b64string) throws Exception {if (b64string == null) {return b64string;} String returnString = decode (b64string) .readLine (); if (returnString == null) {return returnString;} return returnString. trim ();} public static ByteArrayOutputStream encode (String plaintext) throws Exception {ByteArrayOutputStream out = new ByteArrayOutputStream (); byte [] in = plaintext.getBytes (); ByteArrayOutputStream inStream = new ByteArrayOutputStream (); inStream.write (in, 0 , In.Length); // PAD IF ((in.Length% 3) == 1) {Instream.write (0); instream.write (0);} else if ((in.length% 3) == 2) {Instream.write (0);} instream.writeto (mimeutility.encode (out, "base64")); return out;}
Public Static String Encodesstring (String Plaintext) THROWS Exception {Return Encode (Plaintext) .tostring ();
} /// The program ends.
An interesting discovery (on the format of the Microsoft MHT file): We have a variety of options when using IE to save web files. 1, the web page is all. You will find an HTML file and a corresponding folder. In this way, when we open this web file, we can see additional elements of the web page (pictures, style sheets, frames, etc.) at the same time. The Windows system has established this file with the folder. When you move the HTML file, you find that the folder is also moved simultaneously. When you delete this folder, you will find that the file is also deleted. We can change the folder first, and then you can safely delete the folder. 2, web documentation, single format, MHT file. This way we can save all the contents of the web page in a file. You can open this file with Notepad, will find: The body inside is encoded in Base64, and the entire file structure is very like the structure of the mail. Is that so? Remove the MHT to EML, then double click, it can really be opened with Outlook Express. It turns out that the MHT file is an email, and the content is encoded in Base64. Similarly, you can drag messages from Outlook Express to the outside and generate a * .eml file. Try the file change. Interested. Who has a further analysis of MHT format, please continue.
Thank you.