07.07.2023 Views

Implementing-cryptography-using-python

Create successful ePaper yourself

Turn your PDF publications into a flip-book with our unique Google optimized e-Paper software.

Chapter 4 ■ Cryptographic Math and Frequency Analysis 115

Pseudorandomness

You will now examine pseudorandom number generation (PRNG) and why it

is insecure for use in cryptography. The goal of PRNG is to have a reproducible

sequence of random-feeling numbers. PRNG offers benefits when running

simulations that need to be consistent between each execution. Some examples

of simulation that benefit from the consistency of a PRNG generator include

testing stock market predictions, testing scientific experiments, rolling dice in

games, and generating symmetric-key encryption.

In the early days of computing, when applications needed to simulate nuclear

reactions but also needed to be reproducible in the case of an error in the program,

John von Neumann generated one of the first pseudorandom generators using

the middle-square method. The method generates a sequence of n-digit numbers

based on the digits in the middle of the number and then squares them. For

example, if you have a seed number of 682117, you square it to get 465283601689.

The middle numbers are 283601. Square this number and repeat. So while the

number appears random, you can reproduce the generated numbers as long as

you start with the same seed.

Take a look at how this can be done using Python:

n = int(input("Please enter a six-digit number: "))

for i in range(1,10):

n = int(str(n * n).zfill(12)[3:9])

print(n)

Please enter a six-digit number: 682117

283601

429527

493443

485994

190168

163868

852721

133103

716408

PRNG in encryption needs to have two properties that ensure its security.

When the properties exist, the PRNG is known as a cryptographically secure

pseudorandom number generator (CSPRNG). A CSPRNG must have the following

properties:

■■

Next-bit test: You should not be able to guess the next bit with no better

than 50% probability. This means given S i + 1 , S i + 2 , S i + 3 , . . . , S i + n you

should not be able to guess S i + n + 1 .

■ ■ State compromised extension: You should not be able to calculate

S i , S i − 1 , . . . , given S i + 1 , . . . , S i + n .

Hooray! Your file is uploaded and ready to be published.

Saved successfully!

Ooh no, something went wrong!