Encryption method and system for subroutines and interrupt programs in PLC application programs

By encrypting subroutines and interrupt programs in PLC applications, the problem of insufficient security of existing PLC program protection methods is solved, and effective protection of core code and simplified maintenance process is realized.

CN119337341BActive Publication Date: 2025-04-18SHENZHEN HUICHEN AUTOMATION TECH CO LTD
View PDF 3 Cites 0 Cited by

Patent Information

Application Number
CN202411885769.6
Authority / Receiving Office
CN · China
Patent Type
Patents(China)
Current Assignee / Owner
Filing Date
2024-12-20
Publication Date
2025-04-18
Estimated Expiration
2044-12-20

AI Technical Summary

Technical Problem

The existing PLC program protection methods are insufficiently secure and are easily cracked, and cannot effectively prevent the program from being illegally copied and used, especially the protection measures for critical subroutines or interrupted programs are insufficient.

Method used

By creating a program organization unit, the program code that needs to be encrypted is written in a subroutine or interrupt service program, and the encryption library is called on its first line to realize exclusive encryption of the program code. The encrypted program is stored in the PLC's flash memory and is updated only by re-downloading after clearing.

Benefits of technology

It effectively enhances the security of the program, prevents core code from being stolen or tampered, simplifies the maintenance and update of the program, and improves the execution efficiency of the program.

✦ Generated by Eureka AI based on patent content.

Smart Images

  • Figure CN119337341B_ABST
    Figure CN119337341B_ABST
Patent Text Reader

Abstract

The present invention provides an encryption method and system for subroutines and interrupt programs in a PLC application, and relates to the technical field of PLC, including creating a program organization unit including a main program, a subroutine and an interrupt service program, writing a program code to be encrypted into a subroutine or an interrupt service program, and calling an encryption library in the first line. The PLC identifies an encryption library call instruction, encrypts the program and allocates a dedicated storage area address, and the encrypted program is permanently stored in a flash memory. After the encryption is completed, only an empty subroutine or interrupt service program framework is retained in the programming software, and the main program implements device control by accessing the dedicated storage area address of the encryption program. When uploading a program, only an empty framework is uploaded, and the encryption code is shielded. The present invention effectively prevents the PLC program from being illegally copied and tampered with by encrypting and hiding the core program code, thereby improving the security of the program.
Need to check novelty before this filing date? Find Prior Art

Description

Technical Field

[0001] The invention relates to PLC technology, and in particular to an encryption method and system for subroutines and interrupt programs in PLC application programs. Background Art

[0002] Programmable logic controllers (PLCs) are widely used in the field of industrial automation control. At their core are user-written applications that control various production processes. With the increasing popularity of PLC applications, the demand for the protection of program intellectual property rights is also increasing. Traditional PLC program protection methods mainly include setting passwords, limiting uploads and downloads, etc., but these methods have limited security and are easy to be cracked. For example, some professional cracking tools can bypass password protection and illegally obtain PLC programs, resulting in intellectual property leakage and economic losses.

[0003] The existing technology mainly has the following defects and shortcomings:

[0004] 1. Traditional password protection mechanisms have low security, are easily cracked, and cannot effectively prevent programs from being illegally copied and used.

[0005] 2. Some PLCs allow users to read program codes through specific operations or software tools. Even if password protection is set, there is a risk of being cracked.

[0006] 3. For critical subroutines or interrupt programs, existing protection measures usually cannot provide targeted encryption and protection, making them easy targets for attackers. Summary of the invention

[0007] The embodiment of the present invention provides an encryption method and system for subroutines and interrupt programs in a PLC application program, which can solve the problems in the prior art.

[0008] According to a first aspect of the embodiments of the present invention,

[0009] Provides encryption methods for subroutines and interrupt programs in PLC application programs, including:

[0010] Creating a program organization unit, the program organization unit includes a main program, a subroutine and an interrupt service program, writing a program code that needs to be encrypted and protected in the subroutine or the interrupt service program, and calling an encryption library in the first line of the program code of the subroutine or the interrupt service program, the encryption library is used to implement exclusive encryption of the program code;

[0011] The program code containing the calling instruction of the encryption library is downloaded to the programmable logic controller, the programmable logic controller recognizes the calling instruction of the encryption library and encrypts the subroutine or the interrupt service program, and allocates a dedicated storage area address to the subroutine or the interrupt service program during the encryption process; the encrypted subroutine or interrupt service program is permanently stored in the flash memory of the programmable logic controller, and the encrypted program in the flash memory can only be updated by re-downloading after clearing the operation;

[0012] After completing the encrypted storage, the program code content of the subroutine or interrupt service program in the programming software is completely deleted, and only the program framework of the empty subroutine or interrupt service program is retained, and the program framework is used to establish a call association with the main program; a call link with the encrypted subroutine or interrupt service program is established in the main program, and the control of the device is achieved by accessing the dedicated storage area address of the encrypted subroutine or interrupt service program, wherein the storage area address includes a current value storage area, a set value storage area, and an output value storage area; when performing a program upload operation on the programmable logic controller, the programmable logic controller only uploads the empty subroutine or interrupt service program framework to the programming software, and the encrypted program code content is shielded during the upload process.

[0013] The program code to be encrypted is written in the subroutine or the interrupt service program, and the encryption library is called in the first line of the program code of the subroutine or the interrupt service program. The encryption library is used to implement exclusive encryption of the program code, including:

[0014] The subroutine has an independent running space and a local variable area, and the interrupt service program has an independent interrupt vector table and execution priority; an input parameter verification mechanism is established in the subroutine or interrupt service program, and the input parameter verification mechanism includes a data type checking module, a value range verification module and a logic relationship verification module to verify the legality of the input data;

[0015] Inserting an encryption library call instruction at the first line of the subroutine or interrupt service program, wherein the encryption library call instruction includes an encryption library identifier, version information and call parameters, and the encryption library call instruction is used to start encryption protection;

[0016] An encryption process is started based on a layered architecture of the encryption library, wherein the layered architecture includes a basic encryption layer for implementing an encryption algorithm, a dynamic protection layer for generating dynamic keys, and an interface adaptation layer for processing hardware compatibility; characteristic information of the subroutine or interrupt service program is extracted, including instruction sequence characteristics, data structure characteristics, and control flow characteristics, and the characteristic information is combined with a hardware binding identifier to generate an encryption key; the subroutine or interrupt service program is encrypted using the encryption key, and code obfuscation is performed through instruction reordering, false branch insertion, and control flow transformation to generate an encrypted program.

[0017] The programmable logic controller identifies the call instruction of the encryption library and performs encryption processing on the subroutine or the interrupt service program, and allocates a dedicated storage area address for the subroutine or the interrupt service program during the encryption process, including:

[0018] After receiving the program download instruction, the instruction parsing module performs feature code matching on the encryption library call instruction in the first line of the program code, compares the encryption library call instruction with the preset encryption library call instruction feature code, and checks the version number of the encryption library, obtains the permission status and integrity check value;

