Sunday, November 27, 2011

Java Source Code Hiding

I was thinking of a way to hide complex algorithms in source code and it turns out that Java Compiler can compile source code with unicode code character encoding. So for instance a typical HelloWorld class like this
public class HelloWorld {
    public static void main(String[] args) {
        System.out.println("Hello, World!");
    }
}
 
Can be written as

\u0070\u0075\u0062\u006C\u0069\u0063\u0020\u0063\u006C\u0061\u0073\u0073\u0020\u0048\u0065\u006C\u006C\u006F\u0057\u006F\u0072\u006C\u0064\u0020\u007B\u0009\u0070\u0075\u0062\u006C\u0069\u0063\u0020\u0073\u0074\u0061\u0074\u0069\u0063\u0020\u0076\u006F\u0069\u0064\u0020\u006D\u0061\u0069\u006E\u0028\u0053\u0074\u0072\u0069\u006E\u0067\u005B\u005D\u0020\u0061\u0072\u0067\u0073\u0029\u0020\u007B\u0009\u0009\u0053\u0079\u0073\u0074\u0065\u006D\u002E\u006F\u0075\u0074\u002E\u0070\u0072\u0069\u006E\u0074\u006C\u006E\u0028\u0022\u0048\u0065\u006C\u006C\u006F\u002C\u0020\u0057\u006F\u0072\u006C\u0064\u0021\u0022\u0029\u003B\u0009\u007D\u007D

And it would compile just fine using javac and will run and print Hello, World!. I wrote a small application which converts any java source file to Unicode encoding so that you can hide your source within your computer from people who have access.

import java.io.*;
/*
 * @author Shazin Sadakath
 */
public class JavaUnicodeEncoding {
    public static void main(String[] args) throws Exception {
        BufferedReader br = null;
        BufferedWriter bw = null;
        try {
            br = new BufferedReader(new FileReader(args[0]));
            bw = new BufferedWriter(new FileWriter(args[1]));
            String line = null;
            while((line = br.readLine()) != null) {
                for(char c:line.toCharArray()) {
                    bw.write(String.format("\\u%04X",(int) c));
                }
                bw.flush();
            }
        } catch(ArrayIndexOutOfBoundsException e) {
            usage();
        } catch(Exception e) {
            System.err.println(e.getMessage());
        } finally {
            if(br != null) {
                br.close();
            }
            
            if(bw != null) {
                bw.close();
            }
        }
    }
    
    private static void usage() {
        System.out.println("java JavaUnicodeEncoding <input class filename /> <output class filename/>");
    }
}
You can even use the two interchangeably also
public class HelloWorld {
    public static void main(String[] args) {
        \u0053\u0079\u0073\u0074\u0065\u006D\u002E\u006F\u0075\u0074\u002E\u0070\u0072\u0069\u006E\u0074\u006C\u006E\u0028\u0022\u0048\u0065\u006C\u006C\u006F\u002C\u0020\u0057\u006F\u0072\u006C\u0064\u0021\u0022\u0029\u003B
    }
}

2 comments:

Movies Gallery 2011 said...

I have no words for this great post such a awe-some information i got gathered. Thanks to Author.
Vee Eee Technologies| Vee Eee Technologies|

Admin said...

hello could you please tell me step by step how to hide source code in blogger