Encryption method, decryption method, device and electronic device
By using multiple keys for encryption and decryption operations and location exchange, the problem of not being able to ensure data security and execution efficiency at the same time in the prior art is solved, and an efficient and secure encryption and decryption process is achieved.
Patent Information
- Application Number
- CN202111616945.2
- Authority / Receiving Office
- CN · China
- Patent Type
- Patents(China)
- Current Assignee / Owner
- Filing Date
- 2021-12-27
- Publication Date
- 2025-05-16
- Estimated Expiration
- 2041-12-27
AI Technical Summary
The prior art cannot ensure the security and execution efficiency of data at the same time when encrypting or decrypting data.
By obtaining the data to be encrypted or decrypted and multiple keys for encryption and decryption, these keys are used for specific operations and location exchange, to enhance the security of the data and improve the efficiency of the encryption and decryption process.
It realizes the security and execution efficiency of data during the encryption and decryption process, and solves the problems of low security or low efficiency in the prior art.
Smart Images

Figure CN114254366B_ABST
Abstract
Description
Technical Field
[0001] The present invention relates to the field of computers, and in particular to an encryption method, a decryption method, a device and an electronic device. Background Art
[0002] With the rapid development of network and communication technology, digital information has been widely used and disseminated on the Internet. For data involving confidential information such as mobile phone numbers and ID card numbers, its security needs to be ensured when it is transmitted and stored on the public network.
[0003] In the related art, keys are used to encrypt and decrypt data involving confidential information. For example, complex keys and encryption and decryption methods are used to encrypt or decrypt data to ensure the security of the data, but this method has the problem of low efficiency in the encryption or decryption process. Alternatively, a method of using a single key for encryption and decryption can be used to encrypt or decrypt data to ensure the efficiency of encryption or decryption, but this method has the problem of low security.
[0004] Therefore, in the related art, when encrypting or decrypting data, there is a problem that data security and execution efficiency cannot be ensured at the same time. Summary of the invention
[0005] The embodiments of the present invention provide an encryption method, a decryption method, a device and an electronic device to at least solve the technical problem that when encrypting or decrypting data, data security and execution efficiency cannot be ensured at the same time.
[0006] According to a first aspect of an embodiment of the present invention, there is provided an encryption method, comprising: obtaining data to be encrypted; obtaining a first key and a second key for encrypting the data to be encrypted; determining, based on a first position correspondence, that a data bit of the data to be encrypted corresponds to a first target bit in the first key and the second key; performing a first operation on a character of the data bit of the data to be encrypted and a character of the corresponding first target bit to obtain an encrypted character of the encryption bit corresponding to the data bit of the data to be encrypted; performing a position swap operation on the encrypted characters of multiple encryption bits included in the data to be encrypted to obtain encrypted data corresponding to the data to be encrypted.
[0007] Optionally, the method also includes: grouping the data to be encrypted according to the length of the first key and the length of the second key to obtain multiple groups of data to be encrypted; the first position correspondence relationship includes: the 2m-1th data bit in the first key corresponds to the 2m-1th data bit in each group of data to be encrypted in the multiple groups of data to be encrypted, and the 2mth data bit in the second key corresponds to the 2mth data bit in each group of data to be encrypted in the multiple groups of data to be encrypted, where m is an integer greater than or equal to 1.
[0008] Optionally, the method for obtaining the first key and the second key for encrypting the data to be encrypted includes: according to a univariate nth-order equation and the solution of the univariate nth-order equation, using Vieta's theorem to solve the unknown coefficients of the univariate nth-order equation, wherein one coefficient of the univariate nth-order equation is known; based on the unknown coefficients obtained, generating the first key and the second key for encrypting the data to be encrypted.
[0009] Optionally, the method also includes: obtaining a third key for encrypting the data to be encrypted; determining, based on a second position correspondence, that a data bit of the data to be encrypted after executing the first operation corresponds to a second target bit in the third key; performing a second operation on the character of the data bit of the data to be encrypted after executing the first operation and the character of the second target bit to obtain a target encrypted character of the encryption bit corresponding to the data bit of the data to be encrypted; performing a position swap operation on the encrypted characters of the multiple encryption bits included in the data to be encrypted to obtain the encrypted data corresponding to the data to be encrypted includes: performing a position swap operation on the target encrypted characters of the multiple encryption bits included in the data to be encrypted to obtain the encrypted data corresponding to the data to be encrypted.
[0010] According to a second aspect of an embodiment of the present invention, a decryption method is provided, comprising: obtaining encrypted data; obtaining a first key and a second key for decrypting the encrypted data; performing a position recovery operation on encrypted characters of a plurality of encryption bits included in the encrypted data to obtain target encrypted data, wherein the position recovery operation is an inverse operation of performing a position swap operation on the encrypted characters of the plurality of encryption bits when obtaining the encrypted data; determining, based on a first position correspondence, that a data bit of the target encrypted data corresponds to a first target bit in the first key and the second key; performing an inverse operation of a first operation operation on the characters of the data bits of the target encrypted data and the characters of the corresponding first target bits to obtain decrypted data corresponding to the encrypted data.
[0011] Optionally, the method also includes: grouping the target encrypted data according to the length of the first key and the length of the second key to obtain multiple grouped target encrypted data; the first position correspondence relationship includes: the 2m-1th data bit in the first key corresponds to the 2m-1th data bit in each grouped target encrypted data in the multiple grouped target encrypted data, and the 2mth data bit in the second key corresponds to the 2mth data bit in each grouped target encrypted data in the multiple grouped target encrypted data, where m is an integer greater than or equal to 1.
[0012] Optionally, the method for obtaining the first key and the second key for decrypting the encrypted data includes: according to a univariate nth-order equation and the solution of the univariate nth-order equation, using Vieta's theorem to solve the unknown coefficients of the univariate nth-order equation, wherein one coefficient of the univariate nth-order equation is known; and based on the obtained unknown coefficients, generating the first key and the second key for decrypting the encrypted data.
[0013] Optionally, the method also includes: obtaining a third key for decrypting the encrypted data; determining, based on the second position correspondence, that the data bit of the target encrypted data after performing the inverse operation of the first operation corresponds to the second target bit in the third key; performing the inverse operation of the first operation on the character of the data bit of the target encrypted data and the character of the corresponding first target bit to obtain the decrypted data corresponding to the encrypted data, including: performing the inverse operation of the second operation on the character of the data bit of the target encrypted data after performing the inverse operation of the first operation and the character of the second target bit to obtain the decrypted data corresponding to the encrypted data.
[0014] According to a third aspect of the present invention, there is provided an encryption device, comprising: a first acquisition module, for acquiring data to be encrypted; a second acquisition module, for acquiring a first key and a second key for encrypting the data to be encrypted; a first determination module, for determining, based on a first position correspondence, that a data bit of the data to be encrypted corresponds to a first target bit in the first key and the second key; a first operation module, for performing a first operation operation on characters of the data bits of the data to be encrypted and characters of the corresponding first target bits, to obtain encrypted characters of encryption bits corresponding to the data bits of the data to be encrypted; and a first exchange module, for performing a position exchange operation on encrypted characters of multiple encryption bits included in the data to be encrypted, to obtain encrypted data corresponding to the data to be encrypted.
[0015] According to a fourth aspect of the present invention, there is provided a decryption device, comprising a third acquisition module for acquiring encrypted data; a fourth acquisition module for acquiring a first key and a second key for decrypting the encrypted data; a second exchange module for performing a position recovery operation on encrypted characters of a plurality of encryption bits included in the encrypted data to acquire target encrypted data, wherein the position recovery operation is: performing an inverse operation of a position exchange operation on the encrypted characters of the plurality of encryption bits when the encrypted data is obtained; a second determination module for determining, based on a first position correspondence, that a data bit of the target encrypted data corresponds to a first target bit in the first key and the second key; and a second operation module for performing an inverse operation of a first operation operation on a character of a data bit of the target encrypted data and a character of a corresponding first target bit to acquire decrypted data corresponding to the encrypted data.
[0016] According to a fifth aspect of the present invention, there is provided an electronic device, comprising: a processor; and a memory for storing instructions executable by the processor; wherein the processor is configured to execute the instructions to implement any of the methods described above.
[0017] According to a sixth aspect of the present invention, a computer-readable storage medium is provided, characterized in that when instructions in the computer-readable storage medium are executed by a processor of an electronic device, the electronic device is enabled to execute any of the methods described above.
[0018] In an embodiment of the present invention, by obtaining data to be encrypted and a first key and a second key for encrypting the data to be encrypted, the data to be encrypted is encrypted using the characters of the first target bits corresponding to the data bits of the data to be encrypted in the first key and the second key. Compared with the method of encrypting with only one key in the prior art, the security of the data is improved, and the encrypted characters of the encryption bits in the data to be encrypted are exchanged to obtain the encrypted data corresponding to the data to be encrypted. The method encrypts the data to be encrypted by a dual encryption method of multiple key encryption and position exchange, which improves the security of the data to be encrypted and does not reduce the execution efficiency of the encryption process, thereby achieving both ensuring data security and improving the execution efficiency of the encryption process, thereby solving the technical problem in the related art that, when encrypting data, data security and execution efficiency cannot be ensured at the same time. The method of the embodiment of the present invention is applicable to any encryption scenario using a key.
[0019] In an embodiment of the present invention, by obtaining encrypted data and a first key and a second key for decrypting the encrypted data, a position recovery operation is performed on the encrypted characters of multiple encrypted bits included in the encrypted data to obtain target encrypted data, and then according to the first position correspondence, it is determined that the data bit of the target encrypted data corresponds to the first target bit in the first key and the second key, and the character of the data bit of the target encrypted data is subjected to the inverse operation of the first operation with the character of the corresponding first target bit, so as to obtain the decrypted data corresponding to the encrypted data. In this method, by using multiple keys to perform operation operations and position exchange on the encrypted data, the security of the data is improved, and the execution efficiency of the encryption process is guaranteed, so that both data security and the execution efficiency of the encryption process can be ensured, thereby solving the technical problem in the related art that when decrypting data, data security and execution efficiency cannot be ensured at the same time; and the method of the embodiment of the present invention is applicable to any encryption scenario using a key and the corresponding decryption scenario. BRIEF DESCRIPTION OF THE DRAWINGS
[0020] The drawings described herein are used to provide a further understanding of the present invention and constitute a part of this application. The exemplary embodiments of the present invention and their descriptions are used to explain the present invention and do not constitute an improper limitation of the present invention. In the drawings:
[0021] Figure 1 is a schematic diagram of an encryption method according to an embodiment of the present invention;
[0022] Figure 2 is a schematic diagram of another encryption method according to an embodiment of the present invention;
[0023] Figure 3 is a flow chart of a decryption method according to an embodiment of the present invention;
[0024] Figure 4 is a flow chart of another decryption method according to an embodiment of the present invention;
[0025] Figure 5 is a structural block diagram of an encryption device according to an embodiment of the present invention;
[0026] Figure 6 It is a structural block diagram of a decryption device according to an embodiment of the present invention. DETAILED DESCRIPTION
[0027] In order to enable those skilled in the art to better understand the scheme of the present invention, the technical scheme in the embodiments of the present invention will be clearly and completely described below in conjunction with the drawings in the embodiments of the present invention. Obviously, the described embodiments are only part of the embodiments of the present invention, not all of the embodiments. Based on the embodiments of the present invention, all other embodiments obtained by ordinary technicians in this field without creative work should fall within the scope of protection of the present invention.
[0028] It should be noted that the terms "first", "second", etc. in the specification and claims of the present invention and the above-mentioned drawings are used to distinguish similar objects, and are not necessarily used to describe a specific order or sequence. It should be understood that the data used in this way can be interchanged where appropriate, so that the embodiments of the present invention described herein can be implemented in an order other than those illustrated or described herein. In addition, the terms "including" and "having" and any variations thereof are intended to cover non-exclusive inclusions, for example, a process, method, system, product or device that includes a series of steps or units is not necessarily limited to those steps or units that are clearly listed, but may include other steps or units that are not clearly listed or inherent to these processes, methods, products or devices.
[0029] According to an embodiment of the present invention, an encryption method is provided. It should be noted that the steps shown in the flowchart of the accompanying drawings can be executed in a computer system such as a set of computer executable instructions, and although a logical order is shown in the flowchart, in some cases, the steps shown or described can be executed in an order different from that here.
[0030] Figure 1 is a flow chart of an encryption method according to an embodiment of the present invention. Figure 1 As shown, the method comprises the following steps:
[0031] Step S100, obtaining data to be encrypted.
[0032] As an optional implementation, the data to be encrypted includes data containing private information such as identity card number and telephone number.
[0033] Step S110: obtaining a first key and a second key for encrypting the data to be encrypted.
[0034] As an optional implementation, the first key and the second key may be randomly generated by using a predetermined key generation method, or may be directly obtained and pre-agreed.
[0035] Step S120: determining, based on the first position correspondence, that the data bit of the to-be-encrypted data corresponds to the first target bit in the first key and the second key.
[0036] It should be noted that the first position correspondence relationship is used to determine the correspondence between the data bits of the data to be encrypted and the first key and the second key. For example, the first position correspondence relationship can be determined according to the position parity, or according to other position identifiers. It can be flexibly determined according to specific encryption requirements.
[0037] Step S130, performing a first operation on the character of the data bit of the data to be encrypted and the character of the corresponding first target bit to obtain an encryption character of the encryption bit corresponding to the data bit of the data to be encrypted.
[0038] In one embodiment, performing a first operation on the characters of the data bits of the data to be encrypted and the characters of the corresponding first target bits includes: performing a first operation on the ASCII (American Standard Code for Information Interchange) code of the characters of the data bits of the data to be encrypted and the ASCII code of the characters of the corresponding first target bits.
[0039] In one embodiment, the first target bits include: data bits of the to-be-encrypted data corresponding to first sub-target bits of the first key, and data bits of the to-be-encrypted data corresponding to second sub-target bits of the second key.
[0040] The above-mentioned first operation may be performed in a variety of ways, for example, including: performing an addition operation on the ASCII code of the character of the data bit of the data to be encrypted and the ASCII code of the character of the corresponding first sub-target bit, and performing a subtraction operation on the ASCII code of the character of the data bit of the data to be encrypted and the ASCII code of the character of the corresponding second sub-target bit; or, performing a subtraction operation on the ASCII code of the character of the data bit of the data to be encrypted and the ASCII code of the character of the corresponding first sub-target bit, and performing an addition operation on the ASCII code of the character of the data bit of the data to be encrypted and the ASCII code of the character of the corresponding second sub-target bit; or, performing a subtraction operation on the ASCII code of the character of the data bit of the data to be encrypted and the ASCII code of the character of the corresponding first sub-target bit and the second sub-target bit; or, performing an addition operation on the ASCII code of the character of the data bit of the data to be encrypted and the ASCII code of the character of the corresponding first sub-target bit and the second sub-target bit.
[0041] Step S140, performing a position exchange operation on the encryption characters of the multiple encryption bits included in the data to be encrypted, to obtain encrypted data corresponding to the data to be encrypted.
[0042] In this optional embodiment, by obtaining data to be encrypted and a first key and a second key for encrypting the data to be encrypted, the data to be encrypted is encrypted using the characters of the first target bits corresponding to the data bits of the data to be encrypted in the first key and the second key. Compared with the method of encrypting with only one key in the related art, the security of the data is improved, and the encrypted characters of the encryption bits in the data to be encrypted are swapped to obtain the encrypted data corresponding to the data to be encrypted. In this method, the data to be encrypted is encrypted by performing calculation operations and position swapping with multiple keys, which improves the security of the data to be encrypted, increases the anti-attack capability, and ensures the execution efficiency of the encryption process, thereby achieving both ensuring data security and improving the execution efficiency of the encryption process, thereby solving the technical problem in the related art that, when encrypting data, data security and execution efficiency cannot be ensured at the same time. The method of the embodiment of the present invention is applicable to any encryption scenario using keys, that is, the method can provide a key selection technology for any encryption scenario using keys, and has a wide range of uses.
[0043] As an embodiment of the present invention, the encryption method further includes:
[0044] Step S111, grouping the data to be encrypted according to the length of the first key and the length of the second key to obtain a plurality of groups of data to be encrypted.
[0045] In step S120, based on the first position correspondence, it is determined that the data bit of the data to be encrypted corresponds to the first target bit in the first key and the second key, wherein the first position correspondence includes: the 2m-1th data bit in the first key corresponds to the 2m-1th data bit in each group of data to be encrypted in the multiple groups of data to be encrypted, and the 2mth data bit in the second key corresponds to the 2mth data bit in each group of data to be encrypted in the multiple groups of data to be encrypted, wherein m is an integer greater than or equal to 1.
[0046] In one embodiment, the length of the first key is equal to the length of the second key. When the data to be encrypted are grouped, the length of each group of data in the plurality of groups of data to be encrypted is equal to the length of the first key and the second key.
[0047] In one embodiment, the length of the first key is not equal to the length of the second key, and the shorter key is supplemented with the "-" character to ensure that the lengths of the first key and the second key are equal. When the length of the data to be encrypted is not equal to an integer multiple of the lengths of the first key and the second key, the data to be encrypted is supplemented with characters such as "," to ensure that after the encrypted data is grouped, the length of each grouped data is equal to the length of the first key and the second key. It should be clear that the characters used to supplement the key and the characters used to supplement the encrypted data can be any characters.
[0048] For example, the data to be encrypted is a string containing 15 characters, the first key is a string containing 8 characters, and the second key is a string containing 7 characters. When encrypting, the character "-" is added to the first or last position of the second key, and the supplemented second key contains 8 characters, and its length is equal to the length of the first key; when grouping the data to be encrypted, the character "," is added to the first or last position of the data to be encrypted to form a string containing 16 characters, and the supplemented data to be encrypted are grouped according to the length of the first key and the supplemented second key to obtain two groups of grouped data, and the two groups of grouped data each include 8 characters.
[0049] In this embodiment of the invention, the data to be encrypted is grouped, and the odd bits in the first key are matched with the odd bits of the data to be encrypted in each group, and the even bits in the second key are matched with the even bits of the data to be encrypted in each group, and based on the characters of the odd bits in the first key and the characters of the even bits in the second key, the characters of the corresponding data bits in the data to be encrypted are encrypted, thereby improving the security of the data to be encrypted and the execution efficiency of the encryption process.
[0050] As an embodiment of the present invention, when obtaining the first key and the second key used to encrypt the data to be encrypted, multiple methods may be used, for example, the following optional methods may be used:
[0051] According to an nth-order equation in one variable and its solution, Vieta's theorem is used to solve the unknown coefficients of the nth-order equation in one variable, wherein one coefficient of the nth-order equation in one variable is known; based on the obtained unknown coefficients, a first key and a second key for encrypting the data to be encrypted are generated.
[0052] For example, for the quadratic equation ax 2 +bx+c=0, where the two solutions x1 and x1 of the equation and any one of the coefficients a, b and c are known. The two unknown coefficients of the quadratic equation are solved by using Vieta's theorem, and the two unknown coefficients are used as the first key and the second key respectively.
[0053] In this embodiment of the invention, a first key and a second key for encrypting data to be encrypted are obtained based on Vieta's theorem, and the key has high security. When the data to be encrypted is encrypted based on the first key and the second key, the security of the data to be encrypted is high.
[0054] Figure 2 is a flow chart of another encryption method according to an embodiment of the present invention. Figure 2 As shown, the encryption method also includes:
[0055] Step S131, obtaining a third key for encrypting the data to be encrypted.
[0056] Step S132, determining, based on the second position correspondence, that the data bit of the to-be-encrypted data after the first operation is performed corresponds to the second target bit in the third key.
[0057] Step S133, performing a second operation on the character of the data bit of the to-be-encrypted data after the first operation and the character of the second target bit, to obtain a target encryption character of the encryption bit corresponding to the data bit of the to-be-encrypted data.
[0058] Among them, performing a position swap operation on the encryption characters of multiple encryption bits included in the data to be encrypted to obtain encrypted data corresponding to the data to be encrypted includes: performing a position swap operation on the target encryption characters of multiple encryption bits included in the data to be encrypted to obtain encrypted data corresponding to the data to be encrypted.
[0059] In one embodiment, the second position correspondence relationship may be: the kth data bit in the third key corresponds to the kth data bit in the data to be encrypted after the first operation is performed, where k is an integer greater than or equal to 1. The second operation is: performing an addition operation or a subtraction operation on the ASCII code of the character of the data bit of the data to be encrypted after the first operation is performed and the ASCII code of the character of the corresponding second target bit.
[0060] In one embodiment, the method for obtaining the third key includes: according to a cubic equation and the solution of the cubic equation, using Vieta's theorem to solve the unknown coefficients of the cubic equation, wherein one coefficient of the cubic equation is known; based on the obtained unknown coefficients, generating a third key for encrypting the encrypted data.
[0061] In this optional embodiment, the third key is used to re-encrypt the data to be encrypted after the first operation is performed, thereby improving the security of the data to be encrypted.
[0062] Taking the case where the data to be encrypted is the document password "177477", the first key is "1656", and the second key is "112" as an example, the method for encrypting the document password "1774774" includes the following steps:
[0063] Step S10, use the character "-" to supplement the second key to obtain the supplemented second key "-112"; use "," to supplement the document password "177477" to obtain the supplemented document password ",,177477"; group the supplemented document password ",,177477" to obtain two groups of grouped data to be encrypted ",,17" and "7477".
[0064] Step S11, for the grouped data to be encrypted ",,17": add the ASCII code of the first character "1" of the first key "1656" and the ASCII code of the first character "," in the grouped data to be encrypted ",,17", add the ASCII code of the third character "5" of the first key "1656" and the ASCII code of the third character "1" in the grouped data ",,17", subtract the ASCII code of the second character "1" of the supplemented second key "-112" from the ASCII code of the second character "," in the grouped data ",,17", subtract the ASCII code of the fourth character "2" of the supplemented second key "-112" from the ASCII code of the fourth character "7" in the grouped data ",,17", thereby obtaining the encrypted characters of each encryption bit of the grouped data to be encrypted ",,17".
[0065] For the grouped data "7477" to be encrypted: add the ASCII code of the first character "1" of the first key "1656" and the ASCII code of the first character "7" in the grouped data "7477", add the ASCII code of the third character "5" of the first key "1656" and the ASCII code of the third character "7" in the grouped data "7477", subtract the ASCII code of the second character "1" of the supplemented second key "-112" from the ASCII code of the second character "4" in the grouped data "7477", subtract the ASCII code of the fourth character "2" of the supplemented second key "-112" from the ASCII code of the fourth character "7" in the grouped data "7477", thereby obtaining the encrypted characters of each encrypted bit of the grouped data "7477".
[0066] Step S12, concatenating the two groups of encrypted data using the first key and the second key, and swapping the encrypted characters of the odd-numbered encryption bits of the concatenated data with the encrypted characters of the even-numbered encryption bits adjacent to the odd-numbered encryption bits, to obtain the encrypted data corresponding to the document password "177477"; that is: for the concatenated data, swapping the encrypted characters of the first encryption bit with the encrypted characters of the second encryption bit, swapping the encrypted characters of the third encryption bit with the encrypted characters of the fourth encryption bit, swapping the encrypted characters of the fifth encryption bit with the encrypted characters of the sixth encryption bit, swapping the encrypted characters of the seventh encryption bit with the encrypted characters of the eighth encryption bit, to obtain the encrypted data of the document password "177477".
[0067] Alternatively, for the grouped data to be encrypted ",,17", the encrypted character of the first encryption bit and the encrypted character of the second encryption bit in the grouped data to be encrypted ",,17" encrypted using the first key and the second key are swapped, and the encrypted character of the third encryption bit and the encrypted character of the fourth encryption bit are swapped; for the grouped data to be encrypted "7477", the encrypted character of the first encryption bit and the encrypted character of the second encryption bit in the grouped data to be encrypted "7477" encrypted using the first key and the second key are swapped, and the encrypted character of the third encryption bit and the encrypted character of the fourth encryption bit are swapped; then the two groups of grouped data to be encrypted that have been processed as above are spliced to obtain the encrypted data corresponding to the document password "177477".
[0068] It should be noted that the above-mentioned first operation and second operation are addition or subtraction, which is only a simple example. Of course, it can also be any other achievable operation, for example, it can be numerical multiplication, division, modulo operation, etc., and it can also be logical operation and operation, or operation, etc.
[0069] Figure 3 is a flow chart of a decryption method according to an embodiment of the present invention. Figure 3 As shown, the decryption method includes the following steps:
[0070] Step S200, obtaining encrypted data.
[0071] Step S210: Obtain a first key and a second key for decrypting encrypted data.
[0072] Step S220, performing a position recovery operation on the encrypted characters of the multiple encryption bits included in the encrypted data to obtain target encrypted data.
[0073] The position recovery operation is to perform the inverse operation of the position exchange operation on the encrypted characters of the multiple encryption bits when the encrypted data is obtained.
[0074] For example, in the process of obtaining the encrypted data "FEHG", a position exchange operation was performed: the encrypted character "E" and the encrypted character "F" were exchanged, and the encrypted character "G" and the encrypted character "H" were exchanged. Then the position recovery operation is: the encrypted character "F" and the encrypted character "E" are exchanged, and the encrypted character "H" and the encrypted character "G" are exchanged, thereby obtaining the target encrypted data "EFGH".
[0075] Step S230: Determine, based on the first position correspondence, that the data bit of the target encrypted data corresponds to the first target bit in the first key and the second key.
[0076] Step S240, performing an inverse operation of the first operation on the character of the data bit of the target encrypted data and the character of the corresponding first target bit to obtain decrypted data corresponding to the encrypted data.
[0077] The first operation is an operation performed on the characters of the data bits of the data to be encrypted and the characters of the corresponding first target bits when the target encrypted data is obtained.
[0078] For example, when the target encrypted data "EFGH" is obtained, the first operation is performed: the ASCII code of the first character "A" in the data to be encrypted "ABCD" is subtracted from the ASCII code of the first character in the first key, the ASCII code of the second character "B" in the data to be encrypted "ABCD" is added to the ASCII code of the second character in the second key, the ASCII code of the third character "C" in the data to be encrypted "ABCD" is subtracted from the ASCII code of the third character in the first key, and the ASCII code of the fourth character "D" in the data to be encrypted "ABCD" is added to the ASCII code of the fourth character in the second key, thereby obtaining the target encrypted data " EFGH"; the inverse operation of the first operation is: add the ASCII code of the first character "E" in the data to be encrypted "EFGH" and the ASCII code of the first character in the first key, subtract the ASCII code of the second character "F" in the data to be encrypted "EFGH" from the ASCII code of the second character in the second key, add the ASCII code of the third character "G" in the data to be encrypted "EFGH" and the ASCII code of the third character in the first key, subtract the ASCII code of the fourth character "H" in the data to be encrypted "EFGH" from the ASCII code of the fourth character in the second key, thereby obtaining the decrypted data "ABCD" corresponding to the encrypted data.
[0079] In an embodiment of the present invention, by obtaining encrypted data and a first key and a second key for decrypting the encrypted data, a position recovery operation is performed on the encrypted characters of multiple encrypted bits included in the encrypted data to obtain target encrypted data, and then according to the first position correspondence, it is determined that the data bit of the target encrypted data corresponds to the first target bit in the first key and the second key, and the character of the data bit of the target encrypted data is subjected to the inverse operation of the first operation operation with the character of the corresponding first target bit, so as to obtain the decrypted data corresponding to the encrypted data. In this method, the encrypted data is decrypted by using a method of performing position exchange and operation operations on the encrypted data, which improves the security of the data and ensures the execution efficiency of the decryption process, thereby achieving both ensuring data security and improving the execution efficiency of the decryption process, thereby solving the technical problem in the related art that when decrypting data, data security and execution efficiency cannot be ensured at the same time; and the method of the embodiment of the present invention is applicable to any decryption scenario using a key.
[0080] As an embodiment of the present invention, the method further includes:
[0081] Step S211, grouping the target encrypted data according to the length of the first key and the length of the second key to obtain a plurality of grouped target encrypted data.
[0082] The first position correspondence relationship includes: the 2m-1th data bit in the first key corresponds to the 2m-1th data bit in each of the multiple grouped target encrypted data, and the 2mth data bit in the second key corresponds to the 2mth data bit in each of the multiple grouped target encrypted data, where m is an integer greater than or equal to 1.
[0083] In this embodiment of the invention, the encrypted data is grouped, and the odd bits in the first key are corresponded to the odd bits of the encrypted data of each group, and the even bits in the second key are corresponded to the even bits of the encrypted data of each group. Based on the characters of the odd bits in the first key and the characters of the even bits in the second key, the characters of the corresponding data bits in the encrypted data are decrypted, thereby improving the security of the data and the execution efficiency of the decryption process.
[0084] As an embodiment of the present invention, a method for obtaining a first key and a second key for decrypting encrypted data includes:
[0085] According to an nth-order equation and its solution, Vieta's theorem is used to solve the unknown coefficients of the nth-order equation, wherein one coefficient of the nth-order equation is known; based on the obtained unknown coefficients, a first key and a second key for decrypting encrypted data are generated.
[0086] In this embodiment of the invention, a first key and a second key for decrypting encrypted data are obtained based on Vieta's theorem. If the key has high security, then when the encrypted data is decrypted based on the first key and the second key, the data has high security.
[0087] Figure 4 is a flow chart of a decryption method according to another embodiment of the present invention. Figure 4 As shown, the decryption method also includes:
[0088] Step S231, obtaining a third key for decrypting the encrypted data.
[0089] Step S232: Determine, based on the second position correspondence, that the data bit of the target encrypted data after the inverse operation of the first operation is performed corresponds to the second target bit in the third key.
[0090] Step S240, performing the inverse operation of the first operation on the characters of the data bits of the target encrypted data and the characters of the corresponding first target bits to obtain decrypted data corresponding to the encrypted data, including: performing the inverse operation of the second operation on the characters of the data bits of the target encrypted data after performing the inverse operation of the first operation and the characters of the second target bits to obtain decrypted data corresponding to the encrypted data.
[0091] Specifically, the decrypted data corresponding to the encrypted data is obtained by performing the inverse operation of the first operation on the characters of the data bits of the target encrypted data and the characters of the corresponding first target bits, and performing the inverse operation of the second operation on the characters of the data bits of the target encrypted data after the inverse operation of the first operation and the characters of the second target bits. The second operation is an operation performed on the characters of the data bits of the data to be encrypted after the first operation and the characters of the second target bits when the target encrypted data is obtained.
[0092] In this embodiment of the invention, the target data to be encrypted is re-encrypted using the third key after the first operation is performed, thereby improving the security of the data to be encrypted.
[0093] As an optional embodiment of the present invention, the key encryption method includes the following steps:
[0094] Step S1, specify two numbers x1 and x2 as two solutions of the quadratic equation x2+bx+c=0, obtain the values of coefficients b and c according to Vieta's theorem, and then use them as keys key1 and key2 respectively; when the lengths of keys key1 and key2 are different, use the character "-" to supplement the shorter key so that the lengths of the two keys are the same.
[0095] Step S2, grouping the plaintext data (data to be encrypted) according to the length of keys key1 and key2, and obtaining multiple grouped plaintext data segments, the length of each plaintext data segment is equal to the length of keys key1 and key2; when the length of the plaintext data is not equal to an integer multiple of the length of keys key1 and key2, use the character " " (space) to supplement the plaintext data to ensure that the length of each grouped plaintext data segment after grouping is equal to the length of keys key1 and key2.
[0096] Step S3, converting characters of each plaintext data segment into ASCII codes, adding the ASCII codes of characters of odd-numbered digits in each plaintext data segment to the ASCII codes of characters of corresponding odd-numbered digits in key key1, and subtracting the ASCII codes of characters of even-numbered digits in each plaintext data segment from the ASCII codes of characters of corresponding even-numbered digits in key key2;
[0097] Step S4, swapping the positions of the characters of the odd-numbered digits in each grouped plaintext data segment after the operation using the key and the characters of the even-numbered digits following the odd-numbered digits and adjacent to the odd-numbered digits;
[0098] Step S5, converting the ASCII codes of the characters in the plaintext data segments of each group after position exchange into characters and "," to generate ciphertext (encrypted data).
[0099] In this optional embodiment, two keys are used to encrypt the data to be encrypted, and the characters in each group of plaintext data segments operated using the keys are swapped to obtain the encrypted data, thereby improving the security of the encryption, increasing the ability to resist attacks, and having high execution efficiency. It also solves the problem of low data security caused by using only one key for data encryption in the prior art. This encryption method can be applied to any encryption scenario using keys and has a wide range of uses.
[0100] As an optional embodiment of the present invention, the key decryption method includes the following steps:
[0101] Step S6, input the ciphertext (encrypted data) and the two solutions x1 and x2 of the quadratic equation x2+bx+c=0, obtain the values of the coefficients b and c according to Vieta's theorem, and then use them as keys key1 and key2 respectively; when the lengths of keys key1 and key2 are different, use the character "-" to supplement the shorter key to make the two keys the same length.
[0102] Step S7, grouping the ciphertext according to "," to obtain multiple grouped ciphertext data segments, the length of each ciphertext data segment is equal to the length of the keys key1 and key2.
[0103] Step S8, swapping the positions of the characters of the odd-numbered bits and the even-numbered bits adjacent to the odd-numbered bits in each grouped ciphertext data segment to obtain grouped ciphertext data segments in the correct order.
[0104] Step S9, subtract the ASCII codes of the odd-numbered characters in each correctly ordered grouped ciphertext data from the ASCII codes of the corresponding odd-numbered characters in the key key1, and add the ASCII codes of the even-numbered characters in each correctly ordered grouped ciphertext data to the ASCII codes of the corresponding even-numbered characters in the key key2 to obtain the grouped plaintext data segment.
[0105] Step S10, convert the ASCII code of each character in each group of plaintext data segments into characters for splicing, and obtain plaintext data (decrypted data corresponding to the encrypted data). In this optional embodiment, the encrypted data is decrypted using a method of character position exchange and another key for decryption, and the decrypted data corresponding to the encrypted data is obtained, which improves the security of the data, increases the anti-attack capability, and has high execution efficiency. The encryption method can be applied to any encryption scenario using a key and has a wide range of uses.
[0106] Figure 5 is a structural block diagram of an encryption device according to an embodiment of the present invention. Figure 5 The encryption device shown includes: a first acquisition module 30, a second acquisition module 31, a first determination module 32, a first operation module 33 and a first exchange module 34, and the device is described below.
[0107] The first acquisition module 30 is used to acquire the data to be encrypted.
[0108] The second acquisition module 31 is connected to the first acquisition module 30 and is used to acquire a first key and a second key for encrypting the data to be encrypted.
[0109] The first determination module 32 is connected to the second acquisition module 31 and is used to determine, based on the first position correspondence, that the data bit of the to-be-encrypted data corresponds to the first target bit in the first key and the second key.
[0110] The first operation module 33 is connected to the first determination module 32 and is used to perform a first operation on the character of the data bit of the to-be-encrypted data and the character of the corresponding first target bit to obtain the encryption character of the encryption bit corresponding to the data bit of the to-be-encrypted data.
[0111] The first exchange module 34 is connected to the first operation module 33 and is used to perform a position exchange operation on the encryption characters of the plurality of encryption bits included in the data to be encrypted to obtain the encrypted data corresponding to the data to be encrypted.
[0112] In an embodiment of the present invention, an encryption device is used to implement an encryption method. When implementing the encryption method, data to be encrypted and a first key and a second key for encrypting the data to be encrypted are obtained, the data to be encrypted is encrypted using characters of a first target bit corresponding to a data bit of the data to be encrypted in the first key and the second key, and the encrypted characters of the encryption bits in the encrypted data are swapped to obtain encrypted data corresponding to the data to be encrypted. In the present method, by using multiple keys to perform calculation operations and position swaps on the encrypted data, the security of the data to be encrypted is improved, and the execution efficiency of the encryption process can be guaranteed, thereby achieving both ensuring data security and improving the execution efficiency of the encryption process, thereby solving the technical problem in the related art that, when encrypting data, data security and execution efficiency cannot be ensured at the same time.
[0113] Figure 6 is a structural block diagram of a decryption device according to an embodiment of the present invention. Figure 6 As shown, the decryption device includes: a third acquisition module 40, a fourth acquisition module 41, a second exchange module 42, a second determination module 43 and a second operation module 44. The device is described below.
[0114] The third acquisition module 40 is used to acquire encrypted data.
[0115] The fourth acquisition module 41 is connected to the third acquisition module 40 and is used to acquire the first key and the second key for decrypting the encrypted data.
[0116] The second exchange module 42 is connected to the fourth acquisition module 41, and is used to perform a position recovery operation on the encrypted characters of multiple encryption bits included in the encrypted data to obtain target encrypted data, wherein the position recovery operation is: performing an inverse operation of the position exchange operation on the encrypted characters of multiple encryption bits when obtaining the encrypted data.
[0117] The second determination module 43 is connected to the second exchange module 42 and is used to determine, based on the first position correspondence, that the data bit of the target encrypted data corresponds to the first target bit in the first key and the second key.
[0118] The second operation module 44 is connected to the second determination module 43 and is used to perform an inverse operation of the first operation on the character of the data bit of the target encrypted data and the character of the corresponding first target bit to obtain decrypted data corresponding to the encrypted data.
[0119] In an embodiment of the present invention, a decryption device is used to implement a decryption method, by obtaining encrypted data and a first key and a second key for decrypting the encrypted data, performing a position recovery operation on the encrypted characters of multiple encrypted bits included in the encrypted data, obtaining target encrypted data, and then determining that the data bit of the target encrypted data corresponds to the first target bit in the first key and the second key according to the first position correspondence, and performing an inverse operation of the first operation on the character of the data bit of the target encrypted data and the character of the corresponding first target bit, to obtain decrypted data corresponding to the encrypted data. In this method, by using multiple keys to perform operation operations and position exchange on encrypted data, the security of the data is improved, and the execution efficiency is guaranteed, thereby achieving both ensuring data security and improving the execution efficiency of the decryption process, thereby solving the technical problem in the related art that when decrypting data, data security and execution efficiency cannot be ensured at the same time. And the method of the embodiment of the present invention is applicable to any encryption scenario using a key and the corresponding decryption scenario.
[0120] An embodiment of the present invention provides an electronic device, comprising: a processor; and a memory for storing instructions executable by the processor; wherein the processor is configured to execute the instructions to implement the method described in any one of the above embodiments and optional embodiments.
[0121] An embodiment of the present invention provides a computer-readable storage medium. When instructions in the computer-readable storage medium are executed by a processor of an electronic device, the electronic device can execute any one of the methods in the above embodiment and optional embodiments.
[0122] The serial numbers of the above embodiments of the present invention are only for description and do not represent the advantages or disadvantages of the embodiments.
[0123] In the above embodiments of the present invention, the description of each embodiment has its own emphasis. For parts that are not described in detail in a certain embodiment, reference can be made to the relevant descriptions of other embodiments.
[0124] In the several embodiments provided in this application, it should be understood that the disclosed technical content can be implemented in other ways. Among them, the device embodiments described above are only schematic. For example, the division of units can be a logical function division. There may be other division methods in actual implementation. For example, multiple units or components can be combined or integrated into another system, or some features can be ignored or not executed. Another point is that the mutual coupling or direct coupling or communication connection shown or discussed can be through some interfaces, indirect coupling or communication connection of units or modules, which can be electrical or other forms.
[0125] The units described as separate components may or may not be physically separated, and the components shown as units may or may not be physical units, that is, they may be located in one place or distributed on multiple units. Some or all of the units may be selected according to actual needs to achieve the purpose of the present embodiment.
[0126] In addition, each functional unit in each embodiment of the present invention may be integrated into one processing unit, or each unit may exist physically separately, or two or more units may be integrated into one unit. The above-mentioned integrated unit may be implemented in the form of hardware or in the form of software functional units.
[0127] If the integrated unit is implemented in the form of a software functional unit and sold or used as an independent product, it can be stored in a computer-readable storage medium. Based on this understanding, the technical solution of the present invention, in essence, or the part that contributes to the prior art, or all or part of the technical solution can be embodied in the form of a software product, and the computer software product is stored in a storage medium, including a number of instructions for a computer device (which can be a personal computer, a server or a network device, etc.) to perform all or part of the steps of the method described in each embodiment of the present invention. The aforementioned storage medium includes: U disk, read-only memory (ROM, Read-Only Memory), random access memory (RAM, Random Access Memory), mobile hard disk, magnetic disk or optical disk and other media that can store program codes.
[0128] The above is only a preferred embodiment of the present invention. It should be pointed out that for ordinary technicians in this technical field, several improvements and modifications can be made without departing from the principle of the present invention. These improvements and modifications should also be regarded as the scope of protection of the present invention.
Claims
1. An encryption method, characterized in that: include: Obtain the data to be encrypted; Obtaining a first key and a second key for encrypting the data to be encrypted, wherein the method for obtaining the first key and the second key for encrypting the data to be encrypted comprises: solving the unknown coefficients of the n-order equation using Vieta's theorem according to a n-order equation and a solution of the n-order equation, wherein a coefficient of one term of the n-order equation is known; generating the first key and the second key for encrypting the data to be encrypted based on the unknown coefficients obtained; Determining, based on the first position correspondence, that a data bit of the to-be-encrypted data corresponds to a first target bit in the first key and the second key; Performing a first operation on the character of the data bit of the data to be encrypted and the character of the corresponding first target bit to obtain an encryption character of the encryption bit corresponding to the data bit of the data to be encrypted; A position exchange operation is performed on the encryption characters of the multiple encryption bits included in the data to be encrypted to obtain encrypted data corresponding to the data to be encrypted.
2. The method according to claim 1, characterized in that The method further comprises: Grouping the data to be encrypted according to the length of the first key and the length of the second key to obtain a plurality of grouped data to be encrypted; The first position correspondence relationship includes: the 2m-1th data bit in the first key corresponds to the 2m-1th data bit in each group of the multiple grouped data to be encrypted, and the 2mth data bit in the second key corresponds to the 2mth data bit in each group of the multiple grouped data to be encrypted, where m is an integer greater than or equal to 1.
3. The method according to any one of claims 1 to 2, characterized in that The method further comprises: Acquire a third key for encrypting the data to be encrypted; Determining, based on the second position correspondence, that a data bit of the to-be-encrypted data after the first operation is performed corresponds to a second target bit in the third key; Perform a second operation on the character of the data bit of the data to be encrypted after the first operation is performed and the character of the second target bit to obtain a target encryption character of the encryption bit corresponding to the data bit of the data to be encrypted; The step of performing a position swap operation on the encryption characters of the multiple encryption bits included in the data to be encrypted to obtain the encrypted data corresponding to the data to be encrypted includes: performing a position swap operation on the target encryption characters of the multiple encryption bits included in the data to be encrypted to obtain the encrypted data corresponding to the data to be encrypted.
4. A decryption method, characterized in that: include: Get encrypted data; Obtaining a first key and a second key for decrypting the encrypted data, wherein the method for obtaining the first key and the second key for decrypting the encrypted data comprises: solving an unknown coefficient of a univariate n-order equation and a solution of the univariate n-order equation using Vieta's theorem, wherein a coefficient of one term of the univariate n-order equation is known; and generating the first key and the second key for decrypting the encrypted data based on the unknown coefficients obtained; Performing a position recovery operation on the encrypted characters of the multiple encryption bits included in the encrypted data to obtain target encrypted data, wherein the position recovery operation is an inverse operation of performing a position swap operation on the encrypted characters of the multiple encryption bits when obtaining the encrypted data; Determining, based on the first position correspondence, that a data bit of the target encrypted data corresponds to a first target bit in the first key and the second key; An inverse operation of the first operation is performed on the character of the data bit of the target encrypted data and the character of the corresponding first target bit to obtain decrypted data corresponding to the encrypted data.
5. The method according to claim 4, characterized in that The method further comprises: grouping the target encrypted data according to the length of the first key and the length of the second key to obtain a plurality of grouped target encrypted data; The first position correspondence relationship includes: the 2m-1th data bit in the first key corresponds to the 2m-1th data bit in each of the multiple grouped target encrypted data, and the 2mth data bit in the second key corresponds to the 2mth data bit in each of the multiple grouped target encrypted data, where m is an integer greater than or equal to 1.
6. The method according to any one of claims 4 to 5, characterized in that The method further comprises: Obtaining a third key for decrypting the encrypted data; Determining, based on the second position correspondence, that a data bit of the target encrypted data after performing the inverse operation of the first operation corresponds to a second target bit in the third key; Performing an inverse operation of a first operation on the characters of the data bits of the target encrypted data and the characters of the corresponding first target bits to obtain decrypted data corresponding to the encrypted data includes: performing an inverse operation of a second operation on the characters of the data bits of the target encrypted data after performing the inverse operation of the first operation and the characters of the second target bits to obtain decrypted data corresponding to the encrypted data.
7. An encryption device, characterized in that: include: A first acquisition module, used to acquire data to be encrypted; a second acquisition module, configured to acquire a first key and a second key for encrypting the data to be encrypted, wherein the second acquisition module is further configured to solve an unknown coefficient of a univariate n-order equation and a solution of the univariate n-order equation using Vieta's theorem, wherein a coefficient of one term of the univariate n-order equation is known; and generate the first key and the second key for encrypting the data to be encrypted based on the unknown coefficients; A first determining module, configured to determine, based on a first position correspondence, that a data bit of the to-be-encrypted data corresponds to a first target bit in the first key and the second key; A first operation module, used for performing a first operation on the character of the data bit of the data to be encrypted and the character of the corresponding first target bit to obtain an encryption character of the encryption bit corresponding to the data bit of the data to be encrypted; The first exchange module is used to perform a position exchange operation on the encryption characters of multiple encryption bits included in the data to be encrypted to obtain encrypted data corresponding to the data to be encrypted.
8. A decryption device, characterized in that: include: A third acquisition module is used to acquire encrypted data; a fourth acquisition module, configured to acquire a first key and a second key for decrypting the encrypted data, wherein the fourth acquisition module is further configured to solve an unknown coefficient of a univariate n-order equation using Vieta's theorem according to a univariate n-order equation and a solution of the univariate n-order equation, wherein a coefficient of one term of the univariate n-order equation is known; and to generate the first key and the second key for decrypting the encrypted data based on the unknown coefficients obtained; a second exchange module, configured to perform a position recovery operation on the encrypted characters of the plurality of encryption bits included in the encrypted data to obtain target encrypted data, wherein the position recovery operation is an inverse operation of the position exchange operation performed on the encrypted characters of the plurality of encryption bits when obtaining the encrypted data; A second determining module, configured to determine, based on the first position correspondence, that a data bit of the target encrypted data corresponds to a first target bit in the first key and the second key; The second operation module is used to perform an inverse operation of the first operation on the character of the data bit of the target encrypted data and the character of the corresponding first target bit to obtain decrypted data corresponding to the encrypted data.
9. An electronic device, characterized in that: include: processor; a memory for storing instructions executable by the processor; The processor is configured to execute the instructions to implement the method according to any one of claims 1 to 6.
10. A computer-readable storage medium, characterized in that: When the instructions in the computer-readable storage medium are executed by a processor of an electronic device, the electronic device is enabled to perform the method as claimed in any one of claims 1 to 6.
Citation Information
Patent Citations
Data anti-counterfeiting encryption method and device, computer equipment and storage medium
CN113660620A