# The chi-square test for cryptography

I have a question about the chi-square test How can we use it in cryptography? And what results should we expect from it when a file is encrypted? I mean, when a file is encrypted, should its value increase?

• I did link to the list of questions about chi-squared hoping that you might be able to get something out of it. Please indicate what you've tried to find out and where you are stuck by hitting Edit under the question. Jan 18, 2021 at 11:42
• "A new test for randomness and its application to some cryptographic problems" by Ryabko, Stognienko and Shokin in Journal of Statistical Planning and Inference 123 (2004): boris.ryabko.net/jspi.pdf Jan 18, 2021 at 12:40
• The relevant article cited above was found on the Wikipedia page on X². Jan 18, 2021 at 12:41
• @ Maarten Bodewes I encrypted a file and now I want to use chi-square on file before encryption and after that and then compare them with each other to be sure, it is well encrypted Jan 18, 2021 at 14:37
• @A. Hersean Thanks for your help. Jan 18, 2021 at 14:46

$$\chi^2$$ measures the uniformity of a sample distribution. It's commonly used to test samples for randomness (I don't want any hassle on this), to do with true and pseudo random number generators. Uniformity is absolutely necessary for cryptography which you can read about elsewhere herein.

E.g.

$dd if=/dev/urandom of=/tmp/urandom bs=1000 count=1000$ent /tmp/urandom
Entropy = 7.999799 bits per byte.

Optimum compression would reduce the size
of this 1000000 byte file by 0 percent.

Chi square distribution for 1000000 samples is 278.05, and randomly
would exceed this value 15.37 percent of the times.

Arithmetic mean value of data bytes is 127.3541 (127.5 = random).
Monte Carlo value for Pi is 3.141468566 (error 0.00 percent).
Serial correlation coefficient is 0.000781 (totally uncorrelated = 0.0).


A mean value is about 255 for an eight bit window (byte) if a thing is randomesque, or properly encrypted. So yes, generally the thing's information entropy increases to ~8 bits/byte or ~1 bit/bit when encrypted.

• Thanks for your help. But I have a question. If I have a file and encrypt it, is the value of chi-square different before and after encryption? If the chi-square value for the encrypted file increases, can we say that the encryption is done well? Jan 18, 2021 at 14:42
• @ph9675 Humans (and me) tend to focus on information, rather than data. Do you understand? Well encrypted information is computationally indistinguishable from a random sequence. So even if your un-encrypted information is all zeros, once encrypted it will have an information entropy of 8 bits/byte. It will increase. So $\chi^2$ will be ~ 255 for an eight bit window. It will look like random gibberish. Jan 18, 2021 at 15:15
• @ph9675 But no. You can't say that encryption is done well just because the cipher text looks randomesque. I would consider it an alpha level test of the software. Nothing more. That's what this entire site is about... Jan 18, 2021 at 15:17
• Thank you so much for your help and guidance. Jan 18, 2021 at 15:25