Julia program obfuscation for commercial code

As promised, here is some C++ sample code running on linux with julia installed in the /opt/julia-1.7.1/ folder.

To use the sample code:

  1. Put all files in the same folder
  2. [if needed] Edit the makefile to point to your julia installation folder
  3. Open a command prompt and run “make”
  4. Encrypt your file by running “./obfuscator something.jl something.encryptedjl”
  5. Execute the encrypted file by running “./program something.encryptedjl”

Encryption

encryption.h
#include <string>
using namespace std;

void encrypt_decrypt(string &msg, string const& key);
encryption.cpp
#include "encryption.h"
using namespace std;

// simple message XOR cipher encryption suggestion from :
// http://www.cplusplus.com/forum/windows/128374/#msg694527
void encrypt_decrypt(string &msg, string const& key)
{
    for (string::size_type i = 0; i < msg.size(); ++i)
        msg[i] ^= key[i%key.size()];
}

Obfuscation program

obfuscator.cpp
#include <iostream>
#include <fstream>
#include <streambuf>

#include "encryption.h"
using namespace std;

const string encryption_key = "julia";

int main (int argc, char *argv[]) {
    if (argc < 2 || argc > 3) {
        cout << "Usage: obfuscator input_file [output_file]" << endl;
        return 1;
    }

    ifstream ifs(argv[1]);
    string fileContent( (istreambuf_iterator<char>(ifs)),
                      istreambuf_iterator<char>() );

    string outputFilename = 3 == argc ? string(argv[2]) : string(argv[1]) + ".encryptedjl";

    encrypt_decrypt(fileContent, encryption_key);

    ofstream ofs(outputFilename);
    ofs << fileContent;

    return 0;
}

C++ program

program.cpp
#include <iostream>
#include <fstream>
#include <streambuf>

#include "julia.h"
#include "encryption.h"
using namespace std;

const string encryption_key = "julia";

JULIA_DEFINE_FAST_TLS // only define this once, in an executable (not in a shared library) if you want fast code.

int main (int argc, char *argv[]) {
    if (argc != 2) {
        cout << "Usage: program input_file" << endl;
        return 1;
    }

    // required: setup the Julia context
    jl_init();
    
    // load up the julia file into memory
    ifstream filestream(argv[1]);
    string filecontent( (istreambuf_iterator<char>(filestream)), istreambuf_iterator<char>() );

    encrypt_decrypt(filecontent,encryption_key);
    
    jl_eval_string(filecontent.c_str());

    jl_atexit_hook(0);
    return 0;
}

Makefile

Makefile
CXX=/usr/bin/g++
JULIAINCPATH=/opt/julia-1.7.1/include/julia
JULIALIBPATH=/opt/julia-1.7.1/lib
CXXOPTS=-Wall -Werror
LDOPTS=-Wl,-rpath,$(JULIALIBPATH)
PATHS=-I$(JULIAINCPATH) -L$(JULIALIBPATH)
LIBS=-ljulia

all : obfuscator program

encryption.o : encryption.cpp encryption.h
	$(CXX) -c $(CXXOPTS) $< -o $@

obfuscator : obfuscator.cpp encryption.o
	$(CXX) $(CXXOPTS) $^ -o $@

program : program.cpp encryption.o
	$(CXX) $(CXXOPTS) $(PATHS) $(LDOPTS) $^ -o $@ $(LIBS)
7 Likes