MCU serial communication encryption method based on time

The MCU serial communication encryption method generates random seeds and secret keys through time synchronization and XOR operation, solves the security and resource occupation issues of MCU serial communication, realizes lightweight encryption/decryption, adapts to various application scenarios, and ensures the reliability and confidentiality of data transmission.

CN120705890APending Publication Date: 2025-09-26CHINA COAL TECH & ENG GRP CHONGQING RES INST CO LTD
View PDF 0 Cites 0 Cited by

Patent Information

Application Number
CN202510816123.0
Authority / Receiving Office
CN · China
Patent Type
Applications(China)
Current Assignee / Owner
Filing Date
2025-06-18
Publication Date
2025-09-26

AI Technical Summary

Technical Problem

The existing MCU serial port communication method has the following problems: insufficient plaintext communication security, large computational complexity, high resource consumption, weak anti-interference ability, and poor real-time performance, making it difficult to adapt to various application scenarios.

Method used

It adopts a time-based MCU serial port communication encryption method, generates random seeds and secret keys through host and slave time synchronization, and uses XOR operations to encrypt/decrypt data, reducing the number of interactions and the amount of calculation, achieving lightweight encryption/decryption, and adapting to multiple operating systems.

Benefits of technology

It provides a serial communication solution with small resource usage, high security and good real-time performance, adapts to various application scenarios, is compatible with various embedded devices, ensures the reliability and confidentiality of data transmission, and avoids data leakage.

✦ Generated by Eureka AI based on patent content.

Smart Images

  • Figure CN120705890A_ABST
    Figure CN120705890A_ABST
Patent Text Reader

Abstract

The invention relates to a time-based MCU serial communication encryption method, which belongs to the field of embedded MCU serial communication and the technical field of single-chip microcomputer communication, and comprises the following steps: S1, carrying out time synchronization on a host and a slave; s2, the host carries out symmetric encryption processing on data to be sent, and sends the encrypted data to the slave; s3, the slave receives the encrypted data and decrypts the encrypted data; s4, the slave machine judges whether the decrypted data conforms to a communication protocol or not, if yes, decryption succeeds, the time of the host machine and the time of the slave machine are synchronous, and the subsequent data encryption transmission process is started from the step S2; and S5, increasing the current time of the slave by one minute, re-executing decryption processing, and if the current time of the slave does not conform to the communication protocol, returning to the step S1, and performing time synchronization on the slave again by the host.
Need to check novelty before this filing date? Find Prior Art

Description

Technical Field

[0001] The invention belongs to the field of embedded MCU serial port communication and the field of single-chip computer communication technology, and relates to a time-based MCU serial port communication encryption method. Background Art

[0002] With the development of computer systems and microcontrollers (MCUs), communication functions have become increasingly important. Computers and microcontrollers exchange information through communication interfaces. Serial communication, as one of the most common communication methods, is the most widely used communication method for microcontrollers. Serial communication is categorized as simplex, half-duplex, and full-duplex. Data is transmitted sequentially, one bit at a time. Its key features are simple communication lines and low costs. Serial communication typically requires not only a defined interface standard (TTL level, RS-232, RS-485), but also a communication protocol (including a data frame header, function word, data length, data, data checksum, and data frame trailer) between the two communicating parties. Data can then be exchanged according to this agreed-upon protocol.

[0003] However, this communication method still involves plaintext communication. For confidential products, the security of its data interaction is a necessary consideration. Interface standards are commonly used, so the adopted interface standard can generally be determined by the chip on the circuit board. The second most common feature is the communication protocol. Communication protocols typically have a relatively fixed structure (data frame header, function word, data length, data, data checksum, and data frame trailer), with the data frame header and frame trailer being essentially fixed. By monitoring data interaction over a long period of time, key data in the communication protocol can be further analyzed. Furthermore, if the monitored data is directly used for transmission, the microcontroller can be tricked into forging a fake host. While the communication protocol can make data transmission "opaque," it can also be forged, and its security is insufficient for use in confidential devices. Although some existing symmetric encryption technologies (such as AES and 3DES) can encrypt serial communication data, microcontroller resources are limited, and such encryption algorithms are computationally intensive and resource-intensive.

[0004] The existing technical solution is mainly implemented through the following steps: ① The host (the device communicating with the single-chip microcomputer) first sends an array A to start an interaction command; ② The slave (single-chip microcomputer) receives the interaction command and replies with array B, which is recorded as dataB[], and arranges the data according to the agreed rules: dataB[] = indexdata[index], and the index position of index in array B is recorded as I; ③ The host receives array B and parses it, first takes out the value of dataB[I] and records it as index2, then takes out the value of dataB[index2] and records it as index3, and then the host sends a command array C, which is recorded as dataC[], and puts the command to be sent in dataC[index3]. The host sends the command dataC[] = (dataC[index3] = cmd); ④ The slave receives array C, takes the data with index index in dataC[] as the command to be executed; ⑤ The slave determines whether the command meets the requirements and executes or refuses to execute the command. The above-mentioned prior art needs to initiate a start interaction command each time data is interacted (the host needs to send a start interaction command, the host then receives array B and parses it, and finally forms array C and sends it to the slave). If garbled data appears in the start interaction command, it may cause data loss, and the interaction must be re-established. The communication is time-consuming, the real-time performance is poor, and the communication method has weak anti-interference ability. The prior art is essentially still plain text communication, and its effective execution command is just in a different position in the data each time (dataC[index]=cmd, index is a random number, but cmd is the actual execution command to be transmitted). If a large amount of packet capture analysis is performed, the byte that appears the most times in the communication data can be found, and the byte with the highest repetition rate is the execution command cmd. Since the execution command in the technology is only a single byte, it is not applicable to longer communication data and can only be applied to communication requirements of a single byte. Summary of the Invention

[0005] In view of this, the purpose of the present invention is to provide a time-based MCU serial port communication encryption method, which reduces the number of data encryption / decryption interactions and enables the MCU to realize self-encryption / decryption; reduces the amount and time of encryption / decryption calculations, lightweight encryption / decryption code, and can adapt to a variety of application scenarios; the encryption / decryption algorithm is highly practical, can facilitate the porting of different operating systems, and can effectively avoid data leakage.

[0006] In order to achieve the above object, the present invention provides the following technical solutions:

[0007] A time-based MCU serial port communication encryption method includes the following steps:

[0008] S1: Master and slave perform time synchronization;

[0009] S2: The host symmetrically encrypts the data to be sent and sends the encrypted data to the slave;

[0010] S3: The slave receives the encrypted data and performs decryption processing;

[0011] S4: The slave determines whether the decrypted data complies with the communication protocol. If so, the decryption is successful, the time of the host and the slave are synchronized, and the subsequent data encryption transmission process can start from step S2; if not, step S5 is executed:

[0012] S5: Add one minute to the current time of the slave and re-execute the decryption process. If it still does not comply with the communication protocol, return to step S1 and the host re-synchronizes the time of the slave.

[0013] Furthermore, in step S1, the host and the slave perform time synchronization, including: the host sends unified time data to the slave in plain text using a communication protocol agreed upon by both parties, and the slave parses the received time data and updates the RTC time of the slave device.

[0014] Furthermore, step S2 specifically includes the following steps:

[0015] S21: The host calculates a random seed a1 based on the current RTC time T1;

[0016] S22: The host calculates the secret key b1 based on the random seed a1 obtained in step S21 and the random number algorithm agreed upon by both parties;

[0017] S23: The host symmetrically encrypts the data to be sent based on the obtained secret key b1, performs an XOR operation on the data A to be sent and the secret key b1 to obtain data B, and sends the encrypted data B to the slave.

[0018] Furthermore, the data encryption process is described by the following formula:

[0019] E k (T1,A)=B

[0020] Where, E k () represents the encryption algorithm; T1 represents the current time of the host; A represents the data to be encrypted; B represents the encrypted data.

[0021] Furthermore, step S3 specifically includes the following steps:

[0022] S31: After receiving the encrypted data B, the slave calculates a random seed a2 based on the current RTC time T2;

[0023] S32: The slave calculates the secret key b2 according to the random seed a2 obtained in step S31 using a random number algorithm;

[0024] S33: The slave performs symmetrical decryption processing on the received data according to the secret key b2 obtained in step S32, and performs an XOR operation on the received data B and the secret key b2 to obtain data C.

