Math 373/578: Using matlab in Cryptography (Spring 2012) Place: Room 422, Armstrong Hall Preparations: (1) visit http://www.math.wvu.edu/~hjlai/Math373_Matlab/ and download all the files into a folder (Math373-Matlab) (2) Open Matlab, change the current directory to this folder. (Click the logo at the right end of ”current Directory” for ”Browse for folder”, and change the folder). (3) Start working on the mathematics. (4) Ordinary Computations (Review) Example Compute 2 7 + 135/5. >> 2^7 + 135/5 ans = 155 Example Compute 11(126/(9 + 7) - 2 ( 72/12)). >> 11*(126/9+7 - 2^(72/12)) ans = -473 Example Compute 6 2 - √ 1024. >> 6^2 - \sqrt{1024} ans = 4 1
This document is posted to help you gain knowledge. Please leave a comment to let me know what you think about it! Share it to your friends and learn new things together.
Transcript
Math 373/578: Using matlab in Cryptography (Spring 2012)
Place: Room 422, Armstrong Hall
Preparations:
(1) visit
http://www.math.wvu.edu/~hjlai/Math373_Matlab/
and download all the files into a folder (Math373-Matlab)
(2) Open Matlab, change the current directory to this folder. (Click the logo at the right end
of ”current Directory” for ”Browse for folder”, and change the folder).
(3) Start working on the mathematics.
(4) Ordinary Computations (Review)
Example Compute 27 + 135/5.
>> 2^7 + 135/5
ans =
155
Example Compute 11(126/(9 + 7)− 2(72/12)).
>> 11*(126/9+7 - 2^(72/12))
ans =
-473
Example Compute 62 −√
1024.
>> 6^2 - \sqrt{1024}
ans =
4
1
Part 1: Number Theory Basics
(1) Factor 123456 into primes.
factor(123456)
(2) For x, y ∈ Z− {0}, find gcd(x, y).
gcd(x,y)
Example: Find gcd(23456, 987654).
gcd(23456, 987654)
Ans =
2
Thus 2 = gcd(23456, 987654).
(3) For x, y ∈ Z− {0}, find u, v such that gcd(x, y) = ux+ vy.
[a,b,c]=gcd(x,y)
Example: Find u, v such that gcd(23456, 987654) = 23456u+ 987654v.
>> [d,u,v]=gcd(23456, 987654)
d =
2
u =
-3158
v =
75
This means u = −3158 and v = 75. Thus 2 = gcd(23456, 987654) = 23456·(−3158)+987654·75.
(4) Solve equation ax+ by = 1 when (a, b) = 1.
Example a = 65337 and b = 3511, what is the relationship between this problem and the
example in (3)?
[d,u,v]=gcd(65337, 3511)
How do you interpret the answers?
(5) Knowing n, we can find φ(n) and factor n by
n = 1234567
eulerphi(n)
factor(n)
This can be used for deciphering an RSA coded message in the future. However, Matlab may
have trouble factoring numbers bigger than 109.
2
Part 2: Operations involving integers modulo m(1) Given m > 1 and a ∈ Z, find b ∈ Z with 0 ≤ b < m such that a ≡ b (mod m).
mod (a,m)
(2) Given m > 1 and a, b ∈ Z, find c ∈ Z with 0 ≤ c < m such that a + b ≡ c (mod m). (Do
the same for subtractions and multiplications)
mod (a+b,m)
(3) Find multiplicative inverse of a (mod m) (assuming that we have already known that a and
m are relatively prime).
Example: Find the multiplicative inverse of 8787 (mod 91919).
>> powermod(8787, -1, 91919)
ans =
71374
Thus 8787−1 ≡ 71734 (mod 91919).
(4) Find multiplicative inverse of a (mod m) (assuming that we do not know if a and m are
relatively prime).
Example: Determine if 23456 has an inverse mod 987654, if it does, find it.
>> [d,u,v] = gcd(23456, 987654)
d =
2
u =
-3158
v =
75
This means the gcd(23456, 987654) = 2, and so the inverse does not exist.
Example: Determine if 23456 has an inverse mod 987651, if it does, find it.
>> [d,u,v] = gcd(23456, 987651)
d =
1
u =
256892
v =
-6101
This means the gcd(23456, 987651) = 1 = 256892 · 23456 + (−6101) · 987651, and so 23456−1 ≡256892 (mod 987651).
(5) Find modular exponentiation.
Example: Compute 234567 (mod 9871)
3
>> powermod(234, 567, 9871)
ans =
5334
Thus 234567 ≡ 5334 (mod 9871).
(6) Solving equations.
Example: Solve 7654x ≡ 2389 (mod 65537). What do we do? We first find the multiplicative
inverse of the coefficient of x.
>> powermod(7654, -1, 65537)
ans =
54637
>> mod(ans*2389, 65537)
ans =
43626
Thus the answer is x ≡ 43626 (mod 65537).
(8) Computation modulo m (Reviews)
Example Compute (234)(456) (mod 789).
>> mod(234*456, 789)
ans =
189
Example Compute 234 + 456 (mod 567).
>> mod(234*456, 789)
ans =
123
Example Compute 234567 (mod 9871).
>> powermod(234, 456, 9871)
ans =
5334
Example Compute multiplicative inverse of 8787 (mod 91919).
>> powermod(8787, -1, 91919)
ans =
71374
(9) Shift Ciphers and Affine Ciphers
(A) Decoding with Shift-cipher
Example Decrypt the Ceasar-encrypted message ’wklvverxogehtxlwhhdvb’
4
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), 3−1 ≡ 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
6
Part 3: Numbers with different bases(1) Converting and Base-b number to a base-10 number
To convert a base-b number n = (dk−1dk−2 · · · d1d0)b to base-10, by definition, the answer is
n = n = dk−1bk−1 + dk−2b
k−2 + · · ·+ d1b+ d0.
Example Convert a number-26 number (HPAC)26 to base-10. We can first get the corre-
sponding numerical values H = 7, P = 15, A = 0 and C = 2, and get the answer by
>> n = 7*26^3 + 15*26^2 + 2
n =
133174
(2) Converting and Base-10 number to a base-b number
Example Convert the base-10 number n = 133174 to base-26.
>> n = 133174
n =
133174
>> d0 = mod(133174, 26)
d0 =
2
>> n1 = (n - 2)/26
n1 =
5122
>>d1 = mod(n1, 26)
d1 =
0
>> n2 = (n1 - 0)/26
n2 =
197
>>d2 = mod(n2, 26)
d2 =
15
>> n3 = (n2 - 15)/26
n3 =
7
>>d3 = mod(n3, 26)
d3 =
7
Thus the answer is n = (7 15 0 2)26 = (HPAC)26.
(3) Operations of base-b numbers
There are many ways to perform the operations of base-b numbers. One way to use matlab is
7
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.
8
Part 4: Discrete Log and RSA
(1) Find a Primitive Root
Example Find a primitive root for the prime p = 65537
>> primitiveroot(65537)
ans =
3
Thus 3 is a primitive root for 65537. (Remark: the function ”primitiveroot” finds the small-