Dark Horse programmer-encryption and decryption of Java class loaders and class files

Source: Internet
Author: User

-------------------- Android training, Java training, and hope to communicate with you! ----------------------

I learned how to encrypt and decrypt the class file today. I think it is interesting to introduce it.

First, the encryption of the class file is actually the binary of the class file.CodeThrough specificAlgorithmMake changes, so that the binary code in the package is messy, and the default Class Loader cannot load the correct information, decryption is to re-restore the messy binary code through a specific algorithm. This process involves a class loader, and the default Class Loader cannot be decrypted, in this case, we need to specify a specific class loader and a custom Class Loader for the encrypted class file, to customize the class loader, you need to understand the class loader and its delegation mechanism. Next, we will introduce,

The class loader is a tool for loading classes. It can load class files to the memory. By default, the system has three main class loaders: Bootstrap and extclassloader, the class where each loader loads a specific location. The class loader is also a Java class, but Bootstrap is not a Java class and serves as the parent class of other class loaders.

The following describes the delegation mechanism of the Class Loader. First, we know that appclassloader loads the jar and directory specified by classpath, and exclassloader loads JRE/lib/EXT /*. jar, bootstrap loads JRE/lib/RT. jar, the three class loaders are parent-child relationships. When the class file is loaded, it does not mean that the appclassloader can load the specified class file, but delegates it to its father extclassloader, extclassloader is also delegated to the final Bootstrap. It can only be delegated downward when no Bootstrap can be found, and is finally loaded by appclassloader.

Therefore, when we want to customize the class loader, we also need to follow its delegate mechanism. First, the custom Class Loader class must inherit the classloader class, then rewrite the core method findclass for loading the class so that it can load the specified directory. In this process, decrypt the class file and load it, in this way, only the specified class loader can load the specified class file. If there are so many objects, let's give them an instance: The following is the source file of the custom class loader.

 
// This is the encrypted class public class temp extends Date {Public String tostring () {return "Hello kugoo ";}}

 

 
// This is an encryption class that encrypts binary files. Public class encryption {public static void main (string [] ARGs) {fileinputstream FD = NULL; fileoutputstream Fos = NULL; string Path = ARGs [0]. substring (ARGs [0]. lastindexof ('\') + 1); try {FD = new fileinputstream (ARGs [0]); Fos = new fileoutputstream (ARGs [1] + file. separator + path); Cypher (FCM, FOS); // This method is included in the following custom method. The same method is used for encryption and decryption, and will not be overwritten here} catch (ioexception E) {e. printstacktrace ();} finally {If (F Is! = NULL) Try {fs. Close ();} catch (ioexception e) {e. printstacktrace ();} If (FOS! = NULL) Try {FOS. Close ();} catch (ioexception e) {e. printstacktrace ();}}}}

 

// This is the custom Class Loader public class myclassloader extends classloader {private string classdir; Public myclassloader (string classdir) {This. classdir = classdir;} @ override // rewrite the findclass method protected class <?> Findclass (string name) throws classnotfoundexception {string classfilename = classdir + "\" + name + ". class "; try {fileinputstream FCM = new fileinputstream (classfilename); bytearrayoutputstream baos = new bytes (); // bytearrayoutputstream Fos = new bytearrayoutputstream (); Cypher (FCM, baos ); fiis. close (); byte [] Buf = baos. tobytearray (); Return defineclass (BUF, 0, Buf. length);} catch (exception e) {e . Printstacktrace ();} return NULL;} // reference instructor Zhang. the encryption and decryption files are all in the same algorithm. Simple and different operations or public static void Cypher (inputstream is, outputstream OS) throws ioexception {int Len = 0; while (LEN = is. read ())! =-1) {OS. Write (LEN ^ 0xff );}}}

Then there is the test code. The directory loaded by the class loader is welenlib.

Public class test {public static void main (string [] ARGs) throws exception {class clazz = new myclassloader ("welenlib "). loadclass ("Temp"); date t = (date) clazz. newinstance (); system. out. println (t); system. out. println (T. getclass (). getclassloader (). getclass (). getname ());}}

-------------------- Android training, Java training, and hope to communicate with you! ----------------------
For details, see edu.csdn.net/heima.

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.