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 5 ■ Stream Ciphers and Block Ciphers 145

hexnonce = binascii.hexlify(nonce)

oursecret = 54321

concatenated_hex = hexnonce + format(oursecret, 'x')

even_length = concatenated_hex.rjust(len(concatenated_hex) +

len(concatenated_hex) % 2, '0')

hexhash = hashlib.sha256(binascii.unhexlify(even_length)).hexdigest()

newseed = (int(hexhash, 16)) % 2**32

print(newseed)

To get a consistent key on both the sending and receiving side, we directly

pass the nonce. To test this, replace the previous nonce with “cc4304c09aee” and

keep the original seed of 54321. The new seed that generates should equate to

“3336748862.” Now you need a system for sending the nonce in your ciphertext.

If you have the first six bytes as nonce bytes and the remaining bytes as the

ciphertext, you can pass everything in the same message. Since the underlying

generator is C’s rand function, the encryption is still not strong enough to protect

any secrets, but it is much stronger than it was.

Knowing the first six bytes are nonce bytes, here is a new challenge for you:

Secret Key: 61983

Message: 3e08816f1377f89f1c596fc197dd52946c92577bfd7c25c3

If you get stuck, you can find the solution in the ch5_decrypt2 file on this

book’s website.

Answer: Seed is 42847799; the message is 'this is a message.'

Ideally, by now, you are gaining an understanding of how stream ciphers

work. Stream ciphers are generally not as secure or well-understood as block

ciphers (which we study next). In software, you will most likely deal with

block ciphers, though there are tools such as Wireguard which will use stream

ciphers. Wireguard is a software VPN protocol that uses the ChaCha20 stream

cipher; you will learn more about ChaCha20 later in this chapter. So, while some

tools may use stream ciphers, stream ciphers play a more significant role in the

hardware ecosystem. With space-constrained devices that need encrypted data

streams, we need fast hardware implementations that encrypt bit-by-bit. So,

as you code these, imagine the hardware version. As outlined in the previous

section, the encryption schemes are still not strong enough to protect any classified

data. In this part, you examine a full-strength encryption scheme called

Trivium. Bart Preneel and Christophe De Cannière created it and submitted

it to the eSTREAM competition. eSTREAM is a project that was organized by

the EU Ecrypt network to help identify new stream ciphers that may be suitable

for widespread adoption. The project began in November 2004 and completed

in April 2008. It is designed to provide a reasonably efficient software

encryption implementation and is specified as an International Standard under

ISO/IEC 29192-3.

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

Saved successfully!

Ooh no, something went wrong!