Created
January 6, 2022 02:53
-
-
Save CaledoniaProject/7c14a619174440df018e535c0e204994 to your computer and use it in GitHub Desktop.
Decode and encode BCEL class
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package main; | |
import java.io.*; | |
import java.nio.*; | |
import java.nio.file.*; | |
import org.apache.bcel.classfile.Utility; | |
public class Main { | |
public static void help() { | |
System.out.println( | |
"Usage:\n" + | |
" java -jar bcelClassUtil -d $$BCEL$$...\n" + | |
" java -jar bcelClassUtil -e XX.class\n" | |
); | |
System.exit(0); | |
} | |
public static void main(String[] args) throws Exception { | |
if (args.length != 2) { | |
help(); | |
} | |
if (args[0].equals("-d")) { | |
decode(args[1]); | |
} else if (args[0].equals("-e")) { | |
encode(args[1]); | |
} else { | |
help(); | |
} | |
} | |
public static void decode(String input) throws Exception { | |
System.out.println("Saving foo.class"); | |
if (input.startsWith("$$BCEL$$")){ | |
input = input.substring(8); | |
} else { | |
throw new Exception("invalid BECL class bytecode"); | |
} | |
byte[] data = Utility.decode(input, true); | |
Files.write(Paths.get("foo.class"), data); | |
} | |
public static void encode(String filename) throws Exception { | |
byte[] data = Files.readAllBytes(Paths.get(filename)); | |
System.out.println("$$BCEL$$" + Utility.encode(data, true)); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment