Disclosure of Invention
The technical problem to be solved by the invention is to provide an anti-counterfeiting code with uniqueness, confidentiality and irregularity, and to verify the anti-counterfeiting code by a special verification method, thereby overcoming the defects of the prior art.
In order to solve the technical problems, the invention adopts the following technical scheme; a commodity anti-counterfeiting code generation and verification method. The method comprises the following steps:
(1) composing basic information (4-bit enterprise code and 4-16 bit sequence code) of the anti-counterfeiting code into 0-level plaintext A;
(2) and the production center acquires the enterprise key K1 corresponding to the enterprise code according to the enterprise code A1, and encrypts the sequence code plaintext A2 based on K1 to obtain an encrypted ciphertext B1. Ciphertext B1 and plaintext A1 constitute level 1 ciphertext B;
(3) generating 1 5-bit random code R according to a system clock at the current moment, and obtaining a 3-bit verification code D2 by the R through a specific verification algorithm;
(4) encrypting the 1-level ciphertext B by the production center according to the production key K2 to obtain a 2-level ciphertext C;
(5) under the control of the random verification code D2, a ciphertext confusion code D1 is obtained by confusing the 2-level ciphertext C;
(6) obtaining a 1-bit verification code D3 by a ciphertext confusion code D1 and a 3-bit random verification code D2 through a specific verification algorithm;
(7) the production center encrypts the confusion ciphertext D consisting of the D1, the D2 and the D3 for the last time according to the website key K3 to obtain a final anti-counterfeiting code E;
(8) and storing the random code R into a database record corresponding to the 0-level plaintext A for future anti-counterfeiting code verification.
In the method for generating and verifying the anti-counterfeiting code of the commodity, the 0-level plaintext a in the step (2) includes a 4-bit enterprise code a1 and a sequence code a2, and the length N of a2 is: a number between 4 and 16, including 4 and 16 (since the sequence code may consist of numbers less than 4 bits, the sequence code length needs to be provided here);
in the method for generating and verifying the merchandise security code, the enterprise key K1 in step (2) is a primary key and is a key corresponding to the enterprise code a1, and different enterprise codes may correspond to different K1 and are composed of N-digit numbers or letters (expressed in the form of a character string);
in the method for generating and verifying the commodity anti-counterfeiting code, the random code R in the step (3) is composed of 5 digits (represented in an integer form), and in order to avoid the behavior of deducing the random number seed by possible malicious guess (deducing the random code according to a limited number of random verification codes and then deducing the random number seed according to the random code), the seed value is set according to the system time, and the random number seed is replaced according to the current system time after a certain time interval. In the invention, in order to prevent guessing random number attack behaviors as far as possible, QueryPerformancefrequency and QueryPerformancecounter functions are adopted to obtain system time in a Windows/C environment. And the seed value is replaced again according to the system time after a certain number of anti-counterfeiting codes are generated. Random numbers are then generated from the function with srand () using a standard C/C + + library.
In the method for generating and verifying the merchandise anti-counterfeiting code, the 3-bit verification code D2 in the step (3) needs to be mapped into 1 random verification code D2 with 3 bits by the 5-bit random code R. The more simply used algorithm is considered to be the modulus calculation.
In the method for generating and verifying the anti-counterfeiting code of the commodity, the production key K2 in the step (4) is a secondary key, and the key set by an anti-counterfeiting code generator is used with the K1 in a set. When K1 is replaced, it can be replaced at the same time. Consists of N +4 digits or letters (expressed in the form of character strings);
in the method for generating and verifying the commodity anti-counterfeiting code, the ciphertext confusion code D1 in the step (5) is controlled by the random verification code D2 (the length of D2 is fixed to be 3).
The commodity anti-counterfeiting code generation and verification method comprises the step (6) of checking a sequence check code D3 by adopting an algorithm similar to an identity card check code counting algorithm (ISO 7064:1983.MOD 11-2).
In the method for generating and verifying the merchandise anti-counterfeiting code, the website key K3 in the step (7) is a three-level key, belongs to a website verification key, and is not replaceable once set and consists of N +8 digits or letters (represented in a character string form).
According to the commodity anti-counterfeiting code generation and verification method, the anti-counterfeiting code verification comprises the following steps:
(1) the production center decrypts the anti-counterfeiting code E according to the website key K3 to obtain an obfuscated ciphertext D, and decomposes the D into D1, D2 and D3;
(2) obtaining a 1-bit verification code AD by a specific verification algorithm (verification is carried out by adopting an identity card check code counting algorithm (ISO 7064:1983.MOD 11-2) similar algorithm) by using a ciphertext confusion code D1 and a 3-bit random verification code D2, comparing the AD with the D3, entering the next step if the AD and the D3 are equal to each other, and otherwise, judging that the anti-counterfeiting code is a pseudo code, and stopping the verification process;
(3) under the control of the random verification code D2, the ciphertext confusion code D1 is subjected to confusion removal to obtain a 2-level ciphertext C;
(4) the production center decrypts the 2-level ciphertext C according to the production key K2 to obtain a 1-level ciphertext B, and decomposes the B into an enterprise code A1 and a sequence encryption code B1;
(5) judging whether the enterprise code A1 is a valid enterprise code, if so, entering the next step, otherwise, judging that the anti-counterfeiting code is a pseudo code, and stopping the verification process;
(6) and the production center acquires the enterprise key K1 corresponding to the enterprise code according to the enterprise code A1, and decrypts the sequence encryption code B1 based on K1 to obtain a sequence code plaintext A2. The enterprise code A1 and the sequence code plaintext A2 form 0-level plaintext A;
(7) according to the enterprise code A1, inquiring whether the sequence code plaintext A2 is a legal sequence code or not in the database, if so, entering the next step (the validity period of the sequence code plaintext A2 can be simultaneously inquired, and if the commodity inquiry corresponding to the sequence code exceeds the validity period, a prompt is given), otherwise, judging that the anti-counterfeiting code is a counterfeiting code, and stopping the verification process;
(8) acquiring a random code R corresponding to the plaintext A from a database record according to the level 0 plaintext A;
(9) and comparing the KD with the D2 by using the 3-bit random verification code KD obtained by the specific verification algorithm (the 5-bit random code R is mapped into 1 3-bit random verification code KD, and the simple algorithm is considered to be modular calculation), and finally judging the anti-counterfeiting code to be true if the KD is equal to the D2, otherwise, judging the anti-counterfeiting code to be the anti-counterfeiting code. The verification process ends.
The invention has the beneficial effects that: compared with the prior art, the method can generate a group of anti-counterfeiting codes with the length variable along with the length of the sequence codes by means of traditional encryption algorithm, special random code verification, check code verification, code word confusion processing and the like under the control of a specific key according to the enterprise codes of production enterprises and the sequence codes corresponding to the commodities. And the anti-counterfeiting code can meet the requirements of uniqueness, confidentiality and irregularity. Under the condition of reasonable computing resources, according to a limited number of anti-counterfeiting codes, under the condition that an anti-counterfeiting code generation software system, database information and a secret key are not disclosed, the anti-counterfeiting codes which can pass anti-counterfeiting verification are difficult to manufacture.
Example 1: as shown in fig. 1, the encryption algorithm of the present invention comprises the following steps:
(1) composing basic information of the anti-counterfeiting code into 0-level plaintext A; length N of enterprise code a1 and sequence code a2, a2, comprising 4 bits: a number between 4 and 16, including 4 and 16 (since the sequence code may consist of numbers less than 4 bits, the sequence code length needs to be provided here); level 0 plaintext a may be generated based on the requirements of the enterprise, where the enterprise code a1 may consist of the manufacturer code, the serial code a2 may consist of the serial number and the date of manufacture, such as 001100556420160427, where the manufacturer code: 0011, serial No.: 005564, date 2016, 04, month 27, and may also contain various codes for other information.
(2) The production center obtains an N-bit enterprise key K1 corresponding to the enterprise code according to the enterprise code A1, and encrypts a sequence code plaintext A2 based on K1 to obtain an encrypted ciphertext B1. Ciphertext B1 and plaintext A1 constitute level 1 ciphertext B;
(3) generating 1 5-bit random code R according to a system clock at the current moment, and obtaining a 3-bit verification code D2 by the R through a specific verification algorithm;
the random code is obtained by setting the seed of srand () to increase from 0, the generated random number is gradually increased, the random number is increased from 38 to 32766 (serial number 0-10022), then from 1 to 32765(20056), then from 0 to 32767(30091), then from 2 to 32765(40125), then from 1 to 32764(50159), and then from 0 to 32766 (60194); if the selected random code R is 26743,
it is necessary to map the random code R of 5 bits to the random authentication code D2 of 1 3-bit number. Generated by a random authentication code generation algorithm-modulo. The largest 3-bit prime number 997 is modulo.
D2=R%997 =26743%997=821。
(4) Encrypting the 1-level ciphertext B by the production center according to the K2 of the N +4 bits of the production key to obtain a 2-level ciphertext C;
(5) under the control of the random verification code D2, a ciphertext confusion code D1 is obtained by confusing the 2-level ciphertext C;
the invention adopts a circular expansion mode to expand the random verification code D2 into a key with the same length as the code word C to be confused. For example, C length is 14, D2= {8,2,1}, and after extension D2' = {8,2,1,8,2,1,8,2,1,8,2 }.
D2' is then encrypted with the secondary ciphertext C via an obfuscation algorithm to form ciphertext obfuscated code D1.
(6) Obtaining a 1-bit verification code D3 by a ciphertext confusion code D1 and a 3-bit random verification code D2 through a specific verification algorithm;
the verification method is characterized in that each code son of the ciphertext obfuscated code D1 and the random verification code D2 is multiplied by a multiplier factor (continuously circulating from right to left, wherein 1 corresponds to a verification bit): 1, 2, 4, 8, 5, 10, 9, 7, 3, 6, and the weight (multiplier factor) is calculated as follows: w is ai=r(i-1)(mod M) where r =2, M = 11) then sums all the products to sum, plus the column check code D3 value, whose sum must satisfy modulo 11 by 1. Namely: 1 ≡ (sum + D3) (mod 11), and D3 was obtained. Using 0821 as an example, if the multiplier factors are 5, 8, 4, 2, sum =5 × 0 +8 × 8 +4 × 2 + 2 × 1=74, 1 ≡ sum + D3 (mod 11), then the serial check code D3= 4.
(7) The production center encrypts the confusion ciphertext D consisting of the D1, the D2 and the D3 for the last time according to the website key K3 to obtain a final anti-counterfeiting code E;
(8) and storing the random code R into a database record corresponding to the 0-level plaintext A for future anti-counterfeiting code verification.
The invention adopts a classic encryption algorithm for encryption.
The enterprise key K1 used in the present invention is a key corresponding to the enterprise code a 1. Different enterprises correspond to different enterprise keys.
The production key K2 used in the present invention is a key set by the anti-counterfeit code generator.
The website key K3 used in the present invention is a website authentication key.
As shown in fig. 2-3: when the anti-counterfeiting code is verified, the verified anti-counterfeiting code E, the random code R, the enterprise key K1, the production key K2 and the website key K3 must be matched with data in an encryption algorithm, and the verification steps are as follows:
(1) the production center decrypts the anti-counterfeiting code E according to the website key K3 to obtain an obfuscated ciphertext D, and decomposes the D into D1, D2 and D3;
(2) obtaining a 1-bit verification code AD by a specific verification algorithm (verification is carried out by adopting an identity card check code counting algorithm (ISO 7064:1983.MOD 11-2) similar algorithm) by using a ciphertext confusion code D1 and a 3-bit random verification code D2, comparing the AD with the D3, entering the next step if the AD and the D3 are equal to each other, and otherwise, judging that the anti-counterfeiting code is a pseudo code, and stopping the verification process;
(3) under the control of the random verification code D2, the ciphertext confusion code D1 is subjected to confusion removal to obtain a 2-level ciphertext C;
(4) the production center decrypts the 2-level ciphertext C according to the production key K2 to obtain a 1-level ciphertext B, and decomposes the B into an enterprise code A1 and a sequence encryption code B1;
(5) judging whether the enterprise code A1 is a valid enterprise code, if so, entering the next step, otherwise, judging that the anti-counterfeiting code is a pseudo code, and stopping the verification process;
(6) and the production center acquires the enterprise key K1 corresponding to the enterprise code according to the enterprise code A1, and decrypts the sequence encryption code B1 based on K1 to obtain a sequence code plaintext A2. The enterprise code A1 and the sequence code plaintext A2 form 0-level plaintext A;
(7) according to the enterprise code A1, inquiring whether the sequence code plaintext A2 is a legal sequence code or not in the database, if so, entering the next step (the validity period of the sequence code plaintext A2 can be simultaneously inquired, and if the commodity inquiry corresponding to the sequence code exceeds the validity period, a prompt is given), otherwise, judging that the anti-counterfeiting code is a counterfeiting code, and stopping the verification process;
(8) acquiring a random code R corresponding to the plaintext A from a database record according to the level 0 plaintext A;
(9) and comparing the KD with the D2 by using the 3-bit random verification code KD obtained by the specific verification algorithm (the 5-bit random code R is mapped into 1 3-bit random verification code KD, and the simple algorithm is considered to be modular calculation), and finally judging the anti-counterfeiting code to be true if the KD is equal to the D2, otherwise, judging the anti-counterfeiting code to be the anti-counterfeiting code. The verification process ends.
The decryption algorithm in the invention adopts the inverse algorithm of the classical encryption algorithm to decrypt.
In the encryption and decryption process, the key used can be any combination of numbers, letters and symbols, but since the multi-representation encryption and decryption algorithm requires pure numbers, the key needs to be converted into numbers before encryption and decryption.
The mathematical method of the confusion resolution algorithm adopted in the invention is the same as the decryption method, namely, the inverse algorithm of the confusion algorithm is decrypted.
The embodiments of the present invention are not limited to the above-described examples, and various changes can be made without departing from the spirit and scope of the present invention.