Sequence test for an image cipher I am reading a paper on security analysis of a cipher image using the paper at the following URL:

https://ieeexplore.ieee.org/stamp/stamp.jsp?tp=&amp;arnumber=8306512&amp;tag=1

It indicates some test that a cipher image should pass for successful encryption. One of the simple test is to count the number of ones and number of zeros in the BINARY SEQUENCE of the image, and they should be equal, i.e 0.5 roughly of the total:



How is it possible to get a proper binary sequence representation of the image? Do I just convert each bit into its binary representation and form a binary sequence, do I have to do something else?
It's simple.  Lena is 512 x 512 pixels with a bit depth of 8.  So you simply export the raw image to a file that will be exactly 262,144 bytes long. It's critical that you export or save the image as a raw binary file, not one of those JPEGey things.  That won't work.

To determine the two chi values, you then consider the file as a series of consecutive individual bits, or as pairs of bits.  This will entail a load of bit shifting /masking operations in your favourite programming language.

A tip.  Install ent, and just run it twice against the encrypted Lena file.  Run one, use the -b option which will treat the file as a series of bits.  The output will produce a chi and p value.  Run again without the b option.  This will process the file as a series of 8 bit values.  If the encryption produces independent  (non correlated) bits, they can be lumped into four pair groups and treated as octets.  Assume the same 5% confidence value, and look at the returned chi value to see if it's acceptable. You'd expect  $219 \leq \chi^2 \leq 293$ for randomly distributed octets.

ent will also give you the Shannon entropy of the images for comparison with those in the paper.

Another tip.  Do not assume that a chi failure means the encryption fails.  Randomness is pesky and you have to try the test several times on repetitive images or keys.