Lab 1 Key

advertisement
Codes Ciphers and Cryptography Lab 1: Modulo Arithmetic and
Additive Ciphers
Ÿ Modular Arithmetic with Mathematica
In this section, we look at the commands used in Mathematica to perform modular arithmetic.
Ÿ How to compute a mod n
Here is how to compute 18 mod 5:
In[1]:=
Out[1]=
Mod@18, 5D
3
Practice Commands 1: Find 4 mod 7, 28 mod 26, and 26 mod 13.
In[2]:=
Out[2]=
In[3]:=
Out[3]=
In[4]:=
Out[4]=
Mod@4, 7D
4
Mod@28, 26D
2
Mod@26, 13D
0
Ÿ How to compute (a+b) mod n
Here is how to add numbers mod n, such as (27+15) mod 26:
In[5]:=
Out[5]=
In[6]:=
Out[6]=
In[7]:=
Out[7]=
In[8]:=
Out[8]=
In[9]:=
Out[9]=
Mod@27 + 15, 26D
16
Mod@27, 26D
1
Mod@15, 26D
15
Mod@27, 26D + Mod@15, 26D
16
Mod@16, 26D
16
Practice Commands 2: Find (170+1932) mod 35 and (123+19) mod 12.
2
390Lab1KeyFall2011.nb
In[10]:=
Out[10]=
In[11]:=
Out[11]=
Mod@170 + 1932, 35D
2
Mod@123 + 19, 12D
10
Ÿ How to compute (a*b) mod n
Here is how to multiply numbers mod n, such as (27*15) mod 26:
In[12]:=
Out[12]=
In[13]:=
Out[13]=
In[14]:=
Out[14]=
In[15]:=
Out[15]=
In[16]:=
Out[16]=
Mod@27 * 15, 26D
15
Mod@27, 26D
1
Mod@15, 26D
15
Mod@27, 26D * Mod@15, 26D
15
Mod@15, 26D
15
Practice Commands 3: Find (170*1932) mod 35 and (123*19) mod 12.
In[17]:=
Out[17]=
In[18]:=
Out[18]=
Mod@170 * 1932, 35D
0
Mod@123 * 19, 12D
9
Ÿ How to find the additive inverse and multiplicative inverse of a mod n
For any real number a, there exists a unique additive inverse, -a. a + -a = -a + a = 0. For any non-zero real number a, there exists
1
a unique multiplicative inverse, a-1 = a . a * a-1 = a-1 * a = 1. To find the additive inverse of a mod n and multiplicative inverse
of a mod n (if it exists), use the following commands:
In[19]:=
Out[19]=
Mod@- 15, 26D
11
Practice Commands 4: Check this answer by adding 15 to 11 mod 26.
In[20]:=
Out[20]=
Mod@15 + 11, 26D
0
390Lab1KeyFall2011.nb
In[21]:=
Out[21]=
3
PowerMod@15, - 1, 26D
7
Practice Commands 5: Check this answer by multiplying 15 by 7 mod 26.
In[22]:=
Out[22]=
Mod@15 * 7, 26D
1
Question 1: Find the additive inverses and multiplicative inverses (if possible) of 0, 1, 2, ... , 6, mod 7 and 0, 1, 2, ... , 9, mod 10.
What do you notice? Check your conjecture with 0, 1, 2, ..., 25 mod 26.
In[23]:=
Out[23]=
In[24]:=
Mod@- 80, 1, 2, 3, 4, 5, 6<, 7D
80, 6, 5, 4, 3, 2, 1<
PowerMod@80, 1, 2, 3, 4, 5, 6<, - 1, 7D
PowerMod::ninv : 0 is not invertible modulo 7. ‡
Out[24]=
In[25]:=
Out[25]=
In[26]:=
8PowerMod@0, - 1, 7D, 1, 4, 5, 2, 3, 6<
Mod@- 80, 1, 2, 3, 4, 5, 6, 8, 9<, 10D
80, 9, 8, 7, 6, 5, 4, 2, 1<
PowerMod@80, 1, 2, 3, 4, 5, 6, 7, 8, 9<, - 1, 10D
PowerMod::ninv : 0 is not invertible modulo 10. ‡
PowerMod::ninv : 2 is not invertible modulo 10. ‡
PowerMod::ninv : 4 is not invertible modulo 10. ‡
General::stop : Further output of PowerMod::ninv will be suppressed during this calculation. ‡
Out[26]=
8PowerMod@0, - 1, 10D, 1, PowerMod@2, - 1, 10D, 7, PowerMod@4, - 1, 10D,
PowerMod@5, - 1, 10D, PowerMod@6, - 1, 10D, 3, PowerMod@8, - 1, 10D, 9<
The additive inverse of 0, 1, 2, 3, 4, 5, and 6 mod 7 are 0, 6, 5, 4, 3, 2, and 1, respectively. The additive inverse of 0, 1, 2, 3, 4, 5,
6, 7, 8, and 9 mod 10 are 0, 9, 8, 7, 6, 5, 4, 3, 2, and 1, respectively.
The multiplicative inverses of 1, 2, 3, 4, 5, and 6 mod 7 are 1, 4, 5, 2, 3, and 6, respectively. 0 is not invertible mod 7. The
multiplicative inverses of 1, 3, 7 and 9 mod 10 are 1, 7, 3, and 9, respectively. 0, 2, 4, 5, 6, and 8 are not invertible mod 10.
Conjecture: The additive inverse of a mod n is n-a mod n. If a is non-zero mod n and a and n have no common factors other than
1, then a has a multiplicative inverse mod n. Thus, for n = 26, each integer from 0 to 25 should have an additive inverse and each
odd integer from 1 to 25, except 13, should have a multiplicative inverse.
In[27]:=
Out[27]=
In[28]:=
Out[28]=
Mod@- 80, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11,
12, 13, 14, 15, 16, 17, 18, 19, 20, 21, 22, 23, 24, 25<, 26D
80, 25, 24, 23, 22, 21, 20, 19, 18, 17,
16, 15, 14, 13, 12, 11, 10, 9, 8, 7, 6, 5, 4, 3, 2, 1<
PowerMod@81, 3, 5, 7, 9, 11, 15, 17, 19, 21, 23, 25<, - 1, 26D
81, 9, 21, 15, 3, 19, 7, 23, 11, 5, 17, 25<
4
390Lab1KeyFall2011.nb
In[29]:=
PowerMod@80, 2, 4, 6, 8, 10, 12, 13, 14, 16, 18, 20, 22, 24<, 26D
PowerMod::argrx : PowerMod called with 2 arguments; 3 arguments are expected. ‡
PowerMod::argrx : PowerMod called with 2 arguments; 3 arguments are expected. ‡
PowerMod::argrx : PowerMod called with 2 arguments; 3 arguments are expected. ‡
General::stop : Further output of PowerMod::argrx will be suppressed during this calculation. ‡
Out[29]=
8PowerMod@0, 26D, PowerMod@2, 26D, PowerMod@4, 26D,
PowerMod@6, 26D, PowerMod@8, 26D, PowerMod@10, 26D, PowerMod@12, 26D,
PowerMod@13, 26D, PowerMod@14, 26D, PowerMod@16, 26D,
PowerMod@18, 26D, PowerMod@20, 26D, PowerMod@22, 26D, PowerMod@24, 26D<
Ÿ Additive Cipher Example with m = 14
In order to list the alphabet letters a, b, ..., z enter the following command. Characters[ ] takes a string in quotes and turns it into
a list of letters. Here the alphabet is saved in memory as a list of strings (letters) with name "PlainText".
In[30]:=
Out[30]=
PlainText = Characters@"abcdefghijklmnopqrstuvwxyz"D
8a, b, c, d, e, f, g, h, i, j, k, l, m, n, o, p, q, r, s, t, u, v, w, x, y, z<
The command ToCharacterCode[ ] converts letters to their ASCII (American Standard Code for Information Interchange) code.
For example, a®97. Since we want to assign the numbers 1, 2, ... , 25, 0 to a, b, ... , y, z, we subtract 96 and convert the result to
numbers modulo 26.
In[31]:=
Out[31]=
PlainTextNumbers = Mod@ToCharacterCode@PlainTextD - 96, 26D
881<, 82<, 83<, 84<, 85<, 86<, 87<, 88<, 89<, 810<, 811<, 812<, 813<, 814<,
815<, 816<, 817<, 818<, 819<, 820<, 821<, 822<, 823<, 824<, 825<, 80<<
The next two commands assign the number 14 to the variable m and add 14 to the plaintext numbers 1, 2, ... , 25, 0 modulo 26.
In[32]:=
m = 14;
In[33]:=
CipherTextNumbers = Mod@PlainTextNumbers + m, 26D
Out[33]=
8815<, 816<, 817<, 818<, 819<, 820<, 821<, 822<, 823<, 824<, 825<, 80<,
81<, 82<, 83<, 84<, 85<, 86<, 87<, 88<, 89<, 810<, 811<, 812<, 813<, 814<<
Finally, to get our ciphertext, we use the command FromCharacterCode[ ] to convert ciphertext numbers back to ciphertext.
The 64 is added to get the correct ASCII code for the corresponding capital letters. For example, 1®1 + 64 = 65®A. The
/."@"®"Z" portion of the command is used to replace the symbol @ with the letter Z. This is necessary because we converted
plaintext z to 0 instead of 26.
In[34]:=
Out[34]=
CipherText = FromCharacterCode@CipherTextNumbers + 64D . "ž" ® "Z"
8O, P, Q, R, S, T, U, V, W, X, Y, Z, A, B, C, D, E, F, G, H, I, J, K, L, M, N<
The next command StringJoin[ ] takes a list of letters such as the ciphertext list and converts it to a string.
In[35]:=
Out[35]=
StringJoin@CipherTextD
OPQRSTUVWXYZABCDEFGHIJKLMN
390Lab1KeyFall2011.nb
5
We can use the next command to put everything together and make a table with four columns: plaintext, plaintext numbers,
ciphertext numbers, ciphertext.
In[36]:=
FrameBox@GridBox@Table@8PlainText@@nDD, PlainTextNumbers@@nDD@@1DD,
CipherTextNumbers@@nDD@@1DD, CipherText@@nDD<, 8n, 1, Length@PlainTextD<D,
RowLines ® True, ColumnLines ® TrueDD  DisplayForm
Out[36]//DisplayForm=
a
b
c
d
e
f
g
h
i
j
k
l
m
n
o
p
q
r
s
t
u
v
w
x
y
z
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
0
15
16
17
18
19
20
21
22
23
24
25
0
1
2
3
4
5
6
7
8
9
10
11
12
13
14
O
P
Q
R
S
T
U
V
W
X
Y
Z
A
B
C
D
E
F
G
H
I
J
K
L
M
N
Ÿ Sample Encryption of a Message Using an Additive Cipher with m = 14
Question 2: Using the commands above as a guide, encrypt the message "thepackagehasbeendelivered" with an affine cipher,
using m=14. The answer is given below, but try it yourself before checking the solution!
In[37]:=
Out[37]=
In[38]:=
Out[38]=
In[39]:=
PlainText = Characters@"thepackagehasbeendelivered"D
8t, h, e, p, a, c, k, a, g, e, h, a, s, b, e, e, n, d, e, l, i, v, e, r, e, d<
PlainTextNumbers = Mod@ToCharacterCode@PlainTextD - 96, 26D
8820<, 88<, 85<, 816<, 81<, 83<, 811<, 81<, 87<, 85<, 88<, 81<, 819<,
82<, 85<, 85<, 814<, 84<, 85<, 812<, 89<, 822<, 85<, 818<, 85<, 84<<
m = 14;
6
390Lab1KeyFall2011.nb
In[40]:=
Out[40]=
In[41]:=
Out[41]=
In[42]:=
Out[42]=
In[43]:=
CipherTextNumbers = Mod@PlainTextNumbers + m, 26D
888<, 822<, 819<, 84<, 815<, 817<, 825<, 815<, 821<, 819<, 822<, 815<, 87<,
816<, 819<, 819<, 82<, 818<, 819<, 80<, 823<, 810<, 819<, 86<, 819<, 818<<
CipherText = FromCharacterCode@CipherTextNumbers + 64D . "ž" ® "Z"
8H, V, S, D, O, Q, Y, O, U, S, V, O, G, P, S, S, B, R, S, Z, W, J, S, F, S, R<
StringJoin@CipherTextD
HVSDOQYOUSVOGPSSBRSZWJSFSR
FrameBox@GridBox@Table@8PlainText@@nDD, PlainTextNumbers@@nDD@@1DD,
CipherTextNumbers@@nDD@@1DD, CipherText@@nDD<, 8n, 1, Length@PlainTextD<D,
RowLines ® True, ColumnLines ® TrueDD  DisplayForm
Out[43]//DisplayForm=
t
h
e
p
a
c
k
a
g
e
h
a
s
b
e
e
n
d
e
l
i
v
e
r
e
d
20
8
5
16
1
3
11
1
7
5
8
1
19
2
5
5
14
4
5
12
9
22
5
18
5
4
8
22
19
4
15
17
25
15
21
19
22
15
7
16
19
19
2
18
19
0
23
10
19
6
19
18
H
V
S
D
O
Q
Y
O
U
S
V
O
G
P
S
S
B
R
S
Z
W
J
S
F
S
R
Ÿ Solution!
In[44]:=
Out[44]=
PlainText = Characters@"thepackagehasbeendelivered"D
8t, h, e, p, a, c, k, a, g, e, h, a, s, b, e, e, n, d, e, l, i, v, e, r, e, d<
390Lab1KeyFall2011.nb
In[45]:=
Out[45]=
PlainTextNumbers = Mod@ToCharacterCode@PlainTextD - 96, 26D
8820<, 88<, 85<, 816<, 81<, 83<, 811<, 81<, 87<, 85<, 88<, 81<, 819<,
82<, 85<, 85<, 814<, 84<, 85<, 812<, 89<, 822<, 85<, 818<, 85<, 84<<
In[46]:=
m = 14;
In[47]:=
CipherTextNumbers = Mod@PlainTextNumbers + m, 26D
Out[47]=
In[48]:=
Out[48]=
In[49]:=
Out[49]=
In[50]:=
7
888<, 822<, 819<, 84<, 815<, 817<, 825<, 815<, 821<, 819<, 822<, 815<, 87<,
816<, 819<, 819<, 82<, 818<, 819<, 80<, 823<, 810<, 819<, 86<, 819<, 818<<
CipherText = FromCharacterCode@CipherTextNumbers + 64D . "ž" ® "Z"
8H, V, S, D, O, Q, Y, O, U, S, V, O, G, P, S, S, B, R, S, Z, W, J, S, F, S, R<
StringJoin@CipherTextD
HVSDOQYOUSVOGPSSBRSZWJSFSR
FrameBox@GridBox@Table@8PlainText@@nDD, PlainTextNumbers@@nDD@@1DD,
CipherTextNumbers@@nDD@@1DD, CipherText@@nDD<, 8n, 1, Length@PlainTextD<D,
RowLines ® True, ColumnLines ® TrueDD  DisplayForm
Out[50]//DisplayForm=
t
h
e
p
a
c
k
a
g
e
h
a
s
b
e
e
n
d
e
l
i
v
e
r
e
d
20
8
5
16
1
3
11
1
7
5
8
1
19
2
5
5
14
4
5
12
9
22
5
18
5
4
8
22
19
4
15
17
25
15
21
19
22
15
7
16
19
19
2
18
19
0
23
10
19
6
19
18
H
V
S
D
O
Q
Y
O
U
S
V
O
G
P
S
S
B
R
S
Z
W
J
S
F
S
R
Ÿ Sample Decryption of a Message Encrypted with an Additive Cipher
The following commands can be used to decrypt a message encrypted with an additive cipher by checking all possible additive
inverses of the unknown number m that was added to the plaintext! This technique is known as brute force because all possibilities are checked. The drawback to this method is that it may take a lot of time to implement.
8
390Lab1KeyFall2011.nb
The following commands can be used to decrypt a message encrypted with an additive cipher by checking all possible additive
inverses of the unknown number m that was added to the plaintext! This technique is known as brute force because all possibilities are checked. The drawback to this method is that it may take a lot of time to implement.
In[51]:=
Out[51]=
In[52]:=
Out[52]=
In[53]:=
Out[53]=
CipherText = "PHHWPHDWWKHXVXDOSODFHDWHLJKWRFORFN"
PHHWPHDWWKHXVXDOSODFHDWHLJKWRFORFN
CipherTextNumbers = ToCharacterCode@CipherTextD - 64
816, 8, 8, 23, 16, 8, 4, 23, 23, 11, 8, 24, 22, 24, 4, 15,
19, 15, 4, 6, 8, 4, 23, 8, 12, 10, 11, 23, 18, 6, 15, 18, 6, 14<
Table@StringReplace@
FromCharacterCode@Mod@CipherTextNumbers + n, 26D + 96D, "`" -> "z"D, 8n, 0, 25<D
8phhwphdwwkhxvxdosodfhdwhljkwrforfn, qiixqiexxliywyeptpegieximklxsgpsgo,
rjjyrjfyymjzxzfquqfhjfyjnlmythqthp, skkzskgzznkayagrvrgikgzkomnzuiruiq,
tllatlhaaolbzbhswshjlhalpnoavjsvjr, ummbumibbpmcacitxtikmibmqopbwktwks,
vnncvnjccqndbdjuyujlnjcnrpqcxluxlt, woodwokddroecekvzvkmokdosqrdymvymu,
xppexpleespfdflwawlnpleptrseznwznv, yqqfyqmfftqgegmxbxmoqmfqustfaoxaow,
zrrgzrnggurhfhnycynprngrvtugbpybpx, asshasohhvsigiozdzoqsohswuvhcqzcqy,
bttibtpiiwtjhjpaeaprtpitxvwidradrz, cuujcuqjjxukikqbfbqsuqjuywxjesbesa,
dvvkdvrkkyvljlrcgcrtvrkvzxykftcftb, ewwlewsllzwmkmsdhdsuwslwayzlgudguc,
fxxmfxtmmaxnlnteietvxtmxbzamhvehvd, gyyngyunnbyomoufjfuwyunycabniwfiwe,
hzzohzvooczpnpvgkgvxzvozdbcojxgjxf, iaapiawppdaqoqwhlhwyawpaecdpkyhkyg,
jbbqjbxqqebrprximixzbxqbfdeqlzilzh, kccrkcyrrfcsqsyjnjyacyrcgefrmajmai,
lddsldzssgdtrtzkokzbdzsdhfgsnbknbj, meetmeattheusualplaceateightoclock,
nffunfbuuifvtvbmqmbdfbufjhiupdmpdl, oggvogcvvjgwuwcnrncegcvgkijvqenqem<
Question 3: Suppose you know the integer m that is added to a plaintext message to get the ciphertext. Modify the commands in
the section above on encryption of a message using an additive cipher with m = 14 to encrypt the message
"theplansareinthehotelsafe" with m = 17 and decrypt the resulting ciphertext by adding the additive inverse of m mod 26. Note:
You can do this last problem without using the brute force method of decryption!
Ÿ Solution for encrypting the message:
In[54]:=
Out[54]=
In[55]:=
Out[55]=
PlainText = Characters@"theplansareinthehotelsafe"D
8t, h, e, p, l, a, n, s, a, r, e, i, n, t, h, e, h, o, t, e, l, s, a, f, e<
PlainTextNumbers = Mod@ToCharacterCode@PlainTextD - 96, 26D
8820<, 88<, 85<, 816<, 812<, 81<, 814<, 819<, 81<, 818<, 85<, 89<,
814<, 820<, 88<, 85<, 88<, 815<, 820<, 85<, 812<, 819<, 81<, 86<, 85<<
In[56]:=
m = 17;
In[57]:=
CipherTextNumbers = Mod@PlainTextNumbers + m, 26D
Out[57]=
In[58]:=
Out[58]=
8811<, 825<, 822<, 87<, 83<, 818<, 85<, 810<, 818<, 89<, 822<, 80<, 85<,
811<, 825<, 822<, 825<, 86<, 811<, 822<, 83<, 810<, 818<, 823<, 822<<
CipherText = FromCharacterCode@CipherTextNumbers + 64D . "ž" ® "Z"
8K, Y, V, G, C, R, E, J, R, I, V, Z, E, K, Y, V, Y, F, K, V, C, J, R, W, V<
390Lab1KeyFall2011.nb
In[59]:=
Out[59]=
StringJoin@CipherTextD
KYVGCREJRIVZEKYVYFKVCJRWV
Ÿ Solution for decrypting the message:
In[60]:=
Out[60]=
In[61]:=
Out[61]=
In[62]:=
Out[62]=
In[63]:=
Out[63]=
In[64]:=
Out[64]=
In[65]:=
Out[65]=
CipherText = Characters@"KYVGCREJRIVZEKYVYFKVCJRWV"D
8K, Y, V, G, C, R, E, J, R, I, V, Z, E, K, Y, V, Y, F, K, V, C, J, R, W, V<
CipherTextNumbers = Mod@ToCharacterCode@CipherTextD - 64, 26D
8811<, 825<, 822<, 87<, 83<, 818<, 85<, 810<, 818<, 89<, 822<, 80<, 85<,
811<, 825<, 822<, 825<, 86<, 811<, 822<, 83<, 810<, 818<, 823<, 822<<
m = Mod@- 17, 26D
9
PlainTextNumbers = Mod@CipherTextNumbers + m, 26D
8820<, 88<, 85<, 816<, 812<, 81<, 814<, 819<, 81<, 818<, 85<, 89<,
814<, 820<, 88<, 85<, 88<, 815<, 820<, 85<, 812<, 819<, 81<, 86<, 85<<
PlainText = FromCharacterCode@PlainTextNumbers + 96D . "`" ® "z"
8t, h, e, p, l, a, n, s, a, r, e, i, n, t, h, e, h, o, t, e, l, s, a, f, e<
StringJoin@PlainTextD
theplansareinthehotelsafe
Please turn in a copy of this lab at the beginning of class on Wednesday, September
14, 2011. Include your input and output for each part of the lab, as well as work for
any practice commands and questions asked in the lab.
9
Download