[0019] Based on the feature code matching and verification results, preprocessing the source code of the subroutine or interrupt service program, dividing the source code into code segments, deleting redundant information and reordering instructions to generate preprocessing code;

[0020] Performing multiple encryption transformations on the preprocessed code, including initialization encryption based on hardware feature codes, secondary encryption based on dynamic key generation based on timestamps, and code obfuscation processing based on instruction set features to form encrypted program code;

[0021] Divide an independent physically isolated address space for the encrypted program code, establish a dynamic mapping table from virtual address to physical address, divide the storage area into an instruction area, a data area and a temporary area, and generate a corresponding address mapping table and access permission table;

[0022] The address mapping table and the access permission table are encrypted, and the encrypted address mapping table and the access permission table are stored in the security area of ​​the programmable logic controller. The address mapping table and the access permission table are dynamically decrypted and used only when the program is running.

[0023] The method further comprises:

[0024] Establishing a data access channel, setting an input buffer and an output buffer in the data access channel, wherein the input buffer performs security verification on input data and then passes it to the encryption program code, and the output buffer performs filtering and formatting on output data of the encryption program code;

[0025] A runtime protection mechanism is established to track the execution status and resource usage of the encrypted program code through a real-time monitoring module. When abnormal access or illegal operation is detected, the exception handling module is triggered to perform security protection operations.

[0026] The program code content of the subroutine or interrupt service program in the programming software is completely deleted, and only the program framework of the empty subroutine or interrupt service program is retained, and the program framework is used to establish a call association with the main program, including:

[0027] The input parameter type, parameter quantity, parameter passing method and return value type information of the subroutine or interrupt service program are obtained through interface layer scanning, local variable declaration, static variable definition and data structure description information are obtained through variable layer scanning, and program basic block structure, branch jump point and loop entry point information are obtained through control flow scanning; the interface layer information, variable layer information and control flow information are respectively constructed into interface feature vectors, variable feature vectors and control flow feature vectors, the feature vectors contain variable scope, life cycle and access permission attributes, and the feature vectors are composed into a framework feature description table with a multi-dimensional matrix structure;

[0028] A reverse recursive method is used to delete the execution code in the subroutine or interrupt service program from the inner code block layer by layer outward, a deletion mark bit is set at the deletion position, and a position mapping relationship is established between the deletion mark bit and the framework feature description table; a triple verification is performed on the deletion operation of the execution code, including basic structure verification, framework feature integrity verification and call relationship maintenance verification, and the deletion operation is confirmed after the verification passes;

[0029] A virtual call link table is established to record the call point information in the main program, wherein the call point information includes the call location, parameter passing rules and return value processing method, and a call identifier is assigned to each call point; a call descriptor including the call parameter type, passing method and expected return value type is generated at the call point of the main program, and an interface descriptor including interface information is generated in the program framework, and a call association is established by comparing the call descriptor and the interface descriptor.

[0030] The device is controlled by accessing the dedicated storage area address of the encrypted subroutine or interrupt service program, wherein the storage area address includes a current value storage area, a set value storage area and an output value storage area including:

[0031] The storage space is divided into a current value storage area, a set value storage area and an output value storage area, wherein the current value storage area stores device status information, sensor data and operation counter data, the set value storage area stores temperature setting values, pressure limit values ​​and time parameters, and the output value storage area stores actuator control signals and output status information;

[0032] Establishing a mapping table from physical address space to virtual address, adding a random offset to the mapping table to form a dynamic address mapping, and configuring independent address mapping tables for the current value storage area, the set value storage area, and the output value storage area respectively;

[0033] The control instruction is written into the instruction buffer for legality verification and parameter checking. After the verification is passed, the control instruction is transferred to the execution buffer to trigger the control operation; a priority scheduling mechanism is established for storage area access, real-time control data access is set to the highest priority, device status information access is set to the second priority, and configuration parameter access is set to the lowest priority.

[0034] The method further comprises:

[0035] When the programmable logic controller is monitored online, the encrypted subroutine or interrupt service program participates in the normal operation of the programmable logic controller and maintains a continuous operation state, and the main program reads the operation data of the encrypted program in real time; when the encrypted subroutine or interrupt service program needs to be modified, the program is updated by modifying the original unencrypted program backed up in advance and re-executing the encryption storage process.

[0036] A second aspect of an embodiment of the present invention provides an encryption system for subroutines and interrupt programs in a PLC application program, comprising:

[0037] The first unit is used to create a program organization unit, the program organization unit includes a main program, a subroutine and an interrupt service program, write the program code that needs to be encrypted and protected in the subroutine or the interrupt service program, and call an encryption library in the first line of the program code of the subroutine or the interrupt service program, the encryption library is used to realize exclusive encryption of the program code;

[0038] The second unit is used to download the program code containing the calling instruction of the encryption library into the programmable logic controller, the programmable logic controller recognizes the calling instruction of the encryption library and encrypts the subroutine or the interrupt service program, and allocates a dedicated storage area address for the subroutine or the interrupt service program during the encryption process; the encrypted subroutine or interrupt service program is permanently stored in the flash memory of the programmable logic controller, and the encrypted program in the flash memory can only be updated by re-downloading after clearing the operation;

[0039] The third unit is used to completely delete the program code content of the subroutine or interrupt service program in the programming software after completing the encrypted storage, and only retain the program framework of the empty subroutine or interrupt service program, and the program framework is used to establish a call association with the main program; establish a call link with the encrypted subroutine or interrupt service program in the main program, and control the device by accessing the dedicated storage area address of the encrypted subroutine or interrupt service program, wherein the storage area address includes a current value storage area, a set value storage area and an output value storage area; when performing a program upload operation on the programmable logic controller, the programmable logic controller only uploads the empty subroutine or interrupt service program framework to the programming software, and the encrypted program code content is shielded during the upload process.

[0040] According to a third aspect of the embodiments of the present invention,

[0041] An electronic device is provided, comprising:

[0042] processor;

[0043] a memory for storing processor-executable instructions;

[0044] The processor is configured to call the instructions stored in the memory to execute the aforementioned method.

[0045] A fourth aspect of the embodiments of the present invention is:

[0046] A computer-readable storage medium is provided, on which computer program instructions are stored. When the computer program instructions are executed by a processor, the aforementioned method is implemented.

[0047] The beneficial effects of this application are as follows:

[0048] Enhance the security of the program and effectively prevent the core code from being stolen or tampered with. Simplify the maintenance and update of the program. You only need to update the encryption library to complete the program upgrade without re-downloading the entire program. Improve the execution efficiency of the program. The encrypted program is stored in a dedicated storage area, which has a faster access speed, thereby improving the control efficiency of the device. BRIEF DESCRIPTION OF THE DRAWINGS

[0049] Figure 1 A schematic flow chart of an encryption method for subroutines and interrupt programs in a PLC application program according to an embodiment of the present invention;

