398 http://inventwithpython.com/hacking
Email questions to the author: [email protected]
decrypt it. But by encrypting a message with her own private key, Alice has digitally signed
the message in a way that cannot be forged. Everyone can decrypt this signed message with her
public key, and since only Alice has access to her private key, only Alice could have produced
this ciphertext. Alice has to stick to her digital signature; she can’t say that Bob forged or
photoshopped it!
This feature is called nonrepudiation. Nonrepudiation is where someone who has made a
statement or claim cannot later refute that they made that statement or claim. Alice could always
claim that her computer was hacked and somebody else had access to her private key, but this
would mean that any other documents she signed could be called into question. (And it would be
very suspicious if Alice’s computer kept “getting hacked” each time she wanted to back out of a
promise.)
Digital signatures can also provide authentication, which allows someone to prove they are who
they say they are. If Alice gets an email claiming to be from the President but wants to be sure it
really is the President, she could always respond with, “Prove you’re the President! Encrypt the
string 'SIMTAVOKXVAHXXSLBGZXVPKNMQMHOYGWFQMXEBCC' with the President’s
private key.” and Alice would be able to decrypt the reply with the President’s public key to see if
it decrypted to her random string. This is called a challenge-response authentication system.
Digital signatures can be used to do many important things, including digital cash, authentication
of public keys, or anonymous web surfing. If you’d like to find out more, go to
http://invpy.com/digitalsignatures.
How the RSA Cipher Program Works
rsaCipher.py
RSA Cipher
http://inventwithpython.com/hacking (BSD Licensed)
- import sys
IMPORTANT: The block size MUST be less than or equal to the key size!
(Note: The block size is in bytes, the key size is in bits. There
are 8 bits in 1 byte.)
- DEFAULT_BLOCK_SIZE = 128 # 128 bytes
- BYTE_SIZE = 256 # One byte has 256 different values.
A single “byte” can hold a number between 0 and 255, that is, 256 different numbers. We will use
this fact in some of the block-related math explained later. This is why the BYTE_SIZE constant
is set to 256. The DEFAULT_BLOCK_SIZE constant is set to 128 because we will be using
block sizes of 128 bytes by default in our program. (Block sizes are explained later.)