Professional Documents
Culture Documents
Math 373/578: Using Matlab in Cryptography (Spring 2012)
Math 373/578: Using Matlab in Cryptography (Spring 2012)
allshift(wklvvkrxogehtxlwhhdvb)
wklvvkrxogehtxlwhhdvb
xlmwwlsyphfiuymxiiewc
ymnxxmtzqigjvznyjjfxd
znoyynuarjhkwaozkkgye
aopzzovbskilxbpallhzf
bpqaapwctljmycqbmmiag
cqrbbqxdumknzdrcnnjbh
drsccryevnloaesdookci
estddszfwompbfteppldj
ftueetagxpnqcgufqqmek
guvffubhyqordhvgrrnfl
hvwggvcizrpseiwhssogm
iwxhhwdjasqtfjxittphn
jxyiixekbtrugkyjuuqio
kyzjjyflcusvhlzkvvrjp
lzakkzgmdvtwimalwwskq
mabllahnewuxjnbmxxtlr
nbcmmbiofxvykocnyyums
ocdnncjpgywzlpdozzvnt
pdeoodkqhzxamqepaawou
qefppelriaybnrfqbbxpv
rfgqqfmsjbzcosgrccyqw
sghrrgntkcadpthsddzrx
thisshouldbequiteeasy
uijttipvmecfrvjuffbtz
vjkuujqwnfdgswkvggcua
A study of the output indicates that the plain text should be thisshouldbequiteeasy or this
should be quite easy
(B) Using Affine cipher to encode plain text.
Example Encrypt the plain text meetmeinstlouis with an affine cipher E3,7 (x) 3x + 7 in
Z26 .
>> affinecrypt(meetmeinstlouis, 3, 7)
ans =
rttmrtfujmoxpfj
(C) Using Affine cipher to decrypt cipher text.
Example The cipher text rttmrtfujmoxpfj was encrypted using the affine function 3x + 7 in
Z26 . Decrypt it.
(Step 1:) Solve y 3x + 7 (mod 26) for x. Since 1 = gcd(3, 26) = (9)(3) + (1)(26), 31 9
(mod 26). As (9)(7) 63 11 (mod 26), multiplying both sides of the equation by 9 to get
9y x + 11 (mod 26), and so x 9y 11 9y + 15 (mod 26).
This can also be done by using matlab:
>> powermod(3, -1, 26)
5
and =
9
>> mod(-9*7, 26)
ans =
15
(Step 2) Knowing that the decrypt function is x 9y + 15, we can decrypt the message by
>> affinecrypt(rttmrtfujmoxpfj, 9, 15)
ans =
meetmeinstlouis
to first convert the base-b numbers to base-10, and use matlab to do the operations, then use
matlab to convert the answers back to base-b.
Example Multiply HE by IS in Z26 .
Step 1: Convert HE = 7(26) + 4 = 186 and IS = 8(28) + 18 = 226.
Step 2: Compute the base-10 multiplication.
Step 3: Convert the answer back to Base-26.
>> p=263
p =
263
>> c = [246 18 156 0 256 127 18 156 96 256 235 0 132 68]
c =
Columns 1 through 5
246
18
156
0
Columns 6 through 10
127
18
156
96
Columns 11 through 14
235
0
132
68
>> m = powermod(c, d, p)
m =
Columns 1 through 5
19
17
4
0
Columns 6 through 10
20
17
4
8
Columns 11 through 14
11
0
13
3
256
256
18
18
yy =
633
Thus x 232 311 and y 2153 633 (mod 907), and so the common key can be computed
by k = xy (mod p)
>> k = powermod(xx,y,p)
k =
121
or by k = y x (mod p)
>> k = powermod(yy,x,p)
k =
121
(5) Example: Finding p and q when given n = pq and (n).
Given n = pq = 1009427 and (n) = 1007400. To find p and q, we compute
>> n = 1009427
n =
1009427
>> f = eulerphi(n)
f =
1007400
>> s = n - f + 1
s =
2028
>> d = sqrt(s^2 - 4*n)
d =
274
>> p = (s + d)/2
p =
1151
>> q = (s-d)/2
q =
877
Therefore, p = 1151 and q = 877.
(6) Example: RSA with a single letter plain text Let p = 167, q = 547, n = 91349,
e = 5 and cipher text c 88291 (mod n). To find plain text m, we first find (n) = 90636,
>> n = 91349
n =
91349
>> eulerphi(n)
ans =
90636
11
13
Solution: (i) For sigk (m, r) = (h, g) = (480, 532), Bob recognizes that b = 88, h = 480, and
g = 532. He computes
>> p = 641
p =
641
>> a = 3
a =
3
>> b = 88
b =
88
>> h = 480
h =
480
>> g = 532
g =
532
>> d = mod(powermod(b, h, p) * powermod(h, g, p), p)
d =
191
>> m = 121
m =
121
>> s = powermod(a, m, p)
s =
300
Since s 6 d (mod 641), this should be rejected.
(ii) For sigk (m, r) = (h, g) = (480, 21), Bob uses the previous data except g = 21. So he does
the following computation.
>> g = 21
g =
21
>> d = mod(powermod(b, h, p) * powermod(h, g, p), p)
d =
300
>> s = powermod(a, m, p)
s =
300
As d s (mod 641), Bob accepts it.
14
x 3 (mod 5)
.
x 2 (mod 7)
15
1 2 3
2 3; 4 5 6; 7 8 10]
2
5
8
3
6
10
(B) Example Compute the inverse of M mod 26. To do that, we first compute the inverse
of M as it is a real number matrix. Then converted it to an integer valued matrix. In the last
step, we take mod 26 in every entry, as shown below.
Step 1: Compute the inverse of M . (We need the comment format rat; to out put the
rational numbers. Without it, the output might be decimals, and the next step will not work
out easily.)
>> format rat;
>> Minv = inv(M)
Minv =
-2/3
-2/3
1
-4/3
11/3
-2
1
-2
1
Step 2: Need to rationalize this matrix before we take modulo m. Note that every fractional
entry of Minv has a denominator 3. Multiply everything by 3 to make it an integer valued
matrix. (You can also multiply 27 here as 27 1 (mod 26)).
>> M1=(Minv*3)
M1 =
-2
-4
-2
11
3
-6
3
-6
3
Step 3: Find the inverse of M (mod 26) by modifying M 1. Note that 31 9 (mod 26)
>> M2=round(mod(M1*9, 26))
M2 =
8
16
1
8
21
24
1
24
1
Then M 1 = M 2.
16
1 13 2
A= 3
4 1 .
2 16 1
>> A = [1 13
A =
1
13
3
4
-2 16
2; 3 4 1; -2 16 1]
2
1
1
2 7 1
1 13 2
4 1 0 1 12 .
3
2 0 1
2 16 1
We can first define these matrix and then multiply them together.
>> A = [1 13 2; 3 4 1; -2 16 1]
A =
1
13
2
3
4
1
-2 16
1
>> B = [2 7 1; 0 1 12; -2 0 1]
B =
2
7
1
0
1
12
-2 0
1
>> C = A*B
If we want to compute the matrix multiplication in Zm , for example, computing AB in Z26 ,
then we use the following comment.
>> mod(A*B, 26)
17
14
ans =
15
ans =
16
9
4
1
1
1
2
4
3
9
4
16
5
8
6
2
7
15
8
13
9
13
10
15
11
2
12
8
13
16
14
9
15
4
16
1
ans =
14
ans =
15
ans =
16
4
7
15
= (11, 10)
3P
= (23, 28)
22
4P
= (0, 10)
5P
= (19, 7)
6P
= (18, 19)
7P
= (15, 6)
8P
= (20, 24)
24