Class loader, encryption

Source: Internet
Author: User

Classloaderattachment.java

public class Classloaderattachment extends Date {public String toString () {
return "Hello,itcast";
}

}


Myclassloader.java

public class Myclassloader extends classloader{

/**
* @param args
*/
public static void Main (string[] args) throws Exception {
TODO auto-generated Method Stub
String Srcpath = args[0];
String destdir = args[1];
FileInputStream fis = new FileInputStream (Srcpath);
String destfilename = srcpath.substring (srcpath.lastindexof (' \ ') +1);
String DestPath = destdir + "\" + destfilename;
FileOutputStream fos = new FileOutputStream (destpath);
Cypher (Fis,fos);
Fis.close ();
Fos.close ();
}

private static void Cypher (InputStream IPs, OutputStream Ops) throws exception{
int b =-1;
while ((B=ips.read ())!=-1) {
Ops.write (b ^ 0xff);
}
}

Private String Classdir;

@Override
Protected class<?> Findclass (String name) throws ClassNotFoundException {
TODO auto-generated Method Stub
String classfilename = classdir + "\" + name.substring (Name.lastindexof ('. ') +1) + ". Class";
try {
FileInputStream fis = new FileInputStream (classfilename);
Bytearrayoutputstream BOS = new Bytearrayoutputstream ();
Cypher (Fis,bos);
Fis.close ();
System.out.println ("AAA");
byte[] bytes = Bos.tobytearray ();
Return defineclass (bytes, 0, bytes.length);
catch (Exception e) {
TODO auto-generated Catch block
E.printstacktrace ();
}
return null;
}

Public Myclassloader () {

}

Public Myclassloader (String classdir) {
This.classdir = Classdir;
}
}


Classloadertest.java

public class Classloadertest {

/**
* @param args
*/
public static void Main (string[] args) throws Exception {
TODO auto-generated Method Stub
System.out.println (
ClassLoaderTest.class.getClassLoader (). GetClass (). GetName ()
);
System.out.println (
System.class.getClassLoader ()
);
SYSTEM.OUT.PRINTLN ("xxx");
ClassLoader loader = ClassLoaderTest.class.getClassLoader ();
while (loader!= null) {
System.out.println (Loader.getclass (). GetName ());
Loader = Loader.getparent ();
}
SYSTEM.OUT.PRINTLN (loader);

System.out.println (New Classloaderattachment (). toString ());
System.out.println ("xxx2");
Class clazz = new Myclassloader ("Itcastlib"). LoadClass ("Cn.itcast.day2.ClassLoaderAttachment");
Date D1 = (date) clazz.newinstance ();
System.out.println (D1);
}

}

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.