Disclosure of Invention
In order to meet the above defects or improvement demands of the prior art, the invention provides an image encryption and decryption method, an image encryption and decryption device and a readable storage medium, wherein the image encryption and decryption method is a steganography-based airspace image content encryption algorithm. By the invention, the user can restrict the access right of partial people to the image partial content. The user himself or an authorized party restores the original content by means of the protected image when the conditions allow. The invention can carry out local encryption protection on the image content, and the encryption position information is embedded into the image itself in a way of attack. The encryption algorithm has high running speed and high encryption security.
The technical scheme adopted by the invention for solving the technical problems is as follows:
an image encryption method, comprising:
forming a marking element, wherein the marking element is used for distinguishing the content to be encrypted from the content not to be encrypted in the original image;
an encryption key is adopted to randomly form an encryption sequence, and the encryption sequence and the marking element are operated to obtain an encryption element;
and encrypting the image to be encrypted through the encryption element to form an encrypted image.
Specifically, the marking element is a binary marking matrix, if the spatial pixel matrix of the original image is Ih×w =(Ii,j )h×w The marking matrix is mh×w =(mi,j )h×w When Ii,j For pixels requiring encryption mi,j =1, otherwise mi,j =0;
Where i=1, 2,3, …, h and j=1, 2,3, …, w, Ii,j For a single pixel of the image, the value is between 0 and 255, and h and w are the height and width of the original image respectively.
Specifically, the encryption sequence is an encryption matrix, and n+1 binary encryption matrices composed of random 0 and 1 are generated by using a key, which are respectively in turn
Wherein the parameter N is a natural number greater than 0 and less than 8 selected according to individual pixels of the image;
employing a marking matrix to encrypt the first N encryption matrices
And clearing the element corresponding to the non-encrypted pixel position to obtain an encrypted element.
Specifically, bits and operations are respectively adopted on the first N encryption matrixes and the marking matrix to obtain first N mask matrixes, the (n+1) th encryption matrix is not processed to directly form the (n+1) th mask matrix, and the (n+1) th mask matrix is an encryption element.
Specifically, the method comprises the following steps: the marking element is embedded into the original image to form the image to be encrypted.
Specifically, a marking matrix is used to replace a bit of a single pixel of the original image, so as to form the image to be encrypted. The bit of the phase is agreed in advance by both sides.
In particular, the marking matrix is used to replace the lowest bit of a single pixel of the original image.
Specifically, the marking element is not embedded into the original image, and the image to be encrypted is the original image. Specifically, during encryption, the first N bit planes of the pixel matrix of the encrypted image and the first N encryption mask matrices are subjected to exclusive OR operation to form the encrypted image.
Specifically, the encryption element performs bit exclusive OR operation with the image to be encrypted to form an encrypted image.
Specifically, the N bit planes of the pixel matrix of the encrypted image except the bit plane embedded by the marking matrix are respectively xored with the first N mask matrices, and the last mask matrix is xored with the bit plane embedded by the marking matrix to form the encrypted image.
Specifically, the first N bitplanes of the pixel matrix of the encrypted image are respectively xored with the first N mask matrices, and the last mask matrix is xored with the bitplanes embedded by the mark matrix to form the encrypted image.
Specifically, for the 1 st, 2 nd, … nd, N,8 th n+1 bit plane of the image pixel matrix to be encrypted from high to low, exclusive-or operation is performed with the mask matrix respectively, if N is smaller than 7, the n+1 th to 7 th bit of the image pixel after encryption is the same as before encryption, and an encrypted image pixel matrix is obtained.
In particular, the parameter N is preferably 3, 4, 5, 6 or 7. The encryption effect can be ensured.
Specifically, three color channels of RGB of the BMP image are encrypted as three images, respectively.
The invention also provides an image decryption method, which comprises the following steps:
forming a decryption sequence by using the decryption key;
the decryption sequence and the marking element are used for operation to obtain a decryption element, and the marking element is a binary marking matrix used for distinguishing encrypted content from unencrypted content during encryption;
and decrypting the encrypted image by using the decryption element to obtain a decrypted image.
Specifically, the decryption key is the same as the encryption key used in encryption, and the formed decryption sequence is the same as the encryption sequence formed in encryption.
Specifically, the decryption sequence is a decryption matrix, and the decryption key generates n+1 binary decryption matrices consisting of random 0 and random 1; wherein the parameter N is a natural number which is more than 0 and less than 8 and is selected according to single pixels of the image when in encryption;
bit and operation are respectively adopted for the first N decryption matrixes and the marking matrix, so that the first N mask matrixes adopted during encryption are obtained, and the (n+1) th decryption matrix is the (n+1) th mask matrix adopted during encryption without processing;
the n+1 mask matrices form decryption elements.
Specifically, the exclusive or operation of the bit plane is performed on the encrypted image pixel matrix and the decryption element formed by the mask matrix, so as to obtain the decrypted image pixel matrix.
Specifically, the marking matrix is obtained by performing exclusive OR operation on the (n+1) th matrix in the decryption matrix and the corresponding bit plane replaced by the marking matrix in the encrypted image.
The present invention also provides an image encryption apparatus including:
the marking unit is used for generating marking elements which are used for distinguishing the content needing to be encrypted from the content needing to be encrypted;
an encryption key, the encryption key randomly forming an encryption sequence;
the computing unit is used for carrying out operation on the encryption sequence and the marking element to obtain an encryption element;
and an encryption unit that encrypts the image to be encrypted by the encryption element, thereby forming an encrypted image.
Specifically, the encryption device further includes: and the preprocessing unit marks the original image by using the marking element to form an image to be encrypted.
The present invention also provides an image decryption apparatus including:
a decryption key, the decryption key randomly forming a decryption sequence;
the calculating unit is used for calculating the decryption sequence and the marking element to obtain a decryption element; the marking element is a binary marking matrix used for distinguishing encrypted content from unencrypted content in encryption;
and the decryption unit decrypts the encrypted image by using the decryption element to obtain a decrypted image.
The invention also provides an electronic device comprising:
a processor;
a memory storing processor-executable instructions, wherein:
the processor reads instructions from the memory to implement the steps of the method as described above.
The invention also provides a readable storage medium having stored thereon computer instructions, characterized in that the instructions when executed by a processor implement the steps of the method as described above.
The invention can realize local encryption of the image, and the user can restrict the access right of partial people to the partial content of the image. The user himself or an authorized party restores the original content by means of the protected image when the conditions allow. Compared with the prior art, the invention has at least the following advantages:
(1) The invention can realize the encryption protection of the local content of the image without integral encryption;
(2) The invention can embed the encryption position information into the image itself, and the decryptor can decrypt the image content only by obtaining the decryption key;
(3) The invention has fast encryption and decryption operation speed and high security.
Detailed Description
The present invention will be described in further detail with reference to the drawings and examples, in order to make the objects, technical solutions and advantages of the present invention more apparent. It should be understood that the specific embodiments described herein are for purposes of illustration only and are not intended to limit the scope of the invention. In addition, the technical features of the embodiments of the present invention described below may be combined with each other as long as they do not collide with each other.
Fig. 1 is a schematic diagram of an image encryption step according to the present invention. The specific steps are as follows.
Let the spatial pixel matrix of the original image to be encrypted be Ih×w =(Ii,j )h×w I=1, 2,3, …, h and j=1, 2,3, …, w, wherein Ii,j Is a single pixel of the image, and the value is between 0 and 255; h and w are the height and width of the image, respectively. The user needs to choose the value of the parameter N in advance, where n=any one of 1,2,3, …, 7.
A marking matrix is generated. In this step a marking matrix m is generatedh×w =(mi,j )h×w I=1, 2,3, …, h and j=1, 2,3, …, w. When Ii,j Let m for pixels that need encryptioni,j =1, otherwise mi,j =0。
The marker matrix is steganographically embedded in the original image. The embedding may be bit substitution, i.e. substitution of bits in the image pixel value with bits that need to be embedded. In this step m will bei,j Replacement Ii,j I.e. the position-marker matrix mh×w Embedding image Ih×w And obtaining the image to be encrypted. The images before and after embedding basically have no change, and the embedded image to be encrypted is marked as I'h×w =(I′i,j )h×w 。
And generating N+1 mask matrixes by using the key K according to the parameter N selected in advance by the user. This step uses the key K to generate N+1 binary matrices consisting of random 0 and 1, respectively denoted as
Further, n+1 mask matrices are obtained as follows:
…
wherein ∈A is the bit and operation, i.e., the operation will be
And (3) clearing mask elements corresponding to the non-encrypted pixel positions to obtain a mask matrix, namely the encrypted elements.
The image is encrypted using a mask matrix exclusive-or operation. In this step, for the image pixel matrix I 'to be encrypted'
h×w =(I′
i,j )
h×w From high to low numbers 1,2, …, N,8 these n+1 bit planes are respectively associated with
Performing exclusive OR operation, i.e.)>
Here->
And->
Respectively representing the pixels I 'of the image before the encryption of the image to be encrypted'
i,j And an encrypted image pixel I
i,j From the high to the low number of kth bits, k=1, 2, ··, N,8. If N<7,I″
i,j N+1 to 7 bits and I'
i,j The same holds true. Finally obtain I
h×w =(I″
i,j )
h×w Is an encrypted image pixel matrix. Will I'
h×w And storing the image file to obtain a final encrypted image.
Fig. 2 is a schematic diagram of an image decryption step according to the present invention. The image decryption step is opposite to the encryption step, and the specific steps are as follows.
N+1 mask matrices are generated using the key K. This step uses the key K to generate N+1 binary matrices consisting of random 0 and 1, respectively denoted as
The n+1 matrices are identical to the matrix generated at the time of encryption. Due to the fact that therein
Thus use mask matrix->
And the encrypted image pixel matrix I
h ″
×w =(I″
i,j )
h×w The lowest bit plane is exclusive-ored to obtain an encrypted pixel marking matrix m
h×w ={m
i,j }
h×w I.e.
The mask matrix of the bit-planes is then obtained, i.e. the decryption elements are as follows:
…
the image is decrypted using a mask matrix exclusive-or operation. In this step for a matrix I of image pixels
h ″
×w =(I″
i,j )
h×w 1,2, …, N,8 N+1 bit levels from high to lowFaces respectively
Performing exclusive OR operation, i.e. pixel->
Here->
And->
Respectively represent pixels I 'before encryption'
i,j And an encrypted pixel I'
i,j From the high to the low number of kth bits, k=1, 2, ··, N,8. If N<7,I′
i,j N+1 to 7 th bits and I'
i,j The same applies. Finally obtain I'
h×w =(I′
i,j )
h×w To decrypt the image pixel matrix. Will I'
h×w And storing the image file to obtain a final decrypted image.
In the step of embedding the marking matrix into the original image, the marking of the original image can be realized by embedding the encrypted pixel marking matrix into other bit planes. When the marking matrix is embedded into other bits, the first N bit planes are respectively exclusive-ored with the first N mask matrices to realize content encryption, and the last mask matrix is exclusive-ored with the bit planes embedded by the marking matrix to encrypt marking information. And after the exclusive OR operation is completed, obtaining the encrypted image. Other steps are changed correspondingly.
In the step of embedding the marking matrix into the original image by steganography, the marking matrix can also be independently stored, and if the marking matrix is independently stored, the image to be encrypted is the original image Ih×w =(Ii,j )h×w . Since the marking information does not need to be embedded in the imageTherefore, the encryption method only needs to exclusive-or the first N encryption mask matrixes and the first N bit planes. Other steps are changed correspondingly.
In the step of encrypting the image by using the mask matrix exclusive-or operation, exclusive-or operation can be performed on other bit planes of the pixel matrix of the image to be encrypted and the mask matrix respectively, specifically, exclusive-or operation is performed on N bit planes of the pixel matrix of the encrypted image except for the bit plane embedded by the mark matrix and the first N mask matrices respectively, and exclusive-or operation is performed on the last mask matrix and the bit plane embedded by the mark matrix to form the encrypted image. The first N mask matrices and the last mask matrix of the present invention are only for convenience of description, and do not represent the positional relationship thereof in actual operation. According to practical needs, the last mask matrix can be placed at the first, and the first N mask matrices are placed at the back, so long as the corresponding operation relation is maintained. For convenience, the mask matrix that performs exclusive-or operation with the bit plane in which the tag matrix is embedded is placed at the last.
Example 1
Taking an 8 x 8 color BMP image, selecting n=4 as an example, encrypting three RGB color channels of the BMP image, and using the original image as three images by an encryptor, wherein one color channel is used as an example, the steps are as follows:
referring to fig. 3, it is a process of replacing the lowest bit of a single pixel of an original image with a marking matrix.
Generating a marking matrix: the encryptor generates amarker matrix 3a consisting of 0 and 1 from the pixels involved in the content to be encrypted of the BMP image.
Steganographically embedding the marking matrix into onecolor channel matrix 3b of the original image: the two parties agree in advance that the encryptor embeds the marking matrix in the lowest (last) bit plane of the image to obtain a marked image, i.e. theimage matrix 3c to be encrypted.
As in fig. 4 and 5, a mask matrix is generated: the encryptor generates 5 randombinary matrices 4a-4e by using the key, performs an and operation on the first 4encryption matrices 4a-4d and the markingmatrix 3a to obtain first 4mask matrices 5a-5d, and thelast encryption matrix 4e directly forms a 5th mask matrix 5e without processing.
Encrypting the image using a mask matrix exclusive-or operation: the encryptor uses themask matrices 5a-5e to exclusive-or with the 1 st, 2 nd, 3 rd, 4 th, 8 th bit planes of the single pixel of theimage 3c to be encrypted, respectively, to obtain an encrypted image spatial matrix (as shown in fig. 6).
After the above steps, the RGB three color component images are encrypted, and the three component images are combined and stored as BMP images as the final encrypted image.
After the above encryption process is completed, the encryptor may send the encrypted image and the key to the decryptor.
The decryption principle is based on the feature that the original value before the exclusive or is obtained after two exclusive or operations are performed on any bit. The image decryption steps are as follows:
for the BMP color image that has been encrypted as described above, the decryptor uses the key at the time of encryption as the decryption key. The three channels of RGB are decrypted as three images, respectively.
First a mask matrix is generated: the decryptor generates 5 decryption matrices as in fig. 4 (i.e. encryption matrices at encryption) using the key, then uses thepixel marking matrix 3a where the 5th decryption matrix 4e is directly different from the lowest bit plane of the encrypted image matrix (shown in fig. 6) or may obtain the encryption, and further uses the markingmatrix 3a to exclusive-or with the first four randombinary decryption matrices 4a-4d to obtain decryption elements, i.e. mask matrices fig. 5a-5d.
Decrypting the image using a mask matrix exclusive-or operation: the decryptor uses the mask matrix to exclusive-or the top four and 8 th bit planes of the image, i.e. the 1 st, 2 nd, 3 rd, 4 th and 8 th bit planes, to restore the image before encryption. The restored image is a marked image, and the marked image is only different from the original image in the least significant bit. From the view of the image content, the content difference of the two is not perceived, and the content of the image is restored.
Example two
Taking an 8 x 8 color BMP image as an example, selecting n=4, taking an RGB three color channels of the BMP image as three images for encryption, taking one color channel as an example, embedding a mark matrix into a 7 th bit plane from high to low of the image to obtain a marked image, namely an image matrix to be encrypted (as shown in fig. 7).
For the 5 th bit plane of the 1 st, 2 nd, … th, 4 th and 7 th bit planes of the image pixel matrix to be encrypted from high to low, exclusive OR operation is respectively carried out with the mask matrix, and the 5 th, 6 th and 8 th bits of the image pixels after encryption are the same as those before encryption, so as to obtain an encrypted image pixel matrix (as shown in figure 8). Correspondingly, when the marking matrix is extracted in the decryption step, the marking matrix is extracted from the 7 th bit plane, namely, the 5 th mask matrix and the bit plane embedded by the marking matrix, namely, the 7 th bit plane are subjected to exclusive OR operation to obtain the marking matrix.
The decryptor uses the mask matrix to exclusive-or the 5 th bit plane of the 1 st, 2 nd, 3 rd, 4 th and 7 th bit planes of the image, and restores the image before encryption. The other steps are the same as example one.
When the tag matrix is embedded in other bits (here, 7 th bit), the 1 st, 2 nd, 3 rd and 4 th bit planes are exclusive-ored with the 1 st, 2 nd, 3 rd and 4 th mask matrices to realize content encryption, and the 5 th mask matrix is exclusive-ored with the bit plane in which the tag matrix is embedded, namely, the 7 th bit plane to encrypt tag information.
Example III
In this embodiment, the image to be encrypted is the original image, and theoriginal image 3b shown in fig. 3 and the mask matrix shown in fig. 5 are directly xored to obtain an encrypted image, and the encrypted image is sent to the decryptor separately after encryption. The decryptor does not need to perform the extraction operation of the mark matrix, directly uses the random matrix generated by the mark matrix and the secret key as shown in fig. 4 to perform operation to obtain the mask matrix, and uses the exclusive or of the first 4 mask matrices and the first 4 bit planes of the encrypted image to obtain the decrypted image. The final decrypted image is the original image. By independently storing the storage flag matrix, it is possible to ensure that the decrypted image is completely identical to the original image. Since the tag information does not need to be embedded in the image, the encryption method only needs to exclusive-or the first 4 encryption mask matrices with the first 4 bit planes. By adopting the method, the secret key can only be used for generating N binary encryption matrixes, and the N encryption matrixes are respectively subjected to AND operation with the marking matrixes to obtain N mask matrixes.
According to the invention, the user can restrict the access rights of partial persons to the image partial content. The user himself or an authorized party restores the original content by means of the protected image when the conditions allow. Because the encrypted image is generated based on the data encryption of the partial area of the original image, the normal identifiable of the non-sensitive content of other positions of the image can be ensured on the premise of protecting the partial sensitive information. In addition, the data volume of the decrypted image of the terminal is also less, the time required by the decryption of the terminal is shorter, the real-time performance of the ornamental original image is improved, and the use experience of a user is further improved.
Fig. 9 is a schematic structural view of an image encryption device according to the present invention, the encryption device comprising:
and the marking unit is used for generating marking elements which are used for distinguishing the content needing to be encrypted from the content needing to be encrypted.
And the preprocessing unit marks the original image by using the marking element to form an image to be encrypted.
An encryption key, the encryption key randomly forming an encryption sequence.
And the calculating unit is used for calculating the encryption sequence and the marking element to obtain the encryption element.
And an encryption unit that encrypts the image to be encrypted by the encryption element, thereby forming an encrypted image.
The encryption device may also be provided without a preprocessing unit, i.e. the marking matrix is stored independently without being embedded in the image, without marking the original image.
Fig. 10 is a schematic structural view of an image decryption apparatus according to the present invention, the decryption apparatus comprising:
decryption keys that randomly form a decryption sequence.
The calculating unit is used for calculating the decryption sequence and the marking element to obtain a decryption element; the marking element is a binary marking matrix for distinguishing encrypted content from unencrypted content when encrypted.
And the decryption unit decrypts the encrypted image by using the decryption element to obtain a decrypted image.
Fig. 11 is a block diagram of an electronic device, shown in an embodiment of the invention. Referring to fig. 10, the electronic device may be an encryption device or a decryption device. The encryption device and the decryption device may be servers or terminals. The electronic device comprises a processor 1000 and a memory 2000 in which processor-executable instructions are stored, and the processor is in communication with the memory via a communication bus, from which the instructions can be read to implement the steps of the encryption and decryption methods described above.
The embodiments of the present invention also provide a readable storage medium having stored thereon computer instructions which, when executed by a processor, perform the steps of the encryption and decryption methods described above. The readable storage medium may be applied to a terminal, or may be applied to a server, and a technician may select according to a specific scenario, which is not limited herein.
It will be readily appreciated by those skilled in the art that the foregoing description is merely a preferred embodiment of the invention and is not intended to limit the invention, but any modifications, equivalents, improvements or alternatives falling within the spirit and principles of the invention are intended to be included within the scope of the invention.