[0025] Furthermore, the data decryption process is described by the following formula:

[0026] D k (T2,B)=C

[0027] Where D k () represents the decryption algorithm; T2 represents the current time of the slave; B represents the data to be decrypted; C represents the decrypted data.

[0028] The beneficial effects of the present invention are: 1) providing a small-resource, lightweight serial communication encryption solution for embedded devices such as microcontrollers (MCUs), making the encryption solution of the present invention compatible with a variety of embedded devices; 2) providing a method for reliable data transmission and data confidentiality for serial communication data interaction, thereby ensuring data security in serial communication; 3) providing an independent copyright principle for the product, avoiding secondary development by peers, and ensuring the brand recognition of the product.

[0029] Other advantages, objects, and features of the present invention will be described in part in the following description and, in part, will be apparent to those skilled in the art upon examination of the following description or may be learned from practice of the present invention. The objects and other advantages of the present invention may be realized and obtained through the following description. BRIEF DESCRIPTION OF THE DRAWINGS

[0030] In order to make the purpose, technical solutions and advantages of the present invention more clear, the present invention will be described in detail below with reference to the accompanying drawings, in which:

[0031] Figure 1 The following is a flow chart of the time-based MCU serial communication encryption method. DETAILED DESCRIPTION

[0032] The following describes the embodiments of the present invention by means of specific examples, and those skilled in the art can easily understand other advantages and effects of the present invention from the contents disclosed in this specification. The present invention can also be implemented or applied through other different specific embodiments, and the details in this specification can also be modified or changed in various ways based on different viewpoints and applications without departing from the spirit of the present invention. It should be noted that the illustrations provided in the following embodiments are only schematic illustrations of the basic concept of the present invention, and the following embodiments and features in the embodiments can be combined with each other without conflict.

[0033] It should be noted that the illustrations provided in the following embodiments are merely schematic illustrations of the basic concept of the present invention. Therefore, the illustrations only show components related to the present invention and are not drawn according to the number, shape, and size of components in actual implementation. In actual implementation, the type, quantity, and proportion of each component may be changed arbitrarily, and the component layout may also be more complex.

[0034] In the following description, numerous details are discussed to provide a more thorough explanation of the embodiments of the present invention. However, it will be apparent to those skilled in the art that the embodiments of the present invention may be practiced without these specific details. In other embodiments, well-known structures and devices are shown in block diagram form rather than in detail to avoid obscuring the embodiments of the present invention.

[0035] Example 1:

[0036] The present invention provides a time-based MCU serial port communication encryption method, which includes: generating and calculating a random seed, defining and implementing a custom random algorithm, obtaining a secret key through the custom random number generation algorithm, encrypting / decrypting communication data through the secret key, an extensible complete communication protocol (composed of a data frame header, a function word, a data length, data, a CRC check code, and a data frame tail), ensuring CRC check of data transmission errors, determining the timeliness of the secret key, master / slave time synchronization processing, and self-adjusting the slave time difference.

[0037] like Figure 1 As shown, the present invention mainly includes the following steps (defining the host as a device communicating with the microcontroller and defining the slave as the microcontroller):

[0038] ① Both the master and the slave need to have built-in RTC time. When the master and the slave establish communication for the first time, the master sends the unified time data to the slave in plain text using the communication protocol agreed upon by both parties. After receiving the time data, the slave parses it and updates the RTC time of the slave device.

[0039] ② The host calculates a random seed a1 based on the current RTC time T1 (this calculation method does not involve seconds, that is, the random seed calculated within one minute is the same);

[0040] ③ The host calculates the secret key b1 based on the random seed a1 obtained in step ② and the random number algorithm agreed upon by both parties;

[0041] ④ The host symmetric encrypts the data to be sent based on the secret key b1 obtained in step ③ (the symmetric encryption process is mainly performed by XOR operation, and the data A to be sent is XORed with the secret key b1 to obtain data B), and sends the encrypted data (data B) to the slave;

[0042] ⑤After receiving the encrypted data B, the slave calculates a random seed a2 based on the current RTC time T2 (the calculation method is the same as step ②);

[0043] ⑥ The slave calculates the secret key b2 using the random seed a2 obtained in step ⑤ according to the random number algorithm (the calculation method is the same as step ③);

