Image encryption and decryption method, device and storage medium based on lossless compression algorithm
By preprocessing the image data and Huffman encoding compression, and using binary key encryption, the problem of easy cracking and low compression ratio in the prior art image compression and decryption methods is solved, and efficient compression and powerful encryption effects are achieved.
Patent Information
- Application Number
- CN202211563595.2
- Authority / Receiving Office
- CN · China
- Patent Type
- Patents(China)
- Current Assignee / Owner
- Filing Date
- 2022-12-07
- Publication Date
- 2025-03-25
- Estimated Expiration
- 2042-12-07
AI Technical Summary
The existing lossless image compression method is easy to crack and has a low compression ratio, and takes up more memory.
The image encryption method based on the lossless compression algorithm is adopted, and a large number of continuous 0s are generated by preprocessing the original image data, and the Huffman encoding and continuous data representation methods are used for compression, and the 254-bit binary key is used for encryption.
It improves the compression ratio of the picture, reduces the space memory occupied, and at the same time enhances the difficulty of cracking of the picture, effectively protecting the privacy of the picture.
Smart Images

Figure CN115941323B_ABST
Abstract
Description
Technical Field
[0001] The present invention relates to the field of picture encryption and decryption algorithms, and in particular to a picture encryption and decryption method, device and storage medium based on a lossless compression algorithm. Background Art
[0002] In the information age, people pay more and more attention to privacy protection in the process of image dissemination. It is a common practice to compress and encrypt images before dissemination. Existing lossless image compression methods include Huffman coding and LZW compression algorithm, but these methods are generally easy to crack, have low compression ratios, and occupy more space and memory. Summary of the invention
[0003] The present invention provides a method, device and storage medium for encrypting and decrypting a picture based on a lossless compression algorithm, so as to solve the problems of easy cracking and low compression ratio in the prior art picture compression and decryption methods.
[0004] In order to achieve the above object, the technical solution adopted by the present invention is:
[0005] A method for encrypting an image based on a lossless compression algorithm comprises the following steps:
[0006] Step 1: Get the original image data img and preprocess the original image data. The process is as follows:
[0007] Traverse each channel in the image data in sequence. For each channel, traverse all columns in sequence first, and start from the second column to convert the value of each column into the difference between the pixel values of the column and the previous column. Then traverse all rows in sequence, and start from the second row to convert the value of each row into the difference between the pixel values of the row and the previous row, thereby obtaining the preprocessed image data img_new;
[0008] Step 2: compress the image data img_new preprocessed in step 1. The process is as follows:
[0009] Step 2.1, build the int_bin function based on Python language, and get the dictionary represented by dictm based on the rules of int_bin function, where m ranges from integers 1 to 9, thereby obtaining dictionaries dict1, dict2, dict3, dict4, dict5, dict6, dict7, dict8, dict9, the key of dictionary dictm is integer n, n ranges from 0 to 2 to the power of m minus 1, and the value of the dictionary is the m-bit binary string converted from the key of the dictionary, that is, the integer n. If the actual length length1 of the converted binary data is less than m, then add m-length1 bits of character 0 in front of the converted binary data;
[0010] Step 2.2, set up blank lists list1, list2, list1_end, list2_end, loop through all channels of the preprocessed image data img_new, use k to represent the corresponding channel number, then loop through all rows of each channel, use i to represent the corresponding row number, then loop through all columns of each channel, use j to represent the corresponding column number, and add the above loop traversal results to the blank list according to the following rules:
[0011] If the traversed integer is equal to 0, first determine whether it is the last column. If it is the last column, add the string element 000 to the list list1. If it is not the last column, calculate the total number of consecutive characters 0, and limit the maximum value A of the consecutive number of times count of character 0. A is an integer greater than 6. Based on the consecutive number count, make judgments and add strings: when the consecutive number count is equal to 1, add the string 000 to the list list1; when the consecutive number count ranges from 2 to 5, add the string 001 to the list list1, and add the string dict2[ count-2], the elements in [] are the keys of the dictionary, which are used to get the values of the dictionary; when the number of consecutive times count ranges from 6 to A, the string 010 is added to the list list1, and the string dict6[count-6] is added to the list list2, and [] is the value of the dictionary dict6 with the key count-6; when the number of consecutive times count is greater than A, let count equal to A, then add the string 010 to the list list1, and add the string dict6[A-6] to the list list2, and finally let j=j+count-1, so that count times have been traversed;
[0012] If the traversed integer num is equal to 1, then add the string 011 to the list list1;
[0013] If the traversed integer num is equal to -1, then add the string 100 to the list list1;
[0014] If the traversed integer num is equal to 2, then add the string 101 to the list list1;
[0015] If the traversed integer num is equal to -2, then add the string 110 to the list list1;
[0016] If the traversed integer num is equal to 3 or 4, add the string 111 to list1 and add the string 00+dict1[num-3] to list2;
[0017] If the traversed integer num ranges from 5 to 8, add the string 111 to list1 and add the string 100+dict2[num-5] to list2;
[0018] If the traversed integer range is from 9 to 10, add the string 111 to list1 and add the string 101+dict9[num] to list2;
[0019] If the traversed integer num is equal to -3 or -4, then add the string 111 to list1 and add the string 01+dict1[-num-3] to list2;
[0020] If the traversed integer num ranges from -8 to -5, then add the string 111 to list1 and add the string 110+dict2[-num-5] to list2;
[0021] If the traversed integer num ranges from -10 to -9, then add the string 111 to list1, and add the string 111+dict9[-num] to list2;
[0022] After completing the above process, set j=j+1, so that the column number increases by 1, the value corresponding to the column number has been traversed, and the length of the lists list1 and list2 is determined at this time. If the length of the list list1 is greater than 50, add the element list1 to the list list1_end, and then set the list list1 to an empty list, that is, [], to facilitate the subsequent storage of new string elements. If the length of the list list2 is greater than 50, add the element list2 to the list list2_end, and then set the list list2 to an empty list;
[0023] Finally, set empty strings str1 and str2, and concatenate all string elements stored in list list1_end into str1, and concatenate all string elements stored in list list2_end into str2.
[0024] Step 3: Encrypt the data compressed in step 2. The process is as follows:
[0025] Step 3.1, set a 254-bit binary key;
[0026] Step 3.2: For str1 and str2 obtained in step 2, the encrypted strings str1_end and str2_end are obtained respectively using the key;
[0027] Step 3.3, the length of the encrypted string str1_end is represented by a 30-bit binary string str3, the width of the image is represented by a 13-bit binary string str4, and the height of the image is represented by a 13-bit binary string str5, and the following operations are performed: first, str1_end is added to the empty string str_end, and then str3, str4, and str5 are added to the front of the string str_end in sequence;
[0028] After completing the above operations, calculate the length of the string str_end, that is, the integer num_end, divide num_end by 24 and round down to get length, set the encrypted image width width=length, the encrypted image height heigth=length, enter the while loop to determine whether width*heigth*24 is greater than or equal to num_end, if not greater than or equal to num_end, let width increase by 1 and continue to loop and execute until width*heigth*24 is greater than or equal to num_end, calculate width*height*24-num to get the string 0 that needs to be added to the front of the string str_end, and fill the string str_end based on the calculation result;
[0029] Step 3.4, store the padded string str_end obtained in step 3.3 into the array img_end of size height, width, 3 through three layers of for loops;
[0030] Step 3.5, write the img_end data into the picture to get the compressed and encrypted picture.
[0031] In the further step 2.1, the rule of the int_bin function is: for the integer num converted to binary data of length num1, if the actual length of the converted binary data num2 is less than num1, the character 0 of num1-num2 is added in front of the converted binary data.
[0032] In the further step 2.2, a for loop is used to loop through all channels of the preprocessed image data img_new, and then a for loop is used to loop through all rows of each channel, and finally a while loop is used to loop through all columns of each channel.
[0033] In the further step 3.1, the 254 binary key is represented by a 33-bit unordered integer from 0 to 255.
[0034] Furthermore, the encryption process in step 3.2 is as follows: perform an XOR operation on the corresponding position of str1 and the key, add the result to the empty string str1_end, move the key to the next position after the XOR operation is completed, and continue the XOR operation until all XOR operations on str1 are completed, and obtain the encrypted string str1_end;
[0035] Perform an XOR operation on the corresponding position of str2 and the key, add the result to the empty string str2_end, move the key to the next position and continue XORing until all XOR operations on str2 are completed, and get the encrypted string str2_end.
[0036] Furthermore, in step 3.4, when the array img_end is subjected to three layers of for loops, the first layer of loops is for traversing all the rows of each channel, that is, for i in range(height), the second layer of loops is for traversing all the columns of each channel, that is, for j in range(width), and the third layer of loops is for traversing the channels, that is, for k in range(3). The loop body content is to truncate the string str_end once every 8 bits and convert it into an integer stored in the i-th row, j-th column, and k-th channel of img_after, that is, img_after[i, j, k] = int(str_end[(width * height * k + i *width + j) * 8:(width * height * k + i * width + j + 1) * 8], 2).
[0037] Furthermore, the lossless compression ratio of the compressed and encrypted image obtained in step 3.4 is (img.shape[0]*img.shape[1]) / (img_after.shape[0]*img_after.shape[1]), where img is the original image data, img.shape[0] is the height of the original image, img.shape[1] is the width of the original image, img_after.shape[0] is the height of the compressed and encrypted image, and img_after.shape[1] is the width of the compressed and encrypted image.
[0038] A method for decrypting and decompressing a picture processed by the above encryption and compression method comprises the following steps:
[0039] Step S1: decrypt the encrypted image. The process is as follows:
[0040] Step S11, store the encrypted image in img, convert the integers stored in img into 8-bit binary strings in the order of the 0th channel to the 2nd channel, the 0th row to the last row, and the first column to the last column, and add them to the end of the empty string str_end in sequence;
[0041] Step S12, set the index k1 of the string str_end=0, and determine whether str_end[k1] is the string 0 through a while loop. If it is equal to the string 0, k1 is incremented by 1 and the loop continues. If it is not equal to the string 0, the loop is exited, and k1 is incremented by 1 again, and the loop is not continued, and the next step is executed;
[0042] Step S13, let the height be equal to the integer converted from the binary string truncated from k1 to k1+12 with the index of the string str_end, that is, height=int(str_end[k1:k1+13],2), and let k1 increase by 13, that is, k1=k1+13; then let the width be equal to the integer converted from the binary string truncated from k1 to k1+12 with the index of the string str_end, that is, width=int(str_end[k1:k1+13],2), and let k1 increase by 13, that is, k1=k1+30; then let sum1 be equal to the integer converted from the binary string truncated from k1 to k1+30 with the index of the string str_end, that is, sum1 = int(str_end[k1:k1 +30], 2), and let k1 increase by 30, that is, k1=k1+30; then let str1_end equal to the binary character truncated from the index of string str_end from k1 to k1+id_sum, that is, str1_end=str_end[k1:k1+id_sum], and let k1 increase by sum1, that is, k1=k1+sum1; finally, let str2_end equal to the binary character truncated from the index of string str_end from k1 to k1, that is, str2_end=str_end[k1:-1], thus obtaining str1_end and str2_end;
[0043] Step S14, for str1_end and str2_end, perform an XOR operation on str1_end and the corresponding position of the key, add the result to the empty string, and after the XOR is completed, move the key to the next position and continue the XOR until all str1_end are XORed, thereby obtaining the string str1;
[0044] Perform XOR operation on str2_end and the corresponding position of the key, add the result to the empty string, move the key to the next position and continue XOR until all str2_end are XORed, thus obtaining the string str2;
[0045] Step S2: decompress the image data decrypted in step S1. The process is as follows:
[0046] Create an ndarray array img1 without any specific values, whose dimensions are height, width, and 3, that is, img1 = np.empty((height,width,3)), let i = 0, j = 0, k = 0, k2 = 0, where i, j, k are the dimensions of the array img1, k2 is the index of the string str1, let img1 enter the while loop, and the termination condition of the loop is k greater than or equal to 3;
[0047] Let str3 be equal to the binary string truncated from k2*3 to (k2+1)*3 of the string str1, that is, str3=str1[k2*3:(k2+1)*3], and then let k2 increment by 1; if str3 is not equal to the string 111, continue to judge, if str3 is equal to the string 000, let the value of the i-th row, j-th column, and k-th channel of the array img1 be equal to 0, that is, img1[i,j,k]=0, if str3 is equal to the string 001, let count = int(str2[k2:k2+ 2], 2)+2, and let k1 increment by 2;
[0048] Next, for loop count times, the loop body content is to set the value of the i-th row, j-th column, and k-th channel of the array img1 to 0, that is, img1[i,j,k]=0,j=j+1; during the loop, if j is equal to width, then set j=0,i=i+1, if i is equal to height, then set k=k+1,i=0,j=0, if k is equal to 3, then jump out of the loop, and finally set j=j-1, because j is added once more in the last loop process;
[0049] If str3 is equal to the string 001, let count be equal to the integer converted from the binary string truncated from k2 to k2+2 of str2 plus 2, that is, count = int(str2[k2:k2 + 2], 2) + 2, let k1=k1+2; then for loop count times, the loop body content is the value of the i-th row, j-th column, and k-th channel of the array img1 is equal to 0, that is, img1[i,j,k]=0,j=j+1; during the loop, if j is equal to width, let j=0,i=i+1, if i is equal to height, let k=k+1,i=0,j=0, if k is equal to 3, then jump out of the loop; finally, let j=j-1, because j is added once more at the end of the loop;
[0050] If str3 is equal to the string 010, let count be equal to the integer converted from the binary string truncated from the index k2 to k2+6 of str2 plus 6, that is, count = int(str2[k2:k2 + 6], 2) + 6, let k1=k1+6; then for loop count times, the loop body content is the value of the i-th row, j-th column, and k-th channel of the array img1 is equal to 0, that is, img1[i,j,k]=0,j=j+1; during the loop, if j is equal to width, let j=0,i=i+1, if i is equal to height, let k=k+1,i=0,j=0, if k is equal to 3, then jump out of the loop; finally, let j=j-1, because j is added once more at the end of the loop;
[0051] If str3 is equal to the string 000, then let the value of the i-th row, j-th column, and k-th channel of the array img1 be equal to 0, that is, img1[i,j,k]=0;
[0052] If str3 is equal to the string 011, then let the value of the i-th row, j-th column, and k-th channel of the array img1 be equal to 1, that is, img1[i,j,k]=1;
[0053] If str3 is equal to the string 100, then let the value of the i-th row, j-th column, and k-th channel of the array img1 be equal to -1, that is, img1[i,j,k]=-1;
[0054] If str3 is equal to the string 101, then let the value of the i-th row, j-th column, and k-th channel of the array img1 be equal to 2, that is, img1[i,j,k]=2;
[0055] If str3 is equal to the string 110, then let the value of the i-th row, j-th column, and k-th channel of the array img1 be equal to -2, that is, img1[i,j,k]=-2;
[0056] If str3 is equal to the string 111, then enter the while infinite loop, the loop body content is as follows:
[0057] If the binary string truncated from k2 to k2+2 of str2, that is, str2[k2:k2 + 2], is equal to the string 00, then let k2=k2+2, let the value of the i-th row, j-th column, and k-th channel of the array img1 be equal to the integer converted from the binary string truncated from k2 to k2+1 of the string str2, plus 3, that is, img1[i, j, k] = int(str2[k2:k2 + 1], 2) + 3, then let k2=k2+1, and finally jump out of the loop through break;
[0058] If the binary string truncated from k2 to k2+3 of str2, that is, str2[k2:k2 + 3] is equal to the string 100, then let k2=k2+3, let the value of the i-th row, j-th column, and k-th channel of the array img1 be equal to the integer converted from the binary string truncated from k2 to k2+2 of the string str2, plus 5, that is, img1[i, j, k] =int(str2[k2:k2+ 2], 2) + 5, k2=k2+2, and finally break to jump out of the loop;
[0059] If the binary string truncated from k2 to k2+3 of str2, that is, str2[k2:k2 + 3] is equal to the string 101, then let k2=k2+3, let the value of the i-th row, j-th column, and k-th channel of the array img1 be equal to the integer converted from the binary string truncated from k2 to k2+2 of the string str2, plus 5, that is, img1[i, j, k] =int(str2[k2:k2+ 2], 2) + 5, k2=k2+2, and finally jump out of the loop through break;
[0060] If the binary string truncated from k2 to k2+2 of str2, that is, str2[k2:k2 + 2], is equal to the string 01, then let k2=k2+2, let the value of the i-th row, j-th column, and k-th channel of the array img1 be equal to the negative of the integer converted from the binary string truncated from k2 to k2+1 of the string str2, minus 3, that is, img1[i, j, k] =-int(str2[k2:k2 + 1], 2) -3, then let k2=k2+1, and finally jump out of the loop through break;
[0061] If the binary string truncated from k2 to k2+2 of str2, that is, str2[k2:k2 + 3], is equal to the string 110, then let k2=k2+3, let the value of the i-th row, j-th column, and k-th channel of the array img1 be equal to the negative of the integer converted from the binary string truncated from k2 to k2+2 of the string str2, minus 5, that is, img1[i, j, k] =-int(str2[k2:k2 + 2], 2) -5, then let k2=k2+1, and finally jump out of the loop through break;
[0062] If the binary string truncated from k2 to k2+3 of str2, that is, str2[k2:k2 + 3], is equal to the string 111, then let k2=k2+3, let the value of the i-th row, j-th column, and k-th channel of the array img1 be equal to the negative integer converted from the binary string truncated from k2 to k2+9 of the string str2, that is, img1[i, j, k] =-int(str2[k2:k2+ 9], 2), then let k2=k2+9, and finally jump out of the loop through break;
[0063] After completing the above process, let j add 1, because the value of the current column has been traversed. If j is equal to width, let i=i+1, j=0; if i is equal to height, let k=k+1, i=0, j=0; the img1 obtained is the decompressed data;
[0064] Step S3: Process the data decompressed in step S2. The process is as follows:
[0065] Step S3.1, let img_0 be equal to the data of the 0th channel in img1, that is, img_0=img1[:,:,0], let img_1 be equal to img_0 plus the data of the 1st channel in img1, that is, img_1=img_0+img1[:,:,1], let img_2 be equal to img_1 plus the data of the 2nd channel in img1, that is, img_2=img_1+img1[:,:,2], img_0_copy is the data after deep copy of img_0, that is, img_0_copy=copy.deepcopy(img_0), let img_1_copy be the data after deep copy of img_1, that is, img_1_copy=copy.deepcopy(img_1), let img_2_copy be the data after deep copy of img_2, that is, img_2_copy=copy.deepcopy(img_2);
[0066] Step S3.2, let shape be the size of array img1, shape=img1.shape, let img2 be the data after deep copy of img1, that is, img2=copy.deepcopy(img1);
[0067] Through the for loop, traverse from 1 to shape[0] for i, and the loop body content is that all the data in the i-th row of img_2_copy is equal to all the data in the i-th row of img_2 plus all the data in the i-1-th row of img_2_copy, that is, img_2_copy[i,:]=img_2[i,:]+img_2_copy[i-1,:];
[0068] After the for loop, let all the data of the second channel of img2 be the data after the deep copy of img_2_copy, that is, img2[:,:,2]=copy.deepcopy(img_2_copy), and then traverse from 1 to shape[1] for j through the for loop. The content of the loop body is that all the data of the jth column and the second channel of img2 is equal to all the data of the jth column of img_2_copy plus all the data of the second channel of the j-1th column of img2, that is, img2[:,j,2]=img_2_copy[:,j]+img2[:,j-1,2];
[0069] Step S3.3, then traverse from 1 to shape[0] for i through a for loop, the loop body content is that all the data in the i-th row of img_1_copy is equal to all the data in the i-th row of img_1 plus all the data in the i-1-th row of img_1_copy, that is, img_1_copy[i,:]=img_1[i,:]+img_1_copy[i-1,:];
[0070] After the for loop, let all the data of the first channel of img2 be the data after the deep copy of img_1_copy, that is, img2[:,:,1]=copy.deepcopy(img_1_copy), and then traverse from 1 to shape[1] for j through the for loop. The content of the loop body is that all the data of the jth column and the first channel of img2 is equal to all the data of the jth column of img_1_copy plus all the data of the second channel of the j-1th column of img2, that is, img2[:,j,1]=img_1_copy[:,j]+img2[:,j-1,2];
[0071] Step S3.4, traverse from 1 to shape[0] through a for loop, and the loop body content is that all the data in the i-th row of img_0_copy is equal to all the data in the i-th row of img_0 plus all the data in the i-1-th row of img_0_copy, that is, img_0_copy[i,:]=img_0[i,:]+img_0_copy[i-1,:];
[0072] After the for loop, let all the data of the 0th channel of img2 be the data after the deep copy of img_0_copy, that is, img2[:,:,0]=copy.deepcopy(img_0_copy), and then traverse from 1 to shape[1] for j through the for loop. The content of the loop body is the jth column of img2. All the data of the 0th channel is equal to all the data of the jth column of img_0_copy plus all the data of the j-1th column and the 0th channel of img2, that is, img2[:,j,0]=img_0_copy[:,j]+img2[:,j-1,0];
[0073] Step S3.5, write the img2 data into the image to obtain the original image.
[0074] An electronic device comprises a processor and a memory, wherein the memory stores program instructions, and when the processor runs the program instructions in the memory, the steps of the above encryption and lossless compression method are executed.
[0075] A readable storage medium stores program instructions, and when the program instructions are read, the steps of the above-mentioned encryption and lossless compression method are executed.
[0076] In the encryption and lossless compression method of the present invention, the picture data is preprocessed in step 1, so that a large number of continuous zeros appear, which is convenient for subsequent data compression. In step 2, the preprocessed picture data is encoded using a continuous data original representation method, Huffman coding, so that the data can be effectively compressed. In step 3, the data is encrypted using a 254-bit binary key, and the encrypted data is padded with zeros so that the data can be stored in an image close to a square, so that the data is difficult to crack.
[0077] In summary, the image encryption method of the present invention has the advantages of being difficult to crack and having a high compression ratio, saving occupied space and memory, and ensuring good privacy protection during image transmission. BRIEF DESCRIPTION OF THE DRAWINGS
[0078] Figure 1 It is a flowchart of the encryption method according to an embodiment of the present invention.
[0079] Figure 2 It is a flowchart of a decryption method according to an embodiment of the present invention. DETAILED DESCRIPTION
[0080] The present invention is further described below in conjunction with the accompanying drawings and embodiments.
[0081] Embodiment 1
[0082] like Figure 1As shown, this embodiment provides a method for encrypting a picture based on a lossless compression algorithm, comprising the following steps:
[0083] Step 1: Get the original image data img and preprocess the original image data. The process is as follows:
[0084] Traverse each channel in the image data in turn. For each channel, traverse all columns in turn first, and start from the second column to convert the value of each column into the difference between the pixel values of the column and the previous column. Then traverse all rows in turn, and start from the second row to convert the value of each row into the difference between the pixel values of the row and the previous row, thereby obtaining the preprocessed image data img_new.
[0085] Step 2: compress the image data img_new preprocessed in step 1. The process is as follows:
[0086] Step 2.1. Construct the int_bin function based on Python language. Based on the rules of the int_bin function, get the dictionaries dict1, dict2, dict3, dict4, dict5, dict6, dict7, dict8, dict9, represented by dictm, where m ranges from integers 1 to 9. The key of the dictionary dictm is the integer n, which ranges from 0 to 2 to the power of m minus 1. The value of the dictionary is the m-bit binary string converted from the dictionary key, that is, the integer n. If the actual length of the converted binary data, length1, is less than m, add m-length1 bits of character 0 in front of the converted binary data.
[0087] Step 2.2, set up blank lists list1, list2, list1_end, list2_end, loop through all channels of the preprocessed image data img_new, use k to represent the corresponding channel number, then loop through all rows of each channel, use i to represent the corresponding row number, then loop through all columns of each channel, use j to represent the corresponding column number, and add the above loop traversal results to the blank list according to the following rules:
[0088] If the traversed integer is equal to 0, first determine whether it is the last column. If it is the last column, add the string element 000 to the list list1. If it is not the last column, calculate the total number of consecutive characters 0, and limit the maximum value A of the consecutive number of times count of character 0. A is an integer greater than 6. Based on the consecutive number count, make judgments and add strings: when the consecutive number count is equal to 1, add the string 000 to the list list1; when the consecutive number count ranges from 2 to 5, add the string 001 to the list list1, and add the string dict2[ count-2], the elements in [] are the keys of the dictionary and are used to get the values of the dictionary; when the number of consecutive times count ranges from 6 to A, the string 010 is added to the list list1, and the string dict6[count-6] is added to the list list2, and [] is the value of the dictionary dict6 with the key count-6; when the number of consecutive times count is greater than A, let count equal to A, then add the string 010 to the list list1, and add the string dict6[A-6] to the list list2, and finally let j=j+count-1, because count times have been traversed.
[0089] If the traversed integer num is equal to 1, then add the string 011 to the list list1.
[0090] If the traversed integer num is equal to -1, then add the string 100 to the list list1.
[0091] If the traversed integer num is equal to 2, then add the string 101 to the list list1.
[0092] If the traversed integer num is equal to -2, then add the string 110 to the list list1.
[0093] If the traversed integer num is equal to 3 or 4, then add the string 111 to the list list1 and add the string 00+dict1[num-3] to the list list2.
[0094] If the traversed integer num ranges from 5 to 8, then add the string 111 to list1 and add the string 100+dict2[num-5] to list2.
[0095] If the traversed integer range is from 9 to 10, add the string 111 to list1 and add the string 101+dict9[num] to list2.
[0096] If the traversed integer num is equal to -3 or -4, then add the string 111 to the list list1 and add the string 01+dict1[-num-3] to the list list2.
[0097] If the traversed integer num ranges from -8 to -5, then add the string 111 to list1 and add the string 110+dict2[-num-5] to list2.
[0098] If the traversed integer num ranges from -10 to -9, then add the string 111 to list1 and add the string 111+dict9[-num] to list2.
[0099] After completing the above process, set j=j+1 to increase the column number by 1, because the value corresponding to the column number has been traversed, and determine the length of lists list1 and list2 at this time. If the length of list list1 is greater than 50, add element list1 to list list1_end, and then set list list1 to an empty list, i.e. [], to facilitate subsequent storage of new string elements. If the length of list list2 is greater than 50, add element list2 to list list2_end, and then set list list2 to an empty list.
[0100] Finally, set empty strings str1 and str2, and concatenate the string elements stored in all elements of the list list1_end (referring to list1 in the previous step) into str1 in sequence, and concatenate the string elements stored in all elements of the list list2_end into str2 in sequence.
[0101] Step 3: Encrypt the data compressed in step 2. The process is as follows:
[0102] Step 3.1, set a 254-bit binary key;
[0103] Step 3.2: For str1 and str2 obtained in step 2, the key is used to encrypt them respectively to obtain encrypted character strings str1_end and str2_end.
[0104] Step 3.3, represent the length of the encrypted string str1_end with a 30-bit binary string str3, represent the width of the image with a 13-bit binary string str4, represent the height of the image with a 13-bit binary string str5, and perform the following operations: first add str1_end to the empty string str_end, and then add str3, str4, and str5 to the front of the string str_end in sequence.
[0105] After completing the above operations, calculate the length of the string str_end, that is, the integer num_end, divide num_end by 24 and round down to get length, set the encrypted image width width=length, the encrypted image height heigth=length, enter the while loop to determine whether width*heigth*24 is greater than or equal to num_end, if not greater than or equal to num_end, let width increase by 1 and continue to loop and execute until width*heigth*24 is greater than or equal to num_end, calculate width*height*24-num to get the string 0 that needs to be added in front of the string str_end, and fill the string str_end based on the calculation result.
[0106] Step 3.4: Store the padded string str_end obtained in step 3.3 into the array img_end of size (height, width, 3) through three layers of for loops.
[0107] Step 3.5, write the img_end data into the picture to get the compressed and encrypted picture.
[0108] In step 2.1 of this embodiment, the rule of the int_bin function is: for an integer num converted to binary data of length num1, if the actual length num2 of the converted binary data is less than num1, the characters 0 of num1-num2 positions are added in front of the converted binary data.
[0109] In step 2.2 of this embodiment, a for loop is used to loop through all channels of the preprocessed image data img_new, and then a for loop is used to loop through all rows of each channel, and finally a while loop is used to loop through all columns of each channel.
[0110] In step 3.1 of this embodiment, the 254-binary key is represented by a 33-bit unordered integer from 0 to 255.
[0111] The encryption process in step 3.2 of this embodiment is: perform an XOR operation on str1 and the key at the corresponding position, add the result to the empty string str1_end, move the key to the next position after the XOR operation is completed, and continue the XOR operation until all XOR operations on str1 are completed to obtain the encrypted string str1_end.
[0112] Perform an XOR operation on the corresponding position of str2 and the key, add the result to the empty string str2_end, move the key to the next position and continue XORing until all XOR operations on str2 are completed, and get the encrypted string str2_end.
[0113] In step 3.4 of this embodiment, when the array img_end is subjected to three-layer for loop, the first layer of loop is for traversing all rows of each channel, that is, for i in range(height), the second layer of loop is for traversing all columns of each channel, that is, for j in range(width), and the third layer of loop is for traversing the channels, that is, for k in range(3). The loop body content is to truncate the string str_end once every 8 bits and convert it into an integer and store it in the i-th row, j-th column, and k-th channel of img_after, that is, img_after[i, j, k] = int(str_end[(width * height * k + i *width + j) * 8:(width * height * k + i * width + j + 1) * 8], 2).
[0114] The lossless compression ratio of the compressed and encrypted image obtained in step 3.4 of this embodiment is (img.shape[0]*img.shape[1]) / (img_after.shape[0]*img_after.shape[1]), where img is the original image data, img.shape[0] is the height of the original image, img.shape[1] is the width of the original image, img_after.shape[0] is the height of the compressed and encrypted image, and img_after.shape[1] is the width of the compressed and encrypted image.
[0115] Embodiment 2
[0116] This embodiment discloses a method for decrypting a picture processed by the encryption method of the first embodiment. Figure 2 As shown, the following steps are included:
[0117] Step S1: decrypt the encrypted and losslessly compressed image. The process is as follows:
[0118] Step S11, store the encrypted image in img, convert the integers stored in img into 8-bit binary strings in the order of the 0th channel to the 2nd channel, the 0th row to the last row, and the first column to the last column, and add them to the end of the empty string str_end in sequence.
[0119] Step S12, set the index k1 of the string str_end=0, and determine whether str_end[k1] is the string 0 through a while loop. If it is equal to the string 0, k1 is incremented by 1 and the loop continues. If it is not equal to the string 0, the loop is exited, and k1 is incremented by 1 again. The loop is no longer continued and the next step is executed.
[0120] Step S13, let the height be equal to the integer converted from the binary string truncated from k1 to k1+12 with the index of the string str_end, that is, height=int(str_end[k1:k1+13],2), and let k1 increase by 13, that is, k1=k1+13; then let the width be equal to the integer converted from the binary string truncated from k1 to k1+12 with the index of the string str_end, that is, width=int(str_end[k1:k1+13],2), and let k1 increase by 13, that is, k1=k1+30; then let sum1 be equal to the integer converted from the binary string truncated from k1 to k1+30 with the index of the string str_end, that is, sum1 = int(str_end[k1:k1 +30], 2), and let k1 increase by 30, that is, k1=k1+30; then let str1_end equal to the binary character truncated from the index of string str_end from k1 to k1+id_sum, that is, str1_end=str_end[k1:k1+id_sum], and let k1 increase by sum1, that is, k1=k1+sum1; finally, let str2_end equal to the binary character truncated from the index of string str_end from k1 to k1, that is, str2_end=str_end[k1:-1], thus obtaining str1_end and str2_end.
[0121] Step S14, for str1_end and str2_end, perform XOR operation on str1_end and the corresponding position of the key, add the result to the empty string, and after XOR, move the key to the next position and continue XOR until all str1_end are XORed, thereby obtaining the string str1.
[0122] Perform an XOR operation on str2_end and the corresponding position of the key, add the result to the empty string, move the key to the next position and continue XORing until all str2_end are XORed, thus obtaining the string str2.
[0123] Step S2: decompress the image data decrypted in step S1. The process is as follows:
[0124] Create an ndarray array img1 without any specific value, whose size is (height, width, 3), that is, img1 = np.empty((height, width, 3)), let i = 0, j = 0, k = 0, k2 = 0, where i, j, k are the size of the array img1, and k2 is the index of the string str1. Let img1 enter the while loop, and the termination condition of the loop is k greater than or equal to 3.
[0125] Let str3 be equal to the binary string truncated from k2*3 to (k2+1)*3 of the string str1, that is, str3=str1[k2*3:(k2+1)*3], and then let k2 increment by 1; if str3 is not equal to the string 111, continue to judge, if str3 is equal to the string 000, let the value of the i-th row, j-th column, and k-th channel of the array img1 be equal to 0, that is, img1[i,j,k]=0, if str3 is equal to the string 001, let count = int(str2[k2:k2+ 2], 2)+2, and let k1 increment by 2.
[0126] Next, the for loop is executed count times. The loop body sets the value of the i-th row, j-th column, and k-th channel of the array img1 to 0, that is, img1[i,j,k]=0,j=j+1. During the loop, if j is equal to width, set j=0,i=i+1. If i is equal to height, set k=k+1,i=0,j=0. If k is equal to 3, exit the loop. Finally, set j=j-1. This is because j is added once more during the loop.
[0127] If str3 is equal to the string 001, let count be equal to the integer converted from the binary string truncated from the index k2 to k2+2 of str2 plus 2, that is, count = int(str2[k2:k2 + 2], 2) + 2, let k1=k1+2; next, for loop count times, the loop body content is the value of the i-th row, j-th column, and k-th channel of the array img1 equal to 0, that is, img1[i,j,k]=0,j=j+1; during the loop, if j is equal to width, let j=0,i=i+1, if i is equal to height, let k=k+1,i=0,j=0, if k is equal to 3, then jump out of the loop; finally, let j=j-1, because j is added once more at the end of the loop.
[0128] If str3 is equal to the string 010, let count be equal to the integer converted from the binary string truncated from str2 with index k2 to k2+6 plus 6, that is, count = int(str2[k2:k2 + 6], 2) + 6, let k1=k1+6; next, for loop count times, the loop body content is the value of the i-th row, j-th column, and k-th channel of the array img1 equal to 0, that is, img1[i,j,k]=0,j=j+1; during the loop, if j is equal to width, let j=0,i=i+1, if i is equal to height, let k=k+1,i=0,j=0, if k is equal to 3, then jump out of the loop; finally, let j=j-1, because j is added once more at the end of the loop.
[0129] If str3 is equal to the string 000, then the value of the i-th row, j-th column, and k-th channel of the array img1 is set to 0, that is, img1[i,j,k]=0.
[0130] If str3 is equal to the string 011, then the value of the i-th row, j-th column, and k-th channel of the array img1 is equal to 1, that is, img1[i,j,k]=1.
[0131] If str3 is equal to the string 100, then the value of the i-th row, j-th column, and k-th channel of the array img1 is equal to -1, that is, img1[i,j,k]=-1.
[0132] If str3 is equal to the string 101, then let the value of the i-th row, j-th column, and k-th channel of the array img1 be equal to 2, that is, img1[i,j,k]=2.
[0133] If str3 is equal to the string 110, then let the value of the i-th row, j-th column, and k-th channel of the array img1 be equal to -2, that is, img1[i,j,k]=-2.
[0134] If str3 is equal to the string 111, then enter the while infinite loop, the loop body content is as follows:
[0135] If the binary string truncated from index k2 to k2+2 of str2, that is, str2[k2:k2 + 2] is equal to the string 00, then let k2=k2+2, and let the value of the i-th row, j-th column, and k-th channel of the array img1 be equal to the integer converted from the binary string truncated from index k2 to k2+1 of the string str2 plus 3, that is, img1[i, j, k] = int(str2[k2:k2 + 1], 2) + 3, then let k2=k2+1, and finally jump out of the loop through break.
[0136] If the binary string truncated from index k2 to k2+3 of str2, that is, str2[k2:k2 + 3] is equal to the string 100, then let k2=k2+3, and let the value of the i-th row, j-th column, and k-th channel of the array img1 be equal to the integer converted from the binary string truncated from index k2 to k2+2 of the string str2 plus 5, that is, img1[i, j, k] =int(str2[k2:k2+ 2], 2) + 5, k2=k2+2, and finally break to exit the loop.
[0137] If the binary string truncated from index k2 to k2+3 of str2, that is, str2[k2:k2 + 3] is equal to the string 101, then let k2=k2+3, and let the value of the i-th row, j-th column, and k-th channel of the array img1 be equal to the integer converted from the binary string truncated from index k2 to k2+2 of the string str2 plus 5, that is, img1[i, j, k] =int(str2[k2:k2+ 2], 2) + 5, k2=k2+2, and finally exit the loop through break.
[0138] If the binary string truncated from index k2 to k2+2 of str2, that is, str2[k2:k2 + 2] is equal to the string 01, then let k2=k2+2, and let the value of the i-th row, j-th column, and k-th channel of the array img1 be equal to the negative of the integer converted from the binary string truncated from index k2 to k2+1 of the string str2 minus 3, that is, img1[i, j, k] =-int(str2[k2:k2 + 1], 2) -3, then let k2=k2+1, and finally exit the loop through break.
[0139] If the binary string truncated from k2 to k2+2 of str2, that is, str2[k2:k2 + 3] is equal to the string 110, then let k2=k2+3, and let the value of the i-th row, j-th column, and k-th channel of the array img1 be equal to the negative of the integer converted from the binary string truncated from k2 to k2+2 of the string str2, minus 5, that is, img1[i, j, k] =-int(str2[k2:k2 + 2], 2) -5, then let k2=k2+1, and finally exit the loop through break.
[0140] If the binary string truncated from index k2 to k2+3 of str2, that is, str2[k2:k2 + 3] is equal to the string 111, then let k2=k2+3, and let the value of the i-th row, j-th column, and k-th channel of the array img1 be equal to the negative of the integer converted from the binary string truncated from index k2 to k2+9 of the string str2, that is, img1[i, j, k] =-int(str2[k2:k2+ 9], 2), then let k2=k2+9, and finally exit the loop through break.
[0141] After completing the above process, let j add 1, because the value of the current column has been traversed. If j is equal to width, let i=i+1, j=0; if i is equal to height, let k=k+1, i=0, j=0; the img1 obtained in this way is the decompressed data.
[0142] Step S3: Process the data decompressed in step S2. The process is as follows:
[0143] Step S3.1, let img_0 be equal to the data of the 0th channel in img1, that is, img_0=img1[:,:,0], let img_1 be equal to img_0 plus the data of the 1st channel in img1, that is, img_1=img_0+img1[:,:,1], let img_2 be equal to img_1 plus the data of the 2nd channel in img1, that is, img_2=img_1+img1[:,:,2], img_0_copy is the data after deep copy of img_0, that is, img_0_copy=copy.deepcopy(img_0), let img_1_copy be the data after deep copy of img_1, that is, img_1_copy=copy.deepcopy(img_1), let img_2_copy be the data after deep copy of img_2, that is, img_2_copy=copy.deepcopy(img_2).
[0144] Step S3.2, let shape be the size of array img1, shape=img1.shape, let img2 be the data after deep copy of img1, that is, img2=copy.deepcopy(img1).
[0145] Through the for loop, i is traversed from 1 to shape[0]. The loop body content is that all the data in the i-th row of img_2_copy is equal to all the data in the i-th row of img_2 plus all the data in the i-1-th row of img_2_copy, that is, img_2_copy[i,:]=img_2[i,:]+img_2_copy[i-1,:].
[0146] After the for loop, let all the data of the second channel of img2 be the data after the deep copy of img_2_copy, that is, img2[:,:,2]=copy.deepcopy(img_2_copy), and then traverse from 1 to shape[1] for j through the for loop. The content of the loop body is the jth column of img2. All the data of the second channel is equal to all the data of the jth column of img_2_copy plus all the data of the second channel of the j-1th column of img2, that is, img2[:,j,2]=img_2_copy[:,j]+img2[:,j-1,2].
[0147] Step S3.3, then traverse from 1 to shape[0] for i through a for loop, and the loop body content is that all the data in the i-th row of img_1_copy is equal to all the data in the i-th row of img_1 plus all the data in the i-1-th row of img_1_copy, that is, img_1_copy[i,:]=img_1[i,:]+img_1_copy[i-1,:].
[0148] After the for loop, all the data of the first channel of img2 are the data after the deep copy of img_1_copy, that is, img2[:,:,1]=copy.deepcopy(img_1_copy), and then the for loop is used to traverse from 1 to shape[1] for j. The content of the loop body is the jth column of img2. All the data of the first channel is equal to all the data of the jth column of img_1_copy plus all the data of the second channel of the j-1th column of img2, that is, img2[:,j,1]=img_1_copy[:,j]+img2[:,j-1,2].
[0149] Step S3.4, then traverse from 1 to shape[0] for i through a for loop, and the loop body content is that all the data in the i-th row of img_0_copy is equal to all the data in the i-th row of img_0 plus all the data in the i-1-th row of img_0_copy, that is, img_0_copy[i,:]=img_0[i,:]+img_0_copy[i-1,:].
[0150] After the for loop, let all the data of the 0th channel of img2 be the data after the deep copy of img_0_copy, that is, img2[:,:,0]=copy.deepcopy(img_0_copy), and then traverse from 1 to shape[1] for j through the for loop. The content of the loop body is the jth column of img2. All the data of the 0th channel is equal to all the data of the jth column of img_0_copy plus all the data of the j-1th column and 0th channel of img2, that is, img2[:,j,0]=img_0_copy[:,j]+img2[:,j-1,0].
[0151] Step S3.5, write the img2 data into the image to obtain the original image.
[0152] Embodiment 3
[0153] This embodiment discloses an electronic device that can implement the encryption method of embodiment 1 and the decryption method of embodiment 2. The electronic device includes a processor and a memory, and the storage medium of the memory stores encryption program instructions and decryption program instructions that can be read and executed by the processor. When the processor runs the encryption program instructions, the steps of the encryption method described in embodiment 1 are executed. When the processor runs the decryption program instructions, the steps of the decryption method described in embodiment 2 are executed.
[0154] The embodiments described in the present invention are merely descriptions of the preferred implementation modes of the present invention, and are not intended to limit the concept and scope of the present invention. Without departing from the design concept of the present invention, various modifications and improvements made to the technical solutions of the present invention by engineers and technicians in this field should fall within the protection scope of the present invention. The technical contents for which protection is sought in the present invention have all been recorded in the claims.
Claims
1. A picture encryption method based on a lossless compression algorithm, characterized in that: The following steps are involved: Step 1: Get the original image data img and preprocess the original image data. The process is as follows: Traverse each channel in the image data in sequence. For each channel, traverse all columns in sequence first, and start from the second column to convert the value of each column into the difference between the pixel values of the column and the previous column. Then traverse all rows in sequence, and start from the second row to convert the value of each row into the difference between the pixel values of the row and the previous row, thereby obtaining the preprocessed image data img_new; Step 2: compress the image data img_new preprocessed in step 1. The process is as follows: Step 2.1, build the int_bin function based on Python language, and get the dictionary represented by dictm based on the rules of int_bin function, where m ranges from integers 1 to 9, thereby obtaining dictionaries dict1, dict2, dict3, dict4, dict5, dict6, dict7, dict8, dict9, the key of dictionary dictm is integer n, n ranges from 0 to 2 to the power of m minus 1, and the value of the dictionary is the m-bit binary string converted from the key of the dictionary, that is, the integer n. If the actual length length1 of the converted binary data is less than m, then add m-length1 bits of character 0 in front of the converted binary data; Step 2.2, set up blank lists list1, list2, list1_end, list2_end, loop through all channels of the preprocessed image data img_new, use k to represent the channel number currently being processed, then loop through all rows of each channel, use i to represent the corresponding row number, then loop through all columns of each channel, use j to represent the corresponding column number, and add the above loop traversal results to the blank list according to the following rules: If the traversed integer is equal to 0, first determine whether it is the last column. If it is the last column, add the string element 000 to the list list1. If it is not the last column, calculate the total number of consecutive characters 0, and limit the maximum value A of the consecutive number of times count of character 0, A is an integer greater than 6, and judge and add strings based on the consecutive number count: when the consecutive number count is equal to 1, add the string 000 to the list list1; when the consecutive number count ranges from 2 to 5, add the string 001 to the list list1, and add the string dict2[count-2] to the list list2, where d ict2 is the dictionary obtained in step 2.1, count-2 is used as the key of dict2 to get the corresponding value; when the number of consecutive times count ranges from 6 to A, add the string 010 to list1, and add the string dict6[count-6] to list2, [] is the value of the dictionary dict6 with the key count-6; when the number of consecutive times count is greater than A, let count equal to A, then add the string 010 to list1, and add the string dict6[A-6] to list2, and finally let j=j+count-1, thus count times have been traversed; If the traversed integer num is equal to 1, then add the string 011 to the list list1; If the traversed integer num is equal to -1, then add the string 100 to the list list1; If the traversed integer num is equal to 2, then add the string 101 to the list list1; If the traversed integer num is equal to -2, then add the string 110 to the list list1; If the traversed integer num is equal to 3 or 4, add the string 111 to list1 and add the string 00+dict1[num-3] to list2; If the traversed integer num ranges from 5 to 8, add the string 111 to list1 and add the string 100+dict2[num-5] to list2; If the traversed integer range is from 9 to 10, add the string 111 to list1 and add the string 101+dict9[num] to list2; If the traversed integer num is equal to -3 or -4, then add the string 111 to list1 and add the string 01+dict1[-num-3] to list2; If the traversed integer num ranges from -8 to -5, then add the string 111 to list1 and add the string 110+dict2[-num-5] to list2; If the traversed integer num ranges from -10 to -9, then add the string 111 to list1, and add the string 111+dict9[-num] to list2; After completing the above process, set j=j+1, so that the column number increases by 1, the value corresponding to the column number has been traversed, and the length of the lists list1 and list2 is determined at this time. If the length of the list list1 is greater than 50, add the element list1 to the list list1_end, and then set the list list1 to an empty list, that is, [], to facilitate the subsequent storage of new string elements. If the length of the list list2 is greater than 50, add the element list2 to the list list2_end, and then set the list list2 to an empty list; Finally, set empty strings str1 and str2, and concatenate all string elements stored in list list1_end into str1, and concatenate all string elements stored in list list2_end into str2. Step 3: Encrypt the data compressed in step 2. The process is as follows: Step 3.1, set a 254-bit binary key; Step 3.2: For str1 and str2 obtained in step 2, the encrypted strings str1_end and str2_end are obtained respectively using the key; Step 3.3, the length of the encrypted string str1_end is represented by a 30-bit binary string str3, the width of the image is represented by a 13-bit binary string str4, and the height of the image is represented by a 13-bit binary string str5, and the following operations are performed: first, str1_end is added to the empty string str_end, and then str3, str4, and str5 are added to the front of the string str_end in sequence; After completing the above operations, calculate the length of the string str_end, that is, the integer num_end, divide num_end by 24 and round down to get length, set the encrypted image width width=length, the encrypted image height heigth=length, enter the while loop to determine whether width*heigth*24 is greater than or equal to num_end, if not greater than or equal to num_end, let width increase by 1 and continue to loop and execute until width*heigth*24 is greater than or equal to num_end, calculate width*height*24-num to get the string 0 that needs to be added to the front of the string str_end, and fill the string str_end based on the calculation result; Step 3.4, store the padded string str_end obtained in step 3.3 into the array img_end of size height, width, 3 through three layers of for loops; Step 3.5, write the img_end data into the picture to get the compressed and encrypted picture.
2. According to claim 1, a method for encrypting an image based on a lossless compression algorithm is characterized in that: In step 2.1, the rule of the int_bin function is: for an integer num converted to binary data of length num1, if the actual length of the converted binary data num2 is less than num1, then the characters 0 of num1-num2 positions are added to the front of the converted binary data.
3. The image encryption method based on a lossless compression algorithm according to claim 1, characterized in that: In step 2.2, a for loop is used to loop through all channels of the preprocessed image data img_new, then a for loop is used to loop through all rows of each channel, and finally a while loop is used to loop through all columns of each channel.
4. The image encryption method based on a lossless compression algorithm according to claim 1, characterized in that: In step 3.1, the 254-binary key is represented by a 33-bit unordered integer from 0 to 255.
5. The image encryption method based on a lossless compression algorithm according to claim 1, characterized in that: The encryption process in step 3.2 is: perform XOR operation on the corresponding position of str1 and the key, add the result to the empty string str1_end, move the key to the next position and continue XOR until all XOR operations on str1 are completed, and obtain the encrypted string str1_end; Perform an XOR operation on the corresponding position of str2 and the key, add the result to the empty string str2_end, move the key to the next position and continue XORing until all XOR operations on str2 are completed, and get the encrypted string str2_end.
6. The image encryption method based on a lossless compression algorithm according to claim 1, characterized in that: In step 3.4, when three layers of for loops are performed on the array img_end, the first layer of loops is to traverse all the rows of each channel, the second layer is to traverse all the columns of each channel, and the third layer of loops is to traverse the channels. The loop body content is to truncate the string str_end every 8 bits and convert it into an integer stored in the i-th row, j-th column, and k-th channel of img_after.
7. The image encryption method based on a lossless compression algorithm according to claim 5, characterized in that: The lossless compression ratio of the compressed and encrypted image obtained in step 3.4 is (img.shape[0]*img.shape[1]) / (img_after.shape[0]*img_after.shape[1]), where img is the original image data, img.shape[0] is the height of the original image, img.shape[1] is the width of the original image, img_after.shape[0] is the height of the compressed and encrypted image, and img_after.shape[1] is the width of the compressed and encrypted image.
8. A method for encrypting an image based on a lossless compression algorithm according to any one of claims 1 to 7, characterized in that: The following steps are involved: Step S1: decrypt the encrypted image. The process is as follows: Step S11, store the encrypted image in img, convert the integers stored in img into 8-bit binary strings in the order of the 0th channel to the 2nd channel, the 0th row to the last row, and the first column to the last column, and add them to the end of the empty string str_end in sequence; Step S12, set the index k1 of the string str_end=0, and determine whether str_end[k1] is the string 0 through a while loop. If it is equal to the string 0, k1 is incremented by 1 and the loop continues. If it is not equal to the string 0, the loop is exited, and k1 is incremented by 1 again, and the loop is not continued, and the next step is executed; Step S13, let the height be equal to the integer converted from the binary string truncated from k1 to k1+12 with the index of the string str_end, that is, height=int(str_end[k1:k1+13],2), and let k1 increase by 13, that is, k1=k1+13; then let the width be equal to the integer converted from the binary string truncated from k1 to k1+12 with the index of the string str_end, that is, width=int(str_end[k1:k1+13],2), and let k1 increase by 13, that is, k1=k1+30; then let sum1 be equal to the integer converted from the binary string truncated from k1 to k1+30 with the index of the string str_end, that is, sum1 = int(str_end[k1:k1 + 30],2), and let k1 increase by 30, that is, k1=k1+30; then let str1_end equal to the binary character truncated from the index of string str_end from k1 to k1+id_sum, that is, str1_end=str_end[k1:k1+id_sum], and let k1 increase by sum1, that is, k1=k1+sum1; finally, let str2_end equal to the binary character truncated from the index of string str_end from k1 to k1, that is, str2_end=str_end[k1:-1], thus obtaining str1_end and str2_end; Step S14, for str1_end and str2_end, perform an XOR operation on str1_end and the corresponding position of the key, add the result to the empty string, and after the XOR is completed, move the key to the next position and continue the XOR until all str1_end are XORed, thereby obtaining the string str1; Perform XOR operation on str2_end and the corresponding position of the key, add the result to the empty string, move the key to the next position and continue XOR until all str2_end are XORed, thus obtaining the string str2; Step S2: decompress the image data decrypted in step S1. The process is as follows: Create an ndarray array img1 without any specific values, whose dimensions are height, width, and 3, that is, img1=np.empty((height,width,3)), let i=0, j=0, k=0, k2=0, where i, j, k are the dimensions of the array img1, k2 is the index of the string str1, let img1 enter the while loop, and the termination condition of the loop is k greater than or equal to 3; Let str3 be equal to the binary string truncated from k2*3 to (k2+1)*3 of the string str1, that is, str3=str1[k2*3:(k2+1)*3], and then let k2 be incremented by 1; if str3 is not equal to the string 111, continue to judge, if str3 is equal to the string 000, let the value of the i-th row, j-th column, and k-th channel of the array img1 be equal to 0, that is, img1[i,j,k]=0; if str3 is equal to the string 001, let count = int(str2[k2:k2+ 2], 2)+2, and let k1 be incremented by 2; Next, for loop count times, the loop body content is to set the value of the i-th row, j-th column, and k-th channel of the array img1 to 0, that is, img1[i,j,k]=0,j=j+1; during the loop, if j is equal to width, then set j=0,i=i+1, if i is equal to height, then set k=k+1,i=0,j=0, if k is equal to 3, then jump out of the loop, and finally set j=j-1, because j is added once more in the last loop process; If str3 is equal to the string 001, let count be equal to the integer converted from the binary string truncated from the index k2 to k2+2 of str2 plus 2, that is, count = int(str2[k2:k2 + 2], 2) + 2, let k1=k1+2; then for loop count times, the loop body content is the value of the i-th row, j-th column, and k-th channel of the array img1 is equal to 0, that is, img1[i,j,k]=0,j=j+1; During the loop, if j is equal to width, set j=0, i=i+1; if i is equal to height, set k=k+1, i=0, j=0; if k is equal to 3, exit the loop; finally, set j=j-1, because j is added once more at the end of the loop; If str3 is equal to the string 010, let count be equal to the integer converted from the binary string of str2 truncated from index k2 to k2+6 plus 6, that is, count = int(str2[k2:k2 + 6], 2) + 6, let k1=k1+6; then for loop count times, the loop body content is the value of the i-th row, j-th column, and k-th channel of the array img1 is equal to 0, that is, img1[i,j,k]=0,j=j+1; During the loop, if j is equal to width, set j=0, i=i+1; if i is equal to height, set k=k+1, i=0, j=0; if k is equal to 3, exit the loop; finally, set j=j-1, because j is added once more at the end of the loop; If str3 is equal to the string 000, then let the value of the i-th row, j-th column, and k-th channel of the array img1 be equal to 0, that is, img1[i,j,k]=0; If str3 is equal to the string 011, then let the value of the i-th row, j-th column, and k-th channel of the array img1 be equal to 1, that is, img1[i,j,k]=1; If str3 is equal to the string 100, then let the value of the i-th row, j-th column, and k-th channel of the array img1 be equal to -1, that is, img1[i,j,k]=-1; If str3 is equal to the string 101, then let the value of the i-th row, j-th column, and k-th channel of the array img1 be equal to 2, that is, img1[i,j,k]=2; If str3 is equal to the string 110, then let the value of the i-th row, j-th column, and k-th channel of the array img1 be equal to -2, that is, img1[i,j,k]=-2; If str3 is equal to the string 111, then enter the while infinite loop, the loop body content is as follows: If the binary string truncated from k2 to k2+2 of str2, that is, str2[k2:k2 + 2], is equal to the string 00, then let k2=k2+2, let the value of the i-th row, j-th column, and k-th channel of the array img1 be equal to the integer converted from the binary string truncated from k2 to k2+1 of the string str2, plus 3, that is, img1[i, j, k] = int(str2[k2:k2 +1], 2) + 3, then let k2=k2+1, and finally jump out of the loop through break; If the binary string truncated from k2 to k2+3 of str2, that is, str2[k2:k2 + 3] is equal to the string 100, then let k2=k2+3, let the value of the i-th row, j-th column, and k-th channel of the array img1 be equal to the integer converted from the binary string truncated from k2 to k2+2 of the string str2, plus 5, that is, img1[i, j, k] =int(str2[k2:k2 +2], 2) + 5, k2=k2+2, and finally break to jump out of the loop; If the binary string truncated from k2 to k2+3 of str2, that is, str2[k2:k2 + 3] is equal to the string 101, then let k2=k2+3, let the value of the i-th row, j-th column, and k-th channel of the array img1 be equal to the integer converted from the binary string truncated from k2 to k2+2 of the string str2, plus 5, that is, img1[i, j, k] =int(str2[k2:k2 +2], 2) + 5, k2=k2+2, and finally jump out of the loop through break; If the binary string truncated from k2 to k2+2 of str2, that is, str2[k2:k2 + 2], is equal to the string 01, then let k2=k2+2, let the value of the i-th row, j-th column, and k-th channel of the array img1 be equal to the negative of the integer converted from the binary string truncated from k2 to k2+1 of the string str2, minus 3, that is, img1[i, j, k] =-int(str2[k2:k2 + 1], 2) -3, then let k2=k2+1, and finally jump out of the loop through break; If the binary string truncated from k2 to k2+2 of str2, that is, str2[k2:k2 + 3], is equal to the string 110, then let k2=k2+3, let the value of the i-th row, j-th column, and k-th channel of the array img1 be equal to the negative of the integer converted from the binary string truncated from k2 to k2+2 of the string str2, minus 5, that is, img1[i, j, k] =-int(str2[k2:k2 + 2], 2) -5, then let k2=k2+1, and finally jump out of the loop through break; If the binary string of str2 truncated from index k2 to k2+3, that is, str2[k2:k2 + 3] is equal to the string 111, then let k2=k2+3, let the value of the i-th row, j-th column, and k-th channel of the array img1 be equal to the negative integer converted from the binary string of str2 truncated from index k2 to k2+9, that is, img1[i, j, k] =-int(str2[k2:k2 +9], 2), then let k2=k2+9, and finally jump out of the loop through break; After completing the above process, let j add 1, because the value of the current column has been traversed. If j is equal to width, let i=i+1, j=0; if i is equal to height, let k=k+1, i=0, j=0; the img1 obtained is the decompressed data; Step S3: Process the data decompressed in step S2. The process is as follows: Step S3.1, let img_0 be equal to the data of the 0th channel in img1, that is, img_0=img1[:,:,0], let img_1 be equal to img_0 plus the data of the 1st channel in img1, that is, img_1=img_0+img1[:,:,1], let img_2 be equal to img_1 plus the data of the 2nd channel in img1, that is, img_2=img_1+img1[:,:,2], img_0_copy is the data after deep copy of img_0, that is, img_0_copy=copy.deepcopy(img_0), let img_1_copy be the data after deep copy of img_1, that is, img_1_copy=copy.deepcopy(img_1), let img_2_copy be the data after deep copy of img_2, that is, img_2_copy=copy.deepcopy(img_2); Step S3.2, let shape be the size of array img1, shape=img1.shape, let img2 be the data after deep copy of img1, that is, img2=copy.deepcopy(img1); Through the for loop, traverse from 1 to shape[0] for i, and the loop body content is that all the data in the i-th row of img_2_copy is equal to all the data in the i-th row of img_2 plus all the data in the i-1-th row of img_2_copy, that is, img_2_copy[i,:]=img_2[i,:]+img_2_copy[i-1,:]; After the for loop, let all the data of the second channel of img2 be the data after the deep copy of img_2_copy, that is, img2[:,:,2]=copy.deepcopy(img_2_copy), and then traverse from 1 to shape[1] for j through the for loop. The content of the loop body is that all the data of the jth column and the second channel of img2 is equal to all the data of the jth column of img_2_copy plus all the data of the second channel of the j-1th column of img2, that is, img2[:,j,2]=img_2_copy[:,j]+img2[:,j-1,2]; Step S3.3, then traverse from 1 to shape[0] for i through a for loop, the loop body content is that all the data in the i-th row of img_1_copy is equal to all the data in the i-th row of img_1 plus all the data in the i-1-th row of img_1_copy, that is, img_1_copy[i,:]=img_1[i,:]+img_1_copy[i-1,:]; After the for loop, let all the data of the first channel of img2 be the data after the deep copy of img_1_copy, that is, img2[:,:,1]=copy.deepcopy(img_1_copy), and then traverse from 1 to shape[1] for j through the for loop. The content of the loop body is that all the data of the jth column and the first channel of img2 is equal to all the data of the jth column of img_1_copy plus all the data of the second channel of the j-1th column of img2, that is, img2[:,j,1]=img_1_copy[:,j]+img2[:,j-1,2]; Step S3.4, traverse from 1 to shape[0] through a for loop, and the loop body content is that all the data in the i-th row of img_0_copy is equal to all the data in the i-th row of img_0 plus all the data in the i-1-th row of img_0_copy, that is, img_0_copy[i,:]=img_0[i,:]+img_0_copy[i-1,:]; After the for loop, let all the data of the 0th channel of img2 be the data after the deep copy of img_0_copy, that is, img2[:,:,0]=copy.deepcopy(img_0_copy), and then traverse from 1 to shape[1] for j through the for loop. The content of the loop body is the jth column of img2. All the data of the 0th channel is equal to all the data of the jth column of img_0_copy plus all the data of the j-1th column and the 0th channel of img2, that is, img2[:,j,0]=img_0_copy[:,j]+img2[:,j-1,0]; Step S3.5, write the img2 data into the image to obtain the original image.
9. An electronic device, comprising a processor and a memory, wherein the memory stores program instructions, characterized in that: When the processor runs the program instructions in the memory, the steps of the encryption and lossless compression method described in any one of claims 1-7 are executed.
10. A readable storage medium, wherein program instructions are stored in the storage medium, characterized in that: When the program instructions are read, the steps of the encryption and lossless compression method described in any one of claims 1 to 7 are executed.
Citation Information
Patent Citations
Private image encryption method and device, electronic equipment and computer readable storage medium
CN111260532A
Repetition coded compression for encrypting highly correlated data
WO2005078989A1