Main Content

Create a Content-Obscured File with P-Code

A P-code file behaves the same as the MATLAB® source from which it was produced. The P-code file also runs at the same speed as the source file. P-code files are purposely obfuscated.

Note

Security Considerations: The pcode function produces MATLAB program files in a proprietary, obfuscated code format. Consider combining multiple approaches to protect sensitive code or data. For more information, see Security Considerations to Protect Your Source Code.

Create P-Code Files

To generate a P-code file, enter the following command in the MATLAB Command Window:

pcode file1.m file2.m

The command produces the files file1.p, file2.p, and so on. To convert all .m source files residing in your current folder to P-code files, use the command:

pcode *.m

See the pcode function reference page for a description of all syntaxes for generating P-code files.

Obfuscate Local Identifiers

Use matlab.lang.obfuscateNames in conjunction with pcode -R2022a to further obfuscate source-code by replacing names of local variables, local functions, and nested functions with generic names.

Note

Code with obfuscated names may behave differently from source code. matlab.lang.obfuscateNames only obfuscates direct occurrences of names in code, such as x in x = 1, if x < 5, and f(x+y). Occurrences in strings, character vectors, and command-syntax arguments are not obfuscated, such as eval("f(x+y)") or clear x.

For example, you have the source-code for the function myfunc:

function outputArg = myfunc(inputArg1,inputArg2)
    sumVar = inputArg1 + inputArg2;
    prodVar = inputArg1*inputArg2;

    outputArg = sumVar/prodVar;
end

Use matlab.lang.obfuscateNames to write myfunc to a new file with local names obfuscated.

matlab.lang.obfuscateNames("SourceCode\myfunc.m","ObfuscatedCode\myfunc.m")

The new file has the names of local identifiers changed to generic names:

function outputArg = myfunc(inputArg1,inputArg2)
    id242487092 = inputArg1 + inputArg2;
    id40037619 = inputArg1*inputArg2;

    outputArg = id242487092/id40037619;
end

Note that the names of the input and output arguments are not changed. The names ans, varargin, varargout, and names that are used as literal arguments for the load function are never obfuscated.

Thoroughly test obfuscated code to ensure it is functioning as intended. Consider making use of MATLAB Testing Frameworks to write automated tests to verify the behavior of obfuscated code. If the code does not behave as desired, use PreserveNames and other name-value arguments to control the obfuscation process.

Source code written to outputFile is encoded using UTF-8.

Invoke P-Code Files

You can invoke the resulting P-code files in the same way you invoke the MATLAB .m source files from which they were produced. For example, to invoke file myfun.p, type:

[out1,out2] = myfun(in1,in2);

To invoke script myscript.p, type:

myscript;

When you call a P-code file, MATLAB gives it execution precedence over its corresponding .m source file. This is true even if you change the source code at generating the P-code file. Remember to remove the .m source file before distributing your code.

Run Older P-Code Files on Later Versions of MATLAB

P-code files are designed to be independent of the release in which they were created and the release in which they are used (backward and forward compatibility). New and deprecated MATLAB features can cause errors, but these errors would also appear if you used the original MATLAB source file. To fix errors of this kind in a P-code file, fix the corresponding MATLAB source file and create a new P-code file.

P-code files built using MATLAB Version 7.4 (R2007a) and earlier have a different format than those built with more recent versions of MATLAB. These older P-code files do not run in MATLAB V8.6 (R2015b) or later. Rebuild any P-code files that were built with MATLAB V7.4 or earlier using a more recent version of MATLAB, and then redistribute them as necessary.

See Also

Related Topics