Stream ciphers and bitwise XOR

Math 202 Cryptography, September 12, 2016
T. Ratliff, Wheaton College
ratliff_thomas@wheatoncollege.edu

Basics of bitwise XOR, one character at a time

Remember that a stream cipher encrypts by XORing the plaintext bits x_i with the key stream bit s_i

Let’s do this for a single plaintext character “E”. 
When we convert “E” to ASCII, the plaintext will become an 8-bits long. 
We will need an 8-bit long key stream, so any 8-bit number will do. 
I’ll “randomly” pick s=196

Let’s get the ASCII value for “E” and set the value for s

x=ToCharacterCode["E"]
s={196}

Let’s check the binary versions of x and s

BaseForm[x,2]
BaseForm[s,2]

We encrypt by using XOR

y=BitXor[x,s]

Let’s check by comparing the binary version of y

BaseForm[y,2]

Remember we decrypt the ciphertext the same way we encrypt

plaintext=BitXor[y,s]

And let’s convert back to a letter

FromCharacterCode[plaintext]

Apply to a longer string

Let’s apply the same idea to a longer message.
We’ll need a longer key stream.  This one can handle a thousand characters. If you’re curious, I used Mathematica’s pseudorandom number function RandomInteger[ ] to generate this list.

keystream = {132,104,115,51,68,174,122,168,123,43,213,110,203,175,158,193,209,59,80,32,102,171,79,205,242,213,137,148,23,47,150,170,38,87,34,7,74,201,150,132,211,234,234,248,193,186,151,70,141,135,201,118,82,68,222,86,107,20,40,161,77,214,164,206,54,147,210,73,82,51,147,45,238,177,56,132,237,139,143,154,213,51,136,205,12,170,41,143,209,52,103,228,81,65,19,218,249,126,221,106,170,134,204,204,116,60,130,170,77,86,9,243,143,122,184,230,241,229,190,47,81,188,146,246,239,22,155,167,229,68,72,159,185,141,13,79,108,239,196,162,27,186,53,226,4,146,231,206,121,108,218,232,129,117,63,22,23,245,132,122,191,201,111,35,61,232,67,116,147,169,113,26,76,183,202,206,238,172,199,44,199,196,153,149,155,61,107,141,178,46,238,254,42,235,179,196,254,17,78,28,147,172,111,180,213,249,242,143,47,100,213,67,177,192,10,78,235,118,54,214,232,204,159,186,149,14,0,220,19,158,51,74,136,168,228,152,178,95,126,22,205,251,120,118,105,88,6,154,58,232,130,65,6,237,196,108,227,133,32,171,194,136,254,208,125,46,94,194,180,189,150,34,125,111,235,11,242,159,213,52,142,100,225,244,48,64,75,253,215,11,156,85,209,83,72,207,110,192,143,190,3,230,118,168,35,212,174,43,137,43,37,155,47,29,134,190,152,220,142,119,27,199,5,184,28,85,176,218,90,43,238,18,194,112,117,60,79,26,246,199,248,188,249,206,39,191,80,98,216,144,169,13,148,152,201,32,196,96,112,152,84,50,237,43,111,73,30,77,82,61,186,34,212,244,235,65,54,130,15,227,88,154,129,171,182,245,154,171,236,93,60,96,117,34,196,20,72,137,138,13,220,25,107,160,104,141,168,21,178,193,102,240,36,176,219,49,148,150,146,170,181,36,85,7,54,103,127,2,228,50,147,126,51,155,7,118,12,63,207,234,86,129,169,132,130,3,109,228,78,123,22,34,70,22,172,3,204,61,141,109,29,93,120,183,136,176,225,174,208,171,228,245,180,183,55,133,76,113,94,145,230,5,181,157,220,77,188,209,217,25,32,160,223,4,66,26,162,34,117,33,128,11,235,123,82,52,174,149,239,119,42,115,176,44,55,69,223,106,36,231,163,124,233,119,239,78,251,102,55,11,218,181,147,172,242,53,114,195,142,45,124,66,86,212,88,149,0,154,101,193,87,40,157,6,126,21,45,130,65,49,192,114,208,88,206,146,237,217,68,119,222,180,36,52,23,238,64,250,62,11,93,201,75,99,210,58,83,11,227,134,85,72,213,122,206,233,183,14,50,215,25,68,103,0,53,21,86,187,153,145,126,244,198,176,32,211,111,138,222,162,249,133,243,31,82,126,225,101,227,172,75,164,79,187,185,132,115,2,24,128,193,166,168,124,51,101,17,143,172,108,96,85,247,96,204,221,39,55,88,83,45,113,23,217,169,55,183,144,213,93,4,175,8,227,81,223,86,14,217,59,128,0,163,28,206,100,52,8,58,246,200,194,197,90,241,180,214,242,52,80,127,21,100,113,73,7,80,254,168,67,67,219,150,89,67,72,155,206,86,66,166,112,142,213,224,32,103,74,225,96,135,162,181,201,95,86,43,100,37,243,158,107,184,118,208,61,62,122,84,62,46,92,183,84,218,78,197,168,250,56,123,53,196,51,0,122,108,112,244,161,56,168,232,28,44,249,36,93,146,155,21,49,20,245,136,51,95,65,95,237,131,135,244,244,17,4,59,144,13,174,244,43,207,177,229,180,50,53,222,201,153,78,170,158,7,232,4,51,13,98,140,29,204,221,31,193,28,118,159,79,60,218,138,216,214,174,251,115,251,176,124,57,222,195,43,75,25,143,213,136,90,67,244,118,9,176,72,229,67,164,183,69,248,85,200,23,95,58,69,29,165,138,7,179,190,187,222,208,68,139,87,213,44,81,165,37,109,193,31,181,15,116,118,44,187,221,225,234,46,197,252,62,222,236,114,213,30,141,54,134,240,208,211,237,211,149,208,251,131,118,222,121,222,202,174,46,34,31,74,201,68,208,174,58,28,22,38,214,10,42,180,208,69,43,29,181,199,64,208,15,141,212,135,187,16,78,128,110,33,183,224,235,12,157,225,133,156,51,59,81,122,6,202,8,218,146,94,192,157,240,112,235,122,187,114,82,206,154,10,28,155,34,251,229,199,66,238,18,123,200};

