Share but protect – how to encrypt your code

Directly talking to a computer is a difficult thing to do, especially when your computer can execute billions of operations each second. We need an intermediate that convert our language into basic elements of computations. In the early days, this was done using punched cards. We now rely on computer programs called “Compilers” that convert our words into bits and operations on bits.

I could go on forever on the history of programming (maybe I will some day) but my point today is to talk about an important side effect of the need for compilers, encryption. Because the code needs to be compiled and because this compilation process can be very long, program are not usually shared in their readable form but in their compiled form, in machine language. Since we don’t read this language, it basically looks encrypted.

In the early days, it looked natural to only share this way but the computer community quickly realized that this could cause many problems as computer complexity and all the associated software was increasing. This is how the Open Source movement started.

Matlab is a little bit of this and a little bit of that. Many core functionalities of Matlab are ‘encrypted’ or built-in. Still lots of functions are Open-source, you can open its associated M-file. Mathworks have tried to create a sense of open community and many Matlab users share M-file through the Mathworks file-exchange. This is a great and very efficient tool, especially to us researchers. If you have never looked into it, stop now this readings and check it out.

But, in many case, you actually want to protect your work. You are happy to share the program, but you spent so much time on the code that it could be difficult to give away years of development. This is totally understandable.

If you are in that position, you have more or less 3 options.

  1. You convert your code to C/C++ or any other language that you compile
  2. You actually compile your code using Matlab compiler
  3. You don’t compile but you encrypt your source.

Option 1 and 2 are most of the time known, but most people don’t know that they actually can bypass the compilation part and just encrypt their source.  Compiling can be quite annoying if you want to cover all the 6 big plateforms (Windows 32 bit, Windows 64 bit, Mac OS 32 bit, Mac OS 64 bit, Linux 32 bit, Linux 64 bit). Maybe the compiling toolbox is not available to you also.  Anyway I will certainly make a post about these processes one day as compiling your code can be a good last alternative to speed up some calculations.

Let’s talk about option 3 then. A big advantage to this is that you don’t have to release various versions of your compiled program for each plateform.

This is actually surprisingly easy.

Let’s create this function TestPcode.m

function y=TestPcode(x)

% This function test pcode
y=x.^2+10;

Calling TestPcode(3) send you back 14. Now to generate an encrypted version, a P-file, you just have to do :

pcode('TestPcode')

Matlab will generate the P-file in the same location where the M-file is. If you delete the M-file, TestPcode still works. But the P-file is encrypted, you can’t open it to look at the code.

There are a couple important things. If you want the call to “help TestPcode” to work, you must provide a truncated M-file with just :

function y=TestPcode(x)
% This function test pcode

This is how Mathworks actually distribute its built-in functions (ie with a associated M-file to provide the help).
Another important thing is that this also work with function generated with the GUIDE. The GUIDE is used to make interface for your users needs. GUIDE generates a .fig file and .m file. Apply pcode to the .m file and truncate the .m file to only leave the help.

This entry was posted in Intermediate. Bookmark the permalink.

2 Responses to Share but protect – how to encrypt your code

  1. Marcel says:

    Very nice post. Though there are some semantic problems here. The pcode method just obfuscate the code, it doesn’t encrypt it. What MATLAB is even saying:
    “When MATLAB P-codes a file, the file is obfuscated not encrypted. While the content in a .p file is difficult to understand, it should not be considered secure. It is not recommended that you P-code files to protect your intellectual property.”

    Nevertheless, I would be interested if there would be a way to build in a license key to the source code in order to encrypt the methods and thus block or allow execution.

    • Jerome says:

      Thanks Marcel.

      I don’t know how you would define what encryption is. To me the whole point of encryption is to obfuscate to most people.

      I think under the hood, the Matlab engine has the private key to read the code.

      Your suggestion is actually a great idea, it would be nice to be able to provde a private key to the P-file and share this key with people we want to share the code with. It seems like a very nice way to do licensing.

      Maybe Mathworks could think about implementing that?

      However, it seems like it would be possible to build such a thing on top ourselves. You would have to resort on serious usage of “eval” which might not be cleaned.

Leave a Reply

Your email address will not be published. Required fields are marked *