[0050] Figure 2 A schematic diagram of encrypting a program block PIDC according to an embodiment of the present invention;

[0051] Figure 3A schematic diagram of the storage area address in MAIN calling the PIDC encryption subroutine in an embodiment of the present invention;

[0052] Figure 4 Schematic diagram of the MAIN program of an embodiment of the present invention;

[0053] Figure 5 The present invention is a schematic diagram of the structure of the encryption system of the subroutine and interrupt program in the PLC application program according to the embodiment of the present invention. DETAILED DESCRIPTION

[0054] 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 only 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.

[0055] The technical solution of the present invention is described in detail with specific embodiments below. The following specific embodiments can be combined with each other, and the same or similar concepts or processes may not be described in detail in some embodiments.

[0056] Figure 1 FIG. 1 is a flow chart of a method for encrypting subroutines and interrupt programs in a PLC application program according to an embodiment of the present invention. Figure 1 As shown, the method includes:

[0057] S101. Create a program organization unit, the program organization unit includes a main program, a subroutine and an interrupt service program, write the program code that needs to be encrypted and protected in the subroutine or the interrupt service program, and call the encryption library in the first line of the program code of the subroutine or the interrupt service program, the encryption library is used to implement exclusive encryption of the program code;

[0058] S102. Downloading the program code containing the call instruction of the encryption library into the programmable logic controller, the programmable logic controller recognizes the call instruction of the encryption library and encrypts the subroutine or the interrupt service program, and allocates a dedicated storage area address for the subroutine or the interrupt service program during the encryption process; permanently storing the encrypted subroutine or interrupt service program in the flash memory of the programmable logic controller, and the encryption program in the flash memory can only be updated by re-downloading after clearing the operation;

[0059] S103. After completing the encrypted storage, the program code content of the subroutine or interrupt service program in the programming software is completely deleted, and only the program framework of the empty subroutine or interrupt service program is retained, and the program framework is used to establish a calling association with the main program; a calling link with the encrypted subroutine or interrupt service program is established in the main program, and the control of the device is achieved by accessing the dedicated storage area address of the encrypted subroutine or interrupt service program, wherein the storage area address includes a current value storage area, a set value storage area, and an output value storage area; when performing a program upload operation on the programmable logic controller, the programmable logic controller only uploads the empty subroutine or interrupt service program framework to the programming software, and the encrypted program code content is shielded during the upload process.

[0060] In an optional implementation, the program code that needs to be encrypted is written in the subroutine or the interrupt service program, and the encryption library is called in the first line of the program code of the subroutine or the interrupt service program. The encryption library is used to implement exclusive encryption of the program code, including:

[0061] The subroutine has an independent running space and a local variable area, and the interrupt service program has an independent interrupt vector table and execution priority; an input parameter verification mechanism is established in the subroutine or interrupt service program, and the input parameter verification mechanism includes a data type checking module, a value range verification module and a logic relationship verification module to verify the legality of the input data;

[0062] Inserting an encryption library call instruction at the first line of the subroutine or interrupt service program, wherein the encryption library call instruction includes an encryption library identifier, version information and call parameters, and the encryption library call instruction is used to start encryption protection;

[0063] An encryption process is started based on a layered architecture of the encryption library, wherein the layered architecture includes a basic encryption layer for implementing an encryption algorithm, a dynamic protection layer for generating dynamic keys, and an interface adaptation layer for processing hardware compatibility; characteristic information of the subroutine or interrupt service program is extracted, including instruction sequence characteristics, data structure characteristics, and control flow characteristics, and the characteristic information is combined with a hardware binding identifier to generate an encryption key; the subroutine or interrupt service program is encrypted using the encryption key, and code obfuscation is performed through instruction reordering, false branch insertion, and control flow transformation to generate an encrypted program.

[0064] In order to enhance the code security of subroutines or interrupt service routines and prevent reverse engineering and code tampering, a code protection method based on encryption library is proposed. This method encrypts the specified code block exclusively and binds it to the hardware, which increases the difficulty of cracking.

[0065] First, you need to select the subroutine or interrupt service routine that needs to be encrypted. These code blocks have independent running spaces, such as a subroutine has an independent local variable area, and an interrupt service routine has an independent interrupt vector table and execution priority. For example, a subroutine that calculates a checksum or an interrupt service routine that handles serial port communication.

[0066] Next, establish an input parameter verification mechanism in the selected subroutine or interrupt service program. The mechanism consists of three modules: data type checking module, value range verification module, and logic relationship verification module. The data type checking module verifies whether the data type of the input parameter is correct. For example, if the parameter should be an integer, then check whether it is really an integer. The value range verification module checks whether the value of the input parameter is within the allowed range. For example, if the parameter represents a temperature value, then check whether it is within a reasonable temperature range. The logic relationship verification module checks whether the logic relationship between multiple input parameters is correct. For example, if one parameter represents length and the other parameter represents width, then check whether the length is greater than the width. For example, a subroutine that calculates area requires two input parameters: length and width. The data type checking module checks whether these two parameters are both floating point numbers. The value range verification module checks whether both length and width are greater than zero. The logic relationship verification module does not need to be used in this example.

[0067] Then, insert the encryption library call instruction in the first line of the subroutine or interrupt service routine. The instruction contains the encryption library identifier (such as "EncryptLib"), version information (such as "v1.0"), and call parameters (such as a pointer to the code segment to be encrypted). The purpose of this instruction is to start the encryption protection process. For example, insert the instruction `EncryptLib v1.0 start_addressend_address` in the first line of the subroutine, where `start_address` and `end_address` point to the starting address and end address of the code segment to be encrypted, respectively.

[0068] The encryption library adopts a layered architecture, including a basic encryption layer, a dynamic protection layer, and an interface adaptation layer. The basic encryption layer implements specific encryption algorithms, such as AES or RSA algorithms. The dynamic protection layer generates dynamic keys, such as generating keys based on hardware information and timestamps. The interface adaptation layer handles hardware compatibility issues to ensure that the encryption library can run on different hardware platforms. After the encryption process is started, the feature information of the subroutine or interrupt service program is first extracted, such as instruction sequence features (such as the frequency of occurrence of specific instructions), data structure features (such as variable type and size), and control flow features (such as the number of branches and loops). These feature information are then combined with hardware binding identifiers (such as CPU serial number or MAC address) to generate encryption keys.

[0069] Finally, the subroutine or interrupt service routine is encrypted using the generated encryption key. The encryption process uses a variety of code obfuscation techniques, such as instruction reordering (e.g., disrupting the execution order of instructions), false branch insertion (e.g., adding some branches that will never be executed), and control flow transformation (e.g., converting loops into equivalent recursion). These obfuscation techniques make the code more difficult to reverse analyze. For example, a simple addition instruction `a = b + c` can be converted to `temp = b; a = temp + c;`, or an `if (false) { ...}` statement that will never be executed can be inserted.

