简体   繁体   English

如何在Java中将字节数组保存为类文件并在以后反编译?

[英]How to save an array of byte as a class file in java and decompile it later?

I'm doing a wargame and I need to analyze a java app. 我正在做一个战争游戏,我需要分析一个Java应用程序。 So the app loads a corrupted class file, converts it to an array of bytes, does something with it, and then loads the class properly using that array. 因此,该应用程序加载了损坏的类文件,将其转换为字节数组,对其进行处理,然后使用该数组正确加载类。 I need to analyze the class so I wanted to save it as a separate class file to decompile it later using jd-gui. 我需要分析该类,因此我想将其另存为单独的类文件,以便稍后使用jd-gui对其进行反编译。 I use the following code to save the array: 我使用以下代码保存数组:

FileOutputStream output = new FileOutputStream(new File("class.class"));
output.write(arrayOfByte);

But the class doesn't get decompiled afterwards. 但是此后该类不会反编译。 What else do I have to do to make it readable by the decompiler? 为了使反编译器可读,我还需要做什么?

UPDATE: 更新:

What I meant by corrupted, was that the class file is actually incomplete, the app completes the class and makes it a valid class file inside and then loads it. 我所说的损坏是指类文件实际上是不完整的,应用程序完成了该类并使其成为有效的类文件,然后将其加载。 The programmer has done this to prevent the class from being decompiled and read. 程序员这样做是为了防止对该类进行反编译和读取。 This isn't really needed for the question but I thought it would be better if I clarified. 这个问题确实不是必需的,但我认为如果我澄清一下会更好。

Make sure you writing the class java byte code correctly into the path for Example 确保将正确的类Java字节代码写入示例路径

say class fully qualified name including class name is com.test.Sample you should write the class byte code under folder com\\test\\Sample.class. 假设类的完全限定名称(包括类名称)为com.test.Sample,则应在文件夹com \\ test \\ Sample.class下编写类字节码。

Further you make sure that the FileOutputStream is closed properly after writing the byte code. 此外,您还要确保在写入字节码后正确关闭FileOutputStream。

// use fully qualified class name
FileOutputStream output = new FileOutputStream(new File("c:\\com\\test\\Sample.class"));
output.write(arrayOfByte);
output.close();

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM