是的,您可以向类加载器提供您抓取的字节。我在类似的问题中这样做:
public class SecureClassLoader extends URLClassLoader {
@Override
protected Class<?> findClass(final String name) throws ClassNotFoundException {
if (isEncrypted(name)) {
final String resourceName = name.replace('.', '/') + ".class";
URL url = findResource(resourceName);
if (url != null) {
byte[] classBytes = null;
try {
classBytes = EncryptionUtil.decryptBytes(url, key);
return defineClass(name, classBytes, 0, classBytes.length);
} catch (ClassFormatError e) {
log.severe("Bad format for decrypted class " + name);
throw new EncryptedClassNotFoundException(name, e);
} catch (InvalidKeyException e) {
throw new EncryptedClassNotFoundException(name, e);
} catch (IOException e) {
throw new EncryptedClassNotFoundException(name, e);
}
}
}
// default loader
try {
return super.findClass(name);
} catch (ClassNotFoundException e) {
throw new EncryptedClassNotFoundException(name, e);
}
}
private boolean isEncrypted(String className) {
/// some things
}
}
但这不足以保护您的代码。至少,不要为两个用户使用相同的字节(你似乎这样做了)。并混淆你的代码(我使用 proguard)。这将保护您免受普通黑客的攻击,而不是最好的黑客。