[0070] The beneficial effects of this method are reflected in three aspects:

[0071] First, enhance code security: By encrypting and obfuscating key code segments, reverse engineering and code tampering are effectively prevented.

[0072] Second, it increases the difficulty of cracking: the dynamic key generation mechanism and hardware binding technology make cracking more difficult. Even if the attacker obtains the encrypted code, it is difficult to decrypt and run it without the corresponding hardware.

[0073] Third, it is flexible and easy to use: the layered architecture and interface adaptation layer of the encryption library make this method easy to apply to different hardware platforms and software systems without making extensive modifications to the existing code.

[0074] In an optional implementation, the programmable logic controller identifies the call instruction of the encryption library and performs encryption processing on the subroutine or the interrupt service program, and at the same time allocates a dedicated storage area address to the subroutine or the interrupt service program during the encryption process, including:

[0075] After receiving the program download instruction, the instruction parsing module performs feature code matching on the encryption library call instruction in the first line of the program code, compares the encryption library call instruction with the preset encryption library call instruction feature code, and checks the version number of the encryption library, obtains the permission status and integrity check value;

[0076] Based on the feature code matching and verification results, preprocessing the source code of the subroutine or interrupt service program, dividing the source code into code segments, deleting redundant information and reordering instructions to generate preprocessing code;

[0077] Performing multiple encryption transformations on the preprocessed code, including initialization encryption based on hardware feature codes, secondary encryption based on dynamic key generation based on timestamps, and code obfuscation processing based on instruction set features to form encrypted program code;

[0078] Divide an independent physically isolated address space for the encrypted program code, establish a dynamic mapping table from virtual address to physical address, divide the storage area into an instruction area, a data area and a temporary area, and generate a corresponding address mapping table and access permission table;

[0079] The address mapping table and the access permission table are encrypted, and the encrypted address mapping table and the access permission table are stored in the security area of ​​the programmable logic controller. The address mapping table and the access permission table are dynamically decrypted and used only when the program is running.

[0080] The programmable logic controller (PLC) encryption library call and subroutine encryption method is used to enhance the security of the PLC program and prevent unauthorized access and modification. The core of this method is to identify the call instructions of the encryption library, encrypt the specified subroutine or interrupt service program, and allocate a dedicated storage area address to achieve code isolation protection.

[0081] First, the PLC receives the user program download instruction. The instruction parsing module starts working and scans the first line of the program code to find the encryption library call instruction. For example, the first line of the program code may contain the instruction "CALL ENCRYPT_LIB", where "ENCRYPT_LIB" is the name of the encryption library. The instruction parsing module compares this code with the preset encryption library call instruction signature. The preset signature code can be "CALL LIB", " represents a wildcard character, which is used to match various encryption library names. At the same time, the module also checks the version number (e.g. V1.0), permission status (e.g. permission obtained), and integrity check value (e.g. CRC32 check value 0xABCD1234) of the encryption library to ensure that the encryption library used is legal and complete.

[0082] Next, if the signature matches successfully and passes the verification, preprocessing is performed. Assume that the subroutine is named "SUBROUTINE_1" and contains multiple instructions, such as reading input signals, performing logical operations, and outputting control signals. The preprocessing process will divide the code segment of "SUBROUTINE_1" into blocks, such as input processing blocks, logical processing blocks, and output processing blocks according to functions. Then, redundant information, such as comments and blank lines, is deleted. Finally, the instructions are reordered, such as disrupting the order of instructions that were originally executed in sequence, increasing the difficulty of reverse analysis. Finally, the preprocessing code is generated.

[0083] Then, the preprocessed code is subjected to multiple encryption transformations. First, the encryption is initialized according to the hardware feature code of the PLC (such as the CPU serial number 1234567890), for example, the preprocessed code is encrypted once using the AES encryption algorithm. After that, a dynamic key is generated based on the current timestamp (such as 20240727120000), and the encrypted code is encrypted again to further enhance security. Finally, code obfuscation is performed based on the instruction set characteristics of the PLC (such as the instruction set supported by a specific model of PLC), such as replacing certain instructions with instructions that are functionally equivalent but express different functions, thereby increasing the complexity of the code. Finally, the encrypted program code is generated.

[0084] Subsequently, the storage area address is assigned to the encrypted program code. An independent physically isolated address space is divided in the storage area of ​​the PLC, for example, 0x1000-0x1FFF is assigned to "SUBROUTINE_1". A dynamic mapping table of virtual addresses to physical addresses is established, for example, virtual address 0x0000 is mapped to physical address 0x1000, virtual address 0x0001 is mapped to physical address 0x1001, and so on. The storage area is divided into instruction area, data area, and temporary area, and the corresponding address mapping table and access permission table are generated. For example, the instruction area is executable but not writable, the data area is readable and writable but not executable, and the temporary area is readable, writable, and executable.

[0085] Finally, the address mapping table and access permission table are encrypted, for example, using the RSA encryption algorithm. The encrypted address mapping table and access permission table are stored in a secure area of ​​the PLC, such as a specific area of ​​the EEPROM or Flash. These tables are only dynamically decrypted and used when the program is running. When the PLC executes "SUBROUTINE_1", it first decrypts the address mapping table and access permission table, then accesses the corresponding physical address according to the virtual address, and executes the encrypted program code.

[0086] The beneficial effects of this method are reflected in three aspects:

[0087] Enhanced security: Multiple encryption and code obfuscation effectively prevent unauthorized access and modification of PLC programs, protecting intellectual property and control logic.

[0088] Improve reliability: Independent physical isolation address space and access permission control can avoid program operation errors and data corruption, and improve system stability.

[0089] Simplified management: Automatic recognition and processing of encryption library call instructions simplifies the encryption management process of PLC programs and lowers the usage threshold.

[0090] In an optional implementation, the method further includes:

[0091] Establishing a data access channel, setting an input buffer and an output buffer in the data access channel, wherein the input buffer performs security verification on input data and then passes it to the encryption program code, and the output buffer performs filtering and formatting on output data of the encryption program code;

[0092] A runtime protection mechanism is established to track the execution status and resource usage of the encrypted program code through a real-time monitoring module. When abnormal access or illegal operation is detected, the exception handling module is triggered to perform security protection operations.

[0093] In order to protect the security of sensitive data during processing, a runtime protection method for encrypted program code is provided, and its specific implementation is as follows:

[0094] First, establish a data access channel. The core of this channel is to set up an input buffer and an output buffer to isolate the direct interaction between external data and the encryption program code. The input buffer is responsible for receiving external input data and performing security checks before passing the data to the encryption program code. For example, the input data can be format verified, length verified, and special character filtered to prevent malicious data or erroneous data from entering the encryption program code and causing potential security risks. Assuming the input data is "123456;DROP TABLE users;", the input buffer will identify ";DROP TABLEusers;" as an SQL injection attack statement and filter it out, passing only "123456" to the encryption program code. The output buffer is responsible for filtering and formatting the output data of the encryption program code. For example, the output data can be encoded, desensitized, and redundant spaces can be removed to ensure the security, consistency, and readability of the output data. Assuming the output data of the encryption program code is "user: John Doe; password: P@$$wOrd", the output buffer can desensitize the password part and output "user:John Doe; password: ".