Let’s encrypt our favorite message, but we need to adjust the key stream to be the same length as our message.

x=ToCharacterCode["Meet at Dunkin Donuts at midnight. Come alone."]
s=Part[keystream, 1;;Length[x]]

Now we can encrypt

y=BitXor[x,s]

And we can check the binary version.

BaseForm[y,2]

If we were to actually send this, we would need to pad each value to be an 8-bit binary number by adding leading 0’s.

IntegerDigits[y,2,8]

In-Class Work

Decipher the following message that was encoded using the keystream defined above.

y={205,14,83,67,33,193,10,196,30,11,177,1,235,193,241,181,241,89,53,76,15,206,57,168,210,161,225,245,99,15,251,203,82,63,71,106,43,189,255,231,160,202,131,139,225,201,254,43,253,235,172,90,114,45,170,118,2,103,8,206,35,186,221,238,84,246,177,40,39,64,246,13,154,217,93,253,205,239,224,186,187,92,252,237,126,207,72,227,184,78,2,196,57,46,100,250,154,17,176,26,198,239,175,173,0,89,230,138,33,63,111,150,175,19,203,200,209,200,158,101,62,212,252,214,153,121,245,135,171,33,61,242,216,227,99}

Encrypt your own favorite quote using the keystream, and post it to onCourse in the forum “September 12 In Class.”
It’s easiest if after you highlight your ciphertext, if you right-click and select Copy As - Plain Text.
After other students have posted, decrypt some of their messages.