Disclosure of Invention
In view of the above-mentioned drawbacks of the prior art, the present invention provides a linear random encryption and decryption algorithm for character data to solve the deficiencies of the prior art.
In order to achieve the above object, the present invention provides a linear random encryption and decryption algorithm for character data, comprising an encryption step and a decryption step:
the encryption step comprises:
S11, carrying out format judgment on the character string to be encrypted, carrying out next processing if the character string is legal, and returning an error if the character string is illegal;
S12, randomly generating a linear encryption coefficient k;
s13, determining the value range of the encryption parameter b according to the value of the encryption coefficient k;
S14, calculating a ciphertext storage value L of the encryption character string length, wherein L is k + the original character string length;
S15, converting the values of k and L from decimal system to capital letters, and filling 0-3 bits of cipher text bits;
S16, circularly generating encrypted ciphertext bits after the 4 th bit according to an algorithm formula y-kx + b;
S17, confusion arrangement increases cracking difficulty;
The decryption step comprises:
s21, carrying out format judgment on the ciphertext needing to be decrypted, carrying out next processing if the ciphertext is legal, and returning an error if the ciphertext is illegal;
s22, restoring and confusing the structural sequence of the ciphertext before arrangement;
s23, extracting a linear coefficient K value and the length of the character string, judging whether the length of the character string after decryption is equal to the length of the original character string, if not, returning a ciphertext error, and if so, entering the next step;
S24, extracting an encryption parameter b value and a y value of the first-bit ciphertext, and decrypting the first-bit ciphertext according to an algorithm formula y-kx + b;
s25, looping step S24, and decrypting all bit ciphertexts of the original character string.
further, in the algorithm formula y ═ kx + b, y represents an encrypted numerical value, x represents an original numerical value, and k and b are random numbers and are generated by a linear congruence method; y, k and b are positive numbers, x is the original character string and is converted into a decimal number through an ASC code, and the range is 33 to 126; y is expressed by two-digit twenty-six system, and the value range AA to ZZ of the twenty-six system of the root algorithm formula and the ciphertext composition principle y corresponds to decimal data of 0 to 675.
Further, step S15 is specifically: and respectively converting the values of k and L from decimal system into capital letters, filling 0-3 bits of ciphertext bits, wherein the 0 th bit is filled with random numbers, the 1 st bit is filled with encrypted k values, the 2 nd bit is filled with random numbers, and the 3 rd bit is filled with encrypted L values.
Further, the step S16 is specifically:
Filling a random number in the 4 th bit, randomly generating a b value of a first bit ciphertext within the value range of b, converting the b value into a twenty-six system number, and storing the twenty-six system number in the 5 th bit and the 6 th bit;
Encrypting the first digit cipher text, converting the characters of the original cipher text into decimal numbers of ASC codes, and according to a formula: obtaining a y value by y-kx + b, converting the y value from a decimal number into a twenty-six number, and storing the number in the 7 th bit and the 8 th bit;
Filling a random number in the 9 th bit, randomly generating a b value of a second bit ciphertext within the value range of b, converting the b value into a twenty-sixth binary number, and storing the twenty-sixth binary number in the 10 th bit and the 11 th bit;
Encrypting the second digit cipher text, converting the characters of the original cipher text into decimal numbers of ASC codes, and according to a formula: obtaining a y value by y-kx + b, converting the y value from a decimal number into a twenty-six number, and storing the number in the 12 th bit and the 13 th bit;
And by analogy, encrypting the original character ciphertext with all the digits.
further, in the decrypting step: the 5 th bit and the 6 th bit of the ciphertext access the b value of the first bit ciphertext, the 7 th bit and the 6 th bit access the y value after the encryption of the first bit original character, the b value and the y value are converted into decimal according to an algorithm formula after being converted from the twenty-six system: and y-kx + b, calculating the decimal number of the original character in the ASC code, and converting the decimal number into the corresponding character.
the invention has the beneficial effects that:
(1) The encryption algorithm is reversible, and the initial data can be restored through the inverse algorithm after the character string data are encrypted linearly and randomly. This facilitates data synchronization between remotely accessed databases, locally located servers, and also provides the user with the possibility of retrieving passwords for password loss.
(2) The encrypted ciphertext is difficult to crack, the encrypted ciphertext is represented by twenty-six upper-case English letters A-Z, even if the original characters are numbers, the lower-case characters or special characters are all covered in the ciphertext, the cracking rule is difficult, almost no more characters are found, for example, a 12-power combined number of 94 is 475920314814253376475136 when a 12-bit string is encrypted, and according to the operating speed of 50 hundred million per second of the current mainstream cpu i7 processor, at least 300 ten thousand years is needed if the 12-bit encrypted string is to be cracked.
(3) the encryption speed is high, and the consumption is low. The encryption algorithm uses a linear equation of once, y, kx + b, the number of cipher text iterations is small, the value of a linear coefficient k is randomly generated in a specific range, the range of the b value is determined according to the k value, the cipher text of a certain character can be calculated, and the consumption of system resources is very low.
The conception, the specific structure and the technical effects of the present invention will be further described with reference to the accompanying drawings to fully understand the objects, the features and the effects of the present invention.
Detailed Description
The invention provides a linear random encryption and decryption algorithm for character data, which comprises an encryption step and a decryption step:
the encryption step comprises:
S11, carrying out format judgment on the character string to be encrypted, carrying out next processing if the character string is legal, and returning an error if the character string is illegal;
S12, randomly generating a linear encryption coefficient k;
s13, determining the value range of the encryption parameter b according to the value of the encryption coefficient k;
s14, calculating a ciphertext storage value L of the encryption character string length, wherein L is k + the original character string length;
s15, converting the values of k and L from decimal system to capital letters, and filling 0-3 bits of cipher text bits;
s16, circularly generating encrypted ciphertext bits after the 4 th bit according to an algorithm formula y-kx + b;
S17, confusion arrangement increases cracking difficulty;
The decryption step comprises:
S21, carrying out format judgment on the ciphertext needing to be decrypted, carrying out next processing if the ciphertext is legal, and returning an error if the ciphertext is illegal;
s22, restoring and confusing the structural sequence of the ciphertext before arrangement;
S23, extracting a linear coefficient K value and the length of the character string, judging whether the length of the character string after decryption is equal to the length of the original character string, if not, returning a ciphertext error, and if so, entering the next step;
s24, extracting an encryption parameter b value and a y value of the first-bit ciphertext, and decrypting the first-bit ciphertext according to an algorithm formula y-kx + b;
s25, looping step S24, and decrypting all bit ciphertexts of the original character string.
the algorithm formula y is kx + b, wherein y represents an encrypted numerical value, x represents an original numerical value, and k and b are random numbers and are generated by adopting a linear congruence method; y, k and b are positive numbers, x is the original character string and is converted into a decimal number through an ASC code, and the range is 33 to 126; y is expressed by two-digit twenty-six system, and the value range AA to ZZ of the twenty-six system of the root algorithm formula and the ciphertext composition principle y corresponds to decimal data of 0 to 675.
wherein, step S15 specifically includes: and respectively converting the values of k and L from decimal system into capital letters, filling 0-3 bits of ciphertext bits, wherein the 0 th bit is filled with random numbers, the 1 st bit is filled with encrypted k values, the 2 nd bit is filled with random numbers, and the 3 rd bit is filled with encrypted L values.
wherein, the step S16 specifically includes:
filling a random number in the 4 th bit, randomly generating a b value of a first bit ciphertext within the value range of b, converting the b value into a twenty-six system number, and storing the twenty-six system number in the 5 th bit and the 6 th bit;
encrypting the first digit cipher text, converting the characters of the original cipher text into decimal numbers of ASC codes, and according to a formula: obtaining a y value by y-kx + b, converting the y value from a decimal number into a twenty-six number, and storing the number in the 7 th bit and the 8 th bit;
filling a random number in the 9 th bit, randomly generating a b value of a second bit ciphertext within the value range of b, converting the b value into a twenty-sixth binary number, and storing the twenty-sixth binary number in the 10 th bit and the 11 th bit;
Encrypting the second digit cipher text, converting the characters of the original cipher text into decimal numbers of ASC codes, and according to a formula: obtaining a y value by y-kx + b, converting the y value from a decimal number into a twenty-six number, and storing the number in the 12 th bit and the 13 th bit;
and by analogy, encrypting the original character ciphertext with all the digits.
Wherein, in the decryption step: the 5 th bit and the 6 th bit of the ciphertext access the b value of the first bit ciphertext, the 7 th bit and the 6 th bit access the y value after the encryption of the first bit original character, the b value and the y value are converted into decimal according to an algorithm formula after being converted from the twenty-six system: and y-kx + b, calculating the decimal number of the original character in the ASC code, and converting the decimal number into the corresponding character.
the following illustrates the principle method of the present invention with reference to specific examples:
The encryption only effectively encrypts visible character data within 33 to 126 in an ASC code decimal range, and the encryption is designed and realized by carrying out algorithm encryption and decryption on 12-bit character data in the embodiment. The ASC code range from 33 to 126 includes all english letters, numbers, punctuation marks, etc.
The method is characterized in that a linear encryption algorithm y is designed to be kx + b, wherein y represents an encrypted numerical value, x represents an original numerical value, k and b are random numbers, and the random numbers are generated by a linear congruence method. y, k and b are positive numbers, and x is the original character string and is converted into a decimal number through an ASC code, and the range is 33 to 126. y is expressed by two-digit twenty-six system, and the value range AA to ZZ of the twenty-six system of the root algorithm formula and the ciphertext composition principle y corresponds to decimal data of 0 to 675.
according to an algorithm formula: kx + b (0< y <675, 33< x <126)
y is represented in two-digit twenty-six, and when y is 675 and b is 0, x is the maximum value 126, resulting in an integer value of 5 for k. As can be seen from the algorithm formula, the k value decreases when the b value increases, and in order to narrow the range, the range of the k value is 1, 2, 3, 4 and 5. According to the algorithm design, the value of k is randomly generated by adopting a linear congruence method. We can determine the range of b values when k takes different values.
0< b <549 when k is 1
0< b <423 when k is 2
0< b <297 when k is 3
0< b <171 when k is 4
0< b <45 when k is 5
And b is a random number, after the k value is determined, the b value is randomly generated in the interval according to the value range, the b value is finally converted into a twenty-six system number, and two-bit system storage is allocated in a ciphertext structure.
And (3) ciphertext structure design:
taking the encrypted 12-bit character string data as an example, the encrypted ciphertext consists of 64-bit capital letters, each bit of data is represented by the capital letters A-Z, 2 capital letters are used for representing the ciphertext of each bit of original character after linear encryption, 1 capital letter is used for representing a linear encryption coefficient k value, 2 capital letters are used for representing a b value, 1 capital letter is used for representing the length of the encrypted character string data, in order to change the length bits into random numbers, the designed length value is equal to the actual character length plus the k value, and the random numbers are represented by 1 capital letter.
The values and encryption modes of the following variables can be determined according to the design:
linear coefficient k: the value range is 0< k <6, and corresponding capital letters A to E are arranged in sequence, namely 1 represents A, and 5 represents E.
b, variable value: according to the randomly generated k value, after the range is determined, a decimal number is randomly generated and then converted into a twenty-six system, namely a two-digit capital letter, and the two-digit ciphertext bit is used for storage.
ciphertext x: converting each original character into a decimal number according to an ASC code, wherein the value range is 33-126, and then according to a linear formula: y-kx + b, each y value representing a bit of the encrypted ciphertext.
the value of the y variable is: the value range 0< y <675, and is represented by the formula: and (5) after y is kx + b, converting the y value into a twenty-six system, and storing the y value by using two ciphertext bits.
in order to increase the random complexity of the ciphertext, each bit of ciphertext corresponds to a bit of random number b. The data bits except the ciphertext bit in the 64-bit data are filled with random numbers, and the data are subjected to staggered and confusing arrangement after being linearly encrypted according to an algorithm formula so as to increase the difficulty of cracking.
The algorithm of this example uses the following ciphertext structure:
Position 0: random number
1 st position: encrypted k value
Position 2: random number
position 3: the length L of the encrypted data is k + the length of the original character string
Position 4: random number
5 th to 6 th: b value random number
7 th to 8 th: bit 1 encrypted original data
position 9: random number
10 th to 11 th: b value random number
12 th to 13 th: 2 nd bit encrypted original data
by analogy with that
position 59: random number
at position 60-61: b value random number
62 nd to 63 th: 12 th bit encrypted original data
the flow chart of the encryption and decryption algorithm of the present embodiment is shown in fig. 1 and 2:
Description of encryption flow:
(1) And judging the format of the character string to be encrypted, if the character string is legal, performing the next processing, and if the character string is illegal, returning an error.
(2) the linear encryption coefficient k value is randomly generated in the range of 1 to 5.
(3) And determining the value range of b according to the value of k.
(4) and calculating a ciphertext storage value L of the encryption character string length, wherein the L is k + the original character string length.
(5) And respectively converting the values of k and L from decimal system into capital letters, filling 0-3 bits of ciphertext bits, wherein the 0 th bit is filled with random numbers, the 1 st bit is filled with encrypted k values, the 2 nd bit is filled with random numbers, and the 3 rd bit is filled with encrypted L values.
(6) And filling a random number in the 4 th bit, randomly generating a b value of the first bit ciphertext within the value range of b, converting the b value into a twenty-six system number, and storing the twenty-six system number in the 5 th bit and the 6 th bit.
(7) Encrypting the first digit cipher text, converting the characters of the original cipher text into decimal numbers of ASC codes, and according to a formula: and y is obtained as kx + b, and the y value is converted into a twenty-six number from a decimal number and stored in the 7 th bit and the 8 th bit.
(8) And the 12-bit original characters are encrypted sequentially by following the 6 th encryption step and the 7 th encryption step.
(9) The staggered confusion arrangement increases the difficulty of cracking.
description of decryption flow:
(1) and (4) carrying out format judgment on the ciphertext needing to be decrypted, carrying out next processing if the ciphertext is legal, and returning an error if the ciphertext is illegal.
(2) And restoring the structural sequence of the ciphertext before the confusion arrangement.
(3) and extracting the length bit of the encrypted character, judging whether the length bit is equal to 12 after decryption, and returning a ciphertext error if the length bit is not equal to 12.
(4) and extracting the b value and the y value of the first bit of ciphertext, and decrypting the first bit of ciphertext. The 5 th and 6 th bits access the b value of the first bit ciphertext, and the 7 th and 6 th bits access the encrypted y value of the first bit original character. After converting the b and y values from the twenty-six system to the decimal system, according to an algorithm formula: and y-kx + b, calculating the decimal number of the original character in the ASC code, and converting the decimal number into the corresponding character.
(5) And (5) sequentially decrypting the 12-bit encrypted ciphertext by circulating the decryption step of the step 4.
As shown in table 1 below, the cipher text obtained by encrypting the same segment of string data each time is random, the combination number of 12-bit characters is 94 to the power of 12, which is equal to 475920314814253376475136, and according to the operation speed of 50 hundred million per second of the current mainstream cpu i7 processor, if the 12-bit encrypted string needs to be cracked at least 300 ten thousand years, verification techniques such as pictures and numbers are often added in practical business applications, so that a cracker cannot input the 12-bit encrypted string such many times in one second, and the cracking is almost impossible.
TABLE 1 examples of encryption and decryption
in summary, the encryption and decryption algorithm of the present invention has the following advantages:
(1) the encryption algorithm is reversible, and the initial data can be restored through the inverse algorithm after the character string data are encrypted linearly and randomly. This facilitates data synchronization between remotely accessed databases, locally located servers, and also provides the user with the possibility of retrieving passwords for password loss.
(2) The encrypted ciphertext is difficult to crack, the encrypted ciphertext is represented by twenty-six upper-case English letters A-Z, even if the original characters are numbers, the lower-case characters or special characters are all covered in the ciphertext, the cracking rule is difficult, almost no more characters are found, for example, a 12-power combined number of 94 is 475920314814253376475136 when a 12-bit string is encrypted, and according to the operating speed of 50 hundred million per second of the current mainstream cpu i7 processor, at least 300 ten thousand years is needed if the 12-bit encrypted string is to be cracked.
(3) The encryption speed is high, and the consumption is low. The encryption algorithm uses a linear equation of once, y, kx + b, the number of cipher text iterations is small, the value of a linear coefficient k is randomly generated in a specific range, the range of the b value is determined according to the k value, the cipher text of a certain character can be calculated, and the consumption of system resources is very low.
the foregoing detailed description of the preferred embodiments of the invention has been presented. It should be understood that numerous modifications and variations could be devised by those skilled in the art in light of the present teachings without departing from the inventive concepts. Therefore, the technical solutions available to those skilled in the art through logic analysis, reasoning and limited experiments based on the prior art according to the concept of the present invention should be within the scope of protection defined by the claims.