[0095] Next, a runtime protection mechanism is established. The core of this mechanism is the real-time monitoring module and the exception handling module. The real-time monitoring module continuously tracks the execution status and resource usage of the encrypted program code, such as CPU occupancy, memory consumption, file access, etc. It records various indicators of the encrypted program code and compares them with the preset security threshold. For example, the CPU occupancy threshold is set to 80%. If the CPU occupancy of the monitored encrypted program code exceeds the threshold, it is considered that there may be an exception and the information is passed to the exception handling module. The exception handling module performs corresponding security protection operations according to the preset security policy. For example, the execution of the encrypted program code can be immediately interrupted, the exception log can be recorded, an alarm message can be sent to the administrator, and even the system self-destruction program can be started to prevent sensitive data leakage. Assuming that the monitoring module detects that the encrypted program code attempts to access the memory area that does not have permission, the exception handling module will immediately terminate the operation of the encrypted program code and record the illegal access in the security log.

[0096] The beneficial effects of this method can be summarized in the following three aspects:

[0097] 1. Enhanced data security: Through security verification of the input buffer and filtering of the output buffer, it can effectively prevent malicious data or illegal operations from attacking the encryption program code, and ensure the confidentiality and integrity of the data.

[0098] 2. Improve system stability: By real-time monitoring of the running status of the encryption program code, potential abnormal situations can be discovered and handled in a timely manner to avoid system crashes or service interruptions, thereby improving system stability and reliability.

[0099] 3. Simplify security management: Preset security policies and automated security protection operations can reduce manual intervention, simplify security management processes, and improve security management efficiency.

[0100] In an optional implementation, the program code content of the subroutine or interrupt service program in the programming software is completely deleted, and only the program framework of the empty subroutine or interrupt service program is retained, and the program framework is used to establish a call association with the main program, including:

[0101] The input parameter type, parameter quantity, parameter passing method and return value type information of the subroutine or interrupt service program are obtained through interface layer scanning, local variable declaration, static variable definition and data structure description information are obtained through variable layer scanning, and program basic block structure, branch jump point and loop entry point information are obtained through control flow scanning; the interface layer information, variable layer information and control flow information are respectively constructed into interface feature vectors, variable feature vectors and control flow feature vectors, the feature vectors contain variable scope, life cycle and access permission attributes, and the feature vectors are composed into a framework feature description table with a multi-dimensional matrix structure;

[0102] A reverse recursive method is used to delete the execution code in the subroutine or interrupt service program from the inner code block layer by layer outward, a deletion mark bit is set at the deletion position, and a position mapping relationship is established between the deletion mark bit and the framework feature description table; a triple verification is performed on the deletion operation of the execution code, including basic structure verification, framework feature integrity verification and call relationship maintenance verification, and the deletion operation is confirmed after the verification passes;

[0103] A virtual call link table is established to record the call point information in the main program, wherein the call point information includes the call location, parameter passing rules and return value processing method, and a call identifier is assigned to each call point; a call descriptor including the call parameter type, passing method and expected return value type is generated at the call point of the main program, and an interface descriptor including interface information is generated in the program framework, and a call association is established by comparing the call descriptor and the interface descriptor.

[0104] The program code slimming method is used to slim down the subroutines or interrupt service routines in the program, retaining only the program framework and its call association with the main program, thereby reducing the code volume and improving code security.

[0105] First, extract the program framework information. Through static analysis of the subroutine or interrupt service program, extract the interface layer, variable layer and control flow information. The interface layer information includes the input parameter type, the number of parameters, the parameter passing method (such as value passing, reference passing or pointer passing) and the return value type. The variable layer information includes local variable declaration (such as int a;), static variable definition (such as static int b = 0;) and data structure description (such as struct data{int x; int y;}). The control flow information includes the program basic block structure, branch jump points (such as if-else statements, switch statements) and loop entry points (such as for loops, while loops). For example, if the input parameters of a subroutine are two integers and the return value is a floating point number, the interface layer information can be expressed as (int, int) ->float. If an integer variable i and a floating point variable f are defined inside the subroutine, the variable layer information can be expressed as {inti; float f;}. If the subroutine contains an if-else branch structure, the control flow information can be represented as {block1, block2}, where block1 and block2 represent if and else code blocks, respectively. The extracted interface layer, variable layer, and control flow information are constructed into interface feature descriptions, variable feature descriptions, and control flow feature descriptions, respectively, and these feature descriptions are combined into a framework feature description table. The framework feature description table contains the scope (e.g., global variables, local variables), life cycle (e.g., static variables, dynamic variables), and access rights (e.g., public, private, protected) attributes of the variables.

[0106] Next, program code is deleted and marked. Using a reverse recursive method, starting from the innermost code block, the execution code in the subroutine or interrupt service program is deleted layer by layer, and the deletion mark bit is set at the deletion position. For example, if a subroutine contains a nested if-else structure, the code in the innermost if-else structure is deleted first, and then deleted layer by layer. A position mapping relationship is established between the deletion mark bit and the framework feature description table, and the code information corresponding to each deletion position is recorded. For example, if the code from line 10 to line 20 is deleted, the deletion mark bit is recorded as 1 in the framework feature description table, and a mapping relationship is established between it and the code information from line 10 to line 20. The deletion operation of the execution code is triple-verified: basic structure verification to ensure that the deletion operation does not destroy the basic structure of the program, such as not deleting key syntax elements such as brackets and semicolons; framework feature integrity verification to ensure that the deletion operation does not affect the integrity of the framework feature description table, such as not deleting key information such as variable declarations and function definitions; call relationship maintenance verification to ensure that the deletion operation does not destroy the call relationship of the program, such as not deleting the function name and parameters in the function call statement. Confirm the deletion operation after the verification is passed.

[0107] Finally, establish call association. Create a virtual call link table to record the call point information in the main program, including the call location (e.g., line number), parameter passing rules (e.g., value passing, reference passing), and return value processing methods (e.g., assigning the return value to a variable). Assign a unique call identifier to each call point. For example, if the main program calls subroutine sub1 at line 100, the call location is recorded as 100 in the virtual call link table, and the call identifier is assigned as call_1. Generate a call descriptor at the call point of the main program, including the call parameter type, passing method, and expected return value type. Generate an interface descriptor in the program framework, including interface information. For example, if the main program passes two integer parameters when calling subroutine sub1 and expects a floating point number to be returned, the call descriptor can be expressed as (int, int)->float. If the interface information of subroutine sub1 is also (int, int) ->float, the interface descriptor is also (int, int) ->float. Establish call association by comparing the call descriptor and the interface descriptor. If the call descriptor and the interface descriptor match, a call association is established and the call identifier is associated with the program frame.