[0044] ⑦ The slave performs symmetric decryption on the received data using the secret key b2 obtained in step ⑥ (symmetric decryption also uses an XOR operation to XOR the received data B with the secret key b2 to obtain data C). The decoded data (data C) is then subjected to a communication protocol check to determine whether it complies with the communication protocol. If it does, the decoding is successful and the master and slave times are synchronized. For subsequent encrypted data transmission, it is sufficient to simply start from step ②. If it does not, the slave's current time is incremented by one minute, and steps ⑤ to ⑦ are repeated. If step ⑦ still does not comply with the communication protocol after the second execution, it is necessary to return to step ① and have the master resynchronize the slave's time.

[0045] For the sake of convenience, the data encryption process in steps ② to ④ is described using the following formula:

[0046] E k (T1,A)=B

[0047] Where, E k () represents the encryption algorithm; T1 represents the current time of the host; A represents the data to be encrypted; B represents the encrypted data.

[0048] For the sake of convenience, the data decryption process in steps ⑤ to ⑦ is described using the following formula:

[0049] D k (T2,B)=C

[0050] Where Dk () represents the decryption algorithm; T2 represents the current time of the slave; B represents the data to be decrypted; C represents the decrypted data.

[0051] Generally speaking, if the master and slave times are synchronized, that is, T1=T2, then after data encryption and decryption, A=C. At this time, data C can meet the communication protocol judgment. If the slave time is not synchronized with the master time, the slave time is added one minute and then the data decryption process is performed to obtain data C', and then data C' is judged by the communication protocol. If data C' still cannot pass the communication protocol judgment, it means that the master and slave times are too different, and the time needs to be resynchronized before data encryption / decryption processing.

[0052] Example 2:

[0053] An electronic device comprising a memory and a processor;

[0054] The memory is used to store computer programs;

[0055] The processor is configured to implement the method described in Example 1 when executing the computer program.

[0056] Example 3:

[0057] A computer-readable storage medium stores a computer program, and when the computer program is executed by a processor, the method described in Example 1 is implemented.

[0058] Example 4:

[0059] A computer program product includes a computer program, which implements the method described in embodiment 1 when executed by a processor.

[0060] In the above embodiments, references to "this embodiment" in the specification indicate that a particular feature, structure, or characteristic described in conjunction with the embodiment is included in at least some embodiments, but not necessarily all embodiments. Multiple occurrences of "this embodiment" do not necessarily refer to the same embodiment.

[0061] In the above embodiments, references to "this embodiment" in the specification indicate that a particular feature, structure, or characteristic described in conjunction with the embodiment is included in at least some embodiments, but not necessarily all embodiments. Multiple occurrences of "this embodiment" do not necessarily refer to the same embodiment.

[0062] In the above embodiments, although the invention has been described in conjunction with specific embodiments thereof, many alternatives, modifications, and variations of these embodiments will be apparent to those skilled in the art based on the foregoing description. For example, other memory structures (e.g., dynamic RAM (DRAM)) may be used with the embodiments discussed. The embodiments of the present invention are intended to encompass all such alternatives, modifications, and variations that fall within the broad scope of the appended claims.

[0063] Regarding the computer-readable storage medium in this embodiment, those skilled in the art will appreciate that all or part of the steps in the aforementioned method embodiments can be implemented using hardware associated with the computer program. The aforementioned computer program can be stored in a computer-readable storage medium. When executed, the program performs the steps in the aforementioned method embodiments. The aforementioned storage medium includes various media capable of storing program code, such as ROM, RAM, magnetic disks, or optical disks.

[0064] The electronic terminal provided in this embodiment includes a processor, a memory, a transceiver and a communication interface. The memory and the communication interface are connected to the processor and the transceiver and complete communication with each other. The memory is used to store computer programs, the communication interface is used for communication, and the processor and the transceiver are used to run computer programs so that the electronic terminal executes the various steps of the above method.

[0065] In this embodiment, the memory may include a random access memory (RAM), and may also include a non-volatile memory (non-volatile memory), such as at least one disk storage.

[0066] The above-mentioned processor can be a general-purpose processor, including a central processing unit (CPU), a network processor (NP), etc.; it can also be a digital signal processor (DSP), an application-specific integrated circuit (ASIC), a field-programmable gate array (FPGA) or other programmable logic devices, discrete gate or transistor logic devices, and discrete hardware components.

