Hacking Secret Ciphers with Python

(Ann) #1
Chapter 24 – Public Key Cryptography and the RSA Cipher 401

translating English letters into electric pulses of dots and dashes. ASCII is just a code. We can
encode characters into ASCII numbers and decode ASCII numbers back to characters.


The chr() function (pronounced “char”, short for “character”) takes an integer ASCII number
as the argument and returns a single-character string. The ord() function (short for “ordinal”)
takes a single-character string as the argument, and returns the integer ASCII value for that
character. Try typing the following into the interactive shell:





chr(65)
'A'
ord('A')
65
chr(73)
'I'
chr(65+8)
'I'
chr(52)
'4'
chr(ord('F'))
'F'
ord(chr(68))
68





But if you have a string with many letters, it may be easier to use the encode() and
decode() string methods explained later in this chapter.


Practice Exercises, Chapter 24, Set B


Practice exercises can be found at http://invpy.com/hackingpractice 24 B.


Blocks


In cryptography, a “block” is a fixed length of bits. In our RSA cipher program, a block is
represented by an integer. We’ve set the block size to 128 bytes, or 1024 bits (since there are 8
bits in 1 byte). Our message string value will be converted into several integer values (i.e. several
blocks).


 It is important to note that the RSA encryption algorithm requires that the block size be
equal or less than the key size. Otherwise, the math doesn’t work and you won’t be able
to decrypt the ciphertext the program produced.
Free download pdf