Encryption method, electronic equipment and storage medium
By blocking the plaintext files and key files, and using the key block to encrypt the corresponding plaintext blocks, the problem of waste of computing power caused by insufficient key amount is solved, and efficient encryption operations and low-cost key management are realized.
Patent Information
- Application Number
- CN202510053081.X
- Authority / Receiving Office
- CN · China
- Patent Type
- Applications(China)
- Current Assignee / Owner
- Filing Date
- 2025-01-14
- Publication Date
- 2025-04-25
AI Technical Summary
In the encryption process, due to insufficient key amount during the prior art, the system occupies computing power when executing the key expansion algorithm, and cannot maximize the application of computing power to encryption operations. Especially when the traffic volume is full, the computing power is significantly wasted.
By cutting the plaintext files and key files in blocks, encrypting the corresponding plaintext blocks with key blocks and splicing them, the plaintext is encrypted in proportion, avoiding key expansion, and improving the encryption operation efficiency and computing power utilization rate.
Without key expansion, the efficiency of encryption operations and computing power utilization are improved, the cost of key management is reduced, and the working efficiency of encryption operations required during data transmission is improved.
Smart Images

Figure CN119892463A_ABST
Abstract
Description
Technical Field
[0001] The present application relates to the field of communication technology, and in particular to an encryption method, electronic equipment and storage medium. Background Art
[0002] At present, in the existing communication technology, in data communication scenarios with high security requirements, data needs to be encrypted before transmission, and in the process of encryption operation, the keys used are constantly consumed. In the era of increasingly digital communication, in the communication process, keys are the core of data encryption and decryption, and the security of keys is directly related to the security and integrity of communication data transmission. With the development of communication services and the surge in data volume, the risk of data leakage has increased, and the complexity and difficulty of key management have also been increasing. Once the key is leaked, the communication data will be illegally accessed and tampered with. Professional key management services are an important means to protect information security, which requires a lot of manpower, material and financial resources and is costly. Therefore, effective key management is of great significance to preventing data leakage, protecting user privacy and complying with laws and regulations. At the same time, how to use the limited amount of keys stored locally to maximize the utilization of encryption operations required in the communication process is also a key factor for enterprises to reduce key management costs.
[0003] In the prior art, the AES key expansion algorithm, hash functions such as SHA-256, Password-Based Key Derivation Function 2 (PBKDF2), and bcrypt are generally used as key expansion algorithms. These algorithms have their own characteristics and are suitable for different security requirements and application scenarios. For example, the AES key expansion algorithm is suitable for applications that require high-speed encryption, while PBKDF2 and bcrypt are suitable for applications that require high security, such as online accounts and financial transactions. Hash functions are suitable for scenarios where the original key needs to be converted into a longer key to enhance security. Patent application number 2023112289577 proposes a key expansion method to solve the problem of insufficient existing key quantity in the encryption process. The key is first expanded to the same length as the plaintext through the expansion algorithm, and then a 1:1 encryption operation is performed. However, the system needs to occupy a certain amount of computing power to execute the key expansion algorithm, so that the system cannot maximize the application of computing power to encryption operations. This will not have any impact on the situation where the encryption business volume is small, but when the business volume is fully loaded, any computing power will be particularly important.
[0004] Therefore, how to improve the utilization of computing power in encryption operations by managing a limited number of keys has become a technical problem that needs to be solved urgently. Summary of the invention
[0005] Purpose of the invention: The present application provides an encryption method, an electronic device and a storage medium, which obtain a ciphertext file by cutting and processing the plaintext file and the key file into blocks, encrypting the corresponding plaintext blocks with the key blocks and then splicing them together; without key expansion, the plaintext is encrypted in proportion, the efficiency of the encryption operation is effectively improved, the utilization rate of computing power in the encryption operation is improved, and the key management cost is reduced.
[0006] Technical solution: The present invention provides an encryption method, which comprises the following steps:
[0007] Step 1: Obtain a plaintext file and obtain plaintext file data; obtain a first key file of length l based on the plaintext file data, wherein the plaintext file data includes a plaintext length L, an encryption ratio 1:R and a basic encryption unit n;
[0008] Step 2: Split the plaintext file into m plaintext blocks, and extract m key blocks from the first key file;
[0009] Step 3: Use the extracted m key blocks to encrypt the m plaintext blocks respectively to obtain the ciphertext file.
[0010] As an improvement of the present invention, in step 1, the specific process of obtaining the first key file based on the plaintext file data is:
[0011] A first key file length l=L / R is obtained based on the plaintext length and the encryption ratio, and a first key file with a key file length of l=L / R is obtained from a local key pool.
[0012] As an improvement of the present invention, in step 2, the specific process of dividing the plaintext file into m plaintext blocks is:
[0013] The plaintext file is divided based on the basic encryption unit n, and the number of plaintext blocks after division is calculated: m = [l / n] + 2;
[0014] Add an identifier to the 1st to mth plaintext blocks, where the identifier indicates the position of the plaintext block in the plaintext file, wherein the lengths of the 1st to (m-1)th plaintext blocks are all n, and the length of the mth plaintext block is L-(m-1)*n.
[0015] As an improvement of the present invention, in step 2, the specific process of extracting m key blocks from the first key file is: for the x-th plaintext block, obtaining key position information in the first key file, and generating the x-th key block according to the key position information; wherein the key position information includes a start bit and an end bit;
[0016] The specific process of obtaining the key location information in the first key file includes:
[0017] When 1≤x≤m-1, the starting bit and ending bit of the x-th key block in the first key file are:
[0018] The starting bit addx0 = (x-1)*n / R+1, the ending bit addx1 = (x-1)*n / R+n;
[0019] When x=m, the starting and ending positions of the xth key block in the key file are:
[0020] The starting bit addx0 = (x-1)*n / R+1, the ending bit addx1 = l;
[0021] Add identifiers to the 1st to mth key blocks, and the identifiers of the key blocks correspond one-to-one with the identifiers of the plaintext blocks.
[0022] As an improvement of the present invention, the specific process of step 3 is as follows:
[0023] Step 3-1: Obtain the 1st to mth plaintext blocks in sequence from the plaintext file, and at the same time, obtain the corresponding 1st to mth key blocks in sequence according to the plaintext block identifiers;
[0024] Step 3-2: Use the 1st to mth key blocks to perform encryption operations on the 1st to mth plaintext blocks respectively:
[0025] Step 3-2-1: According to the identifier of the plaintext block in the plaintext file, obtain the corresponding 1st to (m-1) key blocks, use the 1st to (m-1) key blocks to perform encryption operations on the corresponding 1st to (m-1) plaintext blocks, and obtain the 1st to (m-1) ciphertext blocks;
[0026] Step 3-2-2: According to the identifier of the plaintext block in the plaintext file, obtain the corresponding m-th key block, divide the m-th plaintext block equally according to the encryption ratio 1:R, and obtain R sub-plaintext blocks; use the m-th key block to perform encryption operations on the R sub-plaintext blocks respectively, and assemble the encrypted ciphertext blocks into the m-th ciphertext block;
[0027] Step 3-3: Add identifiers to the 1st to mth ciphertext blocks, and the identifiers of the ciphertext blocks correspond one-to-one with the identifiers of the key blocks and the identifiers of the plaintext blocks;
[0028] Step 3-4: concatenate the 1st to mth ciphertext blocks to form a ciphertext file; wherein the ciphertext file also includes the identification and key addressing information of each ciphertext block, and the key addressing information is the identification of each key block and the corresponding key location information.
[0029] As an improvement of the present invention, in step 3-2, performing encryption operation on the first to m plaintext blocks includes:
[0030] Perform encryption operations on the first to mth plaintext blocks in sequence according to the identifiers using the key blocks with corresponding identifiers; or
[0031] Multiple threads are allocated to the 1st to mth plaintext blocks, and the multiple threads simultaneously perform encryption operations using key blocks with corresponding identifiers.
[0032] As an improvement of the present invention, the specific process of step 3 is as follows:
[0033] Obtaining key blocks 1 to m from the first key file, and sequentially concatenating the key blocks 1 to m to form a second key file;
[0034] The second key file is used to perform an encryption operation on the entire plaintext file to obtain a ciphertext file, wherein the ciphertext file also includes key addressing information, and the key addressing information is an identifier of each key block and corresponding key location information.
[0035] As an improvement of the present invention, an electronic device is also provided, the electronic device comprising:
[0036] a memory for storing a set of instructions; and
[0037] At least one processor is configured to execute the set of instructions so that the electronic device executes the method as described above.
[0038] As an improvement of the present invention, a non-transitory computer-readable storage medium is further provided, wherein the non-transitory computer-readable storage medium stores a set of instructions for a computer, and the set of instructions is used to enable the computer to perform the method described above when executed.
[0039] Beneficial effects:
[0040] 1. Without key expansion, the plaintext can be encrypted proportionally, effectively improving the utilization rate of computing power in encryption operations;
[0041] 2. By encrypting in proportion, the storage space occupied by key files in the system is reduced, and the consumption of required keys is also reduced, thereby reducing the number of key replenishments during encryption operations, effectively reducing key management costs, and improving the efficiency of encryption operations required during data transmission;
[0042] 3. By encrypting the plaintext proportionally, the plaintext file is divided into multiple plaintext blocks, and the corresponding key blocks are extracted. The key blocks used to encrypt different plaintext blocks are non-repetitive, the encryption security is high, and the privacy of the transmitted data is guaranteed. BRIEF DESCRIPTION OF THE DRAWINGS
[0043] 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.
[0044] Figure 1 This is a flowchart of the encryption method of this application;
[0045] Figure 2 This is a flowchart of step 2 of the encryption method of this application;
[0046] Figure 3 A schematic diagram of key position information of a key block according to an embodiment of the present invention;
[0047] Figure 4 It is a schematic diagram of the structure of the 4th plaintext block and the 4th key block of an embodiment of the present invention. DETAILED DESCRIPTION
[0048] 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.
[0049] like Figure 1 As shown, the present invention provides an encryption method, which comprises the following steps:
[0050] Step 1: Obtain a plaintext file and obtain plaintext file data; obtain a first key file with a length of l based on the plaintext file data, wherein the plaintext file data includes a plaintext length L, an encryption ratio 1:R and a basic encryption unit n. In an embodiment of the present invention, n is generally 8 bytes (64 bits, the basic addressing unit of the system). In an embodiment of the present invention, the first key file is obtained according to the plaintext length L and the encryption ratio 1:R: first, the length l=L / R of the first key file is calculated; then, the first key file with a key file length of l=L / R is obtained from the local key pool. The size of the key file is determined according to the encryption ratio. When R>1, the length of the key file is much smaller than the length of the plaintext file with a length of 1:1, so the local storage space can be effectively saved. In an embodiment of the present invention, the encryption ratio can be selected by the user according to the needs, such as 1:2, 1:3, etc. It should be noted that during the communication process, the key file cannot be transmitted together with the encrypted ciphertext in the communication link. Therefore, the first key file is obtained from the local key pool at the data sender, and the key location information of the first key file is sent to the data receiver, and the data receiver obtains it from the local key pool according to the key location information. The local key pools at both the data sender and the data receiver should be deployed synchronously in advance. In an embodiment of the present invention, the key may be a quantum key.
[0051] For example, for the first plaintext file with a plaintext length of 36 bits, an encryption ratio of 1:2, and an encryption unit of 8, the specific process of step 1 above is:
[0052] Obtain the first plaintext file and obtain the plaintext file data: the plaintext length is 36 bits, the encryption ratio is 1:2, and the encryption unit is 8; calculate the length of the first key file l=36 / 2=18, and obtain the first key file with a key file length of 18 from the local key pool.
[0053] Step 2: Split the plaintext file into m plaintext blocks, and extract m key blocks from the first key file;
[0054] like Figure 2 As shown, in the embodiment of the present invention, the specific process of dividing the plaintext file into m plaintext blocks is:
[0055] Based on the basic encryption unit n, the plaintext file is divided, and the number of plaintext blocks after division is calculated: m = [l / n] + 2; an identifier is added to the 1st to mth plaintext blocks, and the identifier indicates the position of the plaintext block in the plaintext file, wherein the lengths of the 1st to (m-1)th plaintext blocks are all n, and the length of the mth plaintext block is L-(m-1)*n. For example, when n = 8, the position of the 1st plaintext block in the plaintext file is that the starting bit is 1 and the ending bit is 8.
[0056] In an embodiment of the present invention, the specific process of extracting m key blocks from the first key file is:
[0057] For the x-th plaintext block, key position information is obtained in the first key file, and the x-th key block is generated according to the key position information; wherein the key position information includes a start bit and an end bit;
[0058] The specific process of obtaining the key location information in the first key file includes:
[0059] When 1≤x≤m-1, the starting bit and ending bit of the x-th key block in the first key file are:
[0060] The starting bit addx0 = (x-1)*n / R+1, the ending bit addx1 = (x-1)*n / R+n;
[0061] When x=m, the starting and ending positions of the xth key block in the key file are:
[0062] The starting bit addx0 = (x-1)*n / R+1, the ending bit addx1 = l;
[0063] Substitute x from 1 to m into the above process in sequence to obtain the 1st to mth key blocks.
[0064] Add identifiers to the 1st to mth key blocks, and the identifiers of the key blocks correspond to the identifiers of the plaintext blocks one by one. For example, the 1st plaintext block in the plaintext file corresponds to the 1st key block in the first key file, the 2nd plaintext block in the plaintext file corresponds to the 2nd key block in the first key file, and so on, until the mth plaintext block in the plaintext file corresponds to the mth key block in the first key file.
[0065] The above step 2 is still described in detail using the first plaintext file in step 1 as an example:
[0066] The plaintext file is segmented based on the basic encryption unit 8, and the number of plaintext blocks after segmentation is calculated: m = [l / n] + 2 = [18 / 8] + 2 = 4;
[0067] Add identifiers to the 1st to 4th plaintext blocks, where the identifiers indicate the positions of the plaintext blocks in the first plaintext file, wherein the lengths of the 1st to 3rd plaintext blocks are all 8, and the length of the 4th plaintext block is L-(m-1)*n=36-(4-1)*8=12.
[0068] Extract 4 key blocks from the first key file. The key position information of the 1st to 4th key blocks is as follows: Figure 3 As shown in Table 1 below:
[0069] Table 1. Plaintext location information and key location information of the first plaintext file
[0070]
[0071] Step 3: Use the extracted m key blocks to encrypt the m plaintext blocks respectively to obtain the ciphertext file.
[0072] In one embodiment of the present invention, the specific process of step 3 is:
[0073] Step 3-1: Obtain the 1st to mth plaintext blocks in sequence from the plaintext file, and at the same time, obtain the corresponding 1st to mth key blocks in sequence according to the plaintext block identifiers;
[0074] Step 3-2: Use the 1st to mth key blocks to perform encryption operations on the 1st to mth plaintext blocks respectively:
[0075] Step 3-2-1: According to the identifier of the plaintext block in the plaintext file, obtain the corresponding 1st to (m-1) key blocks, use the 1st to (m-1) key blocks to perform encryption operations on the corresponding 1st to (m-1) plaintext blocks, and obtain the 1st to (m-1) ciphertext blocks;
[0076] Step 3-2-2: According to the identifier of the plaintext block in the plaintext file, obtain the corresponding m-th key block, divide the m-th plaintext block equally according to the encryption ratio 1:R, and obtain R sub-plaintext blocks; use the m-th key block to perform encryption operations on the R sub-plaintext blocks respectively, and assemble the encrypted ciphertext blocks into the m-th ciphertext block;
[0077] The length of the last plaintext block and the length of the last key block should satisfy the encryption ratio of 1:R. The last key block is used to encrypt the first part of the last plaintext block after it is equally divided according to the encryption ratio of 1:R at a ratio of 1:1. The last key block is repeatedly used to encrypt the other equally divided parts until the encryption operation of the entire plaintext is completed.
[0078] In step 3-2, performing encryption operation on the first to mth plaintext blocks includes:
[0079] The encryption operation is sequentially performed on the first to m plaintext blocks according to the identification using the key blocks with corresponding identifications; or, multiple threads are assigned to the first to m plaintext blocks, and the multiple threads simultaneously perform the encryption operation using the key blocks with corresponding identifications. In an embodiment of the present invention, multiple threads are assigned to all plaintext blocks, and all plaintext blocks are encrypted in multiple threads at the same time, which effectively improves the encryption efficiency and accelerates the speed of forming the ciphertext file.
[0080] Step 3-3: Add identifiers to the 1st to mth ciphertext blocks, and the identifiers of the ciphertext blocks correspond one-to-one with the identifiers of the key blocks and the identifiers of the plaintext blocks;
[0081] Step 3-4: concatenate the 1st to mth ciphertext blocks to form a ciphertext file; wherein the ciphertext file also includes the identification and key addressing information of each ciphertext block, and the key addressing information is the identification of each key block and the corresponding key location information.
[0082] In this step, the 1-m ciphertext blocks can be spliced in a sequential or random form to form a ciphertext file. In the random form, after the ciphertext file is obtained by a bad user, the order is disordered and there is no valid identifier to sort it to form a correct and complete ciphertext file, so the possibility of cracking is lower; the data recipient can reorder the key blocks according to the identifier of the key block and splice them to form a correct and complete ciphertext file.
[0083] In another embodiment of the present invention, the specific process of step 3 may also be:
[0084] Obtaining key blocks 1 to m from the first key file, and sequentially concatenating the key blocks 1 to m to form a second key file;
[0085] The second key file is used to perform an encryption operation on the entire plaintext file to obtain a ciphertext file, wherein the ciphertext file also includes key addressing information, and the key addressing information is the identifier of each key block and the corresponding key location information. It can be seen that the ciphertext file to be transmitted does not contain the key file itself, and only the key location information and related identifiers are transmitted to prevent the data from being intercepted and cracked during the transmission process.
[0086] The above step 3 is still described in detail using the first plaintext file in step 1 as an example:
[0087] Use the extracted 1st to 4th key blocks to perform encryption operations on the 1st to 4th plaintext blocks respectively: First, use the 1st to 3rd key blocks to perform encryption operations on the 1st to 3rd plaintext blocks respectively, and the lengths of the plaintext blocks and key blocks used in the encryption operations are the same. Then, as Figure 4 As shown, looking at the 4th key block and the 4th plaintext block, it can be seen that the 4th key block and the 4th plaintext block meet the encryption ratio of 1:2, and the encryption operation of the 4th plaintext block can be completed by using the 4th key block twice. The 1st to 4th ciphertext blocks generated after the encryption operation are concatenated, and the identifier of each key block and the corresponding key position information are packaged to form a ciphertext file.
[0088] It should be noted that the encryption operations of the 1st to 4th key blocks on the 1st to 4th plaintext blocks can be performed synchronously in different threads, or can be performed sequentially.
[0089] The present invention further provides an electronic device, characterized in that the electronic device comprises:
[0090] a memory for storing a set of instructions; and
[0091] At least one processor is configured to execute the set of instructions so that the electronic device executes the method as described above.
[0092] The present invention also provides a non-transitory computer-readable storage medium, characterized in that the non-transitory computer-readable storage medium stores a set of instructions for a computer, and the set of instructions is used to enable the computer to perform the method described above when executed.
Claims
1. An encryption method, characterized in that: The encryption method comprises the following steps: Step 1: Obtain a plaintext file and obtain plaintext file data; obtain a first key file of length l based on the plaintext file data, wherein the plaintext file data includes a plaintext length L, an encryption ratio 1:R and a basic encryption unit n; Step 2: Split the plaintext file into m plaintext blocks, and extract m key blocks from the first key file; Step 3: Use the extracted m key blocks to encrypt the m plaintext blocks respectively to obtain the ciphertext file.
2. The encryption method according to claim 1, characterized in that: In step 1, the specific process of obtaining the first key file based on the plaintext file data is: A first key file length l=L / R is obtained based on the plaintext length and the encryption ratio, and a first key file with a key file length of l=L / R is obtained from a local key pool.
3. The encryption method according to claim 1, characterized in that: In step 2, the specific process of dividing the plaintext file into m plaintext blocks is: The plaintext file is divided based on the basic encryption unit n, and the number of plaintext blocks after division is calculated: m = [l / n] + 2; Add an identifier to the 1st to mth plaintext blocks, where the identifier indicates the position of the plaintext block in the plaintext file, wherein the lengths of the 1st to (m-1)th plaintext blocks are all n, and the length of the mth plaintext block is L-(m-1)*n.
4. The encryption method according to claim 3, characterized in that: In step 2, the specific process of extracting m key blocks from the first key file is: for the x-th plaintext block, obtaining key position information in the first key file, and generating the x-th key block according to the key position information; wherein the key position information includes a start bit and an end bit; The specific process of obtaining the key location information in the first key file includes: When 1≤x≤m-1, the starting bit and ending bit of the x-th key block in the first key file are: The starting bit addx0 = (x-1)*n / R+1, the ending bit addx1 = (x-1)*n / R+n; When x=m, the starting and ending positions of the xth key block in the key file are: The starting bit addx0 = (x-1)*n / R+1, the ending bit addx1 = l; Add identifiers to the 1st to mth key blocks, and the identifiers of the key blocks correspond one-to-one with the identifiers of the plaintext blocks.
5. The encryption method according to claim 4, characterized in that: The specific process of step 3 is as follows: Step 3-1: Obtain the 1st to mth plaintext blocks in sequence from the plaintext file, and at the same time, obtain the corresponding 1st to mth key blocks in sequence according to the plaintext block identifiers; Step 3-2: Use the 1st to mth key blocks to perform encryption operations on the 1st to mth plaintext blocks respectively: Step 3-2-1: According to the identifier of the plaintext block in the plaintext file, obtain the corresponding 1st to (m-1) key blocks, use the 1st to (m-1) key blocks to perform encryption operations on the corresponding 1st to (m-1) plaintext blocks, and obtain the 1st to (m-1) ciphertext blocks; Step 3-2-2: According to the identifier of the plaintext block in the plaintext file, obtain the corresponding m-th key block, divide the m-th plaintext block equally according to the encryption ratio 1:R, and obtain R sub-plaintext blocks; use the m-th key block to perform encryption operations on the R sub-plaintext blocks respectively, and assemble the encrypted ciphertext blocks into the m-th ciphertext block; Step 3-3: Add identifiers to the 1st to mth ciphertext blocks, and the identifiers of the ciphertext blocks correspond one-to-one with the identifiers of the key blocks and the identifiers of the plaintext blocks; Step 3-4: concatenate the 1st to mth ciphertext blocks to form a ciphertext file; wherein the ciphertext file also includes the identification and key addressing information of each ciphertext block, and the key addressing information is the identification of each key block and the corresponding key location information.
6. The encryption method according to claim 5, characterized in that: In step 3-2, performing encryption operation on the first to mth plaintext blocks includes: Perform encryption operations on the first to mth plaintext blocks in sequence according to the identifiers using the key blocks with corresponding identifiers; or Multiple threads are allocated to the 1st to mth plaintext blocks, and the multiple threads simultaneously perform encryption operations using key blocks with corresponding identifiers.
7. The encryption method according to claim 4, characterized in that: The specific process of step 3 is as follows: Obtaining key blocks 1 to m from the first key file, and sequentially concatenating the key blocks 1 to m to form a second key file; The second key file is used to perform an encryption operation on the entire plaintext file to obtain a ciphertext file, wherein the ciphertext file also includes key addressing information, and the key addressing information is an identifier of each key block and corresponding key location information.
8. An electronic device, characterized in that: The electronic device: a memory for storing a set of instructions; and At least one processor is configured to execute the set of instructions so that the electronic device performs the method as claimed in any one of claims 1 to 7.
9. A non-transitory computer-readable storage medium, characterized in that: The non-transitory computer-readable storage medium stores a set of instructions for a computer, which, when executed, causes the computer to perform the method of any one of claims 1 to 7.