Matlab m file batch encryption method and device and storage medium
By providing a Matlab m-file batch encryption method, the problems of batch encryption and directory structure consistency maintenance in the prior art are solved, efficient and reliable encryption process is realized, and real-time state feedback and automatic deletion of original files are provided.
Patent Information
- Application Number
- CN202411938127.8
- Authority / Receiving Office
- CN · China
- Patent Type
- Applications(China)
- Current Assignee / Owner
- Filing Date
- 2024-12-26
- Publication Date
- 2025-04-25
AI Technical Summary
The existing Matlab's m-file encryption method cannot meet the needs of batch encryption and directory structure consistency maintenance, resulting in incomplete encryption, path dependency failure and error handling is not intuitive.
Provides a Matlab m-file batch encryption method, which recursively traverses and collects m-file paths by specifying the target folder, calls the m2pcode function for encryption, and detects file existence, handles errors, maintains directory structure and provides real-time state feedback during the encryption process.
The batch encryption of m files in the project is realized, ensuring the consistency of the directory structure and path after encryption, enhancing error handling and feedback mechanisms, improving encryption efficiency and user experience, and automatically deleting the original m files to prevent source code leakage.
Smart Images

Figure CN119885224A_ABST
Abstract
Description
Technical Field
[0001] The present invention relates to the technical field of m-file encryption, and in particular to a Matlab m-file batch encryption method, device and storage medium. Background Art
[0002] During the design of automobiles and automobile simulation applications, Matlab is used in many simulation and calculation processes. In the Matlab development environment, m files are common source code file formats, which contain program codes written by developers. For the source code related to automobile design, it is necessary to protect these source codes from being decompiled, tampered or illegally used to achieve the purpose of confidentiality. Developers usually encrypt these m files to generate encrypted p files.
[0003] For complex Matlab projects, especially projects or Matlab applications that contain multiple files, there are usually multiple associated m files, and there may be function calls or path dependencies between them. Therefore, such projects face the following major technical problems during the encryption process: a. Batch encryption requirements: In a project, there are usually a large number of m files. Manual encryption one by one is not only inefficient, but also easy to miss files, resulting in incomplete project encryption. In addition, the encryption process one by one takes a long time and is not suitable for batch processing requirements. Therefore, batch automatic encryption has become a key requirement in the encryption process. b. Directory structure and path dependency issues: Matlab complex projects and applications usually rely on specific directory structures and file paths. If the encrypted file path changes or the directory structure is destroyed, the project may fail to find the file when it is running, affecting the normal function of the project. Therefore, keeping the encrypted directory structure consistent with the source code and ensuring the integrity of the path dependency are important technical challenges in the encryption process.
[0004] Currently, the pcode function provided by Matlab is used to encrypt m files, but the pcode function has significant deficiencies when processing complex projects and cannot meet the needs of batch encryption and directory structure consistency maintenance. Specifically, the pcode function has the following defects: a. Lack of support for complete directory structure: The pcodedirs function in pcode is used to create implicit directory structures such as private, +, and @, but the function only saves the encrypted file to the current working directory and does not fully ensure that the directory structure is consistent with the source file. For complex Matlab projects, especially those with deep directory structure dependencies, the complete directory structure of the encrypted file cannot be maintained. This will cause directory confusion and path dependency failure, thus affecting project operation. b. Limited error handling mechanism: pcode uses warning when processing unencryptable files or invalid file paths, but there is no effective logging or error management mechanism. If some files fail to encrypt in batches, the code only displays a warning, and cannot record and feedback detailed information about the encryption error, which is not intuitive enough for developers to troubleshoot errors. c. Failed p-files are not automatically deleted: During the process of generating p-files, if a compilation error occurs (for example, _pwrite execution fails), there is no mechanism to automatically delete the invalid p-files generated, which may cause invalid files to remain in the folder, thereby interfering with the normal use of the project. d. No interface or interactive feedback is provided: The code does not display the encryption progress, status or results to the user, resulting in the user being unable to grasp the encryption progress in real time or understand possible problems. Summary of the invention
[0005] In order to solve the above technical problem or at least partially solve the above technical problem, the present invention provides a Matlab m-file batch encryption method, device and storage medium.
[0006] In a first aspect, the present invention provides a Matlab m-file batch encryption method, comprising:
[0007] Specify the target folder where the m-file to be encrypted is located;
[0008] After specifying the target folder where the m-file to be encrypted is located, recursively traverse the specified target folder and its subfolders, collect the paths of all m-files in the target folder, and store the paths of all m-files in the m-file array;
[0009] Traverse the m-files and m-file paths in the m-file array, and execute for each m-file traversed:
[0010] Check whether the m-file exists in the folder pointed to by its m-file path;
[0011] If the m-file exists, call the m2pcode function to read the m-file from the folder pointed to by the m-file path, compile and encrypt the m-file into a p-file, the file name of the generated p-file is consistent with the file name of the original m-file, and move the generated p-file to the file where the original m-file is located according to the m-file path. If the m-file is encrypted and compiled into a p-file successfully, remove the original m-file;
[0012] If the m-file does not exist, then: a prompt will be given in real time indicating that the file is missing, and the missing m-file and m-file path will be recorded in the m-file missing array. If there are elements in the m-file missing array after traversing the m-files and m-file paths in the m-file array, then the missing m-file will be output.
[0013] Furthermore, a TextAreaUI component for displaying the folder path is set in the constructed encryption application. Clicking the TextAreaUI component triggers the uigetdir function to pop up a folder selection dialog box. The file selection dialog box provides all local file paths of the device. The target folder where the m file to be encrypted is located is specified through the folder selection operation, and the path of the target folder is passed to the uigetdir function. The uigetdir function displays the path of the target folder in the TextAreaUI component for displaying the folder path.
[0014] Furthermore, the m2pcode function receives an m-file path or an m-file path containing wildcards as input and normalizes the path to ensure that all m-files in the target folder can be correctly read by the m2pcode function.
[0015] Furthermore, the m2pcode function converts each m-file into an encrypted p-file based on the pcode method.
[0016] Furthermore, during the encryption process of any m file compilation, check whether the encryption compilation process is successful:
[0017] If it is unsuccessful, the encryption compilation error is handled by throwing an exception to capture the exception, record the error information and delete the p-file that encounters the exception; by throwing an exception, the exception and error encountered in the current encryption are returned, the m-file involved in the exception and error and its m-file path, the exception status is displayed in real time, and the m-file that failed to encrypt and its m-file path and exception information are recorded, the current m-file that failed to encrypt is skipped, and the subsequent m-files in the m-file array are traversed for processing;
[0018] If the m file is encrypted and compiled into a p file successfully, the original m file will be deleted and the corresponding status information will be output in real time.
[0019] Furthermore, after traversing the m-files and their m-file paths in the m-file array, if there are abnormal m-files, the recorded m-files that failed to be encrypted, their m-file paths and abnormal information are listed, and a folder link is added to the last level folder in the m-file path.
[0020] Furthermore, the execution of the methods in the m2pcode function is controlled by flags, including:
[0021] The flag of whether to perform file validity check. The m2pcode function configures multiple file validity check methods, including: a check method for whether the m file supports pcode encryption, a check method for the m file encoding format, a check method for calling the static code analysis tool of the m file to detect whether the code syntax is incorrect, and a check method for whether other m files or functions called in the m file exist and are executable. Before encryption, select the corresponding validity check method to execute according to the flag of whether to perform file validity check;
[0022] Version flag, the m2pcode function configures the pcode method that supports m-file encryption of different Matlab versions. The version flag specifies the appropriate pcode method for encryption.
[0023] Furthermore, a Select&m2pButtonUI component is set in the constructed encryption application for performing encryption operations. The Select&m2pButtonUI component is a button. Clicking the button triggers the execution of the m-file traversal encryption process. A second group of TextAreaUI components for displaying status is set in the constructed encryption application. During the entire encryption process, the status information is displayed through the second group of TextAreaUI components for displaying status.
[0024] In a second aspect, the present invention provides a Matlab m-file batch encryption device, comprising: at least one processing unit, the processing unit and the storage unit are interconnected through a bus unit, the storage unit stores a computer program, and when the computer program is executed by the processing unit, the Matlab m-file batch encryption method is implemented.
[0025] In a third aspect, the present invention provides a computer-readable storage medium, wherein the computer-readable storage medium stores a computer program, and when the computer program is executed by a processor, the Matlab m-file batch encryption method is implemented.
[0026] The above technical solution provided by the embodiment of the present invention has the following advantages compared with the prior art:
[0027] The present invention aims to realize batch encryption of Dai Anguo m files in the project and improve encryption efficiency; the present invention maintains the directory structure and path consistency of the original m files and the encrypted p files to ensure the integrity of the project's dependencies; the present invention enhances the error handling and feedback mechanism in the encryption process to ensure the reliability of the encryption operation; the present invention performs encryption status feedback in real time to improve user experience; the present invention automatically and securely deletes the original m files to prevent source code leakage.
[0028] In addition, it supports flexible encryption configuration and logo selection to meet the special needs of different projects. BRIEF DESCRIPTION OF THE DRAWINGS
[0029] The accompanying drawings, which are incorporated in and constitute a part of this specification, illustrate embodiments consistent with the invention and, together with the description, serve to explain the principles of the invention.
[0030] In order to more clearly illustrate the embodiments of the present invention or the technical solutions in the prior art, the drawings required for use in the embodiments or the description of the prior art will be briefly introduced below. Obviously, for ordinary technicians in this field, other drawings can be obtained based on these drawings without paying any creative labor.
[0031] Figure 1 A flowchart of a Matlab m-file batch encryption method provided by an embodiment of the present invention;
[0032] Figure 2 A schematic diagram of an interface of an encryption application provided by an embodiment of the present invention;
[0033] Figure 3 A schematic diagram of the batch encryption results of various m files in the project folder D:\test\general provided by an embodiment of the present invention;
[0034] Figure 4 A schematic diagram of a Matlab m-file batch encryption device provided by an embodiment of the present invention. DETAILED DESCRIPTION
[0035] In order to make the purpose, technical solution and advantages of the embodiments of the present invention clearer, the technical solution in the embodiments of the present invention will be clearly and completely described below in conjunction with the drawings in the embodiments of the present invention. Obviously, the described embodiments are part of the embodiments of the present invention, not all of the embodiments. Based on the embodiments of the present invention, all other embodiments obtained by ordinary technicians in this field without creative work are within the scope of protection of the present invention.
[0036] It should be noted that, in this article, the terms "include", "comprises" or any other variations thereof are intended to cover non-exclusive inclusion, so that a process, method, article or device including a series of elements includes not only those elements, but also other elements not explicitly listed, or also includes elements inherent to such process, method, article or device. In the absence of further restrictions, an element defined by the sentence "comprises a ..." does not exclude the existence of other identical elements in the process, method, article or device including the element.
[0037] Example 1
[0038] like Figure 1 As shown, the technology of the present invention implements a Matlab m-file batch encryption method, including:
[0039] S1, specify the target folder where the m file to be encrypted is located.
[0040] In the specific implementation process, this application develops an m-file batch encryption application based on MATLAB APP Designer. Figure 2 As shown, a TextAreaUI component for displaying the folder path is set in the constructed encryption application. Clicking the TextAreaUI component triggers the uigetdir function to pop up a folder selection dialog box. The file selection dialog box provides all local file paths of the device. The target folder where the m file to be encrypted is located is specified through the folder selection operation, and the path of the target folder is passed to the uigetdir function. The uigetdir function displays the path of the target folder in the TextAreaUI component for displaying the folder path.
[0041] S2, after specifying the target folder where the m-file to be encrypted is located, recursively traverse the specified target folder and its subfolders, collect the paths of all m-files in the target folder, and store the paths of all m-files in the m-file array.
[0042] S3, traverse the m-files and m-file paths in the m-file array, and for each m-file, check whether the m-file exists in the folder pointed to by its m-file path.
[0043] If the file exists, execute step S4: call the m2pcode function to read the m file from the folder pointed to by the m file path, compile and encrypt the m file into a p file, the file name of the generated p file is consistent with the file name of the original m file, and move the generated p file to the file where the original m file is located according to the m file path.
[0044] During the specific implementation, the m2pcode function receives an m-file path or an m-file path containing wildcards as input, and standardizes the path to ensure that all m-files in the target folder can be correctly read by the m2pcode function.
[0045] The m2pcode function converts each m-file into an encrypted p-file based on the pcode method.
[0046] In the specific implementation process, the m2pcode function supports the configuration of multiple flags, which control the execution of methods in the m2pcode function. The flags include:
[0047] The flag of whether to perform file validity check, the m2pcode function configures multiple file validity check methods, including: a method for checking whether the m file supports pcode encryption, a method for checking the encoding format of the m file, a method for checking whether the static code analysis tool of the m file is called to detect whether the code syntax is incorrect, and a method for checking whether other m files or functions called in the m file exist and are executable. Before encryption, the corresponding validity check method is selected for execution based on the flag of whether to perform file validity check.
[0048] Version flag, the m2pcode function configures the pcode method that supports m-file encryption of different Matlab versions. The version flag specifies the appropriate pcode method for encryption.
[0049] The setting of flags provides flexibility in the application of various methods in the m2pcode function.
[0050] During the encryption compilation process, check whether the encryption compilation process is successful:
[0051] If it is unsuccessful, the encryption compilation error is handled by throwing an exception, catching the exception, recording the error information and deleting the p-file that encounters the exception to avoid invalid files in the file system, and outputting the corresponding status information.
[0052] Specifically, the exception and error encountered in the current encryption are returned by throwing an exception, and the m-files involved in the exception and error and their m-file paths; the status of the exception is displayed in real time, and the m-files that failed to be encrypted, their m-file paths and exception information are recorded, the m-files that failed to be encrypted are skipped, and the subsequent m-files in the m-file array are traversed for processing.
[0053] After traversing the m-files and their m-file paths in the m-file array, if there are abnormal m-files, the recorded m-files that failed to be encrypted, their m-file paths and abnormal information are listed, and a folder link is added to the last level folder in the m-file path.
[0054] If the m file is encrypted and compiled into a p file successfully, the original m file will be deleted and the corresponding status information will be output in real time.
[0055] If the file does not exist, execute step S5: prompt the file is missing in real time, record the missing m file and the m file path in the m file missing array, if there are elements in the m file missing array after traversing the m files and the m file paths in the m file array, then output the missing m file.
[0056] In the specific implementation process, Figure 2 As shown, a Select&m2pButtonUI component is set in the constructed encryption application for performing encryption operations. The Select&m2pButtonUI component is a button. Clicking the button triggers the execution of S2 and S3. A second group of TextAreaUI components is set in the constructed encryption application for displaying the status. During the entire encryption process, the status information is displayed through the second group of TextAreaUI components for displaying the status.
[0057] During the encryption process, the encryption application automatically maintains the directory structure to ensure that the generated p-file is located in the same path as the original m-file to prevent project dependency errors caused by path changes. The encryption application supports automatic matching of nested directory structures to adapt to the path requirements of complex projects, and recursively traverses subdirectories to ensure that each m-file in the project is correctly encrypted.
[0058] In a specific implementation process, after specifying the target folder where the m file to be encrypted is located, a timestamp is obtained by calling a timestamp function as the time when the target folder where the m file to be encrypted is located is selected.
[0059] Encryption process: The encryption process is mainly completed by the m2pcode function, which supports batch processing of multiple m files and implements the encryption operation through the following steps:
[0060] d. Lack of visual feedback on encryption status: During the batch encryption process, users need to understand the real-time status and completion of encryption. Ideally, the encryption progress should be displayed through a graphical interface or text records to facilitate users to monitor and manage the entire encryption process and reduce the risk of human error.
[0061] The embodiment of the present invention relates to a visualization application for batch encryption of MATLAB m-files, and its structure and functions are as follows:
[0062] Example 2
[0063] See also Figure 4As shown, an embodiment of the present invention provides a Matlab m-file batch encryption device, including: at least one processing unit, the processing unit and the storage unit are interconnected through a bus unit, and the storage unit is a computer-readable storage medium that can be used to store software programs, computer executable programs and modules, such as the software programs, computer executable programs and modules corresponding to a Matlab m-file batch encryption method in an embodiment of the present invention. The processing unit implements the above-mentioned Matlab m-file batch encryption method by running the software programs, computer executable programs and modules stored in the storage unit, including:
[0064] Specify the target folder where the m-file to be encrypted is located;
[0065] After specifying the target folder where the m-file to be encrypted is located, recursively traverse the specified target folder and its subfolders, collect the paths of all m-files in the target folder, and store the paths of all m-files in the m-file array;
[0066] Traverse the m-files and m-file paths in the m-file array, and execute for each m-file traversed:
[0067] Check whether the m-file exists in the folder pointed to by its m-file path;
[0068] If the m-file exists, call the m2pcode function to read the m-file from the folder pointed to by the m-file path, compile and encrypt the m-file into a p-file, the file name of the generated p-file is consistent with the file name of the original m-file, and move the generated p-file to the file where the original m-file is located according to the m-file path. If the m-file is encrypted and compiled into a p-file successfully, remove the original m-file;
[0069] If the m-file does not exist, then: a prompt will be given in real time indicating that the file is missing, and the missing m-file and m-file path will be recorded in the m-file missing array. If there are elements in the m-file missing array after traversing the m-files and m-file paths in the m-file array, then the missing m-file will be output.
[0070] Of course, the computer program stored in the storage unit of a Matlab m-file batch encryption device provided by an embodiment of the present invention is not limited to the method operation described above, and can also execute related operations in a Matlab m-file batch encryption method provided by any embodiment of the present invention.
[0071] Example 3
[0072] An embodiment of the present invention provides a computer-readable storage medium, wherein the computer-readable storage medium stores a computer program. When the computer program is executed, the Matlab m-file batch encryption method is implemented, including:
[0073] Specify the target folder where the m-file to be encrypted is located;
[0074] After specifying the target folder where the m-file to be encrypted is located, recursively traverse the specified target folder and its subfolders, collect the paths of all m-files in the target folder, and store the paths of all m-files in the m-file array;
[0075] Traverse the m-files and m-file paths in the m-file array, and execute for each m-file traversed:
[0076] Check whether the m-file exists in the folder pointed to by its m-file path;
[0077] If the m-file exists, call the m2pcode function to read the m-file from the folder pointed to by the m-file path, compile and encrypt the m-file into a p-file, the file name of the generated p-file is consistent with the file name of the original m-file, and move the generated p-file to the file where the original m-file is located according to the m-file path. If the m-file is encrypted and compiled into a p-file successfully, remove the original m-file;
[0078] If the m-file does not exist, then: a prompt will be given in real time indicating that the file is missing, and the missing m-file and m-file path will be recorded in the m-file missing array. If there are elements in the m-file missing array after traversing the m-files and m-file paths in the m-file array, then the missing m-file will be output.
[0079] A computer-readable storage medium provided by an embodiment of the present invention stores a computer program which is not limited to the method operations described above, but can also execute related operations in a Matlab m-file batch encryption method provided by any embodiment of the present invention.
[0080] In the embodiments provided by the present invention, it should be understood that the disclosed structures and methods can be implemented in other ways. For example, the structural embodiments described above are only schematic. For example, the division of the units is only a logical function division. There may be other division methods in actual implementation, such as multiple units or components can be combined or integrated into another system, or some features can be ignored or not executed. Another point is that the mutual coupling or direct coupling or communication connection shown or discussed can be an indirect coupling or communication connection through some interfaces, structures or units, which can be electrical, mechanical or other forms.
[0081] The units described as separate components may or may not be physically separated, and the components shown as units may or may not be physical units, that is, they may be located in one place or distributed on multiple network units. Some or all of the units may be selected according to actual needs to achieve the purpose of the solution of this embodiment.
[0082] In addition, each functional unit in each embodiment of the present invention may be integrated into one processing unit, or each unit may exist physically separately, or two or more units may be integrated into one unit. The above-mentioned integrated unit may be implemented in the form of hardware or in the form of software functional units.
[0083] The foregoing is merely a specific embodiment of the present invention, which enables those skilled in the art to understand or implement the present invention. Various modifications to these embodiments will be apparent to those skilled in the art, and the general principles defined herein may be implemented in other embodiments without departing from the spirit or scope of the present invention. Therefore, the present invention will not be limited to the embodiments shown herein, but rather to the widest scope consistent with the principles and novel features claimed herein.
Claims
1. A Matlab m-file batch encryption method, characterized in that: include: Specify the target folder where the m-file to be encrypted is located; After specifying the target folder where the m-file to be encrypted is located, recursively traverse the specified target folder and its subfolders, collect the paths of all m-files in the target folder, and store the paths of all m-files in the m-file array; Traverse the m-files and m-file paths in the m-file array, and execute for each m-file traversed: Check whether the m-file exists in the folder pointed to by its m-file path; If the m-file exists, call the m2pcode function to read the m-file from the folder pointed to by the m-file path, compile and encrypt the m-file into a p-file, the file name of the generated p-file is consistent with the file name of the original m-file, and move the generated p-file to the file where the original m-file is located according to the m-file path. If the m-file is encrypted and compiled into a p-file successfully, remove the original m-file; If the m-file does not exist, then: a prompt will be given in real time indicating that the file is missing, and the missing m-file and m-file path will be recorded in the m-file missing array. If there are elements in the m-file missing array after traversing the m-files and m-file paths in the m-file array, then the missing m-file will be output.
2. The Matlab m-file batch encryption method according to claim 1, characterized in that: In the constructed encryption application, a TextAreaUI component is set to display the folder path. Clicking the TextAreaUI component triggers the uigetdir function to pop up a folder selection dialog box. The file selection dialog box provides all local file paths of the device. The target folder where the m file to be encrypted is located is specified through the folder selection operation, and the path of the target folder is passed to the uigetdir function. The uigetdir function displays the path of the target folder in the TextAreaUI component used to display the folder path.
3. The Matlab m-file batch encryption method according to claim 1, characterized in that: The m2pcode function receives an m-file path or an m-file path containing wildcards as input and normalizes the path to ensure that all m-files in the target folder can be correctly read by the m2pcode function.
4. The Matlab m-file batch encryption method according to claim 1, characterized in that: The m2pcode function converts each m-file into an encrypted p-file based on the pcode method.
5. The Matlab m-file batch encryption method according to claim 1, characterized in that: During the encryption process of any m file compilation, check whether the encryption compilation process is successful: If it is unsuccessful, the encryption compilation error is handled by throwing an exception to capture the exception, record the error information and delete the p-file that encounters the exception; by throwing an exception, the exception and error encountered in the current encryption are returned, the m-file involved in the exception and error and its m-file path, the exception status is displayed in real time, and the m-file that failed to encrypt and its m-file path and exception information are recorded, the current m-file that failed to encrypt is skipped, and the subsequent m-files in the m-file array are traversed for processing; If the m file is encrypted and compiled into a p file successfully, the original m file will be deleted and the corresponding status information will be output in real time.
6. The Matlab m-file batch encryption method according to claim 5, characterized in that: After traversing the m-files and their m-file paths in the m-file array, if there are abnormal m-files, the recorded m-files that failed to be encrypted, their m-file paths and abnormal information are listed, and a folder link is added to the last level folder in the m-file path.
7. The Matlab m-file batch encryption method according to claim 1, characterized in that: The execution of the methods in the m2pcode function is controlled by flags, including: The flag of whether to perform file validity check. The m2pcode function configures multiple file validity check methods, including: a check method for whether the m file supports pcode encryption, a check method for the m file encoding format, a check method for calling the static code analysis tool of the m file to detect whether the code syntax is incorrect, and a check method for whether other m files or functions called in the m file exist and are executable. Before encryption, select the corresponding validity check method to execute according to the flag of whether to perform file validity check; Version flag, the m2pcode function configures the pcode method that supports m-file encryption of different Matlab versions. The version flag specifies the appropriate pcode method for encryption.
8. The Matlab m-file batch encryption method according to claim 1, characterized in that: In the constructed encryption application, a Select&m2pButtonUI component is set for performing encryption operations. The Select&m2pButtonUI component is a button. Clicking the button triggers the execution of the m-file traversal encryption process. In the constructed encryption application, a second group of TextAreaUI components for displaying status are set. During the entire encryption process, the status information is displayed through the second group of TextAreaUI components for displaying status.
9. A Matlab m-file batch encryption device, characterized in that: include: At least one processing unit, the processing unit, the storage unit, and the acquisition unit are interconnected through a bus unit, the storage unit stores a computer program, and when the computer program is executed by the processing unit, the Matlab m-file batch encryption method as described in any one of claims 1-8 is implemented.
10. A computer-readable storage medium storing a computer program, characterized in that: When the computer program is executed by a processor, the Matlab m-file batch encryption method as described in any one of claims 1 to 8 is implemented.