Key arrangement method and device based on prefetched key and storage medium
Through the offset pointer position and splicing method of the key file, combined with the memory mapping and ftruncate function, the problem of prefetched keys being unable to be reused is solved, the recovery and organization of key resources is realized, and key waste and asynchronous consumption are avoided. issues, ensuring secure storage and synchronization of key files.
Patent Information
- Application Number
- CN202410851311.2
- Authority / Receiving Office
- CN · China
- Patent Type
- Applications(China)
- Current Assignee / Owner
- Filing Date
- 2024-06-28
- Publication Date
- 2024-08-30
Smart Images

Figure CN118573370A
Abstract
Description
Technical Field
[0001] The present application relates to the technical field of key processing, and in particular to a key organization method, device and storage medium based on pre-fetched keys. Background Art
[0002] In the process of quantum key encryption and decryption, due to the large amount of keys required by applications such as audio and video, pre-fetching is used to obtain keys during use. However, the amount of pre-fetched keys is often too large, resulting in the inability to reuse the pre-fetched keys after returning to the memory. The reason is that if the key is returned to the original position, and the pointer for extracting the key has moved to the next position, the returned key cannot be used. Even if the pointer position is simply offset to the position of the returned key, the subsequent key hole cannot be solved. At present, the only option is to delete the redundant pre-fetched keys, resulting in a waste of keys. At the same time, it also wastes the bandwidth and processing resources of the client, server, and key relay base station, accelerates the consumption of keys, and will bring more frequent key downloads, synchronization bandwidth, and resource consumption.
[0003] This requires considering returning the pre-fetched unused keys to the key pool of each network element, but the key pool is stored in the form of files, and the pre-fetched keys need to be sorted, which is to merge the keys. At this time, since the keys are likely to be in the process of synchronous consumption and are distinguished between the upstream and downstream key pools, they cannot be returned directly, so the key files in the key pool need to be sorted, and the consumption progress of the key files is different, which brings great trouble to the key sorting. Therefore, how to realize the key sorting of the redundant pre-fetched keys has become a technical problem that needs to be solved urgently. Summary of the invention
[0004] Purpose of the invention: In order to solve the related technical problems raised in the background technology, the present invention provides a key organization method, device and storage medium based on pre-fetched keys, which recycles and organizes the remaining keys to be returned in the pre-fetched keys through the offset pointer position of the key file, key splicing and other means, thereby avoiding the waste of key resources.
[0005] Technical solution: The present invention provides a key arrangement method based on pre-fetching keys, comprising the following steps:
[0006] (1) Mapping the key file in the key pool from the disk to the memory by memory mapping, wherein the key file stores key data with a fixed byte length;
[0007] (2) obtaining a pre-fetched key from the key file according to the offset pointer of the key file for use;
[0008] (3) After use, the remaining keys to be returned in the pre-fetched keys are recovered and sorted, and the total amount of remaining keys in the key file is updated.
[0009] Furthermore, the specific process of recycling and sorting the remaining keys to be returned in the pre-fetched keys is as follows:
[0010] 1) According to the current offset pointer position of the key file and the key length stored in the key file, obtain the key length of the remaining unused key in the key file;
[0011] 2) The key to be returned is concatenated to the tail of the remaining unused keys to form the total amount of remaining keys;
[0012] 3) Obtain the key length of the total amount of remaining keys, and compare it with the key length stored in the key file. If the key length of the total amount of remaining keys is greater than the key length stored in the key file, modify the size of the key file in the memory so that the key length stored in the key file is greater than or equal to the key length of the total amount of remaining keys, and then store the total amount of remaining keys in the key file; if the key length of the total amount of remaining keys is less than or equal to the key length stored in the key file, offset the position of the remaining unused keys in the key file so that the keys to be returned are stored in the key file, and then update the offset pointer position in the key file and update the record of the total amount of remaining keys of the current key file.
[0013] Furthermore, the specific process of recycling and sorting the remaining keys to be returned in the pre-fetched keys is as follows:
[0014] 1) According to the current offset pointer position of the key file and the key length stored in the key file, the key length of the remaining unused key and the key length of the used key in the key file are obtained;
[0015] 2) Obtain the key length of the key to be returned, and compare it with the key length of the used key in the key file; if the key length of the key to be returned is less than or equal to the key length of the used key, then concatenate the key to be returned to the header of the remaining unused key to form the total amount of remaining keys, and then update the offset pointer position in the key file and update the record of the total amount of remaining keys in the current key file; if the key length of the key to be returned is greater than the key length of the used key, then concatenate the key to be returned to the header of the remaining unused key to form the total amount of remaining keys, and then modify the size of the key file in the memory so that the key length stored in the key file is greater than or equal to the key length of the total amount of remaining keys, and then store the total amount of remaining keys in the key file.
[0016] Furthermore, if the key length of the key to be returned is greater than the key length of the used key, or if: the key to be returned is split into a first part and a second part of the key to be returned, wherein the first part of the key to be returned has the same key length as the used key, the first part of the key to be returned is stored in the current key file, the offset pointer position in the current key file is updated, and the total key amount of the current key file is updated; the second part of the key to be returned is stored in the tail of the previous key file, and the offset pointer position in the previous key file is updated, and the total key amount of the previous key file is updated.
[0017] Furthermore, modifying the size of the key file in the memory refers to modifying the size of the key file in the memory through the ftruncate function.
[0018] The present invention also includes a computer device, which includes at least a processor and a memory, and the processor is used to implement the key arrangement method as described in any one of the above claims when executing a computer program stored in the memory.
[0019] The present invention also includes a computer-readable storage medium storing a computer program, wherein the computer program, when executed by a processor, implements the key arrangement method as claimed in any one of the above claims.
[0020] Beneficial effects of the present invention:
[0021] 1. The remaining keys to be returned in the pre-fetched keys are recovered and sorted through the offset pointer position of the key file, key splicing, etc., avoiding the waste of key resources;
[0022] 2. The size of the key file mapped to the memory is modified through the ftruncate function, which solves the problem that the total amount of keys is greater than the size of the key file after the keys to be returned are merged with the remaining unused keys, and realizes the secure storage of the merged keys. BRIEF DESCRIPTION OF THE DRAWINGS
[0023] In order to more clearly illustrate the technical solutions in the embodiments of the present application, the drawings required for use in the description of the embodiments will be briefly introduced below. Obviously, the drawings described below are only some embodiments of the present application. For ordinary technicians in this field, other drawings can be obtained based on these drawings without paying creative labor.
[0024] Figure 1 This is a schematic diagram of key arrangement in Example 1;
[0025] Figure 2 This is a schematic diagram of key arrangement in Example 2;
[0026] Figure 3This is a schematic diagram of key organization in Example 3. DETAILED DESCRIPTION
[0027] In order to make the purpose, technical solutions and advantages of the present application clearer, the present application will be further described in detail below in conjunction with the accompanying drawings. Obviously, the described embodiments are only part of the embodiments of the present application, rather than all the embodiments. Based on the embodiments in the present application, all other embodiments obtained by ordinary technicians in this field without making creative work are within the scope of protection of the present application.
[0028] As mentioned in the background technology, in the process of using quantum key encryption and decryption, due to the large amount of keys required by applications such as audio and video, the keys are obtained by pre-fetching during use. However, the amount of pre-fetched keys is often too large during use, resulting in the pre-fetched keys being unable to be reused after being returned to the memory. Therefore, how to organize the redundant pre-fetched keys has become a technical problem that needs to be solved urgently.
[0029] Example 1
[0030] In order to solve the above technical problems, the present invention proposes a key arrangement method based on pre-fetching keys, comprising the following steps:
[0031] (1) Mapping the key file in the key pool from the disk to the memory by memory mapping, wherein the key file stores key data with a fixed byte length;
[0032] (2) obtaining a pre-fetched key from the key file according to the offset pointer of the key file for use;
[0033] (3) After the use is completed, the remaining keys to be returned in the pre-fetched keys are recycled and sorted. The specific process is as follows:
[0034] 1) According to the current offset pointer position of the key file and the key length stored in the key file, obtain the key length of the remaining unused key in the key file;
[0035] 2) The key to be returned is spliced at the tail of the remaining unused keys to form the total amount of remaining keys; the key to be returned is spliced at the tail, mainly considering a situation that the encryption end and the decryption end may be out of sync at a certain moment. After the encryption of the encryption end data is completed, the key offset position has shifted backward, and the sent data is transmitted on the network and has not yet reached the decryption end; at this time, the key offset of the decryption end key file is smaller than the offset of the encryption end. At this time, if the key is sorted, the key pool between the two network elements will have problems and will not be synchronized; to solve this problem, the key to be returned is appended to the tail, so that neither the encryption end nor the decryption end needs to care about the current key offset, and only needs to append and merge the tail in the remaining data;
[0036] 3) Obtain the key length of the total amount of remaining keys, and compare it with the key length stored in the key file. If the key length of the total amount of remaining keys is greater than the key length stored in the key file, modify the size of the key file in the memory so that the key length stored in the key file is greater than or equal to the key length of the total amount of remaining keys, and then store the total amount of remaining keys in the key file; wherein, modifying the size of the key file in the memory refers to modifying the size of the key file in the memory through the ftruncate function. Because the size of the key file stored on the disk is fixed, if the current key file consumption is less than the key to be returned, then after the merger, the key length of the total amount of remaining keys will be greater than the key length stored in the key file, and the key file is mapped from the disk to the memory through memory mapping; after the file is mapped, modify the key file size through the ftruncate function, and then perform key sorting, which can solve the problem that the key length of the total amount of remaining keys is greater than the key length stored in the key file;
[0037] If the key length of the remaining total key amount is less than or equal to the key length stored in the key file, the remaining unused keys in the key file are offset so that the keys to be returned are stored in the key file, and then the offset pointer position in the key file is updated and the total remaining key amount of the current key file is updated, such as Figure 1 As shown. Based on the key arrangement method, the present invention recycles and arranges the remaining keys to be returned in the pre-fetched keys by means of the offset pointer position of the key file, key splicing, etc., thereby avoiding the waste of key resources; at the same time, the size of the key file mapped to the memory is modified by the ftruncate function, thereby solving the problem that the total amount of keys is greater than the size of the key file after the keys to be returned are merged with the remaining unused keys, thereby realizing the secure storage of the merged keys.
[0038] An embodiment of the present invention further provides a computer device, which includes at least a processor and a memory, wherein the memory stores a computer program, and the processor is used to implement the steps in the key organization method of the embodiment of the present invention when executing the computer program stored in the memory.
[0039] An embodiment of the present invention further provides a computer-readable storage medium, on which a computer program is stored. When the computer program is executed by a processor, the steps in the key arrangement method of the embodiment of the present invention are implemented.
[0040] Example 2
[0041] This embodiment is basically the same as the embodiment 1, except that: after the step (3) of the key arrangement method is used, the remaining keys to be returned in the pre-fetched keys are recycled and arranged, and the specific process is as follows:
[0042] 1) According to the current offset pointer position of the key file and the key length stored in the key file, the key length of the remaining unused key and the key length of the used key in the key file are obtained;
[0043] 2) Obtain the key length of the key to be returned, and compare it with the key length of the key already used in the key file. If the key length of the key to be returned is less than or equal to the key length of the key already used, concatenate the key to be returned to the header of the remaining unused key to form the total amount of remaining keys, and then update the offset pointer position in the key file and update the total amount of remaining keys in the current key file, such as Figure 2 As shown; if the key length of the key to be returned is greater than the key length of the used key, the key to be returned is concatenated with the header of the remaining unused key to form the total amount of remaining keys, and then the size of the key file in the memory is modified so that the key length stored in the key file is greater than or equal to the key length of the total amount of remaining keys, and then the total amount of remaining keys is stored in the key file; wherein, modifying the size of the key file in the memory refers to modifying the size of the key file in the memory through the ftruncate function.
[0044] Example 3
[0045] This embodiment is basically the same as the second embodiment, except that: after the step (3) of the key arrangement method is used, the remaining keys to be returned in the pre-fetched keys are recycled and arranged, and the specific process is as follows:
[0046] 1) According to the current offset pointer position of the key file and the key length stored in the key file, the key length of the remaining unused key and the key length of the used key in the key file are obtained;
[0047] 2) Obtain the key length of the key to be returned, and compare it with the key length of the used key in the key file. If the key length of the key to be returned is less than or equal to the key length of the used key, then splice the key to be returned to the header of the remaining unused key to form the remaining key amount, and then update the offset pointer position in the key file and update the record of the remaining key amount of the current key file; if the key length of the key to be returned is greater than the key length of the used key, split the key to be returned to obtain the first part and the second part of the key to be returned, wherein the first part of the key to be returned has the same key length as the used key, store the first part of the key to be returned in the current key file, update the offset pointer position in the current key file and update the record of the key amount of the current key file, at this time, the offset pointer position in the current key file is at the initial position, and the key amount of the current key file is also the storage key amount of the current key file, and the key of the current key file is not consumed; store the second part of the key to be returned in the tail of the previous key file, and update the offset pointer position in the previous key file and update the record of the key amount of the previous key file, such as Figure 3 shown.
Claims
1. A key arrangement method based on pre-fetching keys, characterized in that: The following steps are involved: (1) Mapping the key file in the key pool from the disk to the memory by memory mapping, wherein the key file stores key data with a fixed byte length; (2) obtaining a pre-fetched key from the key file according to the offset pointer of the key file for use; (3) After use, the remaining keys to be returned in the pre-fetched keys are recovered and sorted, and the total amount of remaining keys in the key file is updated.
2. A key arrangement method based on pre-fetching keys according to claim 1, characterized in that: The specific process of recycling and sorting the remaining keys to be returned in the pre-fetched keys is as follows: 1) According to the current offset pointer position of the key file and the key length stored in the key file, obtain the key length of the remaining unused key in the key file; 2) The key to be returned is concatenated to the tail of the remaining unused keys to form the total amount of remaining keys; 3) Obtain the key length of the remaining total key amount, and compare it with the key length stored in the key file. If the key length of the remaining total key amount is greater than the key length stored in the key file, modify the size of the key file in the memory so that the key length stored in the key file is greater than or equal to the key length of the remaining total key amount, and then store the remaining total key amount in the key file; If the key length of the total remaining key amount is less than or equal to the key length stored in the key file, the remaining unused keys in the key file are offset so that the keys to be returned are stored in the key file, and then the offset pointer position in the key file is updated and the total remaining key amount of the current key file is updated.
3. A key arrangement method based on pre-fetching keys according to claim 1, characterized in that: The specific process of recycling and sorting the remaining keys to be returned in the pre-fetched keys is as follows: 1) According to the current offset pointer position of the key file and the key length stored in the key file, the key length of the remaining unused key and the key length of the used key in the key file are obtained; 2) Obtain the key length of the key to be returned, and compare it with the key length of the key already used in the key file. If the key length of the key to be returned is less than or equal to the key length of the key already used, concatenate the key to be returned to the header of the remaining unused key to form the total amount of remaining keys, and then update the offset pointer position in the key file and update the total amount of remaining keys in the current key file; If the key length of the key to be returned is greater than the key length of the used key, the key to be returned will be concatenated with the header of the remaining unused keys to form the total amount of remaining keys, and then the size of the key file in the memory will be modified so that the key length stored in the key file is greater than or equal to the key length of the total amount of remaining keys, and then the total amount of remaining keys will be stored in the key file.
4. A key arrangement method based on pre-fetching keys according to claim 3, characterized in that: If the key length of the key to be returned is greater than the key length of the used key, or if: the key to be returned is split into a first part and a second part, wherein the first part of the key to be returned has the same key length as the used key, the first part of the key to be returned is stored in the current key file, the offset pointer position in the current key file is updated, and the total amount of keys in the current key file is updated; The second part of the key to be returned is stored in the tail of the previous key file, and the offset pointer position in the previous key file is updated and the total amount of keys in the previous key file is updated and recorded.
5. A key arrangement method based on pre-fetching keys according to claim 2 or 3, characterized in that: Modifying the size of the key file in the memory refers to modifying the size of the key file in the memory through the ftruncate function.
6. A computer device, characterized in that: The computer device comprises at least a processor and a memory, and the processor is used to implement the key organization method as described in any one of claims 1 to 5 when executing a computer program stored in the memory.
7. A computer-readable storage medium, characterized in that: It stores a computer program, which, when executed by a processor, implements the key arrangement method as described in any one of claims 1-5.
Citation Information
Cited By
Secret key storage method and device and electronic equipment
CN119483923A