[0108] Beneficial effects:

[0109] 1. Reduce code size: By deleting the execution code in the subroutines and interrupt service routines and retaining only the program framework, the code size can be significantly reduced, thereby saving storage space and transmission bandwidth.

[0110] 2. Improve code security: Deleting execution code can effectively prevent reverse engineering and code analysis, improve code security, and protect intellectual property rights.

[0111] 3. Simplify code maintenance: The retained program framework clearly displays the structure and calling relationship of the program, which facilitates code maintenance and understanding and reduces maintenance costs.

[0112] In an optional implementation, the device is controlled by accessing the dedicated storage area address of the encrypted subroutine or interrupt service program, wherein the storage area address includes a current value storage area, a set value storage area, and an output value storage area including:

[0113] The storage space is divided into a current value storage area, a set value storage area and an output value storage area, wherein the current value storage area stores device status information, sensor data and operation counter data, the set value storage area stores temperature setting values, pressure limit values ​​and time parameters, and the output value storage area stores actuator control signals and output status information;

[0114] Establishing a mapping table from physical address space to virtual address, adding a random offset to the mapping table to form a dynamic address mapping, and configuring independent address mapping tables for the current value storage area, the set value storage area, and the output value storage area respectively;

[0115] The control instruction is written into the instruction buffer for legality verification and parameter checking. After the verification is passed, the control instruction is transferred to the execution buffer to trigger the control operation; a priority scheduling mechanism is established for storage area access, real-time control data access is set to the highest priority, device status information access is set to the second priority, and configuration parameter access is set to the lowest priority.

[0116] The device control method embodiment realizes the control of the device by accessing the dedicated storage area address of the encrypted subroutine or interrupt service program, and the storage area address includes a current value storage area, a set value storage area and an output value storage area.

[0117] First, the storage space of the device is logically divided into three independent areas: the current value storage area, the set value storage area, and the output value storage area. The current value storage area is used to store the real-time status information of the device, such as the temperature sensor reading of 25 degrees Celsius, the pressure sensor reading of 1.5MPa, and the device operation counter value of 1200 times. The set value storage area is used to store the control parameters preset by the user, such as the temperature setting value of 30 degrees Celsius, the pressure limit value of 2.0MPa, and the timer parameter of 60 seconds. The output value storage area is used to store the execution results of the control instructions and the output status information, such as the actuator control signal is "on" and the output status is "normal".

[0118] Next, a mapping table from physical address space to virtual address is established. In order to enhance security, a random offset is introduced into the mapping table to form a dynamic address mapping to prevent unauthorized access. For example, if the physical address of the current value storage area is 0x1000 and the random offset is 0x0500, its virtual address is 0x1500. Similarly, independent address mapping tables and random offsets are configured for the set value storage area and the output value storage area, for example, the set value storage area has a physical address of 0x2000, an offset of 0x0200, and a virtual address of 0x2200; the output value storage area has a physical address of 0x3000, an offset of 0x0700, and a virtual address of 0x3700. In this way, even if an attacker obtains part of the address information, it is difficult to infer the addresses of other storage areas.

[0119] Then, when the device needs to be controlled, the control instruction is first written into the instruction buffer. The instructions in the instruction buffer are verified for legitimacy and parameter checks. For example, check whether the source of the instruction is credible and whether the parameters are within the allowed range. If the verification passes, the control instruction is transferred to the execution buffer to trigger the corresponding control operation. For example, a control instruction sets the temperature to 32 degrees Celsius. After verification and parameter checking, the instruction is transferred to the execution buffer, triggering the heater to start, and the new set value of 32 degrees Celsius is written to the set value storage area.

[0120] Finally, to ensure the real-time and stability of the control system, a priority scheduling mechanism is established for storage area access. Real-time control data access, such as reading sensor data and sending control signals, is set to the highest priority. Device status information access, such as reading device operation counters, is set to the second priority. Configuration parameter access, such as modifying temperature setpoints, is set to the lowest priority. In this way, it can be ensured that critical control operations can be executed in a timely manner and stable operation can be maintained even when the system load is high.

[0121] Beneficial effects:

[0122] 1. Enhanced security: The verification mechanism of dynamic address mapping and instruction buffer can effectively prevent unauthorized access and execution of malicious instructions, thus improving the security of the system.

[0123] 2. Improve real-time performance: The priority scheduling mechanism can ensure the timely execution of key control operations, thereby improving the real-time performance of the system.

[0124] 3. Improve stability: By dividing the storage area and managing the priority, mutual interference between different tasks can be avoided and the stability of the system can be improved.

[0125] In an optional implementation, the method further includes:

[0126] When the programmable logic controller is monitored online, the encrypted subroutine or interrupt service program participates in the normal operation of the programmable logic controller and maintains a continuous operation state, and the main program reads the operation data of the encrypted program in real time; when the encrypted subroutine or interrupt service program needs to be modified, the program is updated by modifying the original unencrypted program backed up in advance and re-executing the encryption storage process.

[0127] The following description provides a more detailed implementation of an online monitoring and updating method for an encrypted program in a programmable logic controller (PLC).

[0128] First, encrypt the PLC subroutine or interrupt service program that needs to be protected. For example, a subroutine that controls the movement of a robotic arm, the original program contains key parameters such as the movement trajectory and speed. We can use the AES encryption algorithm to encrypt the subroutine with a pre-shared key. Assuming that the original program is "MOV X 100, MOV Y200", after AES encryption, the program becomes "F4D7C8...B9A2". The encrypted program is downloaded to the PLC.

[0129] Next, the PLC starts running online. The encrypted subroutine participates in the PLC operation like a normal program and maintains a continuous running state. The main program can read the operation data of the encrypted program in real time. For example, the main program needs to read the current X coordinate of the robot. During the execution of the encrypted subroutine, the X coordinate value will be stored in a specified register address. The main program can directly read the data at this address. Even if the subroutine is encrypted, it does not affect the main program's access to the data. Assuming that the current value of the X coordinate is 150, the data read by the main program is 150.

[0130] When it is necessary to modify the encrypted subroutine, such as adjusting the movement speed of the robot arm, we will not directly modify the encrypted program in the PLC. Instead, we modify the original unencrypted program that was backed up in advance. For example, change "MOV X 100" in the original program to "MOV X 150". After the modification is completed, the modified program is encrypted using the same AES encryption algorithm and key. The encrypted new program is downloaded to the PLC again, replacing the original encrypted program, and the program update is completed.

[0131] The beneficial effects of this method can be summarized in the following three aspects:

[0132] Enhance program security: By encrypting the core program code, it can effectively prevent unauthorized personnel from reading, copying and modifying the program, protecting the security of intellectual property rights and control systems.

[0133] Convenient program update: Since modification and encryption operations are performed offline and will not interrupt the normal operation of the PLC, the program can be updated online, reducing downtime and improving production efficiency.