[0067] The present invention can be used in a wide variety of general-purpose or special-purpose computing system environments or configurations, such as personal computers, server computers, handheld or portable devices, tablet devices, multiprocessor systems, microprocessor-based systems, set-top boxes, programmable consumer electronics, network PCs, minicomputers, mainframe computers, and distributed computing environments that include any of the above.

[0068] The present invention may be described in the general context of computer-executable instructions, such as program modules, executed by a computer. Generally, program modules include routines, programs, objects, components, data structures, and the like that perform specific tasks or implement specific abstract data types. The present invention may also be practiced in distributed computing environments where tasks are performed by remote processing devices connected via a communications network. In a distributed computing environment, program modules may be located in both local and remote computer storage media, including storage devices.

[0069] Finally, it should be noted that the above embodiments are only used to illustrate the technical solutions of the present invention and are not limiting. Although the present invention has been described in detail with reference to the preferred embodiments, those skilled in the art should understand that the technical solutions of the present invention can be modified or replaced by equivalents without departing from the purpose and scope of the technical solutions, which should all be included in the scope of the claims of the present invention.

Claims

1. A time-based MCU serial port communication encryption method, characterized by: The following steps are involved: S1: Master and slave perform time synchronization; S2: The host symmetrically encrypts the data to be sent and sends the encrypted data to the slave; S3: The slave receives the encrypted data and performs decryption processing; S4: The slave determines whether the decrypted data complies with the communication protocol. If so, the decryption is successful, the time of the master and slave are synchronized, and the subsequent data encryption transmission process can start from step S2; if not, step S5 is executed: S5: Add one minute to the current time of the slave and re-execute the decryption process. If it still does not comply with the communication protocol, return to step S1 and the host re-synchronizes the time of the slave.

2. The time-based MCU serial port communication encryption method according to claim 1, wherein: In step S1, the host and the slave perform time synchronization, including: the host sends unified time data to the slave in plain text using a communication protocol agreed upon by both parties, and the slave parses the received time data and updates the RTC time of the slave device.

3. The time-based MCU serial port communication encryption method according to claim 1, wherein: Step S2 specifically includes the following steps: S21: The host calculates a random seed a1 based on the current RTC time T1; S22: The host calculates the secret key b1 based on the random seed a1 obtained in step S21 and the random number algorithm agreed upon by both parties; S23: The host symmetrically encrypts the data to be sent based on the obtained secret key b1, performs an XOR operation on the data A to be sent and the secret key b1 to obtain data B, and sends the encrypted data B to the slave.

4. The time-based MCU serial port communication encryption method according to claim 3, wherein: The data encryption process is described by the following formula: From k (T1,A)=B Where, E k () represents the encryption algorithm; T1 represents the current time of the host; A represents the data to be encrypted; B represents the encrypted data.

5. The time-based MCU serial port communication encryption method according to claim 1, wherein: Step S3 specifically includes the following steps: S31: After receiving the encrypted data B, the slave calculates a random seed a2 based on the current RTC time T2; S32: The slave calculates the secret key b2 according to the random seed a2 obtained in step S31 using a random number algorithm; S33: The slave performs symmetrical decryption processing on the received data according to the secret key b2 obtained in step S32, and performs an XOR operation on the received data B and the secret key b2 to obtain data C.

6. The time-based MCU serial port communication encryption method according to claim 5, characterized in that: The data decryption process is described by the following formula: D k (T2,B)=C Where D k () represents the decryption algorithm; T2 represents the current time of the slave; B represents the data to be decrypted; C represents the decrypted data.

7. An electronic device, characterized in that: including memory and processor; The memory is used to store computer programs; The processor is configured to implement the time-based MCU serial port communication encryption method according to any one of claims 1 to 6 when executing the computer program.

8. A computer-readable storage medium, characterized in that The storage medium stores a computer program, and when the computer program is executed by the processor, the time-based MCU serial port communication encryption method according to any one of claims 1 to 6 is implemented.

9. A computer program product, characterized in that: The invention comprises a computer program, which, when executed by a processor, implements the time-based MCU serial port communication encryption method according to any one of claims 1 to 6.