1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72
|
package crypter;
import java.util.*;
import java.io.*;
import java.lang.reflect.*;
import java.io.FileNotFoundException;
import java.io.IOException;
public class SecureClassLoader extends ClassLoader{
private int key;
public SecureClassLoader(int key) {this.key = key; }
static public boolean encrypt(String className, int key){
className = className.replace('.', '/');
try{
FileInputStream in = new FileInputStream(className + ".class");
FileOutputStream out = new FileOutputStream(className + ".caesar");
{
int ch;
byte c;
while ( (ch = in.read()) != -1) {
c = (byte) (ch + key);
out.write(c);
}
}
in.close();
out.close();
}
catch(IOException e){
System.out.println("Erreur IOEXception encrypt");
return false;
}
return true;
}
protected Class findClass(String className) throws ClassNotFoundException{
byte[] classBytes;
try{
classBytes = loadClassBytes(className);
}
catch (IOException e){
System.out.println("Erreur findClass");
throw new ClassNotFoundException(className);
}
Class cl = defineClass(className, classBytes, 0, classBytes.length);
if (cl == null)
throw new ClassNotFoundException(className);
return cl;
}
private byte[] loadClassBytes(String className) throws IOException {
// Si la classe fait partie d'un package
className = className.replace('.', '/');
FileInputStream in = null;
try {
in = new FileInputStream(className + ".caesar");
ByteArrayOutputStream buffer = new ByteArrayOutputStream();
int ch;
while ((ch = in.read()) != -1){
byte b = (byte)(ch-key);
buffer.write(b);
}
in.close();
return buffer.toByteArray();
}
finally{
if (in != null)
in.close();
}
}
public static void main(String[] args) {
SecureClassLoader secureClassLoader1 = new SecureClassLoader(15);
}
}
merci d'avance |
Partager