[0134] Maintain system stability: The program update process only replaces the encrypted program, and the data interaction method between the main program and the encrypted program remains unchanged, thus ensuring the stability and reliability of system operation.

[0135] Another embodiment of the present application is as follows:

[0136] Provide users with an encryption library. Customers call the library file as the first line of the subroutine to be encrypted, and then download the program to the PLC, which will encrypt the subroutine. After completion, users need to back up the original program and then delete the encrypted part of the program. At this time, although the encrypted program segment cannot be seen in the programming software, it has been recorded in the actual PLC and participates in the normal operation of the PLC. If the user needs to modify the encrypted program segment, he can modify the backed up original program (unencrypted) and then perform the encryption operation again.

[0137] like Figure 2-Figure 4 As shown, you need to back up the original program first; call the encryption library in the first line of the program block "PIDC" to encrypt the subroutine "PIDC". Call the encryption library in the first line of the program block "SBR_1" to encrypt the subroutine "SBR_1". Then delete the logic programs in "PIDC" and "SBR_1", and call the encrypted subroutine in Main. The user can choose the network location to call SBR_1 according to the process requirements; it will not be affected when monitoring the PLC; the user can call the storage area address in the encrypted subroutine in the unencrypted program block to achieve the process requirements. MAIN calls the storage area address in the PIDC encrypted subroutine:

[0138] PIDC encryption subroutine:

[0139] Current value: V720;

[0140] Setting value: VD1404;

[0141] PID output value: V1400.0.

[0142] Figure 5 FIG. 1 is a schematic diagram of the structure of the encryption system of the subroutine and interrupt program in the PLC application program according to the embodiment of the present invention. Figure 5 As shown, the system comprises:

[0143] The first unit is used to create a program organization unit, the program organization unit includes a main program, a subroutine and an interrupt service program, write the program code that needs to be encrypted and protected in the subroutine or the interrupt service program, and call an encryption library in the first line of the program code of the subroutine or the interrupt service program, the encryption library is used to realize exclusive encryption of the program code;

[0144] The second unit is used to download the program code containing the calling instruction of the encryption library into the programmable logic controller, the programmable logic controller recognizes the calling instruction of the encryption library and encrypts the subroutine or the interrupt service program, and allocates a dedicated storage area address for the subroutine or the interrupt service program during the encryption process; the encrypted subroutine or interrupt service program is permanently stored in the flash memory of the programmable logic controller, and the encrypted program in the flash memory can only be updated by re-downloading after clearing the operation;

[0145] The third unit is used to completely delete the program code content of the subroutine or interrupt service program in the programming software after completing the encrypted storage, and only retain the program framework of the empty subroutine or interrupt service program, and the program framework is used to establish a call association with the main program; establish a call link with the encrypted subroutine or interrupt service program in the main program, and control the device by accessing the dedicated storage area address of the encrypted subroutine or interrupt service program, wherein the storage area address includes a current value storage area, a set value storage area and an output value storage area; when performing a program upload operation on the programmable logic controller, the programmable logic controller only uploads the empty subroutine or interrupt service program framework to the programming software, and the encrypted program code content is shielded during the upload process.

[0146] According to a third aspect of the embodiments of the present invention,

[0147] An electronic device is provided, comprising:

[0148] processor;

[0149] a memory for storing processor-executable instructions;

[0150] The processor is configured to call the instructions stored in the memory to execute the aforementioned method.

[0151] A fourth aspect of the embodiments of the present invention is:

[0152] A computer-readable storage medium is provided, on which computer program instructions are stored. When the computer program instructions are executed by a processor, the aforementioned method is implemented.

[0153] The present invention may be a method, an apparatus, a system and / or a computer program product. The computer program product may include a computer-readable storage medium carrying computer-readable program instructions for executing various aspects of the present invention.

[0154] Finally, it should be noted that the above embodiments are only used to illustrate the technical solutions of the present invention, rather than to limit it. Although the present invention has been described in detail with reference to the aforementioned embodiments, those skilled in the art should understand that they can still modify the technical solutions described in the aforementioned embodiments, or replace some or all of the technical features therein with equivalents. However, these modifications or replacements do not cause the essence of the corresponding technical solutions to deviate from the scope of the technical solutions of the embodiments of the present invention.

Claims

1. The encryption method of subroutines and interrupt programs in PLC application programs is characterized in that: include: Creating a program organization unit, the program organization unit includes a main program, a subroutine and an interrupt service program, writing a program code that needs to be encrypted and protected in the subroutine or the interrupt service program, and calling an encryption library in the first line of the program code of the subroutine or the interrupt service program, the encryption library is used to implement exclusive encryption of the program code; The program code containing the calling instruction of the encryption library is downloaded to the programmable logic controller, the programmable logic controller recognizes the calling instruction of the encryption library and encrypts the subroutine or the interrupt service program, and allocates a dedicated storage area address to the subroutine or the interrupt service program during the encryption process; the encrypted subroutine or interrupt service program is permanently stored in the flash memory of the programmable logic controller, and the encrypted program in the flash memory can only be updated by re-downloading after clearing the operation; After completing the encrypted storage, the program code content of the subroutine or interrupt service program in the programming software is completely deleted, and only the program framework of the empty subroutine or interrupt service program is retained, and the program framework is used to establish a call association with the main program; a call link with the encrypted subroutine or interrupt service program is established in the main program, and the control of the device is achieved by accessing the dedicated storage area address of the encrypted subroutine or interrupt service program, wherein the storage area address includes a current value storage area, a set value storage area, and an output value storage area; when performing a program upload operation on the programmable logic controller, the programmable logic controller only uploads the empty subroutine or interrupt service program framework to the programming software, and the encrypted program code content is shielded during the upload process; The program code to be encrypted is written in the subroutine or the interrupt service program, and the encryption library is called in the first line of the program code of the subroutine or the interrupt service program. The encryption library is used to implement exclusive encryption of the program code, including: The subroutine has an independent running space and a local variable area, and the interrupt service program has an independent interrupt vector table and execution priority; an input parameter verification mechanism is established in the subroutine or interrupt service program, and the input parameter verification mechanism includes a data type checking module, a value range verification module and a logic relationship verification module to verify the legality of the input data; Inserting an encryption library call instruction at the first line of the subroutine or interrupt service program, wherein the encryption library call instruction includes an encryption library identifier, version information and call parameters, and the encryption library call instruction is used to start encryption protection; An encryption process is started based on a layered architecture of the encryption library, wherein the layered architecture includes a basic encryption layer for implementing an encryption algorithm, a dynamic protection layer for generating dynamic keys, and an interface adaptation layer for processing hardware compatibility; characteristic information of the subroutine or interrupt service program is extracted, including instruction sequence characteristics, data structure characteristics, and control flow characteristics, and the characteristic information is combined with a hardware binding identifier to generate an encryption key; the subroutine or interrupt service program is encrypted using the encryption key, and code obfuscation is performed through instruction reordering, false branch insertion, and control flow transformation to generate an encrypted program.

