Gopalpur
Gopalpur

Reputation: 121

How to best possibly protect .m file?

I have an .m file which I wish to share with my friends but I am not interested in giving .m file. Could someone help me with best possible ways to convert it to a file that is not decodable? I tried converting it to .p file by simply typing pcode example.m however I don't believe it is really protecting it. I was able to convert my .p file back to .m file with the following link. https://sites.google.com/site/sippeyfunlabs/matlab-hacks/please-do-not-secure-your-password-in-matlab-p-code

This actually confirms that my code is not protected.

It'll be nice if someone shares the best methodology to protect .m file and sharing.

Thanks

Upvotes: 0

Views: 942

Answers (2)

m7913d
m7913d

Reputation: 11072

The link you provided yourself already indicates that it is very difficult to obfuscate MATLAB code:

In fact, MATLAB language is very difficult to be secured or even obfuscated. This is due to the late binding (or dynamic binding) feature of MATLAB. [...] The amount of meta information associated with this feature basically forbid any attempt of adding code level security. Simply put, if there is a MATLAB file, and it calls a function foo inside it. Until the runtime, the MATLAB interpreter do not even know if foo is a function stored in M file or a built-in function or a mex function or even a workspace function handle. Thus, it must store foo as is somewhere inside the generated P-code.

Also to best solution is already mentioned on that page:

If there is really a need to do this, using the good old binary is a much better solution. Or you can put critical code on a server, away from the user.

  • Create a binary, i.e. mex file (see the answer of Wolfie): Note that to some extent also binary code can be decompiled especially small ones.
  • Host your code on a server (and run the code server-side): This is the best method to protect your code. You should let the user upload the input for your script and return the result. You can also automatize this process using a matlab script to make this process transparent to the user.

Upvotes: 2

Wolfie
Wolfie

Reputation: 30101

You could build a mex file.

This will completely obfuscate your actual MATLAB code, since it will be written in C/C++/FORTRAN, but the algorithms will still be there if your friends are determined enough to look for them.

Upvotes: 2

Related Questions