WebSocket-based system stream transmission encryption method and system, and storage medium
By converting JSON data in WebSocket interaction into a phase stream and using obfuscation encryption and data segmentation technology, the security, efficiency and accuracy of JSON plaintext transmission are solved, and efficient and secure data transmission is achieved.
Patent Information
- Application Number
- CN202411742122.8
- Authority / Receiving Office
- CN · China
- Patent Type
- Applications(China)
- Current Assignee / Owner
- Filing Date
- 2024-11-29
- Publication Date
- 2025-03-04
AI Technical Summary
In existing WebSocket interactions, JSON plaintext transmission has problems with security, transmission efficiency and parsing performance, especially when weak network environments and limited resources.
The WebSocket-based phase streaming encryption method is adopted to convert JSON format data into phase stream data, and transmit it through obfuscation encryption and data segmentation to ensure the security and accuracy of the data.
Through the calculating streaming method, the bandwidth resource usage is significantly reduced, the transmission efficiency is improved, and the data security and accuracy are enhanced.
Smart Images

Figure CN119561762A_ABST
Abstract
Description
Technical Field
[0001] The present invention belongs to the field of data transmission, and in particular relates to a binary stream transmission encryption method, system and storage medium based on WebSocket. Background Art
[0002] Currently, the main embedded Web interactive signaling transmission technologies include: HTTP / HTTPS, WebSocket, RESTful API and MQTT protocol combined with Web proxy and other technologies.
[0003] The limitations of different interaction methods are analyzed as follows: running HTTP / HTTPS services will occupy a lot of resources of embedded devices, and its request-response mode also has a certain delay; the main limitation of RESTful API is that different clients may support it differently; the MQTT protocol combined with the Web proxy requires an additional Web proxy server, which increases the complexity of the system. In summary, websocket is the first choice in weak network environments and limited resources. However, when using websocket interaction, most scenarios are plain text transmission of JSON format data, which has certain limitations.
[0004] The limitations and disadvantages of conventional JSON plain text transmission methods are as follows:
[0005] 1. Security: JSON data transmitted in plain text can be easily read and understood. During the transmission process, if there is no additional encryption measures, the data can be easily stolen. The plain text JSON data structure is represented by key-value pairs, which has a simple structure and is less difficult to tamper with.
[0006] 2. Transmission efficiency: JSON is a text format that requires a large number of key-value pairs and specific segmentation characters to describe the data structure, and the data is relatively redundant. When the network bandwidth is limited, the transmission takes a long time, which will affect the real-time performance. Especially when large-scale data is transmitted, the problem of low efficiency is more obvious.
[0007] 3. Parsing performance: The client needs to perform time-consuming operations such as string parsing and object construction to parse JSON format data. Especially when processing large amounts of data or high concurrency, parsing JSON data may become a bottleneck for system performance.
[0008] Therefore, the present application proposes a binary transmission method based on websocket to solve the above problems. Summary of the invention
[0009] In view of one or more of the above defects or improvement requirements of the prior art, the present invention provides a binary stream transmission encryption method, system and storage medium based on WebSocket. The scheme adjusts the transmission data format to binary stream data, thereby achieving data simplification, reducing the bandwidth resources required for transmission, and improving transmission efficiency. At the same time, by adopting obfuscation encryption and data segmentation, the security and accuracy of the data are ensured.
[0010] To achieve the above object, the present invention provides a binary stream transmission encryption method based on WebSocket, the method comprising the following steps:
[0011] According to the original JSON format and the data required by the corresponding function, the data transmission format is agreed upon;
[0012] The server converts the binary stream data into the agreed format and sends it to the client via WebSocket.
[0013] The client establishes a connection with the server through websocket and receives the transmitted binary stream data;
[0014] According to the agreed data format, the received data is processed and the 0xFF mark is searched to determine the start of the data;
[0015] According to the rules, multiple data are cyclically matched in turn. If there is excess or missing data in the binary stream, it means that the data is incorrect;
[0016] If the data accuracy verification fails, the client will communicate with the server again and request the corresponding binary stream data to be resent.
[0017] As a further improvement of the present invention, in the step of agreeing on the data transmission format, in order to ensure the accuracy of the data and encrypt the data transmission to a certain extent, some identifiers need to be agreed upon;
[0018] The 0xFF character is used to mark the start of the data, and the delimiter character is 0xFE.
[0019] As a further improvement of the present invention, the specific steps of processing the data are as follows:
[0020] First initialize the data structure, then fill the data, and finally convert the data from hexadecimal to decimal.
[0021] As a further improvement of the present invention, before cyclically corresponding to multiple data, the data received by the client must start with an agreed data header identifier, and the part before the agreed delimiter is all the main body information of the data segment.
[0022] As a further improvement of the present invention, it is characterized in that after finding the start character and the separator, the first base value of the multiple data in each substructure is the ID of the substructure, and the subsequent part is some attributes of each substructure, and the attribute value occupies a fixed base position in the substructure;
[0023] After matching these fixed data, the first base value behind it is the number of subsequent unfixed values. According to the number of values, the corresponding base stream data is traversed and intercepted to complete the matching process of the current substructure data.
[0024] As a further improvement of the present invention, when the data accuracy verification fails, a loading animation is displayed in the corresponding module area and continues until the server returns correct and complete data.
[0025] As a further improvement of the present invention, in the step of filling in data, the data accuracy is judged according to the agreed data format;
[0026] And the identifier can be used to distinguish different parts of the data to prevent all data from being unusable due to data errors.
[0027] On the other hand, the present invention also provides a binary stream transmission encryption system based on WebSocket, which is used to implement the above-mentioned binary stream transmission encryption method based on WebSocket.
[0028] On this basis, the present invention also provides a storage medium, which, when running on a computer, executes the steps in the above-mentioned binary stream transmission encryption method based on WebSocket.
[0029] The above-mentioned improved technical features can be combined with each other as long as they do not conflict with each other.
[0030] In general, compared with the prior art, the above technical solution conceived by the present invention has the following beneficial effects:
[0031] (1) The WebSocket-based binary stream transmission encryption method, system and storage medium of the present invention transmits data in accordance with the data format agreed upon by the front-end and back-end, and inserts customized meaningless binary symbols to encrypt the data. Only obtaining the binary data cannot correctly understand its meaning, nor can it obtain the correct data content, thereby improving security.
[0032] (2) The WebSocket-based binary stream transmission encryption method, system and storage medium of the present invention insert a pre-agreed binary separator between each part of the data in order to solve the problem that data may be lost, damaged or transmitted abnormally due to network influence during transmission. In this way, when an abnormality occurs in data parsing, the abnormal data can be quickly located, thereby ensuring the accuracy of the data.
[0033] (3) The WebSocket-based binary stream transmission encryption method, system and storage medium of the present invention only need to transmit the values of the key-value pairs in the original JSON format data in sequence according to the data format agreed upon by the front-end and back-end. In this way, data simplification is achieved. At the same time, using binary streams for data transmission can save more than 85% of bandwidth resources. Compared with other transmission methods, binary stream transmission has significant advantages in bandwidth resource utilization and can greatly reduce the occupation of network resources by data transmission. BRIEF DESCRIPTION OF THE DRAWINGS
[0034] In order to more clearly illustrate the embodiments of the present invention or the technical solutions in the prior art, the drawings required for use in the embodiments or the description of the prior art will be briefly introduced below. Obviously, the drawings described below are only some embodiments of the present invention. For ordinary technicians in this field, other drawings can be obtained based on the structures shown in these drawings without paying creative work.
[0035] Figure 1 It is an overall flow chart of a binary stream transmission encryption method, system and storage medium based on WebSocket in an embodiment of the present invention.
[0036] Figure 2 It is a picture of the number of characters occupied when the JSON format is originally used for transmission in the embodiment of the present invention.
[0037] Figure 3 It is a corresponding relationship diagram of the agreed data format in the embodiment of the present invention. DETAILED DESCRIPTION
[0038] The following will be combined with the drawings in the embodiments of the present invention to clearly and completely describe the technical solutions 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.
[0039] It should be noted that if the embodiments of the present invention involve directional indications (such as up, down, left, right, front, back, etc.), the directional indications are only used to explain the relative position relationship, movement status, etc. between the components in a certain specific posture. If the specific posture changes, the directional indication will also change accordingly.
[0040] In addition, if there are descriptions involving "first", "second", etc. in the embodiments of the present invention, the descriptions of "first", "second", etc. are only used for descriptive purposes and cannot be understood as indicating or implying their relative importance or implicitly indicating the number of technical features indicated. Therefore, the features limited to "first" and "second" may explicitly or implicitly include at least one of the features. In addition, if "and / or" or "and / or" appears in the full text, its meaning includes three parallel solutions. Taking "A and / or B" as an example, it includes solution A, solution B, or solutions that satisfy both A and B. In addition, the technical solutions between the various embodiments can be combined with each other, but it must be based on the ability of ordinary technicians in this field to implement. When the combination of technical solutions is contradictory or cannot be implemented, it should be deemed that such combination of technical solutions does not exist and is not within the scope of protection required by the present invention.
[0041] In order to make the purpose, technical solutions and advantages of the present invention more clearly understood, the present invention is further described in detail below in conjunction with the accompanying drawings and embodiments.
[0042] It should be understood that the specific embodiments described herein are only used to explain the present invention and are not used to limit the present invention. In addition, the technical features involved in each embodiment of the present invention described below can be combined with each other as long as they do not conflict with each other.
[0043] See also Figures 1 to 3 The binary stream transmission encryption method based on WebSocket in the preferred embodiment of the present invention adjusts the transmission data format to binary stream data, thereby realizing data simplification, reducing the bandwidth resources required for transmission, and improving transmission efficiency. At the same time, the security and accuracy of the data are ensured by adopting the methods of obfuscation encryption and data segmentation.
[0044] Specifically, the WebSocket-based binary stream transmission encryption method of the present invention includes the following steps: S100: agreeing on the data transmission format according to the original JSON format and the data required by the corresponding function.
[0045] In more detail, in step S100, in order to ensure the accuracy of the data and encrypt the data transmission to a certain extent, some identifiers need to be agreed upon. The identifier is usually a large value that will not appear in the data required by the client, such as 0xFF used to identify the start of the data; and the separator 0xFE. On the one hand, the data header identifier 0xFF and the separator 0xFE can ensure the accuracy of data transmission; on the other hand, the data with various identifiers added to it confuses the original correct data to a certain extent, thereby achieving the effect of data encryption.
[0046] Furthermore, based on Figure 2 It can be seen that when the JSON format is used for transmission, 190 characters are required, and the number of bits occupied is 190×8=1520 bits. The data format agreed in this example is as follows Figure 3 As shown, according to this agreed data format, the data required for the corresponding function is converted into the corresponding binary stream data for transmission (hexadecimal is used as an example in this article). After conversion, the original 190 characters of data required are replaced by 21 hexadecimal numbers, and the number of bits they occupy is 21×8=168bit). In this example, the number of bits occupied by the required data is greatly reduced from 1520 to 168, saving about 88.9% of bandwidth resources and greatly improving transmission efficiency.
[0047] S200: The server sends the binary stream data converted according to the agreed format to the client via WebSocket.
[0048] S300: The client establishes a connection with the server through websocket and receives the transmitted binary stream data.
[0049] S400: Process the received data according to the agreed data format, and search for the 0xFF identifier to determine the start of the data.
[0050] In the data filling phase of step 4, the data accuracy will be judged according to the data format agreed upon above. The identifier can distinguish different parts of the data to prevent the entire data from being unusable due to errors in individual data. At the same time, data segmentation also creates conditions for judging the integrity of the data in the data filling phase.
[0051] Furthermore, in step 400, the specific steps of processing the data are as follows:
[0052] S401: Initialize data structure;
[0053] constjsonData={subNetinfo:{}};
[0054] In this step, an object named jsonData is created, which contains an empty object named subNetinfo. This is to prepare for the subsequent filling of data.
[0055] S402: Data filling
[0056] const jsonData = {
[0057] 0x01:{ / / Hexadecimal key, representing some identifier or classification
[0058] Masternode:0x01, / / Masternode identifier
[0059] MaxNoise:0x9d, / / Maximum noise value
[0060]
[0061] In this step, the jsonData object is reassigned to contain more data. This data is expressed in hexadecimal form and may represent specific configuration or status information. The object under the 0x01 key contains the master node identifier, maximum noise value, and number of online nodes. The subNetinfo object contains multiple subnet information, each of which has a hexadecimal identifier, as well as the corresponding signal-to-noise ratio, error code, and routing information.
[0062] S403: Data conversion, hexadecimal to decimal, the converted data is:
[0063]
[0064] },
[0065] subNetinfo:{
[0066] 2:{ / / Convert 0x02 to decimal 2
[0067] route:[3,4], / / The value in the route is also converted to decimal
[0068] snr:1, / / 0x01 converts to decimal 1 and should not be placed in the array (unless there is a specific reason)
[0069] err:0, / / 0x00 is converted to decimal 0 and should not be placed in the array
[0070] },
[0071] 3:{ / / Convert 0x03 to decimal 3
[0072] route:[], / / No routing information
[0073] snr:1, / / 0x01 converted to decimal 1
[0074] err:0, / / 0x00 converted to decimal 0
[0075] },
[0076] 4:{ / / Convert 0x04 to decimal 4
[0077] route:[3], / / Route to identifier 3 (decimal)
[0078] snr:1, / / Corrected to 1, because 0x01 converted to decimal is 1
[0079] err:0, / / 0x00 converted to decimal 0
[0080] },
[0081] },
[0082] };
[0083] S500: According to the rules, the plurality of data are cyclically matched in sequence. If the binary stream data is redundant or missing, it indicates that the data is incorrect.
[0084] In a specific embodiment, Figure 3 Take the following example to illustrate the specific implementation of the data format agreement: the data received by the client must start with the agreed data header identifier. The part before the agreed delimiter is all the main information of the data segment. The first binary value after the delimiter represents the number of subsequent substructures. The first binary value of each substructure is the ID of the substructure, and the subsequent part is some attributes contained in each substructure. The values of these attributes occupy fixed binary positions in the substructure (i.e., fixed data length). After matching these fixed data, the first binary value after it is the number of subsequent unfixed values, which is "route" in this example. According to the number of "route", the corresponding binary stream data is traversed and intercepted. At this time, all the current substructure data is matched. The subsequent binary data is the next substructure, and the cycle can be repeated. After the regular cycle is completed, if there is excess or missing binary stream data, it means that the data is incorrect.
[0085] S600: If the data accuracy verification fails, the client will communicate with the server again to request resending of the corresponding binary stream data.
[0086] During this process, the client display will also be optimized. If the data required by a module in the page is complete, it will be displayed normally; if the data is incomplete, friendly content such as loading animation will be displayed in the corresponding module area until the server returns correct and complete data. This can avoid displaying wrong data on the page and prevent users from being troubled during use.
[0087] On the other hand, the present invention also provides a binary stream transmission encryption system based on WebSocket, which is used to implement the above-mentioned binary stream transmission encryption method based on WebSocket.
[0088] In another aspect, the present invention further provides a non-transitory computer-readable storage medium having a computer program stored thereon, and when the computer program is executed by a processor, the method provided in each of the above embodiments is implemented, and the method includes the following steps:
[0089] According to the original JSON format and the data required by the corresponding function, the data transmission format is agreed upon;
[0090] The server converts the binary stream data into the agreed format and sends it to the client via WebSocket.
[0091] The client establishes a connection with the server through websocket and receives the transmitted binary stream data;
[0092] According to the agreed data format, the received data is processed and the 0xFF mark is searched to determine the start of the data;
[0093] According to the rules, multiple data are cyclically matched in turn. If there is excess or missing data in the binary stream, it means that the data is incorrect;
[0094] If the data accuracy verification fails, the client will communicate with the server again and request the corresponding binary stream data to be resent.
[0095] The method embodiments described above are merely illustrative, wherein the units described as separate components may or may not be physically separated, and the components displayed as units may or may not be physical units, that is, they may be located in one place, or they may be distributed on multiple network units. Some or all of the modules may be selected according to actual needs to achieve the purpose of the scheme of this embodiment. Those of ordinary skill in the art may understand and implement it without creative work.
[0096] The system is constructed to run the method of the present application. Through the description of the above implementation methods, those skilled in the art can clearly understand that each implementation method can be implemented by means of software plus a necessary general hardware platform, and of course, it can also be implemented by hardware. Based on this understanding, the above technical solution is essentially or the part that contributes to the prior art can be embodied in the form of a software product, which can be stored in a computer-readable storage medium, such as ROM / RAM, a disk, an optical disk, etc., including a number of instructions for a computer device (which can be a personal computer, a server, or a network device, etc.) to execute the methods described in each embodiment or some parts of the embodiments.
[0097] It will be easily understood by those skilled in the art that the above description is only a preferred embodiment of the present invention and is not intended to limit the present invention. Any modifications, equivalent substitutions and improvements made within the spirit and principles of the present invention should be included in the protection scope of the present invention.
Claims
1. A binary stream transmission encryption method based on WebSocket, characterized in that: The method comprises the following steps: According to the original JSON format and the data required by the corresponding function, the data transmission format is agreed upon; The server converts the binary stream data into the agreed format and sends it to the client via WebSocket. The client establishes a connection with the server through websocket and receives the transmitted binary stream data; According to the agreed data format, the received data is processed and the 0xFF mark is searched to determine the start of the data; According to the rules, multiple data are cyclically matched in sequence. If there are redundant or missing binary stream data, it means that the data is incorrect. If the data accuracy verification fails, the client will communicate with the server again and request the corresponding binary stream data to be resent.
2. The WebSocket-based binary stream transmission encryption method according to claim 1, characterized in that: In the step of agreeing on the data transmission format, in order to ensure the accuracy of the data and encrypt the data transmission to a certain extent, the data needs to be constrained by an identifier, which includes a start character and a delimiter for identifying the start of the data.
3. The WebSocket-based binary stream transmission encryption method according to claim 1, characterized in that: The specific steps for processing the data are as follows: First initialize the data structure, then fill the data, and then convert the data from hexadecimal to decimal.
4. The binary stream transmission encryption method based on WebSocket according to any one of claims 1 to 3, characterized in that: Before cyclically matching multiple data, the data received by the client must start with the agreed data header identifier, and the part before the agreed delimiter is all the main information of the data segment.
5. The WebSocket-based binary stream transmission encryption method according to claim 4, characterized in that: After finding the start character and separator, the first binary value of the multiple data in each substructure is the ID of the substructure.
6. The WebSocket-based binary stream transmission encryption method according to claim 5, characterized in that: After completing the correspondence of the data, the first base value behind it is the number of subsequent unfixed values. According to the number of values, the corresponding base stream data is traversed and intercepted to complete the correspondence process of the current substructure data.
7. The WebSocket-based binary stream transmission encryption method according to claim 1, characterized in that: If the data accuracy verification fails, a loading animation will be displayed in the corresponding module area until the server returns correct and complete data.
8. The WebSocket-based binary stream transmission encryption method according to claim 1, characterized in that: In the data filling step, the data accuracy is judged according to the agreed data format; And the identifier can be used to distinguish different parts of the data to prevent all data from being unusable due to data errors.
9. A binary stream transmission encryption system based on WebSocket, characterized in that: Used to implement the WebSocket-based binary stream transmission encryption method described in any one of claims 1 to 8.
10. A storage medium, characterized in that: When the storage medium is running on a computer, the steps of the binary stream transmission encryption method based on WebSocket according to any one of claims 1 to 8 are executed.