2. The method according to claim 1, characterized in that The programmable logic controller identifies the call instruction of the encryption library and performs encryption processing on the subroutine or the interrupt service program, and allocates a dedicated storage area address for the subroutine or the interrupt service program during the encryption process, including: After receiving the program download instruction, the instruction parsing module performs feature code matching on the encryption library call instruction in the first line of the program code, compares the encryption library call instruction with the preset encryption library call instruction feature code, and checks the version number of the encryption library, obtains the permission status and integrity check value; Based on the feature code matching and verification results, preprocessing the source code of the subroutine or interrupt service program, dividing the source code into code segments, deleting redundant information and reordering instructions to generate preprocessing code; Performing multiple encryption transformations on the preprocessed code, including initialization encryption based on hardware feature codes, secondary encryption based on dynamic key generation based on timestamps, and code obfuscation processing based on instruction set features to form encrypted program code; Divide the encrypted program code into an independent physically isolated address space, establish a dynamic mapping table from virtual address to physical address, divide the storage area into an instruction area, a data area and a temporary area, and generate a corresponding address mapping table and access permission table; The address mapping table and the access permission table are encrypted, and the encrypted address mapping table and the access permission table are stored in the security area of ​​the programmable logic controller. The address mapping table and the access permission table are dynamically decrypted and used only when the program is running.

3. The method according to claim 2, characterized in that The method further comprises: Establishing a data access channel, setting an input buffer and an output buffer in the data access channel, wherein the input buffer performs security verification on input data and then passes it to the encryption program code, and the output buffer performs filtering and formatting on output data of the encryption program code; A runtime protection mechanism is established to track the execution status and resource usage of the encrypted program code through a real-time monitoring module. When abnormal access or illegal operation is detected, the exception handling module is triggered to perform security protection operations.

4. The method according to claim 1, characterized in that: The program code content of the subroutine or interrupt service program in the programming software is completely deleted, and only the program framework of the empty subroutine or interrupt service program is retained, and the program framework is used to establish a call association with the main program, including: The input parameter type, parameter quantity, parameter passing method and return value type information of the subroutine or interrupt service program are obtained through interface layer scanning, local variable declaration, static variable definition and data structure description information are obtained through variable layer scanning, and program basic block structure, branch jump point and loop entry point information are obtained through control flow scanning; the interface layer information, variable layer information and control flow information are respectively constructed into interface feature vectors, variable feature vectors and control flow feature vectors, wherein the feature vectors include variable scope, life cycle and access permission attributes, and the feature vectors are composed into a framework feature description table with a multi-dimensional matrix structure; A reverse recursive method is used to delete the execution code in the subroutine or interrupt service program from the inner code block layer by layer outward, a deletion mark bit is set at the deletion position, and a position mapping relationship is established between the deletion mark bit and the framework feature description table; a triple verification is performed on the deletion operation of the execution code, including program basic block structure verification, framework feature integrity verification and call relationship maintenance verification, and the deletion operation is confirmed after the verification passes; A virtual call link table is established to record the call point information in the main program, wherein the call point information includes the call location, parameter passing rules and return value processing method, and a call identifier is assigned to each call point; a call descriptor including the call parameter type, passing method and expected return value type is generated at the call point of the main program, and an interface descriptor including interface information is generated in the program framework, and a call association is established by comparing the call descriptor and the interface descriptor.

5. The method according to claim 1, characterized in that: The device is controlled by accessing the dedicated storage area address of the encrypted subroutine or interrupt service program, wherein the storage area address includes a current value storage area, a set value storage area and an output value storage area including: The storage space is divided into a current value storage area, a set value storage area and an output value storage area, wherein the current value storage area stores device status information, sensor data and operation counter data, the set value storage area stores temperature setting values, pressure limit values ​​and time parameters, and the output value storage area stores actuator control signals and output status information; Establishing a mapping table from physical address space to virtual address, adding a random offset to the mapping table to form a dynamic address mapping, and configuring independent address mapping tables for the current value storage area, the set value storage area, and the output value storage area respectively; The control instruction is written into the instruction buffer for legality verification and parameter checking. After the verification is passed, the control instruction is transferred to the execution buffer to trigger the control operation; a priority scheduling mechanism is established for storage area access, real-time control data access is set to the highest priority, device status information access is set to the second priority, and configuration parameter access is set to the lowest priority.

6. The method according to claim 1, characterized in that The method further comprises: When the programmable logic controller is monitored online, the encrypted subroutine or interrupt service program participates in the normal operation of the programmable logic controller and maintains a continuous operation state, and the main program reads the operation data of the encrypted program in real time; when the encrypted subroutine or interrupt service program needs to be modified, the program is updated by modifying the original unencrypted program backed up in advance and re-executing the encryption storage process.

7. An encryption system for subroutines and interrupt programs in a PLC application, used to implement the method according to any one of claims 1 to 6, characterized in that: include: The first unit is used to create a program organization unit, the program organization unit includes a main program, a subroutine and an interrupt service program, write the program code that needs to be encrypted and protected in the subroutine or the interrupt service program, and call an encryption library in the first line of the program code of the subroutine or the interrupt service program, the encryption library is used to realize exclusive encryption of the program code; The second unit is used to download the program code containing the calling instruction of the encryption library into the programmable logic controller, the programmable logic controller recognizes the calling instruction of the encryption library and encrypts the subroutine or the interrupt service program, and allocates a dedicated storage area address for the subroutine or the interrupt service program during the encryption process; the encrypted subroutine or interrupt service program is permanently stored in the flash memory of the programmable logic controller, and the encrypted program in the flash memory can only be updated by re-downloading after clearing the operation; The third unit is used to completely delete the program code content of the subroutine or interrupt service program in the programming software after completing the encrypted storage, and only retain the program framework of the empty subroutine or interrupt service program, and the program framework is used to establish a call association with the main program; establish a call link with the encrypted subroutine or interrupt service program in the main program, and control the device by accessing the dedicated storage area address of the encrypted subroutine or interrupt service program, wherein the storage area address includes a current value storage area, a set value storage area and an output value storage area; when performing a program upload operation on the programmable logic controller, the programmable logic controller only uploads the empty subroutine or interrupt service program framework to the programming software, and the encrypted program code content is shielded during the upload process.

8. An electronic device, characterized in that: include: processor; a memory for storing processor-executable instructions; The processor is configured to call the instructions stored in the memory to execute the method according to any one of claims 1 to 6.

9. A computer-readable storage medium having computer program instructions stored thereon, characterized in that: When the computer program instructions are executed by a processor, the method according to any one of claims 1 to 6 is implemented.

Citation Information

Patent Citations

  • Programmable logic controller and management method of user programs thereof

    CN102254122A

  • Subroutine encrypting method, verifying method and corresponding PLC programming system

    CN106325202A

  • Content filtering prior to data encryption

    US8468595B1