Skip to content

Instantly share code, notes, and snippets.

@CaledoniaProject
Created January 6, 2022 02:53
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save CaledoniaProject/7c14a619174440df018e535c0e204994 to your computer and use it in GitHub Desktop.
Save CaledoniaProject/7c14a619174440df018e535c0e204994 to your computer and use it in GitHub Desktop.
Decode and encode BCEL class
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