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.

Chapter 5 ■ Stream Ciphers and Block Ciphers 149

One of the disadvantages of using an OTP is that the keys must be as long as

the message it is trying to conceal; therefore, for long messages, you will need

a long key:

def VernamEncDec (text, key):

result = "";

ptr = 0;

for char in text:

result = result + chr(ord(char) ^ ord(key[ptr]));

ptr = ptr + 1;

if ptr == len(key):

ptr = 0;

return result

key = "thisismykey12345";

while True:

input_text = input("\nEnter Text To Encrypt:\t");

ciphertext = VernamEncDec(input_text, key);

print("\nEncrypted Vernam Cipher Text:\t" + ciphertext);

plainttext = VernamEncDec(ciphertext, key);

print("\nDecrypted Vernam Cipher Text:\t" + plainttext);

Salsa20 Cipher

The Salsa20 cipher was developed in 2005 by Daniel Bernstein, and submitted to

eSTREAM. The Salsa20/20 (Salsa20 with 20 rounds) is built on a pseudorandom

function that is based on add-rotate-xor (ARX) operations. ARX algorithms are

designed to have their round function support modular addition, fixed rotation,

and XOR. These ARX operations are popular because they are relatively fast and

cheap in hardware and software, and because they run in constant time, and

are therefore immune to timing attacks. The rotational cryptanalysis technique

attempts to attack such round functions.

The core function of Salsa20 maps a 128-bit or 256-bit key, a 64-bit nonce/IV,

and a 64-bit counter to a 512-bit block of the keystream. Salsa20 provides speeds of

around 4–14 cycles per byte on modern x86 processors and is considered acceptable

hardware performance. The numeric indicator in the Salsa name specifies

the number of encryption rounds. Salsa20 has 8, 12, and 20 variants. One of the

biggest benefits of Salsa20 is that Bernstein has written several implementations

that have been released to the public domain, and the cipher is not patented.

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

Saved successfully!

Ooh no, something went wrong!