MATLAB: How to securely package credentials into a MATLAB Compiler application

applicationcompilerencryptionexecutableMATLAB Compilerobfuscationsecurestandalone

I have created an application using MATLAB Compiler, and my application requires the use of some credentials to access a web service. These credentials are not user-specific, and I distribute this application outside of my company, so I would like to ensure that these credentials will be safe.
How can I package these credentials with my application to keep them secure?
I am aware that I could instead require the user of my application to provide their own credentials, and such a workflow does not suit my requirements.

Best Answer

Please note that there is no general best way to secure credentials because this security measure is very context-dependent. That being said, MATLAB Compiler has capabilities to encrypt obfuscated information in the form of P-files. The following procedure is one way to package credentials with an application, although note that MathWorks makes no guarantees as to the security of such an application.
  1. Hardcode the credentials into an M-file.
  2. Use the 'pcode' function to convert the M-file into a P-file. This obfuscates the credentials.
  3. When MATLAB Compiler creates the application, all P-files and M-files are encrypted.
The end result is that your credentials are both obfuscated and encrypted. However, note that our encryption protocol does not have an "Orange Book" classification. No security protocol or encryption algorithm is ever completely secure. It is possible that a determined attacker could find a way to read the contents of MATLAB files in a deployed application. We expect this would be quite difficult to do, but we neither claim it is impossible, nor can we quantify the level of difficulty.
 
For more information about protecting MATLAB Compiler applications, please see the following page: https://www.mathworks.com/matlabcentral/answers/94674-can-i-depend-on-the-matlab-compiler-4-0-r14-to-protect-my-intellectual-property
Note that you may need to include the P-files in your application manually, as the Dependency Analyzer for MATLAB Compiler may not detect all the necessary P-files. For more information, please see the following documentation page: