07.07.2023 Views

Implementing-cryptography-using-python

You also want an ePaper? Increase the reach of your titles

YUMPU automatically turns print PDFs into web optimized ePapers that Google loves.

58 Chapter 2 ■ Cryptographic Protocols and Perfect Secrecy

realMsg = hexlify(realMessage)

decoyMsg = hexlify(decoyMessage)

realKey = int(realMsg, 16) ^ msg

decoyKey = int(decoyMsg, 16) ^ msg

print ("The secret message is: ", msg)

print ("The real key is: ", realKey)

print ("The decoy key is: ", decoyKey)

print ()

# choose either the decoy key or the real key

key = realKey

plain = otpDec(msg, key)

print (plain)

print ()

key = decoyKey

plain = otpDec(msg, key)

print ()

print (plain)

The output using the keys should resemble the following:

The secret message is: 27865585609579580086126129250694423149352332105546

The real key is: 167190391575317824697301565359800501773353221186411

The decoy key is: 142430497120678699528160795125823853486446056338465

attackthematmidnight!

retreatanddonotattack

Once you use the same key more than once, you open the key to vulnerabilities

as now the same key will convert more than one ciphertext to plaintext.

Since there is most likely only one key that will decrypt two different messages,

the key would be compromised. Using the real key in the preceding example

again would tip the advisories of your encryption methods.

One-Way Hashes

You were briefly introduced to hashes in the previous chapter. As a reminder,

a hash function is an algorithm that changes the input or changes the data of an

arbitrary or random length into a fixed-sized output.

In our previous example, you saw passwords that were hashed using the

SHA-512 algorithm. Storing passwords as hashes provides a level of security

in the event that the password storage database is compromised. Hashes can

be used as a more advanced version of checksums. When we move a file from

one drive to another, we have a checksum on frames called a Frame Check

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

Saved successfully!

Ooh no, something went wrong!