Tip:
Highlight text to annotate it
X
So let's define that a little more precisely.
So we're going to find our set of messages--
is strings of 0s and 1s--so we'll use bits and some fixed length.
So n is number that gives us the maximum length of a message.
Our message is selected from all binary strings of length n.
Our key is also selected from the set of all binary strings of length n.
And then to do encryption--our encryption function--
we're going to think of the message as being this sequence of bits
and the key is also a sequence of bits.
The result of our encryption is the ciphertext,
which is a sequence of bits.
So, length n, where the value of each ciphertext bit
is equal to the XOR of the corresponding message bit
and the corresponding key bit.
So let's try an example.
And for this example, I'm going to give you the ciphertext
and the key and the message.
So suppose our message is the string 'CS,'
but our message space is in bits.
Well, the first thing we need to do is to convert those strings to bits
and we can do that in Python by using ord,
that takes a one character string and turns it into
a decimal number.
And then we need to convert that decimal number into bits.
Into a binary number.
And we need to do this for each character in the string.
We're going to convert it to a character, convert that to bits,
and I'll show you the code for doing that,
we'll leave the more interesting code for you to write.
Here we're converting to bits.
This is a fairly straightforward, but not the shortest way to do this.
We're going to make an array of bits as our result for any decimal number
if it's divisible by 2, we want to have a 0 at the beginning of a result.
If it's not divisible by 2, that's going to be a 1.
And then we divide the number by 2 as we go forward.
So that's going to fill up all the places.
We want our bits to be particular lengths,
so we have a padding,
and for all the characters, we'll use 7 bits.
So we're going to pad the result with leading zeros
until we get to that size.
We can see this--so if we do ord we see that
the number corresponding to the letter C is 67.
If we convert that to bits--and we'll use 7 as our padding--
that gives us enough for 128 different values
which is enough for the ASCII character values
that we get back from ord.
We can see those bits as a list,
and we can see that a little more easily as a string
using the display bits procedure that just turned that into a string.
So now we want to convert more than one character.
To do that, we have a string to bits procedure
that goes through all the characters in the string,
converting each one to bits using convert to bits,
and concatenating those all together to the result.
So now we can do string to bits.
For our two-letter string, and now we get 14 bits as a result.
So if that's our message, then the value of M is what we got there.
So this is our message.
There are 14 bits, n is 14.
That means--to encrypt this using a One-Time Pad,
we need a key that also has 14 bits.
So let's pick our key,
and--we're just going to make up a random key now.
Actually finding random values is very important in cryptography,
and we'll talk about that in a later unit,
but for now let's just make one up.
So suppose this is our key.
Then the ciphertext is just the result of XOR
in each message bit with the corresponding key bit.
So that's our ciphertext.
So the question is, as an interceptor,
you saw just this ciphertext, you don't know anything
about the message or the key, and you're going to guess
possible key values to try to figure out what the message is.
And what key value would you guess
that would mislead you to think that the message
was actually BS instead of CS?