Advertisement
Guest User

Untitled

a guest
May 21st, 2019
79
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.64 KB | None | 0 0
  1. import java.io.File;
  2. import java.io.FileInputStream;
  3. import java.io.FileNotFoundException;
  4. import java.io.IOException;
  5. import java.io.InputStream;
  6.  
  7. public class EncryptedClassLoader extends ClassLoader {
  8. private String key;
  9.  
  10. private File dir;
  11.  
  12.  
  13. public EncryptedClassLoader(String key, File dir, ClassLoader parent) {
  14. super(parent);
  15. this.key = key;
  16. this.dir = dir;
  17. }
  18.  
  19. private void encrypte(byte[] bytes) {
  20. byte[] byteArray = key.getBytes();
  21.  
  22. int sum = 0;
  23.  
  24. for (byte b : byteArray) {
  25. sum += b;
  26. }
  27.  
  28. for (int i = 0; i < bytes.length; i++) {
  29. bytes[i] = (byte) ((byte)(bytes[i] + sum) % 256 - 128);
  30. }
  31. }
  32.  
  33. private void decrypte(byte[] bytes) {
  34. byte[] byteArray = key.getBytes();
  35.  
  36. int sum = 0;
  37.  
  38. for (byte b : byteArray) {
  39. sum += b;
  40. }
  41.  
  42. for (int i = 0; i < bytes.length; i++) {
  43. bytes[i] = (byte) ((byte)(bytes[i] - sum) % 256 - 128);
  44. }
  45. }
  46.  
  47. private byte[] getBytesFromFile(String s) {
  48. byte[] bytes = {};
  49.  
  50. try {
  51. File file = new File(dir + "/" + s + ".class");
  52.  
  53. bytes = new byte[(int) file.length()];
  54. InputStream is = new FileInputStream(file);
  55. is.read(bytes);
  56. is.close();
  57. } catch (FileNotFoundException e) {
  58. e.printStackTrace();
  59. } catch (IOException e) {
  60. e.printStackTrace();
  61. }
  62.  
  63. return bytes;
  64. }
  65.  
  66. @Override
  67. protected Class<?> findClass(String s) throws ClassNotFoundException {
  68.  
  69. byte[] bytes = getBytesFromFile(s);
  70.  
  71. if (bytes.length == 0) {
  72. throw new ClassNotFoundException();
  73. }
  74. //decrypte(bytes);
  75.  
  76. try {
  77. return defineClass(s, bytes, 0, bytes.length);
  78. } catch (Exception e) {
  79. return super.findClass(s);
  80. }
  81. }
  82. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement