Method, device, electronic device and storage medium for encrypting data in a format-preserving manner
Through the use of SM4 key expansion and encryption permutation table, the problems of low encryption and decryption efficiency and high resource consumption in the prior art are solved, and an efficient encryption and decryption process is realized.
Patent Information
- Application Number
- CN202111485409.3
- Authority / Receiving Office
- CN · China
- Patent Type
- Patents(China)
- Current Assignee / Owner
- Priority Date
- 2021-04-02
- Filing Date
- 2021-12-07
- Publication Date
- 2025-02-21
- Estimated Expiration
- 2041-12-07
AI Technical Summary
The existing reserved format encryption and decryption algorithm is inefficient and has high resource consumption, which limits its application.
The extension key is obtained by performing SM4 key expansion operations on the algorithm key, and the SM4 encryption algorithm is used to generate an encrypted permutation table, and the plaintext data is modulo-added and calculated using the extension key, and the encrypted permutation table is queried to obtain the ciphertext data.
Reduces resource consumption in the retained format encryption and decryption process, improves encryption and decryption efficiency, and can use encrypted permutation tables and decrypted permutation tables multiple times without reconstructing, reducing the computational amount.
Smart Images

Figure CN114124359B_ABST
Abstract
Description
Technical Field
[0001] The present application relates to the field of data encryption technology, and in particular to a method for encrypting data with a preserved format, a device for encrypting data with a preserved format, a method for decrypting data with a preserved format, a device for decrypting data with a preserved format, an electronic device, and a storage medium. Background Art
[0002] Format-Preserving Encryption is a special encryption technology that can make the ciphertext format and length the same as the plaintext while achieving confidentiality. Format-Preserving Decryption is the reverse process of format-preserving encryption, which can decrypt the ciphertext data after format-preserving encryption into plaintext data. In some fields that have special requirements on the ciphertext format, such as data masking, format-preserving encryption and format-preserving decryption are often required. Existing format-preserving encryption and decryption algorithms have the problems of low implementation efficiency and high resource consumption, which limits the application of format-preserving encryption and decryption algorithms.
[0003] Therefore, how to reduce the resource consumption of the format-preserving encryption and decryption process and improve the encryption and decryption efficiency is a technical problem that those skilled in the art currently need to solve. Summary of the invention
[0004] The purpose of this application is to provide a method for encrypting data in a format-preserving manner, a device for encrypting data in a format-preserving manner, a method for decrypting data in a format-preserving manner, a device for decrypting data in a format-preserving manner, an electronic device and a storage medium, which can reduce the resource consumption in the format-preserving encryption and decryption process and improve the encryption and decryption efficiency.
[0005] In order to solve the above technical problems, the present application provides a method for encrypting data in a format-preserving manner, the method comprising:
[0006] Performing a key expansion operation based on SM4 on the algorithm key to obtain an extended key;
[0007] Encrypting characters in an alphabet using an SM4 encryption algorithm to obtain an encryption substitution table; wherein the alphabet is a set of multiple numeric characters;
[0008] The extended key is used to perform modular addition calculation on characters in the plaintext data to obtain an index value, and the index value is used to query the encryption permutation table to obtain ciphertext data with the same format as the plaintext data.
[0009] Optionally, characters in the alphabet are encrypted using the SM4 encryption algorithm to obtain an encryption substitution table, including:
[0010] Expand each character in the alphabet to obtain a target character with a preset number of digits, and use the SM4 encryption algorithm to encrypt each target character to obtain a target ciphertext;
[0011] All the target ciphertexts are sorted in increasing or decreasing order so that the ciphertext character corresponding to each character is the position of the target ciphertext of the character in the sorting sequence, thereby obtaining the encryption permutation table.
[0012] Optionally, performing a key expansion operation based on SM4 on the algorithm key to obtain an extended key includes:
[0013] Setting a maximum plaintext string length according to the application environment and determining the total number of characters in the alphabet;
[0014] The extended key is obtained by performing an SM4-based key expansion operation on the algorithm key according to the maximum plaintext character string length and the total number of characters in the alphabet.
[0015] Optionally, performing an SM4-based key expansion operation on the algorithm key according to the maximum plaintext character string length and the total number of characters in the alphabet to obtain the extended key includes:
[0016] The first parameter m and the second parameter r are determined according to the maximum plaintext string length maxlen and the total number of characters base in the alphabet; wherein the first parameter m is the minimum integer that meets the first constraint condition, and the first constraint condition is 2 m ≥base; the second parameter r is the minimum integer that satisfies the second constraint condition, and the second constraint condition is that r is greater than or equal to m multiplied by maxlen;
[0017] Use the SM4 key expansion algorithm to perform key expansion on k and (k+1) mod 2 respectively. 128 、(k+2)mod 2 128 , ..., (k+r)mod2 128 Performing key expansion to obtain multiple extended subkeys; wherein k is the algorithm key, and mod is an integer modulus operation;
[0018] A string concatenation operation is performed on all the extended subkeys to obtain the extended key.
[0019] Optionally, using the extended key to perform a modular addition calculation on characters in the plaintext data to obtain an index value includes:
[0020] The index value of each character in the plaintext data is calculated one by one according to the partial key content of the extended key.
[0021] The present application also provides a method for decrypting data in a format-preserving manner, comprising:
[0022] Acquire ciphertext data; wherein the ciphertext data is obtained by encrypting the plaintext data in a format-preserving manner by a data encryption end using an extended key and an encryption permutation table, wherein the extended key is obtained by performing an SM4-based key expansion operation on an algorithm key, and the encryption permutation table is obtained by encrypting characters in an alphabet using an SM4 encryption algorithm, wherein the alphabet is a set of multiple numeric characters;
[0023] Performing an inverse function transformation operation on the encryption permutation table to obtain a decryption permutation table;
[0024] The ciphertext data is decrypted using the extended key and the decryption permutation table to obtain the plaintext data.
[0025] Optionally, decrypting the ciphertext data using the extended key and the decryption permutation table to obtain the plaintext data includes:
[0026] Using the decryption permutation table to query the ciphertext character of each character in the ciphertext data, and calculating the plaintext character corresponding to each ciphertext character according to the ciphertext character and the extended key;
[0027] The plaintext data is determined based on all the plaintext characters.
[0028] The present application also provides a device for encrypting data in a format-preserving manner, the device comprising:
[0029] A key expansion module, used to generate an algorithm key using an encryption algorithm, and perform a key expansion operation on the algorithm key to obtain an expanded key;
[0030] An encryption permutation table construction module, used for encrypting characters in the alphabet according to the encryption algorithm to obtain an encryption permutation table;
[0031] The data encryption module is used to use the extended key to perform modular addition calculation on the characters in the plaintext data to obtain an index value, and use the index value to query the encryption permutation table to obtain ciphertext data with the same format as the plaintext data.
[0032] The present application also provides a device for decrypting data in a format-preserving manner, comprising:
[0033] A ciphertext acquisition module, used to acquire ciphertext data; wherein the ciphertext data is obtained by encrypting the plaintext data in a format-preserving manner by the data encryption end using an extended key and an encryption permutation table, wherein the extended key is obtained by performing a key expansion operation based on SM4 on the algorithm key, and the encryption permutation table is obtained by encrypting characters in an alphabet using an SM4 encryption algorithm, wherein the alphabet is a set of multiple numeric characters;
[0034] A decryption permutation table generation module, used for performing a function inverse transformation operation on the encryption permutation table to obtain a decryption permutation table;
[0035] The data decryption module is used to decrypt the ciphertext data using the extended key and the decryption permutation table to obtain the plaintext data.
[0036] The present application also provides a storage medium on which a computer program is stored, and the computer program implements the steps of the above method when executed.
[0037] The present application also provides an electronic device, including a memory and a processor, wherein the memory stores a computer program, and the processor implements the steps of the above method when calling the computer program in the memory.
[0038] The present application provides a method for format-preserving encrypted data, comprising: performing an SM4-based key expansion operation on an algorithm key to obtain an extended key; using the SM4 encryption algorithm to encrypt characters in an alphabet to obtain an encryption permutation table; wherein the alphabet is a set of multiple numeric characters; using the extended key to perform a modular addition calculation on characters in plaintext data to obtain an index value, and using the index value to query the encryption permutation table to obtain ciphertext data in the same format as the plaintext data.
[0039] The present application expands the algorithm key to obtain an extended key, and uses an encryption algorithm to encrypt characters in the alphabet to obtain an encryption permutation table. After obtaining the encryption permutation table, the ciphertext characters corresponding to each character in the plaintext data can be calculated through the encryption permutation table. The above-mentioned process of format-preserving encryption based on the encryption permutation table only involves modular addition operations and element access operations on the encryption permutation table. The encryption process has a small amount of calculation, which can reduce the consumption of resources in the process of encrypting data. On the basis of obtaining the encryption permutation table, multiple data encryption operations can be performed without repeatedly constructing the encryption permutation table, thereby reducing the consumption of resources in the process of encrypting data. Therefore, the present application can reduce the resource consumption of the format-preserving encryption process and improve encryption efficiency.
[0040] The present application also provides a method for decrypting data in a format-preserving manner, wherein a function inverse transformation operation is performed on the encryption permutation table to obtain a decryption permutation table, and an inverse operation of the format-preserving encryption is performed using an extended key and the decryption permutation table to obtain plaintext data. Based on the decryption permutation table, multiple data decryption operations can be performed without repeatedly constructing a decryption permutation table. The above-mentioned decryption process based on the decryption permutation table has a small amount of calculation, which can reduce the consumption of resources in the process of decrypting data. Therefore, the present application can reduce the resource consumption of decrypting format-preserving encrypted data and improve the decryption efficiency.
[0041] The present application also provides a device for encrypting data in a format-preserving manner, a device for decrypting data in a format-preserving manner, an electronic device and a storage medium, which have the above-mentioned beneficial effects and are not described in detail here. BRIEF DESCRIPTION OF THE DRAWINGS
[0042] In order to more clearly illustrate the embodiments of the present application, the following is a brief introduction to the drawings required for use in the embodiments. 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 any creative work.
[0043] Figure 1 An architectural diagram of a data encryption and decryption system provided in an embodiment of the present application;
[0044] Figure 2 A schematic diagram of the encryption and decryption principle of a data processing end in a data encryption and decryption system provided in an embodiment of the present application;
[0045] Figure 3 A flowchart of a method for encrypting data in a format-preserving manner provided in an embodiment of the present application;
[0046] Figure 4 A flowchart of a method for constructing an encryption permutation table provided in an embodiment of the present application;
[0047] Figure 5 A flowchart of a method for decrypting data in a format-preserving manner provided in an embodiment of the present application;
[0048] Figure 6 A schematic diagram of the structure of a device for encrypting data in a format-preserving manner provided in an embodiment of the present application;
[0049] Figure 7 A structural diagram of an electronic device provided in an embodiment of the present application. DETAILED DESCRIPTION
[0050] In order to make the purpose, technical solution and advantages of the embodiments of the present application clearer, the technical solution in the embodiments of the present application will be clearly and completely described below in conjunction with the drawings in the embodiments of the present application. Obviously, the described embodiments are part of the embodiments of the present application, not all of the embodiments. Based on the embodiments in the present application, all other embodiments obtained by ordinary technicians in this field without creative work are within the scope of protection of this application.
[0051] The process of data encryption is to process the plaintext according to a certain algorithm to make it unreadable "ciphertext", thereby protecting the data from being stolen or read by illegal persons. Common encryption algorithms use substitution and other techniques to confuse the plaintext, and the output ciphertext format is often very different from the plaintext. Format-Preserving Encryption is a special encryption technology that maintains confidentiality while keeping the ciphertext format, length, etc. the same as the plaintext. Format-Preserving Encryption is widely used in the field of data masking, and in some fields with special requirements for the ciphertext format, format-preserving encryption is also required. Existing format-preserving encryption algorithms have the problems of low implementation efficiency and high resource consumption, which limits the application of conformal encryption algorithms. At the same time, existing format-preserving encryption algorithms can encrypt integer data, but it is difficult to encrypt longer digital data. In order to solve the problems existing in the above-mentioned traditional format-preserving encryption, this embodiment provides a new format-preserving data encryption scheme through the following implementation plans.
[0052] To facilitate understanding of the solution provided by this application, the following describes the system to which this solution applies. Figure 1 , Figure 1 The following is an architecture diagram of a data encryption and decryption system provided in an embodiment of the present application, such as Figure 1 As shown, the system includes: a data sending end 101, a data processing end 102 and a data receiving end 103. The data processing end 102 includes the following three functional modules: a cryptographic environment construction module, a plaintext encryption module and a ciphertext decryption module. The cryptographic environment construction module can perform cryptographic environment construction operations of expanding keys, constructing encryption permutation tables and constructing decryption permutation tables when the data processing end 102 starts running. In the data encryption process, the data sending end 101 sends plaintext data to the data processing end 102, and the plaintext encryption module in the data processing end 102 uses the encryption permutation table to encrypt the plaintext data in a format-preserving manner to obtain ciphertext data so as to transmit the ciphertext data to the data receiving end 103. In the data decryption process, the data sending end 101 sends the ciphertext data to the data processing end 102, and the ciphertext decryption module in the data processing end 102 uses the decryption permutation table to decrypt the ciphertext data to obtain plaintext data so as to transmit the plaintext data to the data receiving end 103. In the above encryption and decryption process, the data processing end 102 can use the constructed encryption permutation table and decryption permutation table for encryption and decryption. The encryption and decryption process has less computational complexity, which can reduce resource consumption and improve the efficiency of format-preserving encrypted data and decrypted data.
[0053] See also Figure 2 , Figure 2 A schematic diagram of the encryption and decryption principle of a data processing end in a data encryption and decryption system provided in an embodiment of the present application. Figure 1For further description of the data encryption and decryption process of the data processing end in the corresponding embodiment, this embodiment can be implemented based on the SM4 encryption algorithm. Figure 2 As shown, the encryption algorithm in the data processing end includes three parts: cryptographic environment construction, data encryption, and data decryption. Among them, the cryptographic environment construction part is used to implement key expansion, construct decryption permutation tables, and construct encryption permutation tables; the data encryption part is used to implement encryption permutation functions; and the data decryption part is used to implement decryption permutation functions. The data processing end only needs to perform the cryptographic environment construction operation once at the beginning of operation, and can subsequently perform multiple data encryption and data decryption. In the above process, the encryption function of SM4 can be used to provide security for constructing decryption permutation tables and encryption permutation tables, and the key expansion algorithm of SM4 can be used for key expansion.
[0054] See below Figure 3 , Figure 3 A flowchart of a method for encrypting data in a format-preserving manner provided in an embodiment of the present application.
[0055] Specific steps may include:
[0056] S301: Performing a key expansion operation based on SM4 on the algorithm key to obtain an extended key;
[0057] Among them, this embodiment can be applied to data processing terminals such as computers and servers to encrypt plaintext data to obtain ciphertext data of the same format. In this embodiment, the SM4 encryption algorithm can be used to generate an algorithm key, and a key expansion operation is performed on the algorithm key to obtain an extended key, so that the extended key can support format-preserving encryption permutation and decryption permutation operations.
[0058] As a feasible implementation, this embodiment may first determine a preset length interval for the key expansion operation, and then perform a key expansion operation on the algorithm key according to the expansion constraint to obtain an expanded key, so that the number of bits of the obtained expanded key is within the preset length interval.
[0059] S302: Encrypt the characters in the alphabet using the SM4 encryption algorithm to obtain an encryption substitution table;
[0060] The alphabet may be a set including a plurality of different numeric symbols. The present embodiment does not limit the arrangement order of the numeric symbols in the alphabet, and the numeric symbols may be arranged in random order or in sequence. For example, an alphabet with a total number of characters of base may be used, and the alphabet is a set {0, 1, 2, ..., base-1}. Since the encryption algorithm used in the present embodiment is the SM4 algorithm, the total number of characters in the alphabet may be limited to be less than or equal to 2. 128 .
[0061] Specifically, in this embodiment, an encryption algorithm can be used to generate a key, and the key generated by the encryption algorithm can be used to encrypt characters in the alphabet to obtain a target ciphertext corresponding to each character. The encryption permutation table is obtained by summarizing the target ciphertexts corresponding to all characters in the alphabet.
[0062] S303: Using the extended key to perform modular addition calculation on the characters in the plaintext data to obtain an index value, and querying the encryption permutation table for the ciphertext character corresponding to the index value to obtain ciphertext data in the same format as the plaintext data.
[0063] After obtaining the extended key and the encryption permutation table, the present embodiment can implement format-preserving encryption based on the extended key and the encryption permutation table. The specific process is as follows: determine the plaintext data, use the extended key to perform modular addition calculations on the characters in the plaintext data to obtain the index values corresponding to each character, and use the ciphertext characters corresponding to the index values in the encryption permutation table as the ciphertext characters corresponding to the characters in the plaintext data. After obtaining the ciphertext characters corresponding to all the characters in the plaintext data, all the ciphertext characters can be used to generate ciphertext data in the same format as the plaintext data.
[0064] This embodiment expands the algorithm key to obtain an extended key, and also uses an encryption algorithm to encrypt characters in the alphabet to obtain an encryption permutation table. After obtaining the encryption permutation table, the ciphertext characters corresponding to each character in the plaintext data can be calculated through the encryption permutation table. The above-mentioned process of format-preserving encryption based on the encryption permutation table only involves modular addition operations and element access operations on the encryption permutation table. The encryption process has a small amount of calculation, which can reduce the consumption of resources in the process of encrypting data. On the basis of obtaining the encryption permutation table, multiple data encryption operations can be performed without repeatedly constructing the encryption permutation table, thereby reducing the consumption of resources in the process of encrypting data. Therefore, this embodiment can reduce the resource consumption of the format-preserving encryption process and improve encryption efficiency.
[0065] As a feasible implementation, this embodiment can use the alphabet to convert non-numeric strings into numeric strings to improve the efficiency of format-preserving encryption of digital data. When encrypting digital data, the index value of each character in the plaintext data can be calculated one by one according to the partial key content of the extended key, and the encrypted length of the data is not limited, so format-preserving encryption of longer digital data can be achieved.
[0066] The above process of converting a non-numeric character string into a numeric character string is as follows: before using the extended key to perform a modular addition calculation on the characters in the plaintext data to obtain an index value, obtain the data to be encrypted; determine whether the data to be encrypted is a numeric character string; if so, set the data to be encrypted to the plaintext data; if not, use the alphabet to map the data to be encrypted to a numeric character string to obtain the plaintext data. In order to prevent confusion, after using the alphabet to map the data to be encrypted to a numeric character string, spaces can also be added between the numeric character strings for segmentation. After obtaining the ciphertext data corresponding to the encrypted data, this embodiment can also use the alphabet to convert the ciphertext data into a non-numeric character string for output.
[0067] An example is given to illustrate the above scheme of using the alphabet to map strings to obtain digital strings: base is used to represent the number of characters in the alphabet, the alphabet is the set {0, 1, ..., base-1}, each character is a decimal number, base ≤ 2 128 If the string to be encrypted with preserved format is not a numeric string, first use the alphabet to convert it into a numeric string. For example, for the lowercase English alphabet {a, b, c, ..., z}, you can define a mapping: a→0, b→1, c→2, ..., z→25, then the string hello is converted into the numeric string 7 4 11 11 13, whose length is 5.
[0068] As for Figure 3 Further introduction to the corresponding embodiment, specifically, the extended key can be obtained by performing a key expansion operation on the algorithm key in the following manner: setting the maximum plaintext string length according to the application environment, and determining the total number of characters in the alphabet; performing an SM4-based key expansion operation on the algorithm key according to the maximum plaintext string length and the total number of characters in the alphabet to obtain the extended key.
[0069] The implementation of the extended algorithm key is explained by taking an example from a practical application: the first parameter m and the second parameter r are determined according to the maximum plaintext string length maxlen and the total number of characters base in the alphabet; wherein the first parameter m is the minimum integer that meets the first constraint condition, and the first constraint condition is 2 m ≥base; the second parameter r is the smallest integer that satisfies the second constraint condition, and the second constraint condition is that r is greater than or equal to m multiplied by maxlen (ie, r ≥ m × maxlen); use the SM4 key expansion algorithm to perform k, (k+1) mod 2 respectively 128 、(k+2)mod 2 128 , ..., (k+r)mod2 128Perform key expansion to obtain multiple extended subkeys; wherein k is the algorithm key and mod is an integer modulus operation; perform string concatenation operation on all the extended subkeys to obtain the extended key. The extended subkeys obtained above may include: ek 0 ,ek 1 ,ek 2 ,…,ek r After performing the string concatenation operation, the obtained extended key ek=ek 0 ||ek 1 ||ek 2 ||…||ek r ;|| represents string concatenation operation.
[0070] In the above embodiment, the key expansion algorithm of SM4 can be used to expand the key k to a length corresponding to the length of the plaintext data to support SM4 encryption, encryption permutation and decryption permutation processes.
[0071] See also Figure 4 , Figure 4 A flowchart of a method for constructing an encrypted permutation table provided in an embodiment of the present application. Figure 3 For further description of the corresponding embodiment, this embodiment can be compared with Figure 3 The corresponding embodiments are combined to obtain a further implementation method. This embodiment may include the following steps:
[0072] S401: Expand each character in the alphabet to obtain a target character with a preset number of digits;
[0073] Among them, this embodiment can expand the characters in the alphabet by padding the high bits with zeros to obtain the target characters with preset digits. This embodiment can use the maximum encryption length of the ciphertext of the encryption algorithm as the number of digits of the target character; for example, if the encryption algorithm is the SM4 encryption algorithm, each character in the alphabet can be expanded to obtain a 128-bit target character.
[0074] S402: Encrypt each of the target characters using the SM4 encryption algorithm to obtain a target ciphertext;
[0075] In this embodiment, a key for encrypting the target characters can be generated according to an encryption algorithm, and each of the target characters can be encrypted using the key to obtain a target ciphertext.
[0076] S403: Sort all the target ciphertexts in ascending or descending order so that the ciphertext character corresponding to each character is the position of the target ciphertext of the character in the sorting sequence, and obtain the encryption permutation table.
[0077] The target ciphertext is an integer with a preset number of digits. The target ciphertext can be sorted from small to large or from large to small according to the rule of high digits first and low digits last, and the target ciphertext is added to the one-dimensional array in sequence according to the sorting results to obtain an encryption permutation table. After constructing the encryption permutation table, the encryption permutation table can be used to perform multiple format-preserving data encryption operations without rebuilding the encryption permutation table, thereby improving the efficiency of the format-preserving encryption process.
[0078] The following example illustrates the process of constructing an encryption permutation table based on the SM4 encryption algorithm:
[0079] For example, the alphabet is the set {0, 1, 2, ..., base-1}, and 0, 1, ..., base-1 can be expanded to 128-bit binary characters. For example, 0 is expanded to 0x 0000 0000 0000 0000, and 10 is expanded to 0x 0000 0000 00001010. Using ECB (Electronic Codebook) mode and key k (the corresponding extended key is ek 0 ), use the SM4 encryption algorithm to encrypt the expanded numbers respectively, and the ciphertexts obtained are c 0 , c 1 , ..., c base-1 . Put c 0 , c 1 , ..., c base-1 These ciphertexts are treated as 128-bit integers (high digits first, low digits last) and sorted from large to small. The ciphertext at position 0 is the largest, and the ciphertext at position base-1 is the smallest. The sorting sequence is denoted by S. The resulting encryption permutation table is a one-dimensional array of size base, denoted by Ptable_enc. The value of Ptable_enc[0] is c 0 At the position in S, the value of Ptable_enc[1] is c 1 The position in S, that is, the value of Ptable_enc[i] is c i The position in S, i is an integer between 0 and base-1 (including 0 and base-1).
[0080] See also Figure 5 , Figure 5 A flowchart of a method for decrypting data in a format-preserving manner provided in an embodiment of the present application. This embodiment introduces Figure 3 The specific process of decrypting the ciphertext data obtained in the corresponding embodiment can be implemented at the data decryption end, and can also be combined with the embodiment Figure 3 The corresponding embodiments are combined to obtain a further implementation method. This embodiment may include the following steps:
[0081] S501: Obtain ciphertext data;
[0082] The ciphertext data may be obtained by encrypting the plaintext data in a format-preserving manner using an extended key and an encryption permutation table at the data encryption end. Specifically, the extended key is obtained by performing a key expansion operation based on SM4 on the algorithm key, and the encryption permutation table is obtained by encrypting characters in an alphabet using the SM4 encryption algorithm, and the alphabet is a set of multiple numeric characters. The format-preserving encryption process may include: performing a modular addition calculation on the characters in the plaintext data using the extended key to obtain an index value, and using the index value to query the encryption permutation table to obtain ciphertext data in the same format as the plaintext data.
[0083] S502: Performing an inverse function transformation operation on the encryption permutation table to obtain a decryption permutation table;
[0084] Among them, on the basis of encrypting the characters in the alphabet according to the encryption algorithm to obtain the encryption permutation table, the encryption permutation table can be subjected to the inverse function transformation operation to obtain the decryption permutation table. For example, if the total number of characters in the alphabet is base, the corresponding decryption permutation table is a one-dimensional array of size base, denoted as Ptable_dec. The encryption permutation table is a one-dimensional array of size base, denoted as Ptable_enc. If the array is regarded as a function, the independent variable is the subscript of the array, and the dependent variable is the value at the subscript of the array, then Ptable_dec is the inverse function of the function Ptable_enc. That is, the value of Ptable_dec[i] is the subscript corresponding to the element with the value i in Ptable_enc.
[0085] S503: Decrypt the ciphertext data using the extended key and the decryption permutation table to obtain the plaintext data.
[0086] This embodiment expands the algorithm key to obtain an extended key, and also uses an encryption algorithm to encrypt characters in the alphabet to obtain an encryption permutation table. Ciphertext data is generated according to the encryption permutation table and the extended key. After obtaining the encryption permutation table, this embodiment performs a function inverse transformation operation on the encryption permutation table to obtain a decryption permutation table, and uses the extended key and the decryption permutation table to perform an inverse operation of the format-preserving encryption to obtain plaintext data. On the basis of the decryption permutation table, multiple data decryption operations can be performed without repeatedly constructing the decryption permutation table. The above-mentioned decryption process based on the decryption permutation table has a small amount of calculation, which can reduce the consumption of resources in the process of decrypting data. Therefore, this embodiment can reduce the resource consumption of decrypting format-preserving encrypted data and improve the decryption efficiency.
[0087] As a feasible implementation, an operation of receiving a decryption instruction for decryption may also exist before S501. After obtaining the decryption instruction, the ciphertext data to be decrypted can be determined according to the decryption instruction, and the ciphertext data can be decrypted using the decryption substitution table to obtain the plaintext data (i.e., the plaintext data that was not encrypted in the previous text). Specifically, in this embodiment, the decryption substitution table can be used to query the ciphertext characters of each character in the ciphertext data, and the plaintext character corresponding to each ciphertext character can be calculated according to the ciphertext character and the extended key; the plaintext data can be determined according to all the plaintext characters. In the above process, the inverse operation of modulo addition calculation can be used on the above ciphertext characters to obtain the plaintext characters corresponding to the ciphertext characters.
[0088] The following uses an actual example to illustrate the above process of encrypting data using the encryption substitution table and decrypting data using the decryption substitution table:
[0089] For example, the plaintext data is p = p[1]p[2]…p[n], where each p[i] is a number in the base alphabet, n is an integer not less than 1, i is an integer between 1 and n (including 1 and n), the length of p is n, n < maxlen, and maxlen is the maximum string length supported by the encryption algorithm. The encryption process for the plaintext p is as follows: The following operations are performed for each plaintext digit p[i]: Step ①, starting from ek 1 From left to right, take the i-th m-bit binary number from ek and denote it as ek i . Consider ek i as an integer, with the left side being the high-order bit, and consider p[i] as the integer represented by its digit (for example, 0 represents the integer 0). Step ②, calculate the index = p[i]+ek i mod base. Step ③, look up the encryption substitution table for substitution, that is, the ciphertext after encrypting p[i] is Ptable_enc[index], denoted as p[i]'. The ciphertext of the plaintext p is p[1]'p[2]'…p[n]'.
[0090] The decryption process for the ciphertext (IV, p[1]'p[2]'…p[n]') is: The following operations are performed for each ciphertext digit p[i]': Step ①, consider p[i]' as the integer represented by its digit (for example, 0 represents the integer 0), look up the decryption substitution table for substitution to obtain Ptable_dec[p[i]']. Step ②, starting from ek 1 From left to right, take the i-th m-bit binary number from ek and denote it as ek i , consider ek i as an integer, with the left side being the high-order bit. Finally, calculate p[i] = Ptable_dec[p[i]'] - ek imodbase. The plaintext of the ciphertext p[1]'p[2]'…p[n]' is p=p[1]p[2]…p[n].
[0091] The process of taking the ith m-bit binary number from ek in the above embodiment is equivalent to dividing ek into multiple m-bit binary strings. When taking the ith m-bit binary number, the ith m-bit binary string counted from left to right is taken as ek. i For example, when m = 1024, the first 1 to 1024 bits of ek are selected from left to right as ek 1 , the second time, select the 1025th to 2048th bits of ek as ek 2 .
[0092] See also Figure 6 , Figure 6 A schematic diagram of the structure of a device for encrypting data in a format-preserving manner provided in an embodiment of the present application;
[0093] The device may include:
[0094] The key expansion module 601 is used to perform a key expansion operation based on SM4 on the algorithm key to obtain an expanded key;
[0095] The encryption permutation table construction module 602 is used to encrypt the characters in the alphabet using the SM4 encryption algorithm to obtain an encryption permutation table; wherein the alphabet is a set of multiple digital characters;
[0096] The data encryption module 603 is used to use the extended key to perform modular addition calculation on the characters in the plaintext data to obtain an index value, and use the index value to query the encryption permutation table to obtain ciphertext data with the same format as the plaintext data.
[0097] This embodiment expands the algorithm key to obtain an extended key, and also uses an encryption algorithm to encrypt characters in the alphabet to obtain an encryption permutation table. After obtaining the encryption permutation table, the ciphertext characters corresponding to each character in the plaintext data can be calculated through the encryption permutation table. The above-mentioned process of format-preserving encryption based on the encryption permutation table only involves modular addition operations and element access operations on the encryption permutation table. The encryption process has a small amount of calculation, which can reduce the consumption of resources in the process of encrypting data. On the basis of obtaining the encryption permutation table, multiple data encryption operations can be performed without repeatedly constructing the encryption permutation table, thereby reducing the consumption of resources in the process of encrypting data. Therefore, this embodiment can reduce the resource consumption of the format-preserving encryption process and improve encryption efficiency.
[0098] Furthermore, the encryption permutation table construction module 602 is used to expand each character in the alphabet to obtain a target character of a preset number of digits, and use the SM4 encryption algorithm to encrypt each of the target characters to obtain a target ciphertext; all the target ciphertexts are sorted in ascending or descending order so that the ciphertext character corresponding to each character is the position of the target ciphertext of the character in the sorting sequence, to obtain the encryption permutation table.
[0099] Furthermore, the key expansion module 601 includes:
[0100] a parameter determination unit, configured to set a maximum plaintext character string length according to an application environment and determine a total number of characters in the alphabet;
[0101] An expansion unit is used to perform an SM4-based key expansion operation on the algorithm key according to the maximum plaintext character string length and the total number of characters in the alphabet to obtain the extended key.
[0102] Furthermore, the expansion unit is used to determine a first parameter m and a second parameter r according to the maximum plaintext string length maxlen and the total number of characters base in the alphabet; wherein the first parameter m is a minimum integer that satisfies a first constraint condition, and the first constraint condition is 2 m ≥ base; the second parameter r is the minimum integer that satisfies the second constraint condition, and the second constraint condition is that r is greater than or equal to m multiplied by maxlen; and is also used to use the SM4 key expansion algorithm to respectively perform k, (k+1) mod 2 128 、(k+2)mod 2 128 , ..., (k+r) mod 2 128 Performing key expansion to obtain multiple extended subkeys; wherein k is the algorithm key and mod is an integer modulus operation; and performing a string concatenation operation on all the extended subkeys to obtain the extended key.
[0103] Furthermore, the data encryption module 603 is used to calculate the index value of each character in the plaintext data one by one according to the partial key content of the extended key.
[0104] Furthermore, it also includes:
[0105] A plaintext data determination module is used to obtain the data to be encrypted before calculating the index value of the characters in the plaintext data according to the extended key; it is also used to determine whether the data to be encrypted is a digital string; if so, the data to be encrypted is set to the plaintext data; if not, the data to be encrypted is mapped to a digital string using the alphabet to obtain the plaintext data.
[0106] Furthermore, it also includes:
[0107] The string segmentation module is used to add spaces between the digital strings for segmentation after mapping the data to be encrypted into digital strings using the alphabet.
[0108] An embodiment of the present application provides a device for decrypting data in a format-preserving manner, and the device may include:
[0109] A ciphertext acquisition module, used to acquire ciphertext data; wherein the ciphertext data is obtained by encrypting the plaintext data in a format-preserving manner by the data encryption end using an extended key and an encryption permutation table, wherein the extended key is obtained by performing a key expansion operation based on SM4 on the algorithm key, and the encryption permutation table is obtained by encrypting characters in an alphabet using an SM4 encryption algorithm, wherein the alphabet is a set of multiple numeric characters;
[0110] A decryption permutation table generation module, used for performing a function inverse transformation operation on the encryption permutation table to obtain a decryption permutation table;
[0111] The data decryption module is used to decrypt the ciphertext data using the extended key and the decryption permutation table to obtain the plaintext data.
[0112] This embodiment expands the algorithm key to obtain an extended key, and also uses an encryption algorithm to encrypt characters in the alphabet to obtain an encryption permutation table. Ciphertext data is generated according to the encryption permutation table and the extended key. After obtaining the encryption permutation table, this embodiment performs a function inverse transformation operation on the encryption permutation table to obtain a decryption permutation table, and uses the extended key and the decryption permutation table to perform an inverse operation of the format-preserving encryption to obtain plaintext data. On the basis of the decryption permutation table, multiple data decryption operations can be performed without repeatedly constructing the decryption permutation table. The above-mentioned decryption process based on the decryption permutation table has a small amount of calculation, which can reduce the consumption of resources in the process of decrypting data. Therefore, this embodiment can reduce the resource consumption of decrypting format-preserving encrypted data and improve the decryption efficiency.
[0113] Furthermore, the data decryption module is used to use the decryption permutation table to query the ciphertext character of each character in the ciphertext data, and calculate the plaintext character corresponding to each ciphertext character based on the ciphertext character and the extended key; and is also used to determine the plaintext data based on all the plaintext characters.
[0114] Since the embodiments of the apparatus part correspond to the embodiments of the method part, please refer to the description of the embodiments of the method part for the embodiments of the apparatus part, which will not be repeated here.
[0115] The present application also provides a storage medium on which a computer program is stored, and when the computer program is executed, the steps provided in the above embodiment can be implemented. The storage medium may include: a USB flash drive, a mobile hard disk, a read-only memory (ROM), a random access memory (RAM), a magnetic disk or an optical disk, and other media that can store program codes.
[0116] The present application also provides an electronic device, see Figure 7 , a structural diagram of an electronic device provided in an embodiment of the present application, such as Figure 7 As shown, a processor 710 and a memory 720 may be included.
[0117] Among them, the processor 710 may include one or more processing cores, such as a 4-core processor, an 8-core processor, etc. The processor 710 can be implemented in at least one hardware form of DSP (Digital Signal Processing), FPGA (Field-Programmable Gate Array), and PLA (Programmable Logic Array). The processor 710 may also include a main processor and a coprocessor. The main processor is a processor for processing data in the awake state, also known as a CPU (Central Processing Unit); the coprocessor is a low-power processor for processing data in the standby state. In some embodiments, the processor 710 may be integrated with a GPU (Graphics Processing Unit), which is responsible for rendering and drawing the content to be displayed on the display screen. In some embodiments, the processor 710 may also include an AI (Artificial Intelligence) processor, which is used to process computing operations related to machine learning.
[0118] The memory 720 may include one or more computer-readable storage media, which may be non-transitory. The memory 720 may also include a high-speed random access memory, and a non-volatile memory, such as one or more disk storage devices, flash memory storage devices. In this embodiment, the memory 720 is at least used to store the following computer program 721, wherein, after the computer program is loaded and executed by the processor 710, it can implement the relevant steps in the method for encrypting data with a retained format and / or the method for decrypting data with a retained format disclosed in any of the aforementioned embodiments. In addition, the resources stored in the memory 720 may also include an operating system 722 and data 723, etc., and the storage method may be temporary storage or permanent storage. Among them, the operating system 722 may include Windows, Linux, Android, etc.
[0119] In some embodiments, the electronic device may further include a display screen 730 , an input / output interface 740 , a communication interface 750 , a sensor 760 , a power source 770 , and a communication bus 780 .
[0120] certainly, Figure 7 The structure of the electronic device shown does not constitute a limitation on the electronic device in the embodiments of the present application. In actual applications, the electronic device may include Figure 7 More or fewer components than shown, or combinations of certain components.
[0121] The various embodiments in the specification are described in a progressive manner, and each embodiment focuses on the differences from other embodiments, and the same and similar parts between the various embodiments can be referred to each other. For the device disclosed in the embodiment, since it corresponds to the method disclosed in the embodiment, the description is relatively simple, and the relevant parts can be referred to the method part description. It should be pointed out that for ordinary technicians in this technical field, without departing from the principles of this application, several improvements and modifications can be made to the present application, and these improvements and modifications also fall within the scope of protection of the claims of this application.
[0122] It should also be noted that, in this specification, relational terms such as first and second, etc. are only used to distinguish one entity or operation from another entity or operation, and do not necessarily require or imply any such actual relationship or order between these entities or operations. Moreover, the terms "comprises", "comprising" or any other variants thereof are intended to cover non-exclusive inclusion, so that a process, method, article or device including a series of elements includes not only those elements, but also other elements not explicitly listed, or also includes elements inherent to such process, method, article or device. In the absence of further restrictions, an element defined by the statement "comprising a ..." does not exclude the presence of other identical elements in the process, method, article or device including the element.
Claims
1. A method for encrypting data in a format-preserving manner, characterized in that: include: Performing a key expansion operation based on SM4 on the algorithm key to obtain an extended key; Encrypting characters in an alphabet using an SM4 encryption algorithm to obtain an encryption substitution table; wherein the alphabet is a set of multiple numeric characters; Using the extended key to perform modular addition calculation on characters in the plaintext data to obtain an index value, and using the index value to query the encryption permutation table to obtain ciphertext data in the same format as the plaintext data; The step of performing a key expansion operation based on SM4 on the algorithm key to obtain an extended key includes: Setting a maximum plaintext string length according to the application environment and determining the total number of characters in the alphabet; According to the maximum plaintext string length and the total number of characters in the alphabet, a key expansion operation based on SM4 is performed on the algorithm key to obtain multiple extended sub-keys, and a string concatenation operation is performed on all the extended sub-keys to obtain the extended key.
2. The method for encrypting data with format preservation according to claim 1, characterized in that: The encryption substitution table is obtained by encrypting the characters in the alphabet using the SM4 encryption algorithm, including: Expand each character in the alphabet to obtain a target character with a preset number of digits, and use the SM4 encryption algorithm to encrypt each target character to obtain a target ciphertext; All the target ciphertexts are sorted in increasing or decreasing order so that the ciphertext character corresponding to each character is the position of the target ciphertext of the character in the sorting sequence, thereby obtaining the encryption permutation table.
3. The method for encrypting data with format preservation according to claim 1, characterized in that: Performing a key expansion operation based on SM4 on the algorithm key according to the maximum plaintext character string length and the total number of characters in the alphabet to obtain the extended key includes: According to the maximum plaintext string length maxlen and the total number of characters in the alphabet base Determine the first parameter m and the second parameter r ; Wherein, the first parameter m is the smallest integer that satisfies the first constraint, which is 2 m ≥base ; The second parameter r is the smallest integer that satisfies the second constraint. The second constraint is r Greater than or equal to m Multiply maxlen ; Use SM4 key expansion algorithm to k 、( k +1) mod 2 128 、( k +2) mod 2 128 ,……,( k + r ) mod 2 128 The key is expanded to obtain multiple extended subkeys; among them, k is the algorithm key, mod is an integer modulus operation; A string concatenation operation is performed on all the extended subkeys to obtain the extended key.
4. The method for encrypting data in a format-preserving manner according to any one of claims 1 to 3, characterized in that: Using the extended key to perform modular addition calculation on characters in the plaintext data to obtain an index value includes: The index value of each character in the plaintext data is calculated one by one according to the partial key content of the extended key.
5. A method for decrypting data in a format-preserving manner, characterized in that: include: Acquire ciphertext data; wherein the ciphertext data is obtained by encrypting the plaintext data in a format-preserving manner by a data encryption end using an extended key and an encryption permutation table, wherein the extended key is obtained by performing an SM4-based key expansion operation on an algorithm key, and the encryption permutation table is obtained by encrypting characters in an alphabet using an SM4 encryption algorithm, wherein the alphabet is a set of multiple numeric characters; Performing an inverse function transformation operation on the encryption permutation table to obtain a decryption permutation table; Decrypting the ciphertext data using the extended key and the decryption permutation table to obtain the plaintext data; Among them, the process of performing an SM4-based key expansion operation on the algorithm key to obtain the extended key includes: setting the maximum plaintext string length according to the application environment, and determining the total number of characters in the alphabet; performing an SM4-based key expansion operation on the algorithm key according to the maximum plaintext string length and the total number of characters in the alphabet to obtain multiple extended sub-keys, and performing a string concatenation operation on all the extended sub-keys to obtain the extended key.
6. The method for decrypting data with a format preserved according to claim 5, characterized in that: Decrypting the ciphertext data using the extended key and the decryption permutation table to obtain the plaintext data includes: Using the decryption permutation table to query the ciphertext character of each character in the ciphertext data, and calculating the plaintext character corresponding to each ciphertext character according to the ciphertext character and the extended key; The plaintext data is determined based on all the plaintext characters.
7. A device for encrypting data in a format-preserving manner, characterized in that: include: A key expansion module, used to perform a key expansion operation based on SM4 on the algorithm key to obtain an extended key; An encryption permutation table construction module, used to encrypt characters in the alphabet using the SM4 encryption algorithm to obtain an encryption permutation table; wherein the alphabet is a set of multiple digital characters; A data encryption module, used to perform modular addition calculation on characters in the plaintext data using the extended key to obtain an index value, and use the index value to query the encryption permutation table to obtain ciphertext data in the same format as the plaintext data; Wherein, the key expansion module includes: a parameter determination unit, configured to set a maximum plaintext character string length according to an application environment and determine a total number of characters in the alphabet; An expansion unit is used to perform a key expansion operation based on SM4 on the algorithm key according to the maximum plaintext string length and the total number of characters in the alphabet to obtain multiple extended sub-keys, and perform a string concatenation operation on all the extended sub-keys to obtain the extended key.
8. A device for decrypting data in a format-preserving manner, characterized in that: include: A ciphertext acquisition module, used to acquire ciphertext data; wherein the ciphertext data is obtained by encrypting the plaintext data in a format-preserving manner by the data encryption end using an extended key and an encryption permutation table, wherein the extended key is obtained by performing a key expansion operation based on SM4 on the algorithm key, and the encryption permutation table is obtained by encrypting characters in an alphabet using an SM4 encryption algorithm, wherein the alphabet is a set of multiple numeric characters; A decryption permutation table generation module, used for performing a function inverse transformation operation on the encryption permutation table to obtain a decryption permutation table; A data decryption module, used to decrypt the ciphertext data using the extended key and the decryption permutation table to obtain the plaintext data; Among them, the process of performing an SM4-based key expansion operation on the algorithm key to obtain the extended key includes: setting the maximum plaintext string length according to the application environment, and determining the total number of characters in the alphabet; performing an SM4-based key expansion operation on the algorithm key according to the maximum plaintext string length and the total number of characters in the alphabet to obtain multiple extended sub-keys, and performing a string concatenation operation on all the extended sub-keys to obtain the extended key.
9. An electronic device, characterized in that: The method comprises a memory and a processor, wherein a computer program is stored in the memory, and when the processor calls the computer program in the memory, the method for encrypting data in a format-preserving manner as claimed in any one of claims 1 to 4 and / or the steps of the method for decrypting data in a format-preserving manner as claimed in any one of claims 5 and 6 are implemented.
10. A storage medium, characterized in that: The storage medium stores computer executable instructions, which, when loaded and executed by a processor, implement the steps of the method for encrypting data in a format-preserving manner as described in any one of claims 1 to 4 and / or the method for decrypting data in a format-preserving manner as described in any one of claims 5 and 6.
Citation Information
Patent Citations
Preserving format encryption method based on traditional block cipher
CN108768617A