[ summary of the invention ]
The invention overcomes the defects of the technology and provides a data encryption method for the electronic valuation balance.
In order to achieve the purpose, the invention adopts the following technical scheme:
a data encryption method for an electronic pricing scale comprises the following steps:
step a, setting original data of N bytes and a 32-bit random number, decomposing the 32-bit random number into four 8-bit random bytes which are respectively a random byte R0, a random byte R1, a random byte R2 and a random byte R3, setting insertion positions in encrypted data to be P0, P1, P2 and P3 respectively, setting% to be a remainder value, right shifting >, and setting 0x03 to be a hexadecimal number;
b, inserting a random byte R0 into the head end of original data, namely P0 is 0;
step c, inserting the random byte R1 into the position P1, P1 ═ R0% (LENX-1) +1+ ((R0> >4) &0x 03);
step d, inserting the random byte R2 into the position P2, P2 ═ P1+1+ ((R0> >4) &0x 03);
step e, inserting the random byte R3 into the position P3, where P3 is P2+1+ ((R0> >4) &0x03), and obtaining a mixed data;
step f, respectively carrying out exclusive or on random bytes RX according to the byte sequence of the original data in the mixed data, wherein the random bytes RX are random bytes R0, R1, R2 or R3, and X is the original data byte serial number N, and then 1 is added to the remainder of 3 to obtain secondary mixed data;
and g, inverting the high-low four bits of the 0 th byte or the hexadecimal number 0x6B, the 1 st byte, inverting the 2 nd byte by bit, inverting the 3 rd byte or the hexadecimal number 0x13 in the secondary mixed data, and so on to obtain the encrypted data.
The data encryption method for the electronic price computing scale is characterized by comprising the following steps: the 32-bit random number is a 32-bit random number produced by a 24-bit analog-to-digital converter of a load cell in an electronic price computing scale.
The data encryption method for the electronic price computing scale is characterized by comprising the following steps: the new 32-bit random number is the old 32-bit random number x 10+ 24-bit analog-to-digital conversion value% 10.
The data encryption method for the electronic price computing scale is characterized by comprising the following steps: r0 ═ Rand32> >24) &0xFF, R1 ═ Rand32> >16) &0xFF, R2 ═ Rand32> >8) &0xFF, R3(Rand32> >0) &0 xFF.
The invention has the beneficial effects that:
the encryption method of the invention effectively protects the internal original data of the electronic scale, so that the electronic scale is not easy to be cracked and identified, and further the electronic scale cannot cheat the modes of imitating a display panel and the like; meanwhile, the encryption algorithm is simple and practical, too many CPU resources are not occupied, the system smoothness and reliability are not influenced, the data decryption difficulty is high after encryption, and the data is difficult to decompose and extract under the condition of not knowing the algorithm.
[ detailed description of the invention ]
The following is a further detailed description of embodiments of the invention:
a data encryption method for an electronic pricing scale comprises the following steps:
step a, setting original data of N bytes, setting the original data length of the pricing scale to LEN, setting the encrypted data length of the pricing scale to LENX +4, setting a 32-bit random number, and decomposing the 32-bit random number into four 8-bit random bytes, namely a random byte R0, a random byte R1, a random byte R2 and a random byte R3, and setting the insertion positions of the encrypted data to P0, P1, P2 and P3 respectively, wherein% is remainder > > is right shift, & AND, 0x03 is hexadecimal number, wherein R0 & (Rand32> >24) &0xFF, R1 & (Rand32> & 16) &0xFF, & 2 & (Rand 48 > &0xFF, & R3(Rand32> & 0) &0 & 5960 & gtFF, wherein 32 nd is 0 xnd, 32 is sextuple random number;
b, inserting a random byte R0 into the head end of original data, namely P0 is 0;
step c, inserting a random byte R1 into a position P1, wherein P1 is R0% (LENX-1) +1+ ((R0> >4) &0x03), i.e., P1 is R0 remainders LENX-1, adds 1, and adds (R0 right-shifts four bits and hexadecimal number 0x03), wherein when P1 exceeds LENX, (LENX-1) is subtracted, and when P0 coincides with the insertion position P1 moves backward by one position, and likewise cannot exceed LENX after moving;
step d, inserting the random byte R2 into the position P2, P2 ═ P1+1+ ((R0> >4) &0x03), i.e. P2 ═ P1 plus 1, plus (R0 right-shifted by four bits and hexadecimal number 0x03), wherein when P2 exceeds LENX, (LENX-1) is subtracted, and when the random byte coincides with the position of insertion P0, P1, P2 is shifted backward by one position until it does not coincide, and after shifting, LENX cannot be exceeded as well;
step e, random byte R3 inserts position P3, P3 is P2+1+ ((R0> >4) &0x03), i.e., P3 is P2 plus 1, plus (R0 is shifted right four bits and hexadecimal number 0x03), when P3 exceeds LENX, minus (LENX-1), and when it coincides with insertion positions P0, P1, P2, P3 is shifted back one position until it does not coincide, and the shift likewise cannot exceed LENX. At this time, primary mixed data is obtained, wherein the positions of R1, R2 and R3 are not fixed and the sequence is not fixed;
step f, respectively carrying out exclusive or on random bytes RX according to the byte sequence of the original data in the primary mixed data, wherein the random bytes RX are random bytes R0, R1, R2 or R3, and X is the original data serial number N, and then adding 1 to the remainder of 3; that is, the 0 th original byte xor R1, the 1 st original byte xor R2, the 2 nd original byte xor R3, the 3 rd original byte xor R0, and so on, to obtain the secondary mixed data;
and g, performing the following operations on the secondary mixed data according to the byte sequence, namely performing operations on the 0 th byte or hexadecimal number 0x6B, reversing the high and low four bits of the 1 st byte, performing bit inversion on the 2 nd byte, performing exclusive OR on the 3 rd byte or the hexadecimal number 0x13, and so on. And finally, obtaining the encrypted data, wherein the length of the encrypted data is equal to the original data length + 4.
The 32-bit random number is a 32-bit random number generated by a 24-bit analog-to-digital converter of a weighing sensor in the electronic price computing scale, and the new 32-bit random number is the old 32-bit random number multiplied by 10+ 24-bit analog-to-digital conversion value% 10, namely the old 32-bit random number is multiplied by 10, and then the remainder value of the 24-bit analog-to-digital conversion value to 10 is added.
Encryption is realized through three steps, and primary mixed data is obtained through an algorithm of inserting 4 random bytes into original data; carrying out an algorithm of sequentially carrying out XOR encryption on the original data in the primary mixed data and the random number in the primary mixed data to obtain secondary mixed data; and the algorithm is used for sequentially encrypting the secondary mixed data in a whole.
Specific examples are:
example A:
a 10 byte raw data is:
{0x00,0x01,0x02,0x03,0x04,0x05,0x06,0x07,0x08,0x09}
step a, obtaining a 32-bit random number as: 0x34B7391F, and split into four 8-bit random numbers: r0 ═ 0x34, R1 ═ 0xb7, R2 ═ 0x39, R3 ═ 0x 1F;
b-e, calculating four insertion positions in the original data as follows: p0 ═ 0, P1 ═ 4, P2 ═ 8, P3 ═ 12;
the primary mixed data obtained after inserting the random number is:
{0x34,0x00,0x01,0x02,0xB7,0x03,0x04,0x05,0x39,0x06,0x07,0x08,0x1F,0 x09};
step f, carrying out exclusive OR on the original data in the primary mixed data according to the byte sequence and the random number in sequence to obtain secondary mixed data:
{0x34,0xB7,0x38,0x1D,0xB7,0xB4,0x3D,0x1A,0x39,0xB1,0x3E,0x17,0x 1F,0xBE};
step g, encrypting the secondary mixed data according to the byte sequence to obtain the encrypted data with the final length of 14 as follows:
{0x5F,0x7B,0xC7,0x0E,0xDC,0x4B,0xC2,0x09,0x52,0x1B,0xC1,0x04,0x 74,0xEB}。
example B:
a 10 byte raw data is:
{0x00,0x01,0x02,0x03,0x04,0x05,0x06,0x07,0x08,0x09};
step a, obtaining 32-bit random digits: 0x52B65A9B, split into four 8-bit random numbers: r0 ═ 0x52, R1 ═ 0xB6, R2 ═ 0x5A, R3 ═ 0x 9B;
b-e, calculating four insertion positions in the original data as follows: p0-0, P1-6, P2-8, P3-10
Inserting random numbers into the primary mixed data:
{0x52,0x00,0x01,0x02,0x03,0x04,0xB6,0x05,0x5A,0x06,0x9B,0x07,0x08, 0x09};
step f, carrying out exclusive OR on the original data in the primary mixed data according to the byte sequence and the random number in sequence to obtain secondary mixed data:
{0x52,0xB6,0x5B,0x99,0xB5,0x5E,0xB6,0x9E,0x5A,0xB0,0x9B,0x5D,0x 93,0xBF};
step g, encrypting the secondary mixed data according to the byte sequence to obtain the encrypted data with the final length of 14 as follows:
{0x39,0x6B,0xA4,0x8A,0xDE,0xE5,0x49,0x8D,0x31,0x0B,0x64,0x4E,0x F8,0xFB}。
when decoding, the encrypted data is decrypted reversely according to the steps g and f, and then the four inserted random bytes R0, R1, R2 and R3 are taken out respectively according to the steps a to e.