0% found this document useful (0 votes)
1K views122 pages

Muïc Luïc: I.3.1. Thaùm Maõ Heä DES - 3 Voøng

The document discusses encryption techniques including shift ciphers, keyword-Caesar ciphers, square ciphers, Vigenere ciphers, and affine ciphers. It provides definitions and mathematical descriptions of how each cipher works by applying encryption and decryption functions to plaintext messages to produce ciphertexts. The document also introduces the concept of a cryptosystem as a set of elements including a plaintext space, ciphertext space, key space, encryption function, and decryption function that satisfies the property that decrypting an encrypted message recovers the original plaintext.

Uploaded by

ngotuannghia1
Copyright
© Attribution Non-Commercial (BY-NC)
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
1K views122 pages

Muïc Luïc: I.3.1. Thaùm Maõ Heä DES - 3 Voøng

The document discusses encryption techniques including shift ciphers, keyword-Caesar ciphers, square ciphers, Vigenere ciphers, and affine ciphers. It provides definitions and mathematical descriptions of how each cipher works by applying encryption and decryption functions to plaintext messages to produce ciphertexts. The document also introduces the concept of a cryptosystem as a set of elements including a plaintext space, ciphertext space, key space, encryption function, and decryption function that satisfies the property that decrypting an encrypted message recovers the original plaintext.

Uploaded by

ngotuannghia1
Copyright
© Attribution Non-Commercial (BY-NC)
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 122

http://thuviendientu.

org

O AN BAO MAT THONG TIN


HE MA DES

MUC LUC
I .1 Gii thieu ....................................................................................................... 3
I.2 Cac He Ma Thong Dung: ................................................................................. 3
e. Phng phap Affine ........................................................................................... 4
f. Phng phap Vigenere ....................................................................................... 5
I.2 LAP MA DES ................................................................................................. 14
I. 3 THAM MA DES............................................................................................ 17
I.3.1. Tham ma he DES - 3 vong ................................................................. 20
II.3.2. Tham ma he DES 6-vong ..................................................................... 24
II.3. 3 Cac tham ma vi sai khac ...................................................................... 28
III.
CAI AT THAM MA DES 3 VONG ...................................................... 28
III.1 Giao Dien .................................................................................................... 28
III.2 X LY ............................................................................................................

NGO TH TUYET HA T012825

http://thuviendientu.org

O AN BAO MAT THONG TIN


HE MA DES

LI NOI AU
Hien nay, nc ta ang trong giai oan tien hanh cong nghiep hoa, hien ai hoa at
nc. Tin hoc c xem la mot trong nhng nganh mui nhon. Tin hoc a va ang ong gop
rat nhieu cho xa hoi trong moi kha canh cua cuoc song.
Ma hoa thong tin la mot nganh quan trong va co nhieu ng dung trong i song xa hoi.
Ngay nay, cac ng dung ma hoa va bao mat thong tin ang c s dung ngay cang pho
bien hn trong cac lnh vc khac nhau tren The gii, t cac lnh vc an ninh, quan s, quoc
phong, cho en cac lnh vc dan s nh thng mai ien t, ngan hang
ng dung ma hoa va bao mat thong tin trong cac he thong thng mai ien t, giao dch
chng khoan, a tr nen pho bien tren the gii va se ngay cang tr nen quen thuoc vi
ngi dan Viet Nam. Thang 7/2000, th trng chng khoan lan au tien c hnh thanh tai
Viet Nam; cac the tn dung bat au c s dung, cac ng dung he thong thng mai ien
t ang bc au c quan tam va xay dng. Do o, nhu cau ve cac ng dung ma hoa va
bao mat thong tin tr nen rat can thiet.

NGO TH TUYET HA T012825

http://thuviendientu.org

I.

O AN BAO MAT THONG TIN


HE MA DES

MOT SO PHNG PHAP MA HOA


I .1 Gii thieu
nh ngha 1.1: Mot he ma mat (cryptosystem) la mot bo-nam (P, C, K, E, D) thoa man
cac ieu kien sau:
1. P la khong gian ban ro. tap hp hu han tat ca cac mau tin nguon can ma hoa co the
co
2. C la khong gian ban ma. tap hp hu han tat ca cac mau tin co the co sau khi ma
hoa
3. K la khong gian khoa. tap hp hu han cac khoa co the c s dung
4. Vi moi khoa k K, ton tai luat ma hoa ek E va luat giai ma dk D tng ng. Luat
ma hoa ek: P
C va luat giai ma ek: C
P la hai anh xa thoa man
dk ek x
x, x P
Tnh chat 4. la tnh chat chnh va quan trong cua mot he thong ma hoa. Tnh chat nay
bao am viec ma hoa mot mau tin x P bang luat ma hoa ek E co the c giai ma
chnh xac bang luat dk D.
nh ngha 1.2: Zm c nh ngha la tap hp {0, 1, ..., m-1}, c trang b phep cong
(ky hieu +) va phep nhan (ky hieu la ). Phep cong va phep nhan trong Zm c thc
hien tng t nh trong Z, ngoai tr ket qua tnh theo modulo m
V du: Gia s ta can tnh gia tr 11 13 trong Z16. Trong Z, ta co ket qua cua phep nhan
11 13=143. Do 143 15 (mod 16) nen 11 13=15 trong Z16.
Mot so tnh chat cua Zm
1. Phep cong ong trong Zm, i.e., a, b Zm, a+b Zm
2. Tnh giao hoan cua phep cong trong Zm, i.e., a, b Zm, a+b =b+a
3. Tnh ket hp cua phep cong trong Zm, i.e., a, b, c Zm, (a+b)+c =a+(b+c)
4. Zm co phan t trung hoa la 0, i.e., a Zm, a+0=0+a=a
5. Moi phan t a trong Zm eu co phan t oi la m a
6. Phep nhan ong trong Zm, i.e., a, b Zm, a b Zm
7. Tnh giao hoan cua phep cong trong Zm, i.e., a, b Zm, a b=b a
8. Tnh ket hp cua phep cong trong Zm, i.e., a, b, c Zm, (a b) c =a (b c)
9. Zm co phan t n v la 1, i.e., a Zm, a 1=1 a=a
10. Tnh phan phoi cua phep nhan oi vi phep cong, i.e.,
a, b, c Zm, (a+b) c
=(a c)+(b c)
11. Zm co cac tnh chat 1, 3 5 nen tao thanh 1 nhom. Do Zm co tnh chat 2 nen tao
thanh nhom Abel. Zm co cac tnh chat (1) (10) nen tao thanh 1 vanh

I.2 Cac He Ma Thong Dung:


a. He Ma ay (Shift Cipher )

NGO TH TUYET HA T012825

http://thuviendientu.org

O AN BAO MAT THONG TIN


HE MA DES

Shift Cipher la mot trong nhng phng phap lau i nhat c s dung e
ma hoa. Thong iep c ma hoa bang cach dch chuyen (xoay vong) tng ky t i k v tr
trong bang ch cai.
Phng phap Shift Cipher
Cho P = C = K = Z26. Vi 0

va
(x,y

25, ta nh ngha

eK = x + K mod 26
dK = y - K mod 26
Z26)

trong o 26 la so ky t trong bang ch cai La tinh, mot cach tng t cung co the
nh ngha cho mot bang ch cai bat ky. ong thi ta de dang thay rang ma ay la mot he
mat ma v dK(eK(x)) = x vi moi x Z26.
b. He KEYWORD-CEASAR
Trong he ma nay khoa la mot t nao o c chon trc, v du PLAIN. T nay
xac nh day so nguyen trong Z26 (15,11,0,8,13) tng ng vi v tr cac ch cai cua cac
ch c chon trong bang ch cai. Bay gi ban ro se c ma hoa bang cach dung cac
ham lap ma theo th t:
e15, e11, e0, e8, e13, e15, e11, e0, e8, e,...
vi eK la ham lap ma trong he ma chuyen.
c. He Ma Vuong (SQUARE)
Trong he nay cac t khoa c dung theo mot cach khac han. Ta dung bang ch
cai tieng Anh (co the bo i ch Q, neu muon tong so cac ch so la mot so chnh phng)
va oi hoi moi ch trong t khoa phai khac nhau. Bay gi moi ch cua bang ch cai
c viet di dang mot hnh vuong, bat au bang t khoa va tiep theo la nhng ch cai
con lai theo th t cua bang ch.
d. Ma the v
Mot he ma khac kha noi tieng . He ma nay a c s dung hang tram nam nay.
Phng phap :
Cho P = C = Z26. K gom tat ca cac hoan v co the co cua 26 ky hieu
0,...,25. Vi moi hoan v
K, ta nh ngha:
e (x) = (x)
va nh ngha
d (y) = -1(y)
vi -1 la hoan v ngc cua hoan v .
Trong ma the v ta co the lay P va C la cac bang ch cai La tinh. Ta s dung Z26
trong ma ay v lap ma va giai ma eu la cac phep toan ai so.
e. Phng phap Affine
Cho P = C = Z26 va cho
NGO TH TUYET HA T012825

http://thuviendientu.org

O AN BAO MAT THONG TIN


HE MA DES

K = {(a,b)
Vi K = (a,b)
va
(x,y

Z26)

Z26 Z26 : gcd(a,26) = 1}


K, ta xac nh
eK(x) = ax+b mod 26
dK = a-1(y-b) mod 26

Phng phap Affine lai la mot trng hp ac biet khac cua Substitution Cipher.
e co the giai ma chnh xac thong tin a c ma hoa bang ham ek E th ek phai la
mot song anh. Nh vay, vi moi gia tr y Z26, phng trnh ax+b y (mod 26) phai co
nghiem duy nhat x Z26.
Phng trnh ax+b y (mod 26) tng ng vi ax (yb ) (mod 26). Vay, ta ch can
khao sat phng trnh ax (yb ) (mod 26)
nh ly1.1: Phng trnh ax+b y (mod 26) co nghiem duy nhat x Z26 vi moi gia tr b Z26
khi va ch khi a va 26 nguyen to cung nhau.
Vay, ieu kien a va 26 nguyen to cung nhau bao am thong tin c ma hoa bang ham ek
co the c giai ma va giai ma mot cach chnh xac.
Goi (26) la so lng phan t thuoc Z26 va nguyen to cung nhau vi 26.
m

nh ly 1.2: Neu n

piei vi pi la cac so nguyen to khac nhau va ei

Z+, 1

m th

i 1
m

piei

piei

i 1

Trong phng phap ma hoa Affine , ta co 26 kha nang chon gia tr b, (26) kha nang chon
gia tr a. Vay, khong gian khoa K co tat ca n (26) phan t.
Van e at ra cho phng phap ma hoa Affine Cipher la e co the giai ma c thong tin
a c ma hoa can phai tnh gia tr phan t nghch ao a1 Z26.
f. Phng phap Vigenere
phng phap ma hoa Vigenere s dung mot t khoa (keyword) co o dai m. Co the xem
nh phng phap ma hoa Vigenere Cipher bao gom m phep ma hoa Shift Cipher c ap
dung luan phien nhau theo chu ky.
Khong gian khoa K cua phng phap Vigenere co so phan t la 26, ln hn han phng
phap so lng phan t cua khong gian khoa K trong phng phap Shift Cipher. Do o, viec
tm ra ma khoa k e giai ma thong iep a c ma hoa se kho khan hn oi vi phng
phap Shift Cipher.

Phng phap ma hoa Vigenere Cipher


Chon so nguyen dng m. nh ngha P = C = K = (Z26)m
K = { (k0, k1, ..., kr-1) (Z26)r}
Vi moi khoa k = (k0, k1, ..., kr-1) K, nh ngha:
NGO TH TUYET HA T012825

http://thuviendientu.org

O AN BAO MAT THONG TIN


HE MA DES

ek(x1, x2, ..., xm) = ((x1+k1) mod 26, (x2+k2) mod n, ..., (xm+km) mod 26)
dk(y1, y2, ..., ym) = ((y1k1) mod n, (y2k2) mod n, ..., (ymkm) mod 26)
vi x, y (Z26)m
g. He ma Hill
Phng phap Hill Cipher c Lester S. Hill cong bo nam 1929: Cho so nguyen dng
m, nh ngha P = C = (Z26)m. Moi phan t x P la mot bo m thanh phan, moi thanh phan
thuoc Z26. Y tng chnh cua phng phap nay la s dung m to hp tuyen tnh cua m thanh
phan trong moi phan t x P e phat sinh ra m thanh phan tao thanh phan t y C.
Phng phap ma hoa Hill Cipher
Chon so nguyen dng m. nh ngha:
P = C = (Z26)m va K la tap hp cac ma tran m m kha nghch
Vi moi khoa k

k1,1

k1, 2

k 2,1

k 2,m

xk

K , nh ngha:

k m , 2 k m ,m

k m ,1

ek x

k1,m

x1 , x2 ,..., xm

k1,1

k1, 2

k 2,1

k 2,m

k m,1

k1,m

vi x=(x1, x2, ..., xm)

k m , 2 k m ,m

va dk(y) = yk1 vi y C
Moi phep toan so hoc eu c thc hien tren Zn

h. Ma hoan v
Nhng phng phap ma hoa neu tren eu da tren y tng chung: thay the moi ky t
trong thong iep nguon bang mot ky t khac e tao thanh thong iep a c ma hoa. Y
tng chnh cua phng phap ma hoan v la van gi nguyen cac ky t trong thong iep
nguon ma ch thay oi v tr cac ky t; noi cach khac thong iep nguon c ma hoa bang
cach sap xep lai cac ky t trong o.

Phng phap ma hoa ma hoan v


Chon so nguyen dng m. nh ngha:
P = C = (Z26)m va K la tap hp cac hoan v cua m phan t {1, 2, ..., m}
Vi moi khoa
K, nh ngha:
e x1 , x2 ,..., xm
x 1 , x 2 ,...x m va

d y1 , y 2 ,..., y m

,y

NGO TH TUYET HA T012825

,...y

http://thuviendientu.org
vi

O AN BAO MAT THONG TIN


HE MA DES

hoan v ngc cua

Phng phap ma hoan v chnh la mot trng hp ac biet cua phng phap Hill. Vi
moi hoan v cua tap hp {1, 2, ..., m} , ta xac nh ma tran k = (ki, j ) theo cong thc sau:

ki, j

1, neu i

0, trong trng hp ngc lai

Ma tran k la ma tran ma moi dong va moi cot co ung mot phan t mang gia tr 1, cac
phan t con lai trong ma tran eu bang 0. Ma tran nay co the thu c bang cach hoan v
cac hang hay cac cot cua ma tran n v Im nen k la ma tran kha nghch. Ro rang, ma hoa
bang phng phap Hill vi ma tran k hoan toan tng ng vi ma hoa bang phng phap
ma hoan v vi hoan v .
d. Ma vong
Trong cac he trc eu cung mot cach thc la cac phan t ke tiep nhau cua ban ro
eu c ma hoa vi cung mot khoa K. Nh vay xau ma y se co dang sau:
y = y1y2... = eK(x1) eK(x2)...
Cac he ma loai nay thng c goi la ma khoi (block cipher).
Con oi vi cac he ma dong. Y tng ay la sinh ra mot chuoi khoa z = z1z2...,
va s dung no e ma hoa xau ban ro x = x1x2...theo qui tac sau:

y1 y 2 ...

ez1 ( x1 )ez2 ( x2 )...

I.3 Quy trnh tham ma:


C moi phng phap ma hoa ta lai co mot phng phap tham ma tng ng nhng
nguyen tac chung e viec tham ma c thanh cong th yeu cau ngi tham ma
phai biet he ma nao c dung hoa. Ngoai ra ta con phai biet c ban ma va ban
ro ng.
nhn chung cac he ma oi xng la de cai at vi toc o thc thi nhanh.
Tnh an toan cua no phu thuoc vao cac yeu to :
Khong gian khoa phai u ln
vi cac phep tron thch hp cac he ma oi xng co the tao ra c mot he
ma mi co tnh an toan cao.
bao mat cho viec truyen khoa cung can c x ly mot cach nghiem tuc.
Va mot he ma hoa d lieu ra i (DES). DES c xem nh la chuan ma hoa d
lieu cho cac ng dung t ngay 15 thang 1 nam 1977 do Uy ban Quoc gia ve Tieu chuan
cua My xac nhan va c 5 nam mot lan lai co chnh sa, bo sung.
DES la mot he ma c tron bi cac phep the va hoan v. vi phep tron thch hp
th viec giai ma no lai la mot bai toan kha kho. ong thi viec cai at he ma nay cho
nhng ng dung thc te lai kha thuan li. Chnh nhng ly do o no c ng dung rong
rai cua DES trong suot hn 20 nam qua, khong nhng tai My ma con la hau nh tren khap
the gii. Mac du theo cong bo mi nhat (nam 1998) th moi he DES, vi nhng kha nang
cua may tnh hien nay, eu co the be khoa trong hn 2 gi. Tuy nhien DES cho en nay
van la mot mo hnh chuan cho nhng ng dung bao mat trong thc te.
II. HE MA CHUAN DES (Data Encryption Standard)

NGO TH TUYET HA T012825

http://thuviendientu.org

O AN BAO MAT THONG TIN


HE MA DES

II.1 ac ta DES
Phng phap DES ma hoa t x co 64 bit vi khoa k co 56 bit thanh mot t co y 64 bit.
Thuat toan ma hoa bao gom 3 giai oan:
1. Vi t can ma hoa x co o dai 64 bit, tao ra t x0 (cung co o dai 64 bit) bang cach
hoan v cac bit trong t x theo mot hoan v cho trc IP (Initial Permutation). Bieu dien
x0 = IP(x) = L0R0, L0 gom 32 bit ben trai cua x0, R0 gom 32 bit ben phai cua x0

L0

R0
x0

Hnh.1 Bieu dien day 64 bit x thanh 2 thanh phan L va R


2. Xac nh cac cap t 32 bit Li, Ri vi 1 i 16theo quy tac sau:
Li = Ri-1
Ri = Li-1 f (Ri-1, Ki)
vi bieu dien phep toan XOR tren hai day bit, K1, K2, ..., K16 la cac day 48 bit phat
sinh t khoa K cho trc (Tren thc te, moi khoa Ki c phat sinh bang cach hoan v
cac bit trong khoa K cho trc).

Li-1

Ri-1

Li

Ki

Ri

Hnh.2 Quy trnh phat sinh day 64 bit LiRi t day 64 bit Li-1Ri-1va khoa Ki
3. Ap dung hoan v ngc IP-1 oi vi day bit R16L16, thu c t y gom 64 bit. Nh
vay, y = IP-1 (R16L16)

NGO TH TUYET HA T012825

http://thuviendientu.org

O AN BAO MAT THONG TIN


HE MA DES

Ham f c s dung bc 2 la
A

E(A)

B1

B2

S1

S2

C1

B3
S3

B4

B5
S5

S4

B7
S6

B8
S7

C2 C3 C4 C5 C6 C7 C8

f(A,J)

NGO TH TUYET HA T012825

B6

S8

http://thuviendientu.org

O AN BAO MAT THONG TIN


HE MA DES

Ham f co gom 2 tham so: Tham so th nhat A la mot day 32 bit, tham so th hai J la
mot day 48 bit. Ket qua cua ham f la mot day 32 bit. Cac bc x ly cua ham f(A, J)nh
sau:
Tham so th nhat A (32 bit) c m rong thanh day 48 bit bang ham m rong E.
Ket qua cua ham E(A) la mot day 48 bit c phat sinh t A bang cach hoan v theo
mot th t nhat nh 32 bit cua A, trong o co 16 bit cua A c lap lai 2 lan trong
E(A).
Thc hien phep toan XOR cho 2 day 48 bit E(A) va J, ta thu c mot day 48 bit B.
Bieu dien B thanh tng nhom 6 bit nh sau:B = B1B2B3B4B5B6B7B8
S dung 8 ma tran S1, S2,..., S8, moi ma tran Si co kch thc 4 16 va moi dong cua
ma tran nhan u 16 gia tr t 0 en 15. Xet day gom 6 bit
Bj = b1b2b3b4b5b6,
Sj(Bj) c xac nh bang gia tr cua phan t tai dong r cot c cua Sj, trong o, ch so
dong r co bieu dien nh phan la b1b6, ch so cot c co bieu dien nh phan la b2b3b4b5.
Bang cach nay, ta xac nh c cac day 4 bit Cj = Sj(Bj), 1 j 8.
Tap hp cac day 4 bit Cj lai. ta co c day 32 bit C = C1C2C3C4C5C6C7C8. Day 32
bit thu c bang cach hoan v C theo mot quy luat P nhat nh chnh la ket qua cua
ham F(A, J)
cac ham c s dung trong DES.
Hoan v khi tao IP se nh sau:

IP
58
60
62
64
57
59
61
63

50
52
54
56
49
51
53
55

42
44
46
48
41
43
45
47

34
36
38
40
33
35
37
39

26
28
30
32
25
27
29
31

18
20
22
24
17
19
21
23

10
12
14
16
9
11
13
15

2
4
6
8
1
3
5
7

ieu nay co ngha la bit th 58 cua x la bit au tien cua IP(x); bit th 50 cua x la
bit th hai cua IP(x) v.v.
Hoan v ngc IP-1 se la:

40
39
38
37
36
35
34

8
7
6
5
4
3
2

NGO TH TUYET HA T012825

48
47
46
45
44
43
42

IP-1
16
15
14
13
12
11
10

56
55
54
53
52
51
50

24
23
22
21
20
19
18

64
63
62
61
60
59
58

32
31
30
29
28
27
26

http://thuviendientu.org
33

O AN BAO MAT THONG TIN


HE MA DES
1

41

49

17

57

25

Ham m rong E c ac ta theo bang sau:

E bang chon bit


32
4
8
12
16
20
24
28

1
5
9
13
17
21
25
29

2
6
10
14
18
22
26
30

3
7
11
15
19
23
27
31

4
8
12
16
20
24
28
32

5
9
13
17
21
25
29
1

Tam S-hop va hoan v P se c bieu dien nh sau:

14
0
4
15

15
3
0
13

10
13
13
1

7
13
10

4
15
1
12

1
13
14
8

0
7
6
10

13
8
6

13
7
14
8

8
4
7
10

9
0
4
13

14
11
9

1
4
8
2

14
7
11
1

14
9
9
0

3
5
0

2
14
13
4

6
15
10
3

6
3
8
6

0
6
12

NGO TH TUYET HA T012825

15
2
6
9

11
2
4
15

3
4
15
9

6
15
11

11
13
2
1

S1
8
3
1 10
11 15
7
5

10
6
12
11

6
12
9
3

12
11
7
14

5
9
3
10

9
5
10
0

0
3
5
6

3
8
13
4

S2
4
9
14 12
1
5
2 11

7
0
8
6

2
1
12
7

13
10
6
12

12
6
9
0

0
9
3
5

5
11
2
14

10
5
15
9

15
6
3
8

S3
5
1
10
2
0 11
7
4

13
8
1
15

12
5
2
14

7
14
12
3

11
12
5
11

4
11
10
5

2
15
14
2

8
1
7
12

9
0
7

S4
10
1
3
4
13 15

2
7
1

8
2
3

5
12
14

11
1
5

12
10
2

4
14
8

15
9
4

7
8
0
13

http://thuviendientu.org
3

2
14
4
11

15

12
11
2
8

4
2
1
12

1
12
11
7

O AN BAO MAT THONG TIN


HE MA DES
10

7
4
10
0

10
7
13
14

13

11
13
7
2

11

12

14

S5
6
8
1
5
8 15
13
6

5
0
9
15

3
15
12
0

15
10
5
9

13
3
6
10

0
9
3
4

14
8
0
5

9
6
14
3

0
6
7
11

13
1
0
14

3
13
4
1

4
14
10
7

14
0
1
6

7
11
13
0

5
3
11
8

11
8
6
13

S6
12
10
9
4

1
15
14
3

10
4
15
2

15
2
5
12

9
7
2
9

2
12
8
5

6
9
12
15

8
5
3
10
S7

4
13
1
6

13
1
7
2

11
0
4
11

2
15
11
1

2
11
11
13

8
13
4
14

14
7
13
8

4
8
1
7

15
4
12
1

6
10
9
4

0
9
3
4

15
3
12
10

8
1
7
10

13
10
14
7

3
14
10
9

12
3
15
5

9
5
6
0

7
12
8
15

5
2
0
14

10
15
5
2

6
8
9
3

1
6
2
12

11
7
14
8

S8
1 10
4 12
2
0
13 15

9
5
6
12

3
6
10
9

14
11
13
0

5
0
15
3

0
14
3
5

12
9
5
6

7
2
8
11

P
16
29
1
5
2
32
19
22

7
12
15
18
8
27
13
11

20
28
23
31
24
3
30
4

21
17
26
10
14
9
6
25

K la xau co o dai 64 bit, trong o co 56 bit dung lam khoa va 8 bit dung e kiem
tra s bang nhau (e phat hien loi). Cac bit cac v tr 8, 16, ..., 64 c xac nh, sao cho

NGO TH TUYET HA T012825

http://thuviendientu.org

O AN BAO MAT THONG TIN


HE MA DES

moi byte cha so le cac so 1. V vay, tng loi co the c phat hien trong moi 8 bit. Cac
bit kiem tra s bang nhau la c bo qua khi tnh lch khoa.
1. Cho khoa 64 bit K, loai bo cac bit kiem tra va hoan v cac bit con lai cua K
tng ng vi hoan v (co nh) PC-1. Ta viet PC-1(K) = C0D0, vi C0 bao gom 28 bit au
tien cua PC-1(K) va D0 la 28 bit con lai.
2. Vi i nam trong khoang t 1 en 16, ta tnh
Ci = LSi(Ci-1)
Di = LSi(Di-1)
va Ki = PC-2(CiDi), LSi bieu dien phep chuyen chu trnh (cyclic shift) sang trai hoac cua
mot hoac cua hai v tr tuy thuoc vao tr cua i; ay mot v tr neu i = 1, 2, 9 hoac 16 va
ay 2 v tr trong nhng trng hp con lai. PC-2 la mot hoan v co nh khac.
Viec tnh lch khoa c minh hoa nh hnh ve sau:

K
PC-1
C0

D0

LS1

LS1

PC-1

PC-1

C1

D1

LS2

LS2

PC-2

K1

PC-2

K16

...

LS16

LS16

C16

D16

Cac hoan v PC-1 va PC-2 c s dung trong viec tnh lch khoa la nh sau:

PC-1
57
1
10

49
58
2

NGO TH TUYET HA T012825

41
50
59

33
42
51

25
34
43

17
26
35

9
18
27

http://thuviendientu.org
19
63
7
14
21

O AN BAO MAT THONG TIN


HE MA DES
11
55
62
6
13

34
7
54
61
5

60
39
46
53
28

52
31
38
45
20

44
23
30
37
12

36
15
22
29
4

PC-2
14
3
23
16
41
30
44
46

17
28
19
7
50
40
49
42

11
15
12
27
31
51
39
50

24
6
4
20
37
45
56
36

1
21
26
13
47
33
34
29

5
10
8
2
55
48
53
32

Bay gi ta se hien th ket qua viec tnh lch khoa. Nh a nhan xet tren, moi
vong s dung khoa 48 bit tng ng vi 48 bit trong K. Cac thanh phan trong cac bang sau
se ch ra cac bit trong K c s dung trong cac vong khac nhau.
I.2 LAP MA DES
ay la v du ve viec lap ma s dung DES. Gia s ta ma hoa ban ro sau trong dang
thap luc phan (Hexadecimal)
0123456789ABCDEF
s dung khoa thap luc phan
133457799BBCDFF1
Khoa trong dang nh phan khong co cac bit kiem tra se la:
00010010011010010101101111001001101101111011011111111000.
Ap dung IP, ta nhan c L0 va R0 (trong dang nh phan) :
L0
L 1 = R0

=
=

11001100000000001100110011111111
11110000101010101111000010101010

16 vong lap ma c the hien nh sau:


E(R0) = 011110100001010101010101011110100001010101010101
K1 = 000110110000001011101111111111000111000001110010
E(R0) K1 = 011000010001011110111010100001100110010100100111
Output S-hop = 01011100100000101011010110010111
f(R0,K1) = 00100011010010101010100110111011
L2 = R1 = 11101111010010100110010101000100
E(R1) =
K2 =
E(R1) K2 =

011101011110101001010100001100001010101000001001
011110011010111011011001110110111100100111100101
000011000100010010001101111010110110001111101100

NGO TH TUYET HA T012825

http://thuviendientu.org
Output S-hop =
f(R1, K2) =
L 3 = R2 =

O AN BAO MAT THONG TIN


HE MA DES
11111000110100000011101010101110
00111100101010111000011110100011
11001100000000010111011100001001

E(R2)
K3
E(R2) K3
S-box output
f(R2, K3)
L 4 = R3

=
=
=
=
=
=

111001011000000000000010101110101110100001010011
010101011111110010001010010000101100111110011001
101100000111110010001000111110000010011111001010
00100111000100001110000101101111
01001101000101100110111010110000
10100010010111000000101111110100

E(R3)
K4
E(R3) K4
S-box output
f(R3, K4)
L 5 = R4

=
=
=
=
=
=

010100000100001011111000000001010111111110101001
011100101010110111010110110110110011010100011101
001000101110111100101110110111100100101010110100
00100001111011011001111100111010
10111011001000110111011101001100
011101110

E(R4)
K5
E(R4) K5
Xuat S-hop
f(R4, K5)
L 6 = R5

=
=
=
=
=
=

101110101110100100000100000000000000001000001010
011111001110110000000111111010110101001110101000
110001100000010100000011111010110101000110100010
01010000110010000011000111101011
00101000000100111010110111000011
10001010010011111010011000110111

E(R5)
K6
E(R5) K6
S-box output
f(R5, K6)
L 7 = R6

=
=
=
=
=
=

110001010100001001011111110100001100000110101111
011000111010010100111110010100000111101100101111
101001101110011101100001100000001011101010000000
01000001111100110100110000111101
10011110010001011100110100101100
11101001011001111100110101101001

E(R6)
K7
E(R6) K7
S-box output
f(R6, K7)
L 8 = R7

=
=
=
=
=
=

111101010010101100001111111001011010101101010011
111011001000010010110111111101100001100010111100
000110011010111110111000000100111011001111101111
00010000011101010100000010101101
10001100000001010001110000100111
00000110010010101011101000010000

E(R7)
K8
E(R7) K8
S-box output

=
=
=
=

000000001100001001010101010111110100000010100000
111101111000101000111010110000010011101111111011
111101110100100001101111100111100111101101011011
01101100000110000111110010101110

NGO TH TUYET HA T012825

http://thuviendientu.org
f(R7, K8) =
L 9 = R8 =
E(R8)
K9
E(R8) K9
S-box output
f(R8, K9)
L10 = R9
E(R9)
K10
E(R9) K10
S-box output
f(R9, K10)
L11 = R10
E(R10)
K11
E(R10) K11
S-box output
f(R10, K11)
L12 = R11

O AN BAO MAT THONG TIN


HE MA DES

00111100000011101000011011111001
11010101011010010100101110010000

=
=
=
=
=
=

011010101010101101010010101001010111110010100001
111000001101101111101011111011011110011110000001
100010100111000010111001010010001001101100100000
00010001000011000101011101110111
00100010001101100111110001101010
00100100011111001100011001111010

=
=
=
=
=
=
=
=
=
=
=
=

000100001000001111111001011000001100001111110100
101100011111001101000111101110100100011001001111
101000010111000010111110110110101000010110111011
11011010000001000101001001110101
01100010101111001001110000100010
10110111110101011101011110110010
010110101111111010101011111010101111110110100101
001000010101111111010011110111101101001110000110
011110111010000101111000001101000010111000100011
01110011000001011101000100000001
11100001000001001111101000000010
11000101011110000011110001111000

E(R11)
K12
E(R11) K12
S-box output
f(R11, K12)
L13 = R12

011000001010101111110000000111111000001111110001
011101010111000111110101100101000110011111101001
000101011101101000000101100010111110010000011000
01111011100010110010011000110101
11000010011010001100111111101010
01110101101111010001100001011000

E(R12)
K13
E(R12) K13
S-box output
f(R12, K13)
L14 = R13

=
=
=
=
=
=

001110101011110111111010100011110000001011110000
100101111100010111010001111110101011101001000001
101011010111100000101011011101011011100010110001
10011010110100011000101101001111
11011101101110110010100100100010
00011000110000110001010101011010

E(R13)
K14
E(R13) K14
S-box output
f(R13, K14)
L15 = R14

=
=
=
=
=
=

000011110001011000000110100010101010101011110100
010111110100001110110111111100101110011100111010
010100000101010110110001011110000100110111001110
01100100011110011001101011110001
10110111001100011000111001010101
11000010100011001001011000001101

NGO TH TUYET HA T012825

http://thuviendientu.org

E(R14)
K15
E(R14) K15
S-box output
f(R14, K15)
L16 = R15

=
=
=
=
=
=

O AN BAO MAT THONG TIN


HE MA DES

111000000101010001011001010010101100000001011011
101111111001000110001101001111010011111100001010
010111111100010111010100011101111111111101010001
10110010111010001000110100111100
01011011100000010010011101101110
01000011010000100011001000110100

E(R15) = 001000000110101000000100000110100100000110101000
K16 = 110010110011110110001011000011100001011111110101
E(R15) K16 = 111010110101011110001111000101000101011001011101
S-box output = 10100111100000110010010000101001
f(R15, K16) = 11001000110000000100111110011000
R16 = 00001010010011001101100110010101
Cuoi cung, ap dung IP-1 cho R16L16 ta nhan c ban ma trong dang thap luc phan
nh sau:
85E813540F0AB405

I. 3 THAM MA DES
Mot phng phap rat noi tieng trong tham ma DES la tham ma vi sai
(differential cryptanalysic) do Biham va Shamir e xuat. o la phng phap tham vi ban
ro c chon. No khong c s dung trong thc te e tham ma DES 16 vong, ma ch
c s dung e tham cac he DES co t vong hn.
Bay gi ta se mo ta nhng y tng c ban cua ky thuat nay. e at muc ch tham
ma, ta co the bo qua hoan v khi tao IP va hoan v ao cua no (bi v ieu o khong can
thiet cho viec tham ma). Nh a nhan xet tren, ta xet cac he DES n vong, vi n 16.
Trong cai at ta co the coi L0R0 la ban ro va LnRn nh la ban ma.
Tham ma vi sai oi hoi phai so sanh x-or (exclusive-or) cua hai ban ro vi x-or cua
hai ban ma tng ng. Noi chung, ta se quan sat hai ban ro L0R0 va L0*R0* vi tr x-or
c ac ta L0R0 = L0R0 L0*R0*. Trong nhng thao luan sau ta se s dung ky hieu ()
e ch x-or cua hai xau bit.
nh ngha 3.1: Cho Sj la mot S-hop (1 j 8). Xet mot cap xau 6-bit la (Bj,Bj* ).
Ta noi rang, xau nhap x-or (cua Sj) la Bj Bj* va xau xuat x-or (cua Sj) la Sj(Bj) Sj(Bj*).
Chu y la xau nhap x-or la xau bit co o dai 6, con xau xuat x-or co o dai 4.
nh ngha 3.2: Vi bat ky Bj (Z2) 6, ta nh ngha tap (Bj) gom cac cap
(Bj,Bj*) co x-or nhap la Bj.
De dang thay rang, bat ky tap (Bj) nao cung co 26 = 64 cap, va do o
(Bj) = {(Bj, Bj

NGO TH TUYET HA T012825

Bj) : Bj

(Z2) 6 }

http://thuviendientu.org

O AN BAO MAT THONG TIN


HE MA DES

Vi moi cap trong (Bj), ta co the tnh xau x-or xuat cua Sj va lap c phan bo
ket qua. Co 64 xau xuat x-or, c phan bo trong 24 = 16 gia tr co the co. Tnh khong
ong eu cua cac phan bo o la c s e ma tham.
V du 3.1: Gia s ta xet S1 la S-hop au tien va xau nhap x-or la 110100. Khi o
(110100) = {(000000, 110100), (000001, 110101), ..., (111111, 001011)}
Vi moi cap trong tap (110100), ta tnh xau xuat x-or cua S1. Chang han,
S1(000000) = E16 = 1110, S1(110100) = 1001,
nh vay xau xuat x-or cho cap (000000,110100) la 0111.
Neu thc hien ieu o cho 64 cap trong (110100) th ta nhan c phan bo cua
cac xau x-or xuat sau:
000 000 001 001 010 010 011 011 100 100 101 101 110 110 111 111
0
1
0
1
0
1
0
1
0
1
0
1
0
1
0
1
0
8 16 6
2
0
0 12 6
0
0
0
0
8
0
6
Trong v du 3.1, ch co 8 trong so 16 xau x-or xuat co the co xuat hien that s. V
du cu the nay a ch ra s phan bo rat khong eu cua cac xau x-or xuat. Noi chung, neu ta
co nh S-hop Sj va xau nhap x-or Bj, th trung bnh co khoang 75 - 80% cac xau x-or
xuat co the co xuat hien thc s.
e mo ta cac phan bo o ta a ra nh ngha sau.
nh ngha 3.3: Vi 1

ngha:
va

8 va vi cac xau bit Bj o dai 6 va Cj o dai 4, ta nh

INj(Bj,Cj) = {Bj

(Z2)6 : Sj(Bj)

Sj(Bj

Bj) = Cj}

Nj(Bj, Cj) = INj(Bj, Cj)


Bang sau se cho cac xau nhap co the co vi xau x-or nhap 110100
Xau xuat x-or
0000
0001

0010

0011
0100
0101
0110

Cac xau nhap co the co


000011, 001111, 011110, 011111
101010, 101011, 110111, 111011
000100, 000101, 001110, 010001
010010, 010100, 011010, 011011
100000, 100101, 010110, 101110
101111, 110000, 110001, 111010
000001, 000010, 010101, 100001
110101, 110110
010011, 100111

000000, 001000, 001101, 010111


NGO TH TUYET HA T012825

http://thuviendientu.org

O AN BAO MAT THONG TIN


HE MA DES
0111
1000
1001
1010
1011
1100
1101
1110
1111

011000, 011101, 100011, 101001


101100, 110100, 111001, 111100
001001, 001100, 011001, 101101
111000, 111101

000110, 010000, 010110, 011100


110010, 100100, 101000, 110010
000111, 001010, 001011, 110011
111110, 111111

Nj(Bj, Cj) tnh so cac cap vi xau nhap x-or bang Bj co xau xuat x-or bang Cj
vi S-hop Sj. Cac cap o co cac xau nhap x-or c ac ta va a ra cach tnh cac xau
xuat x-or co the nhan c t tap INj(Bj, Cj). e y rang, tap nay co the phan thanh
Nj(Bj, Cj) /2 cap, moi cap co xau x-or nhap bang Bj.
Phan bo trong v du 3.1 cha cac tr N1(110100, C1), C1 (Z2)4. Trong bang tren
cha cac tap IN(110100, C1).
Vi moi tam S-hop, co 64 xau nhap x-or co the co. Nh vay, co 512 phan bo co
the tnh c. Nhac lai la, xau nhap cho S-hop vong th i la B= E J, vi E = E(Ri-1) la
m rong cua Ri-1 va J = Ki gom cac bit khoa cua vong i. Bay gi xau nhap x-or (cho tat ca
tam S-hop) co the tnh c nh sau:
B B* = (E J) (E* J) = E E*
ieu nay rat quan trong e thay rang, xau nhap x-or khong phu thuoc vao cac bit
khoa J. (Do o, xau xuat x-or cung khong phu thuoc vao cac bit khoa.)
Ta se viet moi B, E va J nh la noi cua tam xau 6-bit:
B = B1B2B3B4B5B6B7B8
E = E1E2E3E4E5E6E7E8
J = J1J2J3J4J5J6J7J8
*
va ta cung se viet B va E* nh vay. Bay gi gia s la ta a biet cac tr Ej va Ej* vi mot j
nao o, 1 j 8, va tr cua xau xuat x-or cho Sj, Cj = Sj(Bj)
Sj(Bj* ). Khi o se la:
Ej Jj INj(Ej, Cj),
*
vi Ej = Ej
Ej .
nh ngha 3.4: Gia s Ej va Ej* la cac xau bit o dai 6, va Cj la xau bit o dai 4. Ta nh

ngha:

vi Ej = Ej

nh ly 3.1:

testj(Ej, Ej*, Cj) = { Bj


Ej*.

Ej : Bj

INj(Ej, Cj) },

Gia s Ej va Ej* la hai xau nhap cho S-hop Sj, va xau xuat x-or cho Sj la Cj. Ky
hieu Ej = Ej Ej* . Khi o cac bit khoa Jj co trong tap testj(Ej, Ej*, Cj).
NGO TH TUYET HA T012825

http://thuviendientu.org

O AN BAO MAT THONG TIN


HE MA DES

e y, o chnh la cac xau bit Nj(Ej, Cj) o dai 6 trong tap testj(Ej, Ej*, Cj); gia tr
chnh xac cua Jj phai la mot trong so o.

V du 3.2:

Gia s E1 = 000001, E1*= 110101 va C1= 1101. Do o N1(110101,1101) = 8, ung bang 8


xau bit trong tap test1(000001, 110101, 1101). T bang tren ta thay rang
IN1(110100, 1101) = {000110, 010000, 010110, 011100, 100010, 100100, 101000,
110010}
Cho nen
test1(000001, 110101,1101) = {000111, 010001, 010111, 011101, 100011, 100101,
101001, 110011}
Neu ta co mot bo ba th hai nh the E1, E1*, C1, khi o ta se nhan c tap th
hai test1 cua cac tr cho cac bit khoa trong J1. Tr ung cua J1 can phai nam trong giao cua
cac S-hop. Neu ta co mot vai bo ba nh vay, khi o ta co the mau chong tm c cac bit
khoa trong J1. Mot cach ro rang hn e thc hien ieu o la lap mot bang cua 64 bo em
bieu dien cho 64 kha nang cua cua 6 khoa bit trong J1. Bo em se tang moi lan, tng ng
vi s xuat hien cua cac bit khoa trong tap test1 cho mot bo ba cu the. Cho t bo ba, ta hy
vong tm c duy nhat mot bo em co tr t; tr o se tng ng vi tr ung cua cac bit
khoa trong J1.

I.3.1. Tham ma he DES - 3 vong


Bay gi ta se xet y tng va trnh bay cho viec tham ma he DES - ba vong. Ta se
bat au vi cap ban ro va cac ban ma tng ng: L0R0, L0*R0*, L3R3 va L3*R3*. Ta co the
bieu dien R3 nh sau:
R3 = L2 f(R2, K3)
= R1 f(R2, K3)
= L0 f(R0, K1) f(R2, K3)
R3* co the bieu dien mot cach tng t , do vay:
R3 = L0
f(R0, K1)
f(R0*, K1)
f(R2, K3)
f(R2*, K3)
Bay gi, gia s ta a chon c cac ban ro sao cho R0 = R0*, chang han:
R0 = 00...0
Khi o f(R0, K1) = f(R0*, K1), va do o:
R3 = L0 f(R2, K3)
f(R2*, K3)
iem nay R3 la c biet khi no co the tnh c t hai ban ma, va L0 la biet
c khi no co the tnh c t hai ban ro. Ngha la ta co the tnh c f(R2,K3) f(R2*,K3)
t phng trnh:
f(R2, K3) f(R2*, K3) = R3 L0
Bay gi f(R2, K3) = P(C) va f(R2*, K3) = P(C*), vi C va C* tng ng la ky hieu
cua hai xau xuat cua tam S-hop (nhac lai, P la co nh, la hoan v c biet cong khai).
Nen:
P(C)
P(C*) = R3 L0
va ket qua la:
C = C C* = P-1(R3
L0)
(1)

NGO TH TUYET HA T012825

http://thuviendientu.org

O AN BAO MAT THONG TIN


HE MA DES

o la xau xuat x-or cho tam S-hop trong vong ba.


Bay gi, R2 = L3 va R2* = L3* la a biet (chung la mot phan cua cac ban ma). T
ay ta co the tnh:
E = E(L3)
(2)
va
E* = E(L3*)
(3)
s dung ham m rong E c biet cong khai. Chung la nhng xau nhap cho cac S-hop cho
vong ba. Nh vay gi ta a biet E, E*, va C cho vong ba va ta co the tiep tuc xay dng
cac tap test1, ..., test8 cua cac tr co the co cho cac bit khoa trong J1, ..., J8.
Giai thuat va xet co the bieu dien bi cac ma sau:
Input: L0R0, L0*R0*, L3R3 va L3*R3*, vi R0 = R0*
1. Tnh C = P-1(R3
L0)
*
2. Tnh E = E(L3) va E = E(L*)
3. for j = 1 to 8 do
compute testj(Ej, Ej*, Cj)
Viec ma tham se s dung mot so bo ba E, E*, C nh vay. Ta se lap tam bang cac
bo em va do o xac nh c 48 bit trong K3, la khoa cho vong ba. 56 bit trong khoa khi
o co the tm c hoan toan t 28 = 256 kha nang cho 8 bit khoa.
Bay gi ta se minh hoa ieu o qua v du sau.

V du 3.3

Gia s ta co ba cap ban ro va ban ma, vi cac ban ma cung co cac xau x-or c
ma hoa bi cung mot khoa. e ngan gon ta s dung he thap luc phan:
Ban ma

Ban ro

748502CD38451097
3874756438451097
486911026ACDFF31
375BD31F6ACDFF31
357418DA013FEC86
12549847013FEC86

03C70306D8A09F10
78560A0960E6D4CB
45FA285BE5ADC730
134F7915AC253457
D8A31B2F28BBC5CF
0F317AC2B23CB944

T cap au tien ta tnh cac xau nhap cua S-hop (cho vong 3) t cac phng trnh
(2) va (3). Chung la:
E = 000000000111111000001110100000000110100000001100
E* = 101111110000001010101100000001010100000001010010
Xau xuat x-or cua S-hop c tnh bang phng trnh (1) se la:
C = 10010110010111010101101101100111
T cap th hai, ta tnh c cac xau nhap cho S-hop la:
E = 101000001011111111110100000101010000001011110110
E* = 100010100110101001011110101111110010100010101001
va xau xuat x-or cua S-hop la:
NGO TH TUYET HA T012825

http://thuviendientu.org

O AN BAO MAT THONG TIN


HE MA DES

C = 10011100100111000001111101010110
T cap th ba, cac xau nhap cho S-hop se la:
E = 111011110001010100000110100011110110100101011111
E* = 000001011110100110100010101111110101011000000100
va xau xuat x-or cua S-hop la:
C = 11010101011101011101101100101011
Tiep theo, ta lap bang cac tr trong tam mang bo em cho moi cap. Ta se minh hoa
thu tuc vi cac mang em cho J1 t cap au tien. Trong cap nay, ta co E1= 101111 va C1
= 1001. Tap:
IN1(101111, 1001) = {000000, 000111, 101000, 101111}
Do E1 = 000000 ta co:
J1 test1(000000, 101111, 1001) = {000000, 000111, 101000, 101111}
Do o ta tang cac tr 0, 7, 40 va 47 trong cac mang em cho J1.
Cuoi cung ta se trnh bay cac bang. Neu ta xem cac xau bit o dai 6 nh la bieu
dien cua cac so nguyen trong khoang 0-63, th 64 tr se tng ng vi 0, 1, ..., 63. Cac
mang em se la nh sau:
J1
1
0
0
0

0
0
1
0

0
0
0
0

0
0
0
0

0
0
0
0

1
1
1
0

0
1
0
0

1
0
0
0

0
0
1
0

0
0
0
0

0
0
0
0

0
1
0
0

0
1
0
0

0
0
0
0

0
0
0
0

0
0
3
1

1
0
1
1

0
0
0
0

0
0
1
1

1
0
0
0

0
1
0
2

0
0
0
0

0
0
1
0

0
0
0
0

0
0
0
0

0
0
0
0

0
0
0
0

0
0
0
0

0
0
1
1

0
0
1
0

1
1
0
0

0
1
0
0

0
0
0
0

0
0
1
0

2
0
1
0

2
0
1
0

0
1
0
0

0
0
0
0

0
1
1
2

0
1
0
1

J2
0
0
0
0

0
1
0
0

0
0
0
1

1
0
0
1

0
0
0
0

3
2
1
0

0
0
0
0

0
0
0
0
J3

0
0
0
0

0
0
2
0

0
0
0
0

0
3
0
0

1
0
0
0

1
0
0
0

0
0
0
1

0
0
0
0
J4

3
0
1
0

1
0
1
0

0
0
1
0

0
0
0
0

0
1
1
1

0
1
0
1

0
0
0
0

0
0
0
0
J5

NGO TH TUYET HA T012825

http://thuviendientu.org
0
0
0
0

0
0
0
0

0
0
0
2

O AN BAO MAT THONG TIN


HE MA DES
0
0
0
0

0
2
0
0

0
0
0
0

1
0
0
0

0
0
0
0

0
3
0
0

0
0
0
1

1
0
0
0

0
0
0
0

0
0
0
0

0
0
0
0

0
0
0
2

0
0
0
0

0
0
0
0

0
0
0
0

0
0
0
0

0
0
0
0

1
0
0
0

0
0
0
0

0
0
0
0

1
0
0
0

0
0
0
0

0
0
0
0

0
0
0
0

1
1
1
0

1
0
2
0

0
0
1
0

0
0
1
1

0
1
0
1

0
0
1
0

0
0
0
0

0
0
1
0

0
0
0
0

0
0
0
0

0
0
1
0

0
0
0
0

0
0
1
0

J6
1
0
0
1

0
0
0
0

0
0
0
0

1
0
0
1

1
1
1
1

0
1
1
0

0
0
0
1

3
0
1
1
J7

0
0
0
0

0
1
0
0

2
0
2
0

1
0
0
0

0
0
0
0

1
0
0
0

0
0
2
0

3
0
0
0
J8

0
0
0
0

0
0
0
3

0
0
0
0

0
0
0
0

0
0
0
0

0
0
0
0

0
0
0
1

0
0
0
0

Trong moi tam mang em, co duy nhat mot bo em co tr la 3. V tr cua cac bo
em o xac nh cac bit khoa trong J1, ..., J8. Cac v tr o la: 47, 5, 19, 0, 24, 7, 7, 49.
Chuyen cac so nguyen o sang dang nh phan, ta nhan c J1, ..., J8:
J1 = 101111
J2 = 000101
J3 = 010011
J4 = 000000
J5 = 011000
J6 = 000111
J7 = 000111
J8 = 110001
Bay gi ta co the tao ra 48 bit khoa, bang cach quan sat lch khoa cho vong ba. Suy
ra la K co dang:
0001101 0110001 01?01?0 1?00100
0101001 0000??0 111?11? ?100011
vi cac bit kiem tra a c loai bo va ? ky hieu bit khoa cha biet. Khoa ay u (trong
dang thap luc phan, gom ca bit kiem tra) se la:
1A624C89520DEC46

NGO TH TUYET HA T012825

http://thuviendientu.org

O AN BAO MAT THONG TIN


HE MA DES

II.3.2. Tham ma he DES 6-vong


Bay gi ta se mo ta viec m rong y tng tren cho viec tham ma tren he DES 6-vong. Y
tong ay la la chon mot cach can than cap ban ro vi xau x-or ac thu va sau o xac
nh cac xac suat cua cac day ac thu cua cac xau x-or qua cac vong lap ma. Bay gi ta
can nh ngha mot khai niem quan trong sau.
nh ngha 3.5: Cho n
dang

1 la so nguyen. ac trng cua vong th n la mot danh sach cac

L0, R0, L1, R1, p1, ..., Ln, Rn, pn


thoa man cac ieu kien sau:
1. Li = Ri-1 vi 1 i n
2. Cho 1 i n va Li-1, Ri-1 va L*i-1, R*i-1 la a c chon sao cho Li-1 L*i-1 = Li-1 va
Ri-1 R*i-1 = Ri-1. Gia s Li, Ri va Li* , Ri* la tnh c nh viec ap dung mot vong
lap ma DES. Khi o xac suat e Li L*i = Li va Ri R*i = Ri chnh xac bang pi.
(Chu y la, xac suat nay c tnh tren tat ca cac bo co the co cua J = J1...J8) .
Xac suat ac trng c nh ngha bang tch p = p1 ... pn.

Nhan xet: Gia s ta chon L0, R0 va L0*, R0* sao cho L0

L0* = L0 va R0 R0*= R0 va
ta ap dung n vong lap ma cua DES, nhan c L1. ..., Ln va R1, ..., Rn. Khi o ta khong the
oi hoi xac suat e Li Li* = Li va Ri Ri* = Ri cho tat ca i ( 1 i n) la p1 ... pn.
Bi v cac bo -48 trong lch khoa K1, ..., Kn khong phai la oc lap lan nhau. (Neu n bo-48
nay uc chon oc lap mot cach ngau nhien, th ieu xac nhan la ung). Nhng ta se coi
rang p1 ... pn chnh xac la xac xuat o.
Ta con can xac nhan la, cac xac suat pi trong ac trng la cac cap ban ro c xac nh
tuy y (nhng co nh) c ac ta bang xau x-or, vi 48 bit khoa cho mot vong lap ma
DES la co 248 kha nang. Do o viec tham ma se nham vao viec xac nh khoa co nh
(nhng cha biet). Do o can co chon cac ban ma ngau nhien (nhng chung co cac xau xor c ac ta), hy vong rang cac xac suat e cac xau x-or trong n vong lap ma trung hp
vi cac xau x-or, c ac ta trong ac trng, tng oi mot p1, ..., pn tng ng.
Trong v du sau ay, ta se trnh bay ac trng vong 1 e lam c s cho viec tham ma DES
ba vong trong hnh sau (nh tren, ta se s dung cach bieu dien theo he thap luc phan).
L0 =
L1 =

bat ky
0000000016

R0 =
R1 =

0000000016
L0

p=1

Ta cung se mo ta mot ac trng vong 1 khac nh sau


L0 =
L1 =

0000000016
6000000016

NGO TH TUYET HA T012825

R0 =
R1 =

6000000016
0080820016

p = 14/64

http://thuviendientu.org

O AN BAO MAT THONG TIN


HE MA DES

Ta hay xet ac trng sau mot cach chi tiet hn. Khi f(R0, K1) va f(R0*, K1) c tnh, bc
au tien la m rong R0 va R0*. Xau x-or ket qua cua hai m rong la:
001100...0
Tc la xau x-or nhap cho S1 la 001100 va cac xau x-or cho bay S-hop khac eu la 000000.
Cac xau xuat x-or cho S2 en S8 eu la 0000. Xau xuat x-or cho S1 la 1110 vi xac suat
14/64 (do N1(001100, 1110) = 14). Nen ta nhan c:
C = 11100000000000000000000000000000
vi xac suat 14/64. Ap dung P, ta nhan c:
P(C)
P(C*) = 00000000100000001000001000000000
trong dang thap luc phan se la 0080820016. Khi xau nay cong x-or vi L0, ta nhan c
R1 vi xac suat 14/64. Do o L1 = R0.
Viec tham ma DES sau vong da tren ac trng ba vong c cho trong hnh sau.
Trong tham ma 6-vong, ta bat au vi L0R0. L0*R0*, L6R6 va L6*R6*, ma ta phai chon ban
ro sao cho L0= 4008000016 va R.0= 0400000016, ta co the bieu dien R0 nh sau:
L0
L1
L2
L3

=
=
=
=

4008000016
R0 = 0400000016
0400000016
R1 = 0000000016 p = 1/4
0000000016
R2 = 0400000016 p = 1
0400000016
R3 = 4008000016 p = 1/4
R6 = L5 f(R5, K6)
= R4
f(R5, K6)
= L3
f(R3, K4)
f(R5, K6)
*
R6 cung co the bieu dien tng t, ta co
R0 = L3
f(R3, K4)
f(R3*, K4)
f(R5, K6)
f(R5*, K6)
(4)
(e y la tng t nh tham ma 3-vong)
R6 la c biet. T ac trng ta tnh L3 = 0400000016 va R3 = 4008000016 vi xac suat
1/16. Neu nh vay, th xau nhap x-or cho S-hop trong vong 4 co the tnh c nh ham
m rong phai la:
001000000000000001010000...0
Cac xau x-or cho S2, S5, S6, S7 va S8 tat ca eu bang 000000, va v the xau xuat x-or la
0000 cho tat ca nam S-hop o trong vong 4. ieu nay co ngha la, ta co the tnh c cac
xau xuat x-or cho nam S-hop o trong vong 6 nh phng trnh (4). Do o gia s ta tnh:
C1C2C3C4C5C6C7C8 = P-1(R6
04000000)
moi Ci la xau bit co o dai 4. Khi o vi xac suat 1/16, th se dan en la C2, C5, C6, C7
va C8 tng ng la cac xau x-or xuat cua S2, S5, S6, S7 va S8 trong vong 6. Cac xau nhap
cho cac S-hop o trong vong 6 co the tnh c la E2, E5, E6, E7 va E8; va E2*, E5*, E6*, E7*
va E8*, vi
E1E2E3E4E5E6E7E8 = E(R5) = E(L6)
va
E1*E2*E3*E4*E5*E6*E7*E8* = E(R5*) = E(L6*)
co the tnh c t cac ban ro nh sau:
NGO TH TUYET HA T012825

http://thuviendientu.org

O AN BAO MAT THONG TIN


HE MA DES

Input: L0R0, L0*R0*, L6R6 va L6*R6*; vi L0 = 4008000016


va R0 = 0400000016.
1. Tnh C = P-1(R6 0400000016)
2. Tnh E = E(L6) va E* = E(L6*)
3. for j {2,5,6,7,8} do
tnh testj( Ej, Ej*, Cj)
Ta cung se xac nh 30 bit khoa trong J2, J5, J6, J7 va J8 nh trong tham ma 3-vong.
Bai toan, e xau xuat x-or gia nh cho vong 6 la chnh xac ch vi xac suat 1/16. Con
15/16 phan con lai ta se thng nhan c nhng xau vo dung ngau nhien hn la cac bit
khoa.
nh ngha 3.6: Gia s L0 L0* = L0 va R0 R0*= R0. Ta noi rang, cap ban ro L0R0 va
L0* R0* la ung (right) ng vi ac trng neu Li Li* = Li va Ri Ri*= Ri cho moi i, 1 i
n. Cap trai vi cap c nh ngha goi la cap sai (wrong).
Ta mong rang, khoang 1/16 so cap cua ta la ung, con cac cap con lai la cap sai ng vi
ac trng vong ba cua ta.
Chien lc cua ta la tnh Ej. Ej* va Cjnh a mo ta tren va sau o xac nh testj(Ej, Ej*,
Cj) vi j = 2,5,6,7,8. Neu ta bat au vi mot cap ung, th th cac bit khoa chnh xac cho
moi Jj se nam trong tap testj. Neu cap la sai, th tr Cj se khong ung, va o la nguyen do
e gia nh rang, moi tap testj thc chat la ngau nhien.
Ta co the nhan ra cap ung theo phng phap sau: Neu testj = 0, vi bat ky j
{2,5,6,7,8}, khi o ta tat yeu co c cap ung. Bay gi cho mot cap sai, ta co the hy
vong rang, xac suat e testj = 0 cho mot j cu the la xap x 1/5. o la ly do e gia nh la,
Nj(Ej, Cj) = testj va nh a nhan xet t trc, xac suat e Nj(Ej, Cj) = 0 la xap x 1/5.
Xac suat e ca nam testj eu dng la vao khoang 0.85 0.33, qua vay xac suat e t nhat
mot testj bang 0 la vao khoang 0.67. Nen ta co khoang 2/3 so cap la sai, nh vao mot
nhan xet n gian, c goi la phep loc (filtering operation). Ty so cua cac cap ung tren
cac cap con lai sau phep loc la vao khoang:

1 16
16
1 16 15 16 1 3
V du 3.4: Gia s ta co cap ban ro - ban ma sau:
Ban ro
86FA1C2B1F51D3BE
C6F21C2B1B51D3BE

Ban ma
1E23ED7F2F553971
296DE2B687AC6340

Chu y la, L0 = 4008000016 va R0 = 0400000016. Xau nhap va xau xuat cua S-hop cho
vong 6 c tnh nh sau:

NGO TH TUYET HA T012825

http://thuviendientu.org

O AN BAO MAT THONG TIN


HE MA DES
j
2
5
6
7
8

Ej*
010010
111100
000101
010110
101100

Ej
111100
111101
011010
101111
111110

C j
1101
0001
0010
1100
1101

Khi o cac tap testj se la nh sau:

testj

j
2
5
6
7
8

14, 15,26, 30, 32, 33, 48, 52


7, 24, 36, 41, 54, 59
34, 35, 48, 49

Ta thay rang, hai tap test5 va test7 la rong , nen cap nay la cap sai va no b loai bo bang
phep loc.
Bay gi gia s ta co cap sao cho testj > 0 vi j = 2,5,6,7,8 la nhng tap con lai sau phep
loc.(Bi v ta khong biet c la cap nao ung, cap nao sai.) Ta noi rang, xau bit J2J5J6J7J8
o dai 30 la c e xuat bi cap neu Jj testj vi j = 2,5,6,7,8. So cac cap c e xuat
la:

test j
j 2 , 5, 6 , 7 , 8

o la bnh thng vi so xau bit c e xuat la kha ln. (Chang han. ln hn


80000)
Gia s, ta lap bang cho tat ca cac xau c e xuat nhan c t N cap, ma khong b loai
bi phep loc. Vi moi cap ung, th xau bit ung J2J5J6J7J8 se la xau c e xuat. Xau bit
ung se c tnh khoang 3N/16 lan. Xau bit sai thng xuat hien t hn, bi v chung
xuat hien ngau nhien va co khoang 230 kha nang. (La mot so rat ln.)
Ta nhan c mot bang cc ln tat ca cac xau c e xuat, nen ta s dung mot
thuat toan ch oi hoi mot khong gian va thi gian t nhat. Ta co the ma hoa bat ky mot
tap testj nao thanh mot vec t Tj co o dai 64, vi toa o th i cua Tj c at bang 1 (0
i 63), neu xau bit o dai 6 la bieu dien cua i trong tap testj; va toa o th i c at
bang 0 trong trng hp ngc lai ( ieu nay giong nh mang cac bo em ma ta a s
dung trong tham ma DES ba vong).
Vi moi cap con lai, ta xay dng cac vec t nh tren va goi chung la Tji,
j=2,5,6,7,8; 1 i N. Vi I {1, ..., N} ta noi rang I la chap nhan c (allowable) neu
vi moi j {2,5,6,7,8} co t nhat mot toa o bang I trong vec t

NGO TH TUYET HA T012825

http://thuviendientu.org

O AN BAO MAT THONG TIN


HE MA DES

T ji
i I

Neu cap th i la cap ung cho moi i I, th tap I la chap nhan c. Do o ta cho
rang tap chap nhan c co kch thc (xap x) 3N/16, la tap e xuat va ta hy vong la ch
gom cac bit khoa ung ch khong co cac xau khac. ieu nay lam n gian hoa cho viec
xay dng tat ca cac tap chap nhan c I bang mot thuat toan e qui.

II.3. 3 Cac tham ma vi sai khac


Phng phap tham ma vi sai con co the ap dung e tham cac he DES nhieu vong
hn. Vi he DES 8-vong oi hoi 214 ban ro chon va cac he 10-, 12-, 14- va 16-vong oi
hoi co tng ng 224, 231, 239 va 247 ban ma chon. Nen noi chung la kha phc tap.
Cac ky thuat tham ma vi sai c Biham va Shamir phat trien. Cac phng phap tham ma
DES khac a c Matsui s dung nh la tham ma tuyen tnh.

III. HE MA DES 3 VONG


Chng trnh gom hai phan:
Phan Giao Dien (cha trong th muc GiaoDien): Co chc nang x ly
giao dien.

Phan X Ly (cha trong th muc XuLy): co chc nang ho tr cac


ham x ly.

III.1 Giao Dien ( Package GiaoDien).


a. Man hnh chnh (Mainform.vb)

Form lap ma va giai ma DES(Des.vb)


NGO TH TUYET HA T012825

http://thuviendientu.org

O AN BAO MAT THONG TIN


HE MA DES

Source code mot so ham chnh trong form giai ma Des


Imports System.IO
Public Class des Inherits System.Windows.Forms.Form
khai bao bien
Dim str As String
Dim s(7) As DataTable
Dim ip() As String
'Dim iptru() As String
Dim e() As String
Dim p() As String
Dim pc1() As String
Dim pc2() As String
Dim daykhoa(15) As String
Dim x As String
Dim daynhap(29) As String

NGO TH TUYET HA T012825

http://thuviendientu.org

O AN BAO MAT THONG TIN


HE MA DES

Dim daybanma(29) As String


khoi tao
Sub khoitao_s0()
Dim i As Integer
s(0) = New DataTable
For i = 0 To 15
Dim col As DataColumn = New DataColumn
s(0).Columns.Add(col)
Next
For i = 0 To 3
Dim row As DataRow = s(0).NewRow
s(0).Rows.Add(row)
Next
s(0).Rows(0).Item(0) = 14
s(0).Rows(0).Item(1) = 4
s(0).Rows(0).Item(2) = 13
s(0).Rows(0).Item(3) = 1
s(0).Rows(0).Item(4) = 2
s(0).Rows(0).Item(5) = 15
s(0).Rows(0).Item(6) = 11
s(0).Rows(0).Item(7) = 8
s(0).Rows(0).Item(8) = 3
s(0).Rows(0).Item(9) = 10
s(0).Rows(0).Item(10) = 6
s(0).Rows(0).Item(11) = 12
s(0).Rows(0).Item(12) = 5
s(0).Rows(0).Item(13) = 9
s(0).Rows(0).Item(14) = 0

NGO TH TUYET HA T012825

http://thuviendientu.org

O AN BAO MAT THONG TIN


HE MA DES

s(0).Rows(0).Item(15) = 7
s(0).Rows(1).Item(0) = 0
s(0).Rows(1).Item(1) = 15
s(0).Rows(1).Item(2) = 7
s(0).Rows(1).Item(3) = 4
s(0).Rows(1).Item(4) = 14
s(0).Rows(1).Item(5) = 2
s(0).Rows(1).Item(6) = 13
s(0).Rows(1).Item(7) = 1
s(0).Rows(1).Item(8) = 10
s(0).Rows(1).Item(9) = 6
s(0).Rows(1).Item(10) = 12
s(0).Rows(1).Item(11) = 11
s(0).Rows(1).Item(12) = 9
s(0).Rows(1).Item(13) = 5
s(0).Rows(1).Item(14) = 3
s(0).Rows(1).Item(15) = 8
s(0).Rows(2).Item(0) = 4
s(0).Rows(2).Item(1) = 1
s(0).Rows(2).Item(2) = 14
s(0).Rows(2).Item(3) = 8
s(0).Rows(2).Item(4) = 13
s(0).Rows(2).Item(5) = 6
s(0).Rows(2).Item(6) = 2
s(0).Rows(2).Item(7) = 11
s(0).Rows(2).Item(8) = 15
s(0).Rows(2).Item(9) = 12
s(0).Rows(2).Item(10) = 9

NGO TH TUYET HA T012825

http://thuviendientu.org

O AN BAO MAT THONG TIN


HE MA DES

s(0).Rows(2).Item(11) = 7
s(0).Rows(2).Item(12) = 3
s(0).Rows(2).Item(13) = 10
s(0).Rows(2).Item(14) = 5
s(0).Rows(2).Item(15) = 0
s(0).Rows(3).Item(0) = 15
s(0).Rows(3).Item(1) = 12
s(0).Rows(3).Item(2) = 8
s(0).Rows(3).Item(3) = 2
s(0).Rows(3).Item(4) = 4
s(0).Rows(3).Item(5) = 9
s(0).Rows(3).Item(6) = 1
s(0).Rows(3).Item(7) = 7
s(0).Rows(3).Item(8) = 5
s(0).Rows(3).Item(9) = 11
s(0).Rows(3).Item(10) = 3
s(0).Rows(3).Item(11) = 14
s(0).Rows(3).Item(12) = 10
s(0).Rows(3).Item(13) = 0
s(0).Rows(3).Item(14) = 6
s(0).Rows(3).Item(15) = 13
dgs0.DataSource = s(0)
End Sub
Ham khoi tao s1
Sub khoitao_s1()
Dim i As Integer
s(1) = New DataTable
For i = 0 To 15

NGO TH TUYET HA T012825

http://thuviendientu.org

O AN BAO MAT THONG TIN


HE MA DES

Dim col As DataColumn = New DataColumn


s(1).Columns.Add(col)
Next
For i = 0 To 3
Dim row As DataRow = s(1).NewRow
s(1).Rows.Add(row)
Next
s(1).Rows(0).Item(0) = 15
s(1).Rows(0).Item(1) = 1
s(1).Rows(0).Item(2) = 8
s(1).Rows(0).Item(3) = 14
s(1).Rows(0).Item(4) = 6
s(1).Rows(0).Item(5) = 11
s(1).Rows(0).Item(6) = 3
s(1).Rows(0).Item(7) = 4
s(1).Rows(0).Item(8) = 9
s(1).Rows(0).Item(9) = 7
s(1).Rows(0).Item(10) = 2
s(1).Rows(0).Item(11) = 13
s(1).Rows(0).Item(12) = 12
s(1).Rows(0).Item(13) = 0
s(1).Rows(0).Item(14) = 5
s(1).Rows(0).Item(15) = 10
s(1).Rows(1).Item(0) = 3
s(1).Rows(1).Item(1) = 13
s(1).Rows(1).Item(2) = 4
s(1).Rows(1).Item(3) = 7
s(1).Rows(1).Item(4) = 15

NGO TH TUYET HA T012825

http://thuviendientu.org

O AN BAO MAT THONG TIN


HE MA DES

s(1).Rows(1).Item(5) = 2
s(1).Rows(1).Item(6) = 8
s(1).Rows(1).Item(7) = 14
s(1).Rows(1).Item(8) = 12
s(1).Rows(1).Item(9) = 0
s(1).Rows(1).Item(10) = 1
s(1).Rows(1).Item(11) = 10
s(1).Rows(1).Item(12) = 6
s(1).Rows(1).Item(13) = 9
s(1).Rows(1).Item(14) = 11
s(1).Rows(1).Item(15) = 5
s(1).Rows(2).Item(0) = 0
s(1).Rows(2).Item(1) = 14
s(1).Rows(2).Item(2) = 7
s(1).Rows(2).Item(3) = 11
s(1).Rows(2).Item(4) = 10
s(1).Rows(2).Item(5) = 4
s(1).Rows(2).Item(6) = 13
s(1).Rows(2).Item(7) = 1
s(1).Rows(2).Item(8) = 5
s(1).Rows(2).Item(9) = 8
s(1).Rows(2).Item(10) = 12
s(1).Rows(2).Item(11) = 6
s(1).Rows(2).Item(12) = 9
s(1).Rows(2).Item(13) = 3
s(1).Rows(2).Item(14) = 2
s(1).Rows(2).Item(15) = 15
s(1).Rows(3).Item(0) = 13

NGO TH TUYET HA T012825

http://thuviendientu.org

O AN BAO MAT THONG TIN


HE MA DES

s(1).Rows(3).Item(1) = 8
s(1).Rows(3).Item(2) = 10
s(1).Rows(3).Item(3) = 1
s(1).Rows(3).Item(4) = 3
s(1).Rows(3).Item(5) = 15
s(1).Rows(3).Item(6) = 4
s(1).Rows(3).Item(7) = 2
s(1).Rows(3).Item(8) = 11
s(1).Rows(3).Item(9) = 6
s(1).Rows(3).Item(10) = 7
s(1).Rows(3).Item(11) = 12
s(1).Rows(3).Item(12) = 0
s(1).Rows(3).Item(13) = 5
s(1).Rows(3).Item(14) = 14
s(1).Rows(3).Item(15) = 9
dgs1.DataSource = s(1)
End Sub
Ham khoi tao s2
Sub khoitao_s2()
Dim i As Integer
s(2) = New DataTable
For i = 0 To 15
Dim col As DataColumn = New DataColumn
s(2).Columns.Add(col)
Next
For i = 0 To 3
Dim row As DataRow = s(2).NewRow
s(2).Rows.Add(row)

NGO TH TUYET HA T012825

http://thuviendientu.org

O AN BAO MAT THONG TIN


HE MA DES

Next
s(2).Rows(0).Item(0) = 10
s(2).Rows(0).Item(1) = 0
s(2).Rows(0).Item(2) = 9
s(2).Rows(0).Item(3) = 14
s(2).Rows(0).Item(4) = 6
s(2).Rows(0).Item(5) = 3
s(2).Rows(0).Item(6) = 15
s(2).Rows(0).Item(7) = 5
s(2).Rows(0).Item(8) = 1
s(2).Rows(0).Item(9) = 13
s(2).Rows(0).Item(10) = 12
s(2).Rows(0).Item(11) = 7
s(2).Rows(0).Item(12) = 11
s(2).Rows(0).Item(13) = 4
s(2).Rows(0).Item(14) = 2
s(2).Rows(0).Item(15) = 8
s(2).Rows(1).Item(0) = 13
s(2).Rows(1).Item(1) = 7
s(2).Rows(1).Item(2) = 0
s(2).Rows(1).Item(3) = 9
s(2).Rows(1).Item(4) = 3
s(2).Rows(1).Item(5) = 4
s(2).Rows(1).Item(6) = 6
s(2).Rows(1).Item(7) = 10
s(2).Rows(1).Item(8) = 2
s(2).Rows(1).Item(9) = 8
s(2).Rows(1).Item(10) = 5

NGO TH TUYET HA T012825

http://thuviendientu.org

O AN BAO MAT THONG TIN


HE MA DES

s(2).Rows(1).Item(11) = 14
s(2).Rows(1).Item(12) = 12
s(2).Rows(1).Item(13) = 11
s(2).Rows(1).Item(14) = 15
s(2).Rows(1).Item(15) = 1
s(2).Rows(2).Item(0) = 13
s(2).Rows(2).Item(1) = 6
s(2).Rows(2).Item(2) = 4
s(2).Rows(2).Item(3) = 9
s(2).Rows(2).Item(4) = 8
s(2).Rows(2).Item(5) = 15
s(2).Rows(2).Item(6) = 3
s(2).Rows(2).Item(7) = 0
s(2).Rows(2).Item(8) = 11
s(2).Rows(2).Item(9) = 1
s(2).Rows(2).Item(10) = 2
s(2).Rows(2).Item(11) = 12
s(2).Rows(2).Item(12) = 5
s(2).Rows(2).Item(13) = 10
s(2).Rows(2).Item(14) = 14
s(2).Rows(2).Item(15) = 7
s(2).Rows(3).Item(0) = 1
s(2).Rows(3).Item(1) = 10
s(2).Rows(3).Item(2) = 13
s(2).Rows(3).Item(3) = 0
s(2).Rows(3).Item(4) = 6
s(2).Rows(3).Item(5) = 9
s(2).Rows(3).Item(6) = 8

NGO TH TUYET HA T012825

http://thuviendientu.org

O AN BAO MAT THONG TIN


HE MA DES

s(2).Rows(3).Item(7) = 7
s(2).Rows(3).Item(8) = 4
s(2).Rows(3).Item(9) = 15
s(2).Rows(3).Item(10) = 14
s(2).Rows(3).Item(11) = 3
s(2).Rows(3).Item(12) = 11
s(2).Rows(3).Item(13) = 5
s(2).Rows(3).Item(14) = 3
s(2).Rows(3).Item(15) = 12
dgs2.DataSource = s(2)
End Sub
Ham khi tao s3
Sub khoitao_s3()
Dim i As Integer
s(3) = New DataTable
For i = 0 To 15
Dim col As DataColumn = New DataColumn
s(3).Columns.Add(col)
Next
For i = 0 To 3
Dim row As DataRow = s(3).NewRow
s(3).Rows.Add(row)
Next
s(3).Rows(0).Item(0) = 7
s(3).Rows(0).Item(1) = 13
s(3).Rows(0).Item(2) = 14
s(3).Rows(0).Item(3) = 3
s(3).Rows(0).Item(4) = 0

NGO TH TUYET HA T012825

http://thuviendientu.org

O AN BAO MAT THONG TIN


HE MA DES

s(3).Rows(0).Item(5) = 6
s(3).Rows(0).Item(6) = 9
s(3).Rows(0).Item(7) = 10
s(3).Rows(0).Item(8) = 1
s(3).Rows(0).Item(9) = 2
s(3).Rows(0).Item(10) = 8
s(3).Rows(0).Item(11) = 5
s(3).Rows(0).Item(12) = 11
s(3).Rows(0).Item(13) = 12
s(3).Rows(0).Item(14) = 4
s(3).Rows(0).Item(15) = 15
s(3).Rows(1).Item(0) = 13
s(3).Rows(1).Item(1) = 8
s(3).Rows(1).Item(2) = 11
s(3).Rows(1).Item(3) = 5
s(3).Rows(1).Item(4) = 6
s(3).Rows(1).Item(5) = 15
s(3).Rows(1).Item(6) = 0
s(3).Rows(1).Item(7) = 3
s(3).Rows(1).Item(8) = 4
s(3).Rows(1).Item(9) = 7
s(3).Rows(1).Item(10) = 2
s(3).Rows(1).Item(11) = 12
s(3).Rows(1).Item(12) = 1
s(3).Rows(1).Item(13) = 10
s(3).Rows(1).Item(14) = 14
s(3).Rows(1).Item(15) = 9
s(3).Rows(2).Item(0) = 10

NGO TH TUYET HA T012825

http://thuviendientu.org

O AN BAO MAT THONG TIN


HE MA DES

s(3).Rows(2).Item(1) = 6
s(3).Rows(2).Item(2) = 9
s(3).Rows(2).Item(3) = 0
s(3).Rows(2).Item(4) = 12
s(3).Rows(2).Item(5) = 11
s(3).Rows(2).Item(6) = 7
s(3).Rows(2).Item(7) = 13
s(3).Rows(2).Item(8) = 15
s(3).Rows(2).Item(9) = 1
s(3).Rows(2).Item(10) = 3
s(3).Rows(2).Item(11) = 14
s(3).Rows(2).Item(12) = 5
s(3).Rows(2).Item(13) = 2
s(3).Rows(2).Item(14) = 8
s(3).Rows(2).Item(15) = 4
s(3).Rows(3).Item(0) = 3
s(3).Rows(3).Item(1) = 15
s(3).Rows(3).Item(2) = 0
s(3).Rows(3).Item(3) = 6
s(3).Rows(3).Item(4) = 10
s(3).Rows(3).Item(5) = 1
s(3).Rows(3).Item(6) = 13
s(3).Rows(3).Item(7) = 8
s(3).Rows(3).Item(8) = 9
s(3).Rows(3).Item(9) = 4
s(3).Rows(3).Item(10) = 5
s(3).Rows(3).Item(11) = 11
s(3).Rows(3).Item(12) = 12

NGO TH TUYET HA T012825

http://thuviendientu.org

O AN BAO MAT THONG TIN


HE MA DES

s(3).Rows(3).Item(13) = 7
s(3).Rows(3).Item(14) = 2
s(3).Rows(3).Item(15) = 14
dgs3.DataSource = s(3)
End Sub
Ham khi tao s4
Sub khoitao_s4()
Dim i As Integer
s(4) = New DataTable
For i = 0 To 15
Dim col As DataColumn = New DataColumn
s(4).Columns.Add(col)
Next
For i = 0 To 3
Dim row As DataRow = s(4).NewRow
s(4).Rows.Add(row)
Next
s(4).Rows(0).Item(0) = 2
s(4).Rows(0).Item(1) = 12
s(4).Rows(0).Item(2) = 4
s(4).Rows(0).Item(3) = 1
s(4).Rows(0).Item(4) = 7
s(4).Rows(0).Item(5) = 10
s(4).Rows(0).Item(6) = 11
s(4).Rows(0).Item(7) = 6
s(4).Rows(0).Item(8) = 8
s(4).Rows(0).Item(9) = 5
s(4).Rows(0).Item(10) = 3

NGO TH TUYET HA T012825

http://thuviendientu.org

O AN BAO MAT THONG TIN


HE MA DES

s(4).Rows(0).Item(11) = 15
s(4).Rows(0).Item(12) = 13
s(4).Rows(0).Item(13) = 0
s(4).Rows(0).Item(14) = 14
s(4).Rows(0).Item(15) = 9
s(4).Rows(1).Item(0) = 14
s(4).Rows(1).Item(1) = 11
s(4).Rows(1).Item(2) = 2
s(4).Rows(1).Item(3) = 12
s(4).Rows(1).Item(4) = 4
s(4).Rows(1).Item(5) = 7
s(4).Rows(1).Item(6) = 13
s(4).Rows(1).Item(7) = 1
s(4).Rows(1).Item(8) = 5
s(4).Rows(1).Item(9) = 0
s(4).Rows(1).Item(10) = 15
s(4).Rows(1).Item(11) = 10
s(4).Rows(1).Item(12) = 3
s(4).Rows(1).Item(13) = 9
s(4).Rows(1).Item(14) = 8
s(4).Rows(1).Item(15) = 6
s(4).Rows(2).Item(0) = 4
s(4).Rows(2).Item(1) = 2
s(4).Rows(2).Item(2) = 1
s(4).Rows(2).Item(3) = 11
s(4).Rows(2).Item(4) = 10
s(4).Rows(2).Item(5) = 13
s(4).Rows(2).Item(6) = 7

NGO TH TUYET HA T012825

http://thuviendientu.org

O AN BAO MAT THONG TIN


HE MA DES

s(4).Rows(2).Item(7) = 8
s(4).Rows(2).Item(8) = 15
s(4).Rows(2).Item(9) = 9
s(4).Rows(2).Item(10) = 12
s(4).Rows(2).Item(11) = 5
s(4).Rows(2).Item(12) = 6
s(4).Rows(2).Item(13) = 3
s(4).Rows(2).Item(14) = 0
s(4).Rows(2).Item(15) = 14
s(4).Rows(3).Item(0) = 11
s(4).Rows(3).Item(1) = 8
s(4).Rows(3).Item(2) = 12
s(4).Rows(3).Item(3) = 7
s(4).Rows(3).Item(4) = 0
s(4).Rows(3).Item(5) = 14
s(4).Rows(3).Item(6) = 2
s(4).Rows(3).Item(7) = 13
s(4).Rows(3).Item(8) = 6
s(4).Rows(3).Item(9) = 15
s(4).Rows(3).Item(10) = 0
s(4).Rows(3).Item(11) = 9
s(4).Rows(3).Item(12) = 10
s(4).Rows(3).Item(13) = 4
s(4).Rows(3).Item(14) = 5
s(4).Rows(3).Item(15) = 3
dgs4.DataSource = s(4)
End Sub
Ham khi tao S5

NGO TH TUYET HA T012825

http://thuviendientu.org

O AN BAO MAT THONG TIN


HE MA DES

Sub khoitao_s5()
Dim i As Integer
s(5) = New DataTable
For i = 0 To 15
Dim col As DataColumn = New DataColumn
s(5).Columns.Add(col)
Next
For i = 0 To 3
Dim row As DataRow = s(5).NewRow
s(5).Rows.Add(row)
Next
s(5).Rows(0).Item(0) = 12
s(5).Rows(0).Item(1) = 1
s(5).Rows(0).Item(2) = 10
s(5).Rows(0).Item(3) = 15
s(5).Rows(0).Item(4) = 9
s(5).Rows(0).Item(5) = 2
s(5).Rows(0).Item(6) = 6
s(5).Rows(0).Item(7) = 8
s(5).Rows(0).Item(8) = 0
s(5).Rows(0).Item(9) = 13
s(5).Rows(0).Item(10) = 3
s(5).Rows(0).Item(11) = 4
s(5).Rows(0).Item(12) = 14
s(5).Rows(0).Item(13) = 7
s(5).Rows(0).Item(14) = 5
s(5).Rows(0).Item(15) = 11
s(5).Rows(1).Item(0) = 10

NGO TH TUYET HA T012825

http://thuviendientu.org

O AN BAO MAT THONG TIN


HE MA DES

s(5).Rows(1).Item(1) = 15
s(5).Rows(1).Item(2) = 4
s(5).Rows(1).Item(3) = 2
s(5).Rows(1).Item(4) = 7
s(5).Rows(1).Item(5) = 12
s(5).Rows(1).Item(6) = 9
s(5).Rows(1).Item(7) = 5
s(5).Rows(1).Item(8) = 6
s(5).Rows(1).Item(9) = 1
s(5).Rows(1).Item(10) = 13
s(5).Rows(1).Item(11) = 14
s(5).Rows(1).Item(12) = 0
s(5).Rows(1).Item(13) = 11
s(5).Rows(1).Item(14) = 3
s(5).Rows(1).Item(15) = 8
s(5).Rows(2).Item(0) = 9
s(5).Rows(2).Item(1) = 14
s(5).Rows(2).Item(2) = 15
s(5).Rows(2).Item(3) = 5
s(5).Rows(2).Item(4) = 2
s(5).Rows(2).Item(5) = 8
s(5).Rows(2).Item(6) = 12
s(5).Rows(2).Item(7) = 3
s(5).Rows(2).Item(8) = 7
s(5).Rows(2).Item(9) = 0
s(5).Rows(2).Item(10) = 4
s(5).Rows(2).Item(11) = 10
s(5).Rows(2).Item(12) = 1

NGO TH TUYET HA T012825

http://thuviendientu.org

O AN BAO MAT THONG TIN


HE MA DES

s(5).Rows(2).Item(13) = 13
s(5).Rows(2).Item(14) = 11
s(5).Rows(2).Item(15) = 6
s(5).Rows(3).Item(0) = 4
s(5).Rows(3).Item(1) = 3
s(5).Rows(3).Item(2) = 2
s(5).Rows(3).Item(3) = 12
s(5).Rows(3).Item(4) = 9
s(5).Rows(3).Item(5) = 5
s(5).Rows(3).Item(6) = 15
s(5).Rows(3).Item(7) = 10
s(5).Rows(3).Item(8) = 11
s(5).Rows(3).Item(9) = 14
s(5).Rows(3).Item(10) = 1
s(5).Rows(3).Item(11) = 7
s(5).Rows(3).Item(12) = 6
s(5).Rows(3).Item(13) = 0
s(5).Rows(3).Item(14) = 8
s(5).Rows(3).Item(15) = 13
dgs5.DataSource = s(5)
End Sub
Ham khi tao S6
Sub khoitao_s6()
Dim i As Integer
s(6) = New DataTable
For i = 0 To 15
Dim col As DataColumn = New DataColumn
s(6).Columns.Add(col)

NGO TH TUYET HA T012825

http://thuviendientu.org

O AN BAO MAT THONG TIN


HE MA DES

Next
For i = 0 To 3
Dim row As DataRow = s(6).NewRow
s(6).Rows.Add(row)
Next
s(6).Rows(0).Item(0) = 4
s(6).Rows(0).Item(1) = 11
s(6).Rows(0).Item(2) = 2
s(6).Rows(0).Item(3) = 14
s(6).Rows(0).Item(4) = 15
s(6).Rows(0).Item(5) = 0
s(6).Rows(0).Item(6) = 8
s(6).Rows(0).Item(7) = 13
s(6).Rows(0).Item(8) = 3
s(6).Rows(0).Item(9) = 12
s(6).Rows(0).Item(10) = 9
s(6).Rows(0).Item(11) = 7
s(6).Rows(0).Item(12) = 5
s(6).Rows(0).Item(13) = 10
s(6).Rows(0).Item(14) = 6
s(6).Rows(0).Item(15) = 1
s(6).Rows(1).Item(0) = 13
s(6).Rows(1).Item(1) = 0
s(6).Rows(1).Item(2) = 11
s(6).Rows(1).Item(3) = 7
s(6).Rows(1).Item(4) = 4
s(6).Rows(1).Item(5) = 9
s(6).Rows(1).Item(6) = 1

NGO TH TUYET HA T012825

http://thuviendientu.org

O AN BAO MAT THONG TIN


HE MA DES

s(6).Rows(1).Item(7) = 10
s(6).Rows(1).Item(8) = 14
s(6).Rows(1).Item(9) = 3
s(6).Rows(1).Item(10) = 5
s(6).Rows(1).Item(11) = 12
s(6).Rows(1).Item(12) = 2
s(6).Rows(1).Item(13) = 15
s(6).Rows(1).Item(14) = 8
s(6).Rows(1).Item(15) = 6
s(6).Rows(2).Item(0) = 1
s(6).Rows(2).Item(1) = 4
s(6).Rows(2).Item(2) = 11
s(6).Rows(2).Item(3) = 13
s(6).Rows(2).Item(4) = 12
s(6).Rows(2).Item(5) = 3
s(6).Rows(2).Item(6) = 7
s(6).Rows(2).Item(7) = 14
s(6).Rows(2).Item(8) = 10
s(6).Rows(2).Item(9) = 15
s(6).Rows(2).Item(10) = 6
s(6).Rows(2).Item(11) = 8
s(6).Rows(2).Item(12) = 0
s(6).Rows(2).Item(13) = 5
s(6).Rows(2).Item(14) = 9
s(6).Rows(2).Item(15) = 2
s(6).Rows(3).Item(0) = 6
s(6).Rows(3).Item(1) = 11
s(6).Rows(3).Item(2) = 13

NGO TH TUYET HA T012825

http://thuviendientu.org

O AN BAO MAT THONG TIN


HE MA DES

s(6).Rows(3).Item(3) = 8
s(6).Rows(3).Item(4) = 1
s(6).Rows(3).Item(5) = 4
s(6).Rows(3).Item(6) = 10
s(6).Rows(3).Item(7) = 7
s(6).Rows(3).Item(8) = 9
s(6).Rows(3).Item(9) = 5
s(6).Rows(3).Item(10) = 0
s(6).Rows(3).Item(11) = 15
s(6).Rows(3).Item(12) = 14
s(6).Rows(3).Item(13) = 2
s(6).Rows(3).Item(14) = 3
s(6).Rows(3).Item(15) = 12
dgs6.DataSource = s(6)
End Sub
Ham khi tao S7
Sub khoitao_s7()
Dim i As Integer
s(7) = New DataTable
For i = 0 To 15
Dim col As DataColumn = New DataColumn
s(7).Columns.Add(col)
Next
For i = 0 To 3
Dim row As DataRow = s(7).NewRow
s(7).Rows.Add(row)
Next
s(7).Rows(0).Item(0) = 13

NGO TH TUYET HA T012825

http://thuviendientu.org

O AN BAO MAT THONG TIN


HE MA DES

s(7).Rows(0).Item(1) = 2
s(7).Rows(0).Item(2) = 8
s(7).Rows(0).Item(3) = 4
s(7).Rows(0).Item(4) = 6
s(7).Rows(0).Item(5) = 15
s(7).Rows(0).Item(6) = 11
s(7).Rows(0).Item(7) = 1
s(7).Rows(0).Item(8) = 10
s(7).Rows(0).Item(9) = 9
s(7).Rows(0).Item(10) = 3
s(7).Rows(0).Item(11) = 14
s(7).Rows(0).Item(12) = 5
s(7).Rows(0).Item(13) = 0
s(7).Rows(0).Item(14) = 12
s(7).Rows(0).Item(15) = 7
s(7).Rows(1).Item(0) = 1
s(7).Rows(1).Item(1) = 15
s(7).Rows(1).Item(2) = 13
s(7).Rows(1).Item(3) = 8
s(7).Rows(1).Item(4) = 10
s(7).Rows(1).Item(5) = 3
s(7).Rows(1).Item(6) = 7
s(7).Rows(1).Item(7) = 4
s(7).Rows(1).Item(8) = 12
s(7).Rows(1).Item(9) = 5
s(7).Rows(1).Item(10) = 6
s(7).Rows(1).Item(11) = 11
s(7).Rows(1).Item(12) = 0

NGO TH TUYET HA T012825

http://thuviendientu.org

O AN BAO MAT THONG TIN


HE MA DES

s(7).Rows(1).Item(13) = 14
s(7).Rows(1).Item(14) = 9
s(7).Rows(1).Item(15) = 2
s(7).Rows(2).Item(0) = 7
s(7).Rows(2).Item(1) = 11
s(7).Rows(2).Item(2) = 4
s(7).Rows(2).Item(3) = 1
s(7).Rows(2).Item(4) = 9
s(7).Rows(2).Item(5) = 12
s(7).Rows(2).Item(6) = 14
s(7).Rows(2).Item(7) = 2
s(7).Rows(2).Item(8) = 0
s(7).Rows(2).Item(9) = 6
s(7).Rows(2).Item(10) = 10
s(7).Rows(2).Item(11) = 13
s(7).Rows(2).Item(12) = 15
s(7).Rows(2).Item(13) = 3
s(7).Rows(2).Item(14) = 5
s(7).Rows(2).Item(15) = 8
s(7).Rows(3).Item(0) = 2
s(7).Rows(3).Item(1) = 1
s(7).Rows(3).Item(2) = 14
s(7).Rows(3).Item(3) = 7
s(7).Rows(3).Item(4) = 4
s(7).Rows(3).Item(5) = 10
s(7).Rows(3).Item(6) = 8
s(7).Rows(3).Item(7) = 13
s(7).Rows(3).Item(8) = 15

NGO TH TUYET HA T012825

http://thuviendientu.org

O AN BAO MAT THONG TIN


HE MA DES

s(7).Rows(3).Item(9) = 12
s(7).Rows(3).Item(10) = 9
s(7).Rows(3).Item(11) = 0
s(7).Rows(3).Item(12) = 3
s(7).Rows(3).Item(13) = 5
s(7).Rows(3).Item(14) = 6
s(7).Rows(3).Item(15) = 11
dgs7.DataSource = s(7)
End Sub
Khi tao gia tr bien
Sub khoitao()
ip = txtip.Text.Split(" ", ";", ":", ".")
'iptru = txtiptru.Text.Split(" ", " ", ";", ":", ".")
e = txte.Text.Split(" ", ";", ":", ".")
p = txtp.Text.Split(" ", ";", ":", ".")
pc1 = txtpc1.Text.Split(" ", ";", ":", ".")
pc2 = txtpc2.Text.Split(" ", ";", ":", ".")
khoitao_s0()
khoitao_s1()
khoitao_s2()
khoitao_s3()
khoitao_s4()
khoitao_s5()
khoitao_s6()
khoitao_s7()
End Sub
Cat bit cuoi
Function catbitcuoi(ByVal k As String) As String 'dua vao 64 bit tra ra 56 bit

NGO TH TUYET HA T012825

http://thuviendientu.org

O AN BAO MAT THONG TIN


HE MA DES

Dim i As Integer = 0
Dim j As Integer
Dim tam As String
While i < 63
For j = i To i + 6
tam += k.Substring(j, 1)
Next
i=i+8
End While
Return tam
End Function
Function hvpc1(ByVal k As String) As String
Dim tam(63) As Char
Dim i As Integer
For i = 0 To 63
tam(i) = k.Substring(i, 1)
Next
tam = catbitcuoi(tam)
For i = 0 To 55
tam(i) = k.Substring(Integer.Parse(pc1(i) - 1), 1)
Next
Return tam
End Function
Hoa v pc2
Function hvpc2(ByVal str As String) As String
Dim tam(47) As Char
Dim i As Integer
For i = 0 To 47

NGO TH TUYET HA T012825

http://thuviendientu.org

O AN BAO MAT THONG TIN


HE MA DES

tam(i) = str.Substring(Integer.Parse(pc2(i) - 1), 1)


Next
Return tam
End Function
Function ls(ByVal s As String, ByVal n As Integer) As String
Return s.Substring(n, s.Length - n) + s.Substring(0, n)
End Function
Ham tao day khoa
Sub taodaykhoa()
Dim khoa as String =
"00010011001101000101011101111001100110111011110011011111111
10001"
Dim khoa As String = txtkhoak.Text
Dim j As Integer
If khoa.Length > 8 Then
khoa = txtkhoak.Text.Remove(8, khoa.Length - 8)
txtkhoak.Text = khoa
End If
Dim tam As String
For j = 0 To khoa.Length - 1
tam += bi_acsii(Asc(khoa.Substring(j, 1)))
Next
khoa = tam
Dim khoa1 As String = hvpc1(khoa)
Dim d(16) As String
Dim c(16) As String
c(0) = khoa1.Substring(0, 28)
c(0) = ls(c(0), 1)

NGO TH TUYET HA T012825

http://thuviendientu.org

O AN BAO MAT THONG TIN


HE MA DES

d(0) = khoa1.Substring(28, 28)


d(0) = ls(d(0), 1)
daykhoa(0) = hvpc2(c(0) + d(0))
txtdaykhoa.Text += daykhoa(0) + Chr(9)
Dim i As Integer
For i = 1 To 15
If i = 2 - 1 Or i = 9 - 1 Or i = 16 - 1 Then
c(i) = ls(c(i - 1), 1)
d(i) = ls(d(i - 1), 1)
Else
c(i) = ls(c(i - 1), 2)
d(i) = ls(d(i - 1), 2)
End If
daykhoa(i) = hvpc2(c(i) + d(i))
txtdaykhoa.Text += daykhoa(i) + Chr(9)
Next i
End Sub
Mot so ham x ly chuoi nhap
Nhap nh phan
Sub binarynhap()
x = txtchuoinhap.Text
Dim y As String
Dim i As Integer
Dim j As Integer
Dim sokitudu As Integer = x.Length Mod 8
If sokitudu > 0 Then
Dim sokituthem As Integer = 8 - sokitudu
For i = 1 To sokituthem

NGO TH TUYET HA T012825

http://thuviendientu.org

O AN BAO MAT THONG TIN


HE MA DES

x += " "
Next
End If
Dim sodaynhap As Integer = x.Length \ 8
ReDim daynhap(sodaynhap - 1)
For i = 0 To sodaynhap - 1
daynhap(i) = x.Substring(i * 8, 8)
y = ""
For j = 0 To daynhap(i).Length - 1
y += bi_acsii(Asc(daynhap(i).Substring(j, 1)))
Next
daynhap(i) = y
Next
End Sub
Function bi_acsii(ByVal int As Integer) As String
Dim tam(7) As Char
Dim i As Integer
For i = 0 To 7
tam(i) = (int Mod 2).ToString
int \= 2
Next
Array.Reverse(tam)
Return tam
End Function
Mot so ham ma hoa
Ham hoan v ip
Function hvip(ByVal x As String) As String
Dim tam(63) As Char

NGO TH TUYET HA T012825

http://thuviendientu.org

O AN BAO MAT THONG TIN


HE MA DES

Dim i As Integer
For i = 0 To 63
tam(i) = x.Substring(ip(i) - 1, 1)
Next
Return tam
End Function
Ham hoan v e
Function hve(ByVal r As String) As String
Dim tam(47) As Char
Dim i As Integer
For i = 0 To 47
tam(i) = r.Substring(e(i) - 1, 1)
Next
Return tam
End Function
Function hvp(ByVal c As String) As String
Dim tam(31) As Char
Dim i As Integer
For i = 0 To 31
tam(i) = c.Substring(p(i) - 1, 1)
Next
Return tam
End Function
Ham hoan v ip tr
Function hviptru(ByVal c As String) As String
Dim tam(63) As Char
Dim i As Integer
For i = 0 To 63

NGO TH TUYET HA T012825

http://thuviendientu.org

O AN BAO MAT THONG TIN


HE MA DES

tam(ip(i) - 1) = c.Substring(i, 1)
Next
Return tam
End Function
Ma hoa
Function mahoa() As String
binarynhap()
Dim k As Integer
Dim y As String
For k = 0 To daynhap.Length - 1
'x
+=
"0000000100100011010001010110011110001001101010101100110111101111"
x = daynhap(k)
Dim x0 As String = hvip(x)
Dim l(15) As String
Dim r(15) As String
Dim i, j As Integer
Dim l0 As String = x0.Substring(0, 32)
Dim r0 As String = x0.Substring(32, 32)
l(0) = r0
For i = 0 To 31
r(0) += (l0.Substring(i, 1) Xor f(r0, daykhoa(0)).Substring(i,
1)).ToString
Next
For i = 1 To 15
l(i) = r(i - 1)
Dim a As String = f(r(i - 1), daykhoa(i))
For j = 0 To 31
r(i) += (l(i - 1).Substring(j, 1) Xor a.Substring(j, 1)).ToString
NGO TH TUYET HA T012825

http://thuviendientu.org

O AN BAO MAT THONG TIN


HE MA DES

Next j
Next i
Dim v As String = hviptru(r(15) + l(15))
y += v
Next k
Return y
End Function
Ham f
Function f(ByVal r As String, ByVal k As String) As String
Dim i As Integer
Dim e As String
Dim hv As String = hve(r)
For i = 0 To 47
e += (hv.Substring(i, 1) Xor k.Substring(i, 1)).ToString
Next
Dim b(7) As String
Dim c As String
For i = 0 To 7
b(i) = e.Substring(i * 6, 6)
Dim haibitcuoi As String = b(i).Substring(0, 1) + b(i).Substring(5,
1)
Dim bonbitgiua As String = b(i).Substring(1, 4)
Dim srow = thapphan(haibitcuoi)
Dim scol = thapphan(bonbitgiua)
Dim sij As Integer = s(i).Rows(srow).Item(scol)
c += binary(sij)
Next
c = hvp(c)

NGO TH TUYET HA T012825

http://thuviendientu.org

O AN BAO MAT THONG TIN


HE MA DES

Return c
End Function
Ham thap phan
Function thapphan(ByVal b As String) As Integer
Dim i As Integer
Dim tam As Integer = 0
For i = 0 To b.Length - 1
If b.Substring(i, 1) = 1 Then
tam += 2 ^ (b.Length - 1 - i)
End If
Next
Return tam
End Function
Ham nh phan
Function binary(ByVal a As Integer) As String
Dim i As Integer
Dim tam(3) As Char
For i = 0 To 3
tam(i) = (a Mod 2).ToString
a=a\2
Next
Array.Reverse(tam)
Return tam
End Function
Ham oi ra ch
Function doirachu(ByVal y As String) As String
Dim tam As String = y
Dim tam1 As String = ""

NGO TH TUYET HA T012825

http://thuviendientu.org

O AN BAO MAT THONG TIN


HE MA DES

Dim so As Integer
Dim i As Integer
Dim j As Integer
While i < tam.Length - 1
so = 0
For j = i To i + 7
If tam.Substring(j, 1) = 1 Then
so += 2 ^ (7 - (j - i))
End If
Next
tam1 += Chr(so)
i=i+8
End While
Return tam1
End Function
#End Region
mot so ham dung e giai ma
tao ban ma nh phan
Sub binaybanma()
Dim sodaybanma = txtbanma.Text.Length \ 8
Dim i, j As Integer
Dim tam As String
ReDim daybanma(sodaybanma - 1)
For i = 0 To sodaybanma - 1
daybanma(i) = txtbanma.Text.Substring(i * 8, 8)
tam = ""
For j = 0 To daybanma(i).Length - 1
tam += bi_acsii(Asc(daybanma(i).Substring(j, 1)))

NGO TH TUYET HA T012825

http://thuviendientu.org

O AN BAO MAT THONG TIN


HE MA DES

Next
daybanma(i) = tam
Next i
End Sub
Ham giai ma
Function giaima() As String
binaybanma()
Dim k As Integer
x = ""
For k = 0 To daybanma.Length - 1
Dim y0 As String = hvip(daybanma(k))
Dim rr As String = y0.Substring(0, 32)
Dim ll As String = y0.Substring(32, 32)
Dim i, j As Integer
Dim l(15) As String
Dim r(15) As String
r(15) = ll
For i = 0 To 31
l(15) += (rr.Substring(i, 1) Xor f(ll, daykhoa(15)).Substring(i,
1)).ToString
Next
For i = 14 To 0 Step -1
r(i) = l(i + 1)
Dim a As String = f(l(i + 1), daykhoa(i))
For j = 0 To 31
l(i) += (r(i + 1).Substring(j, 1) Xor a.Substring(j, 1)).ToString
Next j
Next i

NGO TH TUYET HA T012825

http://thuviendientu.org

O AN BAO MAT THONG TIN


HE MA DES

x += hviptru(l(0) + r(0))
Next k
Return x
End Function
Mot so s kien giup cho viec thc thi
Private Sub des_Load(ByVal sender As System.Object, ByVal e As
System.EventArgs) Handles MyBase.Load
khoitao()
End Sub
Private Sub btlapma_Click(ByVal sender As System.Object, ByVal e
As System.EventArgs) Handles btlapma.Click
khoitao()
If txtchuoinhap.Text = "" Then
MessageBox.Show("B n hy nh p vo chu i c n m ho")
txtchuoinhap.Focus()
Return
End If
If txtkhoak.Text = "" Or txtkhoak.Text.Length < 8 Then
MessageBox.Show("B n hy nh p vo kha :8 k t ")
txtkhoak.Focus()
Return
End If
txtdaykhoa.Text = ""
taodaykhoa()
txtbanmabit.Text = mahoa()
txtbanma.Text = doirachu(mahoa())
End Sub
Private Sub btthoat_Click(ByVal sender As System.Object, ByVal e As
System.EventArgs) Handles btthoat.Click

NGO TH TUYET HA T012825

http://thuviendientu.org

O AN BAO MAT THONG TIN


HE MA DES

Me.Close()
End Sub
Private Sub btgiaima_Click(ByVal sender As System.Object, ByVal e
As System.EventArgs) Handles btgiaima.Click
If txtkhoak.Text = "" Or txtkhoak.Text.Length < 8 Then
MessageBox.Show("B n hy nh p kha : 8 k t ")
txtkhoak.Focus()
Return
End If
If txtbanma.Text = "" Then
MessageBox.Show("B n hy nh p vo chu i c n gi i m")
txtbanma.Focus()
Return
End If
txtdaykhoa.Text = ""
taodaykhoa()
txtbanrobit.Text = giaima()
txtbanro.Text = doirachu(giaima())
End Sub
End Class

Form tham ma Des (thamma.vb)

NGO TH TUYET HA T012825

http://thuviendientu.org

O AN BAO MAT THONG TIN


HE MA DES

Imports System.Windows.Forms
Public Class thammades Inherits System.Windows.Forms.Form
Khai bao mot so bien va ham
Dim banro() As String
Dim banma() As String
Dim hve(47) As Integer
Dim hvp(31) As Integer
Dim hvpc2(47) As Integer
Dim hvpc1(55) As Integer
Dim hvip(63) As Integer
Dim e() As String
Dim esao() As String
Dim ephay() As String
Dim cphay() As String

NGO TH TUYET HA T012825

http://thuviendientu.org

O AN BAO MAT THONG TIN


HE MA DES

Dim jhop(7, 63) As Integer


Dim s(7) As DataTable
Dim chuoikhoa(255) As String
Dim daykhoa(2) As String
Dim n As Integer '6 ban ro va 6 ban ma
Const m = 16 '8 ki tu
Const hebit = 4 'he 256
Dim flag As Boolean
Khi tao
Sub khoitao_s0()
Dim i As Integer
s(0) = New DataTable
For i = 0 To 15
Dim col As DataColumn = New DataColumn
s(0).Columns.Add(col)
Next
For i = 0 To 3
Dim row As DataRow = s(0).NewRow
s(0).Rows.Add(row)
Next
s(0).Rows(0).Item(0) = 14
s(0).Rows(0).Item(1) = 4
s(0).Rows(0).Item(2) = 13
s(0).Rows(0).Item(3) = 1
s(0).Rows(0).Item(4) = 2
s(0).Rows(0).Item(5) = 15
s(0).Rows(0).Item(6) = 11
s(0).Rows(0).Item(7) = 8

NGO TH TUYET HA T012825

http://thuviendientu.org

O AN BAO MAT THONG TIN


HE MA DES

s(0).Rows(0).Item(8) = 3
s(0).Rows(0).Item(9) = 10
s(0).Rows(0).Item(10) = 6
s(0).Rows(0).Item(11) = 12
s(0).Rows(0).Item(12) = 5
s(0).Rows(0).Item(13) = 9
s(0).Rows(0).Item(14) = 0
s(0).Rows(0).Item(15) = 7
s(0).Rows(1).Item(0) = 0
s(0).Rows(1).Item(1) = 15
s(0).Rows(1).Item(2) = 7
s(0).Rows(1).Item(3) = 4
s(0).Rows(1).Item(4) = 14
s(0).Rows(1).Item(5) = 2
s(0).Rows(1).Item(6) = 13
s(0).Rows(1).Item(7) = 1
s(0).Rows(1).Item(8) = 10
s(0).Rows(1).Item(9) = 6
s(0).Rows(1).Item(10) = 12
s(0).Rows(1).Item(11) = 11
s(0).Rows(1).Item(12) = 9
s(0).Rows(1).Item(13) = 5
s(0).Rows(1).Item(14) = 3
s(0).Rows(1).Item(15) = 8
s(0).Rows(2).Item(0) = 4
s(0).Rows(2).Item(1) = 1
s(0).Rows(2).Item(2) = 14
s(0).Rows(2).Item(3) = 8

NGO TH TUYET HA T012825

http://thuviendientu.org

O AN BAO MAT THONG TIN


HE MA DES

s(0).Rows(2).Item(4) = 13
s(0).Rows(2).Item(5) = 6
s(0).Rows(2).Item(6) = 2
s(0).Rows(2).Item(7) = 11
s(0).Rows(2).Item(8) = 15
s(0).Rows(2).Item(9) = 12
s(0).Rows(2).Item(10) = 9
s(0).Rows(2).Item(11) = 7
s(0).Rows(2).Item(12) = 3
s(0).Rows(2).Item(13) = 10
s(0).Rows(2).Item(14) = 5
s(0).Rows(2).Item(15) = 0
s(0).Rows(3).Item(0) = 15
s(0).Rows(3).Item(1) = 12
s(0).Rows(3).Item(2) = 8
s(0).Rows(3).Item(3) = 2
s(0).Rows(3).Item(4) = 4
s(0).Rows(3).Item(5) = 9
s(0).Rows(3).Item(6) = 1
s(0).Rows(3).Item(7) = 7
s(0).Rows(3).Item(8) = 5
s(0).Rows(3).Item(9) = 11
s(0).Rows(3).Item(10) = 3
s(0).Rows(3).Item(11) = 14
s(0).Rows(3).Item(12) = 10
s(0).Rows(3).Item(13) = 0
s(0).Rows(3).Item(14) = 6
s(0).Rows(3).Item(15) = 13

NGO TH TUYET HA T012825

http://thuviendientu.org

O AN BAO MAT THONG TIN


HE MA DES

End Sub
Khi tao ham s1
Sub khoitao_s1()
Dim i As Integer
s(1) = New DataTable
For i = 0 To 15
Dim col As DataColumn = New DataColumn
s(1).Columns.Add(col)
Next
For i = 0 To 3
Dim row As DataRow = s(1).NewRow
s(1).Rows.Add(row)
Next
s(1).Rows(0).Item(0) = 15
s(1).Rows(0).Item(1) = 1
s(1).Rows(0).Item(2) = 8
s(1).Rows(0).Item(3) = 14
s(1).Rows(0).Item(4) = 6
s(1).Rows(0).Item(5) = 11
s(1).Rows(0).Item(6) = 3
s(1).Rows(0).Item(7) = 4
s(1).Rows(0).Item(8) = 9
s(1).Rows(0).Item(9) = 7
s(1).Rows(0).Item(10) = 2
s(1).Rows(0).Item(11) = 13
s(1).Rows(0).Item(12) = 12
s(1).Rows(0).Item(13) = 0
s(1).Rows(0).Item(14) = 5

NGO TH TUYET HA T012825

http://thuviendientu.org

O AN BAO MAT THONG TIN


HE MA DES

s(1).Rows(0).Item(15) = 10
s(1).Rows(1).Item(0) = 3
s(1).Rows(1).Item(1) = 13
s(1).Rows(1).Item(2) = 4
s(1).Rows(1).Item(3) = 7
s(1).Rows(1).Item(4) = 15
s(1).Rows(1).Item(5) = 2
s(1).Rows(1).Item(6) = 8
s(1).Rows(1).Item(7) = 14
s(1).Rows(1).Item(8) = 12
s(1).Rows(1).Item(9) = 0
s(1).Rows(1).Item(10) = 1
s(1).Rows(1).Item(11) = 10
s(1).Rows(1).Item(12) = 6
s(1).Rows(1).Item(13) = 9
s(1).Rows(1).Item(14) = 11
s(1).Rows(1).Item(15) = 5
s(1).Rows(2).Item(0) = 0
s(1).Rows(2).Item(1) = 14
s(1).Rows(2).Item(2) = 7
s(1).Rows(2).Item(3) = 11
s(1).Rows(2).Item(4) = 10
s(1).Rows(2).Item(5) = 4
s(1).Rows(2).Item(6) = 13
s(1).Rows(2).Item(7) = 1
s(1).Rows(2).Item(8) = 5
s(1).Rows(2).Item(9) = 8
s(1).Rows(2).Item(10) = 12

NGO TH TUYET HA T012825

http://thuviendientu.org

O AN BAO MAT THONG TIN


HE MA DES

s(1).Rows(2).Item(11) = 6
s(1).Rows(2).Item(12) = 9
s(1).Rows(2).Item(13) = 3
s(1).Rows(2).Item(14) = 2
s(1).Rows(2).Item(15) = 15
s(1).Rows(3).Item(0) = 13
s(1).Rows(3).Item(1) = 8
s(1).Rows(3).Item(2) = 10
s(1).Rows(3).Item(3) = 1
s(1).Rows(3).Item(4) = 3
s(1).Rows(3).Item(5) = 15
s(1).Rows(3).Item(6) = 4
s(1).Rows(3).Item(7) = 2
s(1).Rows(3).Item(8) = 11
s(1).Rows(3).Item(9) = 6
s(1).Rows(3).Item(10) = 7
s(1).Rows(3).Item(11) = 12
s(1).Rows(3).Item(12) = 0
s(1).Rows(3).Item(13) = 5
s(1).Rows(3).Item(14) = 14
s(1).Rows(3).Item(15) = 9
End Sub
Khi tao ham s2
Sub khoitao_s2()
Dim i As Integer
s(2) = New DataTable
For i = 0 To 15
Dim col As DataColumn = New DataColumn

NGO TH TUYET HA T012825

http://thuviendientu.org

O AN BAO MAT THONG TIN


HE MA DES

s(2).Columns.Add(col)
Next
For i = 0 To 3
Dim row As DataRow = s(2).NewRow
s(2).Rows.Add(row)
Next
s(2).Rows(0).Item(0) = 10
s(2).Rows(0).Item(1) = 0
s(2).Rows(0).Item(2) = 9
s(2).Rows(0).Item(3) = 14
s(2).Rows(0).Item(4) = 6
s(2).Rows(0).Item(5) = 3
s(2).Rows(0).Item(6) = 15
s(2).Rows(0).Item(7) = 5
s(2).Rows(0).Item(8) = 1
s(2).Rows(0).Item(9) = 13
s(2).Rows(0).Item(10) = 12
s(2).Rows(0).Item(11) = 7
s(2).Rows(0).Item(12) = 11
s(2).Rows(0).Item(13) = 4
s(2).Rows(0).Item(14) = 2
s(2).Rows(0).Item(15) = 8
s(2).Rows(1).Item(0) = 13
s(2).Rows(1).Item(1) = 7
s(2).Rows(1).Item(2) = 0
s(2).Rows(1).Item(3) = 9
s(2).Rows(1).Item(4) = 3
s(2).Rows(1).Item(5) = 4

NGO TH TUYET HA T012825

http://thuviendientu.org

O AN BAO MAT THONG TIN


HE MA DES

s(2).Rows(1).Item(6) = 6
s(2).Rows(1).Item(7) = 10
s(2).Rows(1).Item(8) = 2
s(2).Rows(1).Item(9) = 8
s(2).Rows(1).Item(10) = 5
s(2).Rows(1).Item(11) = 14
s(2).Rows(1).Item(12) = 12
s(2).Rows(1).Item(13) = 11
s(2).Rows(1).Item(14) = 15
s(2).Rows(1).Item(15) = 1
s(2).Rows(2).Item(0) = 13
s(2).Rows(2).Item(1) = 6
s(2).Rows(2).Item(2) = 4
s(2).Rows(2).Item(3) = 9
s(2).Rows(2).Item(4) = 8
s(2).Rows(2).Item(5) = 15
s(2).Rows(2).Item(6) = 3
s(2).Rows(2).Item(7) = 0
s(2).Rows(2).Item(8) = 11
s(2).Rows(2).Item(9) = 1
s(2).Rows(2).Item(10) = 2
s(2).Rows(2).Item(11) = 12
s(2).Rows(2).Item(12) = 5
s(2).Rows(2).Item(13) = 10
s(2).Rows(2).Item(14) = 14
s(2).Rows(2).Item(15) = 7
s(2).Rows(3).Item(0) = 1
s(2).Rows(3).Item(1) = 10

NGO TH TUYET HA T012825

http://thuviendientu.org

O AN BAO MAT THONG TIN


HE MA DES

s(2).Rows(3).Item(2) = 13
s(2).Rows(3).Item(3) = 0
s(2).Rows(3).Item(4) = 6
s(2).Rows(3).Item(5) = 9
s(2).Rows(3).Item(6) = 8
s(2).Rows(3).Item(7) = 7
s(2).Rows(3).Item(8) = 4
s(2).Rows(3).Item(9) = 15
s(2).Rows(3).Item(10) = 14
s(2).Rows(3).Item(11) = 3
s(2).Rows(3).Item(12) = 11
s(2).Rows(3).Item(13) = 5
s(2).Rows(3).Item(14) = 3
s(2).Rows(3).Item(15) = 12
End Sub
Khi tao ham s3
Sub khoitao_s3()
Dim i As Integer
s(3) = New DataTable
For i = 0 To 15
Dim col As DataColumn = New DataColumn
s(3).Columns.Add(col)
Next
For i = 0 To 3
Dim row As DataRow = s(3).NewRow
s(3).Rows.Add(row)
Next
s(3).Rows(0).Item(0) = 7

NGO TH TUYET HA T012825

http://thuviendientu.org

O AN BAO MAT THONG TIN


HE MA DES

s(3).Rows(0).Item(1) = 13
s(3).Rows(0).Item(2) = 14
s(3).Rows(0).Item(3) = 3
s(3).Rows(0).Item(4) = 0
s(3).Rows(0).Item(5) = 6
s(3).Rows(0).Item(6) = 9
s(3).Rows(0).Item(7) = 10
s(3).Rows(0).Item(8) = 1
s(3).Rows(0).Item(9) = 2
s(3).Rows(0).Item(10) = 8
s(3).Rows(0).Item(11) = 5
s(3).Rows(0).Item(12) = 11
s(3).Rows(0).Item(13) = 12
s(3).Rows(0).Item(14) = 4
s(3).Rows(0).Item(15) = 15
s(3).Rows(1).Item(0) = 13
s(3).Rows(1).Item(1) = 8
s(3).Rows(1).Item(2) = 11
s(3).Rows(1).Item(3) = 5
s(3).Rows(1).Item(4) = 6
s(3).Rows(1).Item(5) = 15
s(3).Rows(1).Item(6) = 0
s(3).Rows(1).Item(7) = 3
s(3).Rows(1).Item(8) = 4
s(3).Rows(1).Item(9) = 7
s(3).Rows(1).Item(10) = 2
s(3).Rows(1).Item(11) = 12
s(3).Rows(1).Item(12) = 1

NGO TH TUYET HA T012825

http://thuviendientu.org

O AN BAO MAT THONG TIN


HE MA DES

s(3).Rows(1).Item(13) = 10
s(3).Rows(1).Item(14) = 14
s(3).Rows(1).Item(15) = 9
s(3).Rows(2).Item(0) = 10
s(3).Rows(2).Item(1) = 6
s(3).Rows(2).Item(2) = 9
s(3).Rows(2).Item(3) = 0
s(3).Rows(2).Item(4) = 12
s(3).Rows(2).Item(5) = 11
s(3).Rows(2).Item(6) = 7
s(3).Rows(2).Item(7) = 13
s(3).Rows(2).Item(8) = 15
s(3).Rows(2).Item(9) = 1
s(3).Rows(2).Item(10) = 3
s(3).Rows(2).Item(11) = 14
s(3).Rows(2).Item(12) = 5
s(3).Rows(2).Item(13) = 2
s(3).Rows(2).Item(14) = 8
s(3).Rows(2).Item(15) = 4
s(3).Rows(3).Item(0) = 3
s(3).Rows(3).Item(1) = 15
s(3).Rows(3).Item(2) = 0
s(3).Rows(3).Item(3) = 6
s(3).Rows(3).Item(4) = 10
s(3).Rows(3).Item(5) = 1
s(3).Rows(3).Item(6) = 13
s(3).Rows(3).Item(7) = 8
s(3).Rows(3).Item(8) = 9

NGO TH TUYET HA T012825

http://thuviendientu.org

O AN BAO MAT THONG TIN


HE MA DES

s(3).Rows(3).Item(9) = 4
s(3).Rows(3).Item(10) = 5
s(3).Rows(3).Item(11) = 11
s(3).Rows(3).Item(12) = 12
s(3).Rows(3).Item(13) = 7
s(3).Rows(3).Item(14) = 2
s(3).Rows(3).Item(15) = 14
End Sub
Khi tao ham s4
Sub khoitao_s4()
Dim i As Integer
s(4) = New DataTable
For i = 0 To 15
Dim col As DataColumn = New DataColumn
s(4).Columns.Add(col)
Next
For i = 0 To 3
Dim row As DataRow = s(4).NewRow
s(4).Rows.Add(row)
Next
s(4).Rows(0).Item(0) = 2
s(4).Rows(0).Item(1) = 12
s(4).Rows(0).Item(2) = 4
s(4).Rows(0).Item(3) = 1
s(4).Rows(0).Item(4) = 7
s(4).Rows(0).Item(5) = 10
s(4).Rows(0).Item(6) = 11
s(4).Rows(0).Item(7) = 6

NGO TH TUYET HA T012825

http://thuviendientu.org

O AN BAO MAT THONG TIN


HE MA DES

s(4).Rows(0).Item(8) = 8
s(4).Rows(0).Item(9) = 5
s(4).Rows(0).Item(10) = 3
s(4).Rows(0).Item(11) = 15
s(4).Rows(0).Item(12) = 13
s(4).Rows(0).Item(13) = 0
s(4).Rows(0).Item(14) = 14
s(4).Rows(0).Item(15) = 9
s(4).Rows(1).Item(0) = 14
s(4).Rows(1).Item(1) = 11
s(4).Rows(1).Item(2) = 2
s(4).Rows(1).Item(3) = 12
s(4).Rows(1).Item(4) = 4
s(4).Rows(1).Item(5) = 7
s(4).Rows(1).Item(6) = 13
s(4).Rows(1).Item(7) = 1
s(4).Rows(1).Item(8) = 5
s(4).Rows(1).Item(9) = 0
s(4).Rows(1).Item(10) = 15
s(4).Rows(1).Item(11) = 10
s(4).Rows(1).Item(12) = 3
s(4).Rows(1).Item(13) = 9
s(4).Rows(1).Item(14) = 8
s(4).Rows(1).Item(15) = 6
s(4).Rows(2).Item(0) = 4
s(4).Rows(2).Item(1) = 2
s(4).Rows(2).Item(2) = 1
s(4).Rows(2).Item(3) = 11

NGO TH TUYET HA T012825

http://thuviendientu.org

O AN BAO MAT THONG TIN


HE MA DES

s(4).Rows(2).Item(4) = 10
s(4).Rows(2).Item(5) = 13
s(4).Rows(2).Item(6) = 7
s(4).Rows(2).Item(7) = 8
s(4).Rows(2).Item(8) = 15
s(4).Rows(2).Item(9) = 9
s(4).Rows(2).Item(10) = 12
s(4).Rows(2).Item(11) = 5
s(4).Rows(2).Item(12) = 6
s(4).Rows(2).Item(13) = 3
s(4).Rows(2).Item(14) = 0
s(4).Rows(2).Item(15) = 14
s(4).Rows(3).Item(0) = 11
s(4).Rows(3).Item(1) = 8
s(4).Rows(3).Item(2) = 12
s(4).Rows(3).Item(3) = 7
s(4).Rows(3).Item(4) = 0
s(4).Rows(3).Item(5) = 14
s(4).Rows(3).Item(6) = 2
s(4).Rows(3).Item(7) = 13
s(4).Rows(3).Item(8) = 6
s(4).Rows(3).Item(9) = 15
s(4).Rows(3).Item(10) = 0
s(4).Rows(3).Item(11) = 9
s(4).Rows(3).Item(12) = 10
s(4).Rows(3).Item(13) = 4
s(4).Rows(3).Item(14) = 5
s(4).Rows(3).Item(15) = 3

NGO TH TUYET HA T012825

http://thuviendientu.org

O AN BAO MAT THONG TIN


HE MA DES

End Sub
Khi tao ham s5
Sub khoitao_s5()
Dim i As Integer
s(5) = New DataTable
For i = 0 To 15
Dim col As DataColumn = New DataColumn
s(5).Columns.Add(col)
Next
For i = 0 To 3
Dim row As DataRow = s(5).NewRow
s(5).Rows.Add(row)
Next
s(5).Rows(0).Item(0) = 12
s(5).Rows(0).Item(1) = 1
s(5).Rows(0).Item(2) = 10
s(5).Rows(0).Item(3) = 15
s(5).Rows(0).Item(4) = 9
s(5).Rows(0).Item(5) = 2
s(5).Rows(0).Item(6) = 6
s(5).Rows(0).Item(7) = 8
s(5).Rows(0).Item(8) = 0
s(5).Rows(0).Item(9) = 13
s(5).Rows(0).Item(10) = 3
s(5).Rows(0).Item(11) = 4
s(5).Rows(0).Item(12) = 14
s(5).Rows(0).Item(13) = 7
s(5).Rows(0).Item(14) = 5

NGO TH TUYET HA T012825

http://thuviendientu.org

O AN BAO MAT THONG TIN


HE MA DES

s(5).Rows(0).Item(15) = 11
s(5).Rows(1).Item(0) = 10
s(5).Rows(1).Item(1) = 15
s(5).Rows(1).Item(2) = 4
s(5).Rows(1).Item(3) = 2
s(5).Rows(1).Item(4) = 7
s(5).Rows(1).Item(5) = 12
s(5).Rows(1).Item(6) = 9
s(5).Rows(1).Item(7) = 5
s(5).Rows(1).Item(8) = 6
s(5).Rows(1).Item(9) = 1
s(5).Rows(1).Item(10) = 13
s(5).Rows(1).Item(11) = 14
s(5).Rows(1).Item(12) = 0
s(5).Rows(1).Item(13) = 11
s(5).Rows(1).Item(14) = 3
s(5).Rows(1).Item(15) = 8
s(5).Rows(2).Item(0) = 9
s(5).Rows(2).Item(1) = 14
s(5).Rows(2).Item(2) = 15
s(5).Rows(2).Item(3) = 5
s(5).Rows(2).Item(4) = 2
s(5).Rows(2).Item(5) = 8
s(5).Rows(2).Item(6) = 12
s(5).Rows(2).Item(7) = 3
s(5).Rows(2).Item(8) = 7
s(5).Rows(2).Item(9) = 0
s(5).Rows(2).Item(10) = 4

NGO TH TUYET HA T012825

http://thuviendientu.org

O AN BAO MAT THONG TIN


HE MA DES

s(5).Rows(2).Item(11) = 10
s(5).Rows(2).Item(12) = 1
s(5).Rows(2).Item(13) = 13
s(5).Rows(2).Item(14) = 11
s(5).Rows(2).Item(15) = 6
s(5).Rows(3).Item(0) = 4
s(5).Rows(3).Item(1) = 3
s(5).Rows(3).Item(2) = 2
s(5).Rows(3).Item(3) = 12
s(5).Rows(3).Item(4) = 9
s(5).Rows(3).Item(5) = 5
s(5).Rows(3).Item(6) = 15
s(5).Rows(3).Item(7) = 10
s(5).Rows(3).Item(8) = 11
s(5).Rows(3).Item(9) = 14
s(5).Rows(3).Item(10) = 1
s(5).Rows(3).Item(11) = 7
s(5).Rows(3).Item(12) = 6
s(5).Rows(3).Item(13) = 0
s(5).Rows(3).Item(14) = 8
s(5).Rows(3).Item(15) = 13
End Sub
Khi tao ham s6
Sub khoitao_s6()
Dim i As Integer
s(6) = New DataTable
For i = 0 To 15
Dim col As DataColumn = New DataColumn

NGO TH TUYET HA T012825

http://thuviendientu.org

O AN BAO MAT THONG TIN


HE MA DES

s(6).Columns.Add(col)
Next
For i = 0 To 3
Dim row As DataRow = s(6).NewRow
s(6).Rows.Add(row)
Next
s(6).Rows(0).Item(0) = 4
s(6).Rows(0).Item(1) = 11
s(6).Rows(0).Item(2) = 2
s(6).Rows(0).Item(3) = 14
s(6).Rows(0).Item(4) = 15
s(6).Rows(0).Item(5) = 0
s(6).Rows(0).Item(6) = 8
s(6).Rows(0).Item(7) = 13
s(6).Rows(0).Item(8) = 3
s(6).Rows(0).Item(9) = 12
s(6).Rows(0).Item(10) = 9
s(6).Rows(0).Item(11) = 7
s(6).Rows(0).Item(12) = 5
s(6).Rows(0).Item(13) = 10
s(6).Rows(0).Item(14) = 6
s(6).Rows(0).Item(15) = 1
s(6).Rows(1).Item(0) = 13
s(6).Rows(1).Item(1) = 0
s(6).Rows(1).Item(2) = 11
s(6).Rows(1).Item(3) = 7
s(6).Rows(1).Item(4) = 4
s(6).Rows(1).Item(5) = 9

NGO TH TUYET HA T012825

http://thuviendientu.org

O AN BAO MAT THONG TIN


HE MA DES

s(6).Rows(1).Item(6) = 1
s(6).Rows(1).Item(7) = 10
s(6).Rows(1).Item(8) = 14
s(6).Rows(1).Item(9) = 3
s(6).Rows(1).Item(10) = 5
s(6).Rows(1).Item(11) = 12
s(6).Rows(1).Item(12) = 2
s(6).Rows(1).Item(13) = 15
s(6).Rows(1).Item(14) = 8
s(6).Rows(1).Item(15) = 6
s(6).Rows(2).Item(0) = 1
s(6).Rows(2).Item(1) = 4
s(6).Rows(2).Item(2) = 11
s(6).Rows(2).Item(3) = 13
s(6).Rows(2).Item(4) = 12
s(6).Rows(2).Item(5) = 3
s(6).Rows(2).Item(6) = 7
s(6).Rows(2).Item(7) = 14
s(6).Rows(2).Item(8) = 10
s(6).Rows(2).Item(9) = 15
s(6).Rows(2).Item(10) = 6
s(6).Rows(2).Item(11) = 8
s(6).Rows(2).Item(12) = 0
s(6).Rows(2).Item(13) = 5
s(6).Rows(2).Item(14) = 9
s(6).Rows(2).Item(15) = 2
s(6).Rows(3).Item(0) = 6
s(6).Rows(3).Item(1) = 11

NGO TH TUYET HA T012825

http://thuviendientu.org

O AN BAO MAT THONG TIN


HE MA DES

s(6).Rows(3).Item(2) = 13
s(6).Rows(3).Item(3) = 8
s(6).Rows(3).Item(4) = 1
s(6).Rows(3).Item(5) = 4
s(6).Rows(3).Item(6) = 10
s(6).Rows(3).Item(7) = 7
s(6).Rows(3).Item(8) = 9
s(6).Rows(3).Item(9) = 5
s(6).Rows(3).Item(10) = 0
s(6).Rows(3).Item(11) = 15
s(6).Rows(3).Item(12) = 14
s(6).Rows(3).Item(13) = 2
s(6).Rows(3).Item(14) = 3
s(6).Rows(3).Item(15) = 12
End Sub
Khi tao ham s7
Sub khoitao_s7()
Dim i As Integer
s(7) = New DataTable
For i = 0 To 15
Dim col As DataColumn = New DataColumn
s(7).Columns.Add(col)
Next
For i = 0 To 3
Dim row As DataRow = s(7).NewRow
s(7).Rows.Add(row)
Next
s(7).Rows(0).Item(0) = 13

NGO TH TUYET HA T012825

http://thuviendientu.org

O AN BAO MAT THONG TIN


HE MA DES

s(7).Rows(0).Item(1) = 2
s(7).Rows(0).Item(2) = 8
s(7).Rows(0).Item(3) = 4
s(7).Rows(0).Item(4) = 6
s(7).Rows(0).Item(5) = 15
s(7).Rows(0).Item(6) = 11
s(7).Rows(0).Item(7) = 1
s(7).Rows(0).Item(8) = 10
s(7).Rows(0).Item(9) = 9
s(7).Rows(0).Item(10) = 3
s(7).Rows(0).Item(11) = 14
s(7).Rows(0).Item(12) = 5
s(7).Rows(0).Item(13) = 0
s(7).Rows(0).Item(14) = 12
s(7).Rows(0).Item(15) = 7
s(7).Rows(1).Item(0) = 1
s(7).Rows(1).Item(1) = 15
s(7).Rows(1).Item(2) = 13
s(7).Rows(1).Item(3) = 8
s(7).Rows(1).Item(4) = 10
s(7).Rows(1).Item(5) = 3
s(7).Rows(1).Item(6) = 7
s(7).Rows(1).Item(7) = 4
s(7).Rows(1).Item(8) = 12
s(7).Rows(1).Item(9) = 5
s(7).Rows(1).Item(10) = 6
s(7).Rows(1).Item(11) = 11
s(7).Rows(1).Item(12) = 0

NGO TH TUYET HA T012825

http://thuviendientu.org

O AN BAO MAT THONG TIN


HE MA DES

s(7).Rows(1).Item(13) = 14
s(7).Rows(1).Item(14) = 9
s(7).Rows(1).Item(15) = 2
s(7).Rows(2).Item(0) = 7
s(7).Rows(2).Item(1) = 11
s(7).Rows(2).Item(2) = 4
s(7).Rows(2).Item(3) = 1
s(7).Rows(2).Item(4) = 9
s(7).Rows(2).Item(5) = 12
s(7).Rows(2).Item(6) = 14
s(7).Rows(2).Item(7) = 2
s(7).Rows(2).Item(8) = 0
s(7).Rows(2).Item(9) = 6
s(7).Rows(2).Item(10) = 10
s(7).Rows(2).Item(11) = 13
s(7).Rows(2).Item(12) = 15
s(7).Rows(2).Item(13) = 3
s(7).Rows(2).Item(14) = 5
s(7).Rows(2).Item(15) = 8
s(7).Rows(3).Item(0) = 2
s(7).Rows(3).Item(1) = 1
s(7).Rows(3).Item(2) = 14
s(7).Rows(3).Item(3) = 7
s(7).Rows(3).Item(4) = 4
s(7).Rows(3).Item(5) = 10
s(7).Rows(3).Item(6) = 8
s(7).Rows(3).Item(7) = 13
s(7).Rows(3).Item(8) = 15

NGO TH TUYET HA T012825

http://thuviendientu.org

O AN BAO MAT THONG TIN


HE MA DES

s(7).Rows(3).Item(9) = 12
s(7).Rows(3).Item(10) = 9
s(7).Rows(3).Item(11) = 0
s(7).Rows(3).Item(12) = 3
s(7).Rows(3).Item(13) = 5
s(7).Rows(3).Item(14) = 6
s(7).Rows(3).Item(15) = 11
End Sub
Khi tao hoan v e
Sub khoitao_hve()
hve(0) = 32
hve(1) = 1
hve(2) = 2
hve(3) = 3
hve(4) = 4
hve(5) = 5
hve(6) = 4
hve(7) = 5
hve(8) = 6
hve(9) = 7
hve(10) = 8
hve(11) = 9
hve(12) = 8
hve(13) = 9
hve(14) = 10
hve(15) = 11
hve(16) = 12
hve(17) = 13

NGO TH TUYET HA T012825

http://thuviendientu.org

hve(18) = 12
hve(19) = 13
hve(20) = 14
hve(21) = 15
hve(22) = 16
hve(23) = 17
hve(24) = 16
hve(25) = 17
hve(26) = 18
hve(27) = 19
hve(28) = 20
hve(29) = 21
hve(30) = 20
hve(31) = 21
hve(32) = 22
hve(33) = 23
hve(34) = 24
hve(35) = 25
hve(36) = 24
hve(37) = 25
hve(38) = 26
hve(39) = 27
hve(40) = 28
hve(41) = 29
hve(42) = 28
hve(43) = 29
hve(44) = 30
hve(45) = 31

NGO TH TUYET HA T012825

O AN BAO MAT THONG TIN


HE MA DES

http://thuviendientu.org

O AN BAO MAT THONG TIN


HE MA DES

hve(46) = 32
hve(47) = 1
End Sub
Khi tao hoan v p
Sub khoitao_hvp()
hvp(0) = 16
hvp(1) = 7
hvp(2) = 20
hvp(3) = 21
hvp(4) = 29
hvp(5) = 12
hvp(6) = 28
hvp(7) = 17
hvp(8) = 1
hvp(9) = 15
hvp(10) = 23
hvp(11) = 26
hvp(12) = 5
hvp(13) = 18
hvp(14) = 31
hvp(15) = 10
hvp(16) = 2
hvp(17) = 8
hvp(18) = 24
hvp(19) = 14
hvp(20) = 32
hvp(21) = 27
hvp(22) = 3

NGO TH TUYET HA T012825

http://thuviendientu.org

O AN BAO MAT THONG TIN


HE MA DES

hvp(23) = 9
hvp(24) = 19
hvp(25) = 13
hvp(26) = 30
hvp(27) = 6
hvp(28) = 22
hvp(29) = 11
hvp(30) = 4
hvp(31) = 25
End Sub
Khi tao hoan v pc2
Sub khoitao_hvpc2()
hvpc2(0) = 14
hvpc2(1) = 17
hvpc2(2) = 11
hvpc2(3) = 24
hvpc2(4) = 1
hvpc2(5) = 5
hvpc2(6) = 3
hvpc2(7) = 28
hvpc2(8) = 15
hvpc2(9) = 6
hvpc2(10) = 21
hvpc2(11) = 10
hvpc2(12) = 23
hvpc2(13) = 19
hvpc2(14) = 12
hvpc2(15) = 4

NGO TH TUYET HA T012825

http://thuviendientu.org

hvpc2(16) = 26
hvpc2(17) = 8
hvpc2(18) = 16
hvpc2(19) = 7
hvpc2(20) = 27
hvpc2(21) = 20
hvpc2(22) = 13
hvpc2(23) = 2
hvpc2(24) = 41
hvpc2(25) = 52
hvpc2(26) = 31
hvpc2(27) = 37
hvpc2(28) = 47
hvpc2(29) = 55
hvpc2(30) = 30
hvpc2(31) = 40
hvpc2(32) = 51
hvpc2(33) = 45
hvpc2(34) = 33
hvpc2(35) = 48
hvpc2(36) = 44
hvpc2(37) = 49
hvpc2(38) = 39
hvpc2(39) = 56
hvpc2(40) = 34
hvpc2(41) = 53
hvpc2(42) = 46
hvpc2(43) = 42

NGO TH TUYET HA T012825

O AN BAO MAT THONG TIN


HE MA DES

http://thuviendientu.org

O AN BAO MAT THONG TIN


HE MA DES

hvpc2(44) = 50
hvpc2(45) = 36
hvpc2(46) = 29
hvpc2(47) = 32
End Sub
Khi tao hoa v pc1
Sub khoitao_hvpc1()
hvpc1(0) = 57
hvpc1(1) = 49
hvpc1(2) = 41
hvpc1(3) = 33
hvpc1(4) = 25
hvpc1(5) = 17
hvpc1(6) = 9
hvpc1(7) = 1
hvpc1(8) = 58
hvpc1(9) = 50
hvpc1(10) = 42
hvpc1(11) = 34
hvpc1(12) = 26
hvpc1(13) = 18
hvpc1(14) = 10
hvpc1(15) = 2
hvpc1(16) = 59
hvpc1(17) = 51
hvpc1(18) = 43
hvpc1(19) = 35
hvpc1(20) = 27

NGO TH TUYET HA T012825

http://thuviendientu.org

hvpc1(21) = 19
hvpc1(22) = 11
hvpc1(23) = 3
hvpc1(24) = 60
hvpc1(25) = 52
hvpc1(26) = 44
hvpc1(27) = 36
hvpc1(28) = 63
hvpc1(29) = 55
hvpc1(30) = 47
hvpc1(31) = 39
hvpc1(32) = 31
hvpc1(33) = 23
hvpc1(34) = 15
hvpc1(35) = 7
hvpc1(36) = 62
hvpc1(37) = 54
hvpc1(38) = 46
hvpc1(39) = 38
hvpc1(40) = 30
hvpc1(41) = 22
hvpc1(42) = 14
hvpc1(43) = 6
hvpc1(44) = 61
hvpc1(45) = 53
hvpc1(46) = 45
hvpc1(47) = 37
hvpc1(48) = 29

NGO TH TUYET HA T012825

O AN BAO MAT THONG TIN


HE MA DES

http://thuviendientu.org

O AN BAO MAT THONG TIN


HE MA DES

hvpc1(49) = 21
hvpc1(50) = 13
hvpc1(51) = 5
hvpc1(52) = 28
hvpc1(53) = 20
hvpc1(54) = 12
hvpc1(55) = 4
End Sub
Khi tao hoa v ip
Sub khoitao_hvip()
hvip(0) = 58
hvip(1) = 50
hvip(2) = 42
hvip(3) = 34
hvip(4) = 26
hvip(5) = 18
hvip(6) = 10
hvip(7) = 2
hvip(8) = 60
hvip(9) = 52
hvip(10) = 44
hvip(11) = 36
hvip(12) = 28
hvip(13) = 20
hvip(14) = 12
hvip(15) = 4
hvip(16) = 62
hvip(17) = 54

NGO TH TUYET HA T012825

http://thuviendientu.org

hvip(18) = 46
hvip(19) = 38
hvip(20) = 30
hvip(21) = 22
hvip(22) = 14
hvip(23) = 6
hvip(24) = 64
hvip(25) = 56
hvip(26) = 48
hvip(27) = 40
hvip(28) = 32
hvip(29) = 24
hvip(30) = 16
hvip(31) = 8
hvip(32) = 57
hvip(33) = 49
hvip(34) = 41
hvip(35) = 33
hvip(36) = 25
hvip(37) = 17
hvip(38) = 9
hvip(39) = 1
hvip(40) = 59
hvip(41) = 51
hvip(42) = 43
hvip(43) = 35
hvip(44) = 27
hvip(45) = 19

NGO TH TUYET HA T012825

O AN BAO MAT THONG TIN


HE MA DES

http://thuviendientu.org

hvip(46) = 11
hvip(47) = 3
hvip(48) = 61
hvip(49) = 53
hvip(50) = 45
hvip(51) = 37
hvip(52) = 29
hvip(53) = 21
hvip(54) = 13
hvip(55) = 5
hvip(56) = 63
hvip(57) = 55
hvip(58) = 47
hvip(59) = 39
hvip(60) = 31
hvip(61) = 23
hvip(62) = 15
hvip(63) = 7
End Sub
Khi tao cac ham
Sub khoitao()
khoitao_s0()
khoitao_s1()
khoitao_s2()
khoitao_s3()
khoitao_s4()
khoitao_s5()
khoitao_s6()

NGO TH TUYET HA T012825

O AN BAO MAT THONG TIN


HE MA DES

http://thuviendientu.org

O AN BAO MAT THONG TIN


HE MA DES

khoitao_s7()
khoitao_hve()
khoitao_hvp()
khoitao_hvpc2()
khoitao_hvpc1()
khoitao_hvip()
End Sub
cac ham hoan v
Ham hoan v ip
Function hoanvi_ip(ByVal x As String) As String
Dim tam(63) As Char
Dim i As Integer
For i = 0 To 63
tam(i) = x.Substring(hvip(i) - 1, 1)
Next
Return tam
End Function
Ham hoan v iptru
Function hoanvi_iptru(ByVal c As String) As String
Dim tam(63) As Char
Dim i As Integer
For i = 0 To 63
tam(hvip(i) - 1) = c.Substring(i, 1)
Next
Return tam
End Function
Ham hoan v e
Function hoanvi_e(ByVal r As String) As String

NGO TH TUYET HA T012825

http://thuviendientu.org

O AN BAO MAT THONG TIN


HE MA DES

Dim tam(47) As Char


Dim i As Integer
For i = 0 To 47
tam(i) = r.Substring(hve(i) - 1, 1)
Next
Return tam
End Function
Ham hoan v p
Function hoanvi_p(ByVal c As String) As String
Dim tam(31) As Char
Dim i As Integer
For i = 0 To 31
tam(i) = c.Substring(hvp(i) - 1, 1)
Next
Return tam
End Function
Ham hoan v ptru
Function hoanvi_ptru(ByVal c As String) As String
Dim tam(31) As Char
Dim i As Integer
For i = 0 To 31
tam(hvp(i) - 1) = c.Substring(i, 1)
Next
Return tam
End Function
Ham hoan v pc1
Function hoanvi_pc1(ByVal k As String) As String
Dim tam(63) As Char

NGO TH TUYET HA T012825

http://thuviendientu.org

O AN BAO MAT THONG TIN


HE MA DES

Dim i As Integer
For i = 0 To 63
tam(i) = k.Substring(i, 1)
Next
tam = catbitcuoi(tam)
For i = 0 To 55
tam(i) = k.Substring(Integer.Parse(hvpc1(i) - 1), 1)
Next
Return tam
End Function
Ham hoan v pc1tru
Function hoanvi_pc1tru(ByVal c As String) As String
Dim tam(63) As Char
Dim i As Integer
For i = 0 To 63
tam(i) = "#"
Next
For i = 0 To 55
tam(hvpc1(i) - 1) = c.Substring(i, 1)
Next
Dim tam1 As String
Return tam
End Function
Ham hoan v pc2
Function hoanvi_pc2(ByVal str As String) As String
Dim tam(47) As Char
Dim i As Integer

NGO TH TUYET HA T012825

http://thuviendientu.org

O AN BAO MAT THONG TIN


HE MA DES

For i = 0 To 47
tam(i) = str.Substring(Integer.Parse(hvpc2(i) - 1), 1)
Next
Return tam
End Function
Ham hoan v pc2tru
Function hoanvi_pc2tru(ByVal c As String) As String
Dim tam(55) As Char
Dim i As Integer
For i = 0 To 55
tam(i) = "?"
Next
For i = 0 To 47
tam(hvpc2(i) - 1) = c.Substring(i, 1)
Next
Return tam
End Function
gan va kiem tra du lieu nhap
ham gan d lieu
Function gandulieu() As Boolean
Try
Dim i As Integer = 0
Dim j As Integer
Dim strbanro As String = txtbanro.Text.Replace(Chr(10), "")
Dim strbanma As String = txtbanma.Text.Replace(Chr(10), "")
banro = strbanro.Split(Chr(13))
banma = strbanma.Split(Chr(13))
n = banro.Length - 1

NGO TH TUYET HA T012825

http://thuviendientu.org

O AN BAO MAT THONG TIN


HE MA DES

ReDim e((n - 1) / 2)
ReDim esao((n - 1) / 2)
ReDim ephay((n - 1) / 2)
ReDim cphay((n - 1) / 2)
If (banro.Length - 1 < n Or banma.Length - 1 < n) Then
MessageBox.Show("thi u b n r hay b n m")
Return False
Else
Return True
End If
Catch ex As Exception
MessageBox.Show("D li u nh p khng h p l ")
Return False
End Try
End Function
Ham kiem tra
Function kiemtra() As Boolean
Dim i As Integer
For i = 0 To n
If banro(i) = "" Then
MessageBox.Show("ban nhap cha u " & (i + 1))
Return False
End If
If banro(i).Length <> m Then
MessageBox.Show( (i + 1) & m )
Return False
End If
Next

NGO TH TUYET HA T012825

http://thuviendientu.org

O AN BAO MAT THONG TIN


HE MA DES

For i = 0 To n
If banma(i) = "" Then
MessageBox.Show("B n hy nh p vo b n m th " & (i +
1))
Return False
End If
If banma(i).Length <> m Then
MessageBox.Show("B n m " & (i + 1) & " ph i c " & m
& " k t ")
Return False
End If
Next
i=0
While i <= n
If banro(i).Substring(m / 2, m / 2) <> banro(i + 1).Substring(m / 2, m / 2)
Then
MessageBox.Show("D li u b n r " & (i + 1) & " , " & (i + 2) & "
nh p khng h p l ")
Return False
End If
i=i+2
End While
Return True
End Function
xu li e_esao_ephay_cphay
x ly e, e sao, e phay
Sub xuli_e_esao_ephay()
Dim i As Integer
'e(0)=

NGO TH TUYET HA T012825

http://thuviendientu.org

O AN BAO MAT THONG TIN


HE MA DES

"000000000111111000001110100000000110100000001100"
'e(1)=
"101000001011111111110100000101010000001011110110"
'e(2)=
"111011110001010100000110100011110110100101011111"
'esao(0)="101111110000001010101100000001010100000001010010"
'esao(1)="100010100110101001011110101111110010100010101010"
'esao(2) = "000001011110100110100010101111110101011000000100"
For i = 0 To (n - 1) / 2
Dim l3 = banma(i * 2).Substring(0, 32)
Dim l3sao = banma(i * 2 + 1).Substring(0, 32)
e(i) = hoanvi_e(l3)
esao(i) = hoanvi_e(l3sao)
ephay(i) = phay(e(i), esao(i))
Next
End Sub
Function phay(ByVal a As String, ByVal b As String) As String
Dim i As Integer
Dim c As String
For i = 0 To a.Length - 1
c += (a.Substring(i, 1) Xor b.Substring(i, 1)).ToString
Next
Return c
End Function
X ly c phay
Sub xuli_cphay()
Dim i, j As Integer
For i = 0 To (n - 1) / 2
Dim r3 As String = banma(i * 2).Substring(32, 32)
NGO TH TUYET HA T012825

http://thuviendientu.org

O AN BAO MAT THONG TIN


HE MA DES

Dim r3sao As String = banma(i * 2 + 1).Substring(32, 32)


Dim l0 As String = banro(i * 2).Substring(0, 32)
Dim l0sao As String = banro(i * 2 + 1).Substring(0, 32)
Dim r3phay As String = ""
Dim l0phay As String = ""
For j = 0 To 31
r3phay += (r3.Substring(j, 1) Xor r3sao.Substring(j, 1)).ToString
l0phay += (l0.Substring(j, 1) Xor l0sao.Substring(j, 1)).ToString
cphay(i)
1)).ToString

+=

(r3phay.Substring(j,

1)

Xor

l0phay.Substring(j,

Next
cphay(i) = hoanvi_ptru(cphay(i))
Next
'cphay(0) = "10010110010111010101101101100111"
'cphay(1) = "10011100100111000001111101010110"
'cphay(2) = "11010101011101011101101100101011"
End Sub
cac ham chuyen doi
Function bi_str(ByVal a As String) As String
Dim i As Integer
Dim b As String
For i = 0 To a.Length - 1
b += binary(asc(a.Substring(i, 1)), hebit)
Next
Return b
End Function
Sub bi_banro_banma()
Dim i As Integer

NGO TH TUYET HA T012825

http://thuviendientu.org

O AN BAO MAT THONG TIN


HE MA DES

For i = 0 To n
banro(i) = bi_str(banro(i))
banma(i) = bi_str(banma(i))
Next
End Sub
Function bi_so(ByVal a As Integer) As String
Dim i As Integer
Dim tam(5) As Char
For i = 0 To 5
tam(i) = (a Mod 2).ToString
a \= 2
Next
Array.Reverse(tam)
Return tam
End Function
Function thapphan(ByVal b As String) As Integer
Dim i As Integer
Dim tam As Integer = 0
For i = 0 To b.Length - 1
If b.Substring(i, 1) = 1 Then
tam += 2 ^ (b.Length - 1 - i)
End If
Next
Return tam
End Function
Function binary(ByVal a As Integer, ByVal n As Integer) As String
Dim i As Integer
Dim tam(n - 1) As Char

NGO TH TUYET HA T012825

http://thuviendientu.org

O AN BAO MAT THONG TIN


HE MA DES

For i = 0 To n - 1
tam(i) = (a Mod 2).ToString
a=a\2
Next
Array.Reverse(tam)
Return tam
End Function
Ham tao tap test
Function tap_test() As String()
Dim i, j, k As Integer
Dim ee(7) As String
Dim eephay(7) As String
Dim ccphay(7) As String
Dim test(63) As String
For i = 0 To (n - 1) / 2
For j = 0 To 7
ee(j) = e(i).Substring(j * 6, 6)
eephay(j) = ephay(i).Substring(j * 6, 6)
ccphay(j) = cphay(i).Substring(j * 4, 4)
test = tap_in(ee(j), eephay(j), ccphay(j), s(j))
For k = 0 To 63
If test(k) = 1 Then
jhop(j, k) += 1
End If
Next
Next
Next
End Function

NGO TH TUYET HA T012825

http://thuviendientu.org

O AN BAO MAT THONG TIN


HE MA DES

Ham tao khoa vong 3


Function khoavong3() As String
Dim khoav3 As String
Dim i, j As Integer
Dim count As Integer = 0
Dim vitrimax As Integer
For i = 0 To 7
count = 0
Dim max As Integer = (n + 1) / 2
For j = 0 To 63
If jhop(i, j) = max Then
count += 1
vitrimax = j
End If
Next
If count > 1 Then
lbthongbao.Text = "Nh p thm b n m v b n r, v ch a xc
nh c ph n t max trong jh p"
btthamma.Enabled = False
txtbanro.Focus()
n += 2
ReDim banro(n)
ReDim banma(n)
ReDim e((n - 2) / 2)
ReDim esao((n - 2) / 2)
ReDim ephay((n - 2) / 2)
ReDim cphay((n - 2) / 2)
flag = False

NGO TH TUYET HA T012825

http://thuviendientu.org

O AN BAO MAT THONG TIN


HE MA DES

Exit Function
End If
khoav3 += binary(vitrimax, 6)
Next
txtkhoav3.Text = khoav3
Return khoav3
End Function
Ham X ly khoa
Function xulikhoa() As String
Dim khoa As String = khoavong3()
If flag = False Then
Exit Function
End If
khoa = hoanvi_pc2tru(khoa)
Dim haitambitdau As String = khoa.Substring(0, 28)
Dim haitambitcuoi As String = khoa.Substring(28, 28)
haitambitdau = dichphai(haitambitdau, 4)
haitambitcuoi = dichphai(haitambitcuoi, 4)
khoa = haitambitdau + haitambitcuoi
Dim i As Integer
khoa = hoanvi_pc1tru(khoa)
txtkhoa.Text = khoa
Return khoa 'khoa 56 bit
End Function
Ham tao chuoi khoa
Sub taochuoikhoa(ByVal c As String)
If flag = False Then
Exit Sub

NGO TH TUYET HA T012825

http://thuviendientu.org

O AN BAO MAT THONG TIN


HE MA DES

End If
Dim chuoibinary(255) As String
Dim i As Integer
For i = 0 To 255
chuoibinary(i) = binary(i, 8)
chuoikhoa(i) = taotungkhoa(c, chuoibinary(i))
Next
End Sub
Ham tao tng khoa
Function taotungkhoa(ByVal a As String, ByVal b As String) As
String
Dim i As Integer
Dim j As Integer = 0
Dim tam(63) As Char
For i = 0 To 63
tam(i) = a.Substring(i, 1)
If tam(i) = "?" Then
tam(i) = b.Substring(j, 1)
j += 1
End If
Next
Return tam
End Function
Ham dch phai
Function dichphai(ByVal s As String, ByVal n As Integer) As String
Return s.Substring(s.Length - n, n) + s.Substring(0, s.Length - n)
End Function
Function tap_in(ByVal e As String, ByVal ephay As String, ByVal
cphay As String, ByVal s As DataTable) As String()
NGO TH TUYET HA T012825

http://thuviendientu.org

O AN BAO MAT THONG TIN


HE MA DES

Dim tam(63), b, bsao, c, csao, cphaytam As String


Dim i As Integer
For i = 0 To 63
b = bi_so(i)
Dim haibitcuoi As String = b.Substring(0, 1) + b.Substring(5, 1)
Dim bonbitgiua As String = b.Substring(1, 4)
Dim srow = thapphan(haibitcuoi)
Dim scol = thapphan(bonbitgiua)
Dim sij As Integer = s.Rows(srow).Item(scol)
c = binary(sij, 4)
bsao = phay(ephay, b)
haibitcuoi = bsao.Substring(0, 1) + bsao.Substring(5, 1)
bonbitgiua = bsao.Substring(1, 4)
srow = thapphan(haibitcuoi)
scol = thapphan(bonbitgiua)
sij = s.Rows(srow).Item(scol)
csao = binary(sij, 4)
cphaytam = phay(c, csao)
If cphaytam = cphay Then
tam(thapphan(phay(e, b))) = 1
End If
Next
Return tam
End Function
Mot so Ham cho x ly s kien
Private Sub btthamma_Click(ByVal sender As System.Object,
ByVal ee As System.EventArgs) Handles btthamma.Click
ReDim banro(n)

NGO TH TUYET HA T012825

http://thuviendientu.org

O AN BAO MAT THONG TIN


HE MA DES

ReDim banma(n)
ReDim e((n - 1) / 2)
ReDim esao((n - 1) / 2)
ReDim ephay((n - 1) / 2)
ReDim cphay((n - 1) / 2)
flag = True
If gandulieu() = False Then
Exit Sub
End If
If kiemtra() = False Then
Exit Sub
End If
bi_banro_banma()
xuli_e_esao_ephay()
xuli_cphay()
tap_test()
taochuoikhoa(xulikhoa())
If flag = False Then
Exit Sub
End If
chonkhoadung()
End Sub
Private Sub btthoat_Click(ByVal sender As System.Object, ByVal
ee As System.EventArgs) Handles btthoat.Click
Me.Close()
End Sub
Private Sub thammades_Load(ByVal sender As System.Object,
ByVal ee As System.EventArgs) Handles MyBase.Load

NGO TH TUYET HA T012825

http://thuviendientu.org

O AN BAO MAT THONG TIN


HE MA DES

Dim strbanro As String


strbanro += "748502CD38451097" + Chr(13) + Chr(10)
strbanro += "3874756438451097" + Chr(13) + Chr(10)
strbanro += "486911026ACDFF31" + Chr(13) + Chr(10)
strbanro += "375BD31F6ACDFF31" + Chr(13) + Chr(10)
strbanro += "357418DA013FEC86" + Chr(13) + Chr(10)
strbanro += "12549847013FEC86"
txtbanro.Text = strbanro
Dim strbanma As String
strbanma += "03C70306D8A09F10" + Chr(13) + Chr(10)
strbanma += "78560A0960E6D4CB" + Chr(13) + Chr(10)
strbanma += "45FA285BE5ADC730" + Chr(13) + Chr(10)
strbanma += "134F7915AC253457" + Chr(13) + Chr(10)
strbanma += "D8A31B2F28BBC5CF" + Chr(13) + Chr(10)
strbanma += "0F317AC2B23CB944"
txtbanma.Text = strbanma
khoitao()
End Sub
'des
Ham cat bit cuoi
Function catbitcuoi(ByVal k As String) As String 'dua vao 64 bit tra
ra 56 bit
Dim i As Integer = 0
Dim j As Integer
Dim tam As String
While i < 63
For j = i To i + 6
tam += k.Substring(j, 1)

NGO TH TUYET HA T012825

http://thuviendientu.org

O AN BAO MAT THONG TIN


HE MA DES

Next
i=i+8
End While
Return tam
End Function
Function ls(ByVal s As String, ByVal n As Integer) As String
Return s.Substring(n, s.Length - n) + s.Substring(0, n)
End Function
Ham tao day khoa
Sub taodaykhoa(ByVal khoa As String)
khoa = hoanvi_pc1(khoa)
Dim d(2) As String
Dim c(2) As String
c(0) = khoa.Substring(0, 28)
c(0) = ls(c(0), 1)
d(0) = khoa.Substring(28, 28)
d(0) = ls(d(0), 1)
daykhoa(0) = hoanvi_pc2(c(0) + d(0))
Dim i As Integer
For i = 1 To 2
If i = 1 Then
c(i) = ls(c(i - 1), 1)
d(i) = ls(d(i - 1), 1)
Else
c(i) = ls(c(i - 1), 2)
d(i) = ls(d(i - 1), 2)
End If
daykhoa(i) = hoanvi_pc2(c(i) + d(i))

NGO TH TUYET HA T012825

http://thuviendientu.org

O AN BAO MAT THONG TIN


HE MA DES

Next i
End Sub
Ham x ly chuoi nhap
Function bi_acsii(ByVal int As Integer) As String
Dim tam(7) As Char
Dim i As Integer
For i = 0 To 7
tam(i) = (int Mod 2).ToString
int \= 2
Next
Array.Reverse(tam)
Return tam
End Function
Ham ma hoa
Function mahoa(ByVal x As String) As String
Dim i, j As Integer
Dim l(2) As String
Dim r(2) As String
'x = hoanvi_ip(x)
Dim l0 As String = x.Substring(0, 32)
Dim r0 As String = x.Substring(32, 32)
l(0) = r0
For i = 0 To 31
r(0) += (l0.Substring(i, 1) Xor f(r0, daykhoa(0)).Substring(i,
1)).ToString
Next
For i = 1 To 2
l(i) = r(i - 1)

NGO TH TUYET HA T012825

http://thuviendientu.org

O AN BAO MAT THONG TIN


HE MA DES

Dim a As String = f(r(i - 1), daykhoa(i))


For j = 0 To 31
r(i) += (l(i - 1).Substring(j, 1) Xor a.Substring(j, 1)).ToString
Next j
Next i
Dim t As String = l(2) + r(2)
Return t
'Return hoanvi_iptru(r(2) + l(2))
End Function
Ham tao ham f
Function f(ByVal r As String, ByVal daykhoa_k As String) As String
Dim i As Integer
Dim e As String
Dim hv As String = hoanvi_e(r)
For i = 0 To 47
e

+=

(hv.Substring(i,

1)

Xor

daykhoa_k.Substring(i,

1)).ToString
Next
Dim b(7) As String
Dim c As String
For i = 0 To 7
b(i) = e.Substring(i * 6, 6)
Dim haibitdaucuoi As String = b(i).Substring(0, 1) +
b(i).Substring(5, 1)
Dim bonbitgiua As String = b(i).Substring(1, 4)
Dim srow = thapphan(haibitdaucuoi)
Dim scol = thapphan(bonbitgiua)
Dim sij As Integer = s(i).Rows(srow).Item(scol)
c += binary(sij, 4)
NGO TH TUYET HA T012825

http://thuviendientu.org

O AN BAO MAT THONG TIN


HE MA DES

Next
Return hoanvi_p(c)
End Function
Ham oi ra ch
'Function doirachu(ByVal y As String) As String
'Dim tam As String = y
'Dim tam1 As String = ""
'Dim so As Integer
'Dim i As Integer
'Dim j As Integer
'While i < tam.Length - 1
'so = 0
'For j = i To i + 7
'If tam.Substring(j, 1) = 1 Then
'so += 2 ^ (7 - (j - i))
'End If
'Next
'tam1 += Chr(so)
'i = i + 8
'End While
'Return tam1
'End Function
Function doirachu(ByVal y As String) As String
Dim tam As String = y
Dim tam1 As String = ""
Dim so As Integer
Dim i, j As Integer
While i < tam.Length - 1

NGO TH TUYET HA T012825

http://thuviendientu.org

O AN BAO MAT THONG TIN


HE MA DES

so = 0
For j = i To i + 3
If tam.Substring(j, 1) = 1 Then
so += 2 ^ (3 - (j - i))
End If
Next
tam1 += chucai(so)
i=i+4
End While
Return tam1
End Function
Ch cai
Function chucai(ByVal so As Integer) As Char
Select Case so
Case 0
Return "0"
Case 1
Return "1"
Case 2
Return "2"
Case 3
Return "3"
Case 4
Return "4"
Case 5
Return "5"
Case 6
Return "6"

NGO TH TUYET HA T012825

http://thuviendientu.org

O AN BAO MAT THONG TIN


HE MA DES

Case 7
Return "7"
Case 8
Return "8"
Case 9
Return "9"
Case 10
Return "A"
Case 11
Return "B"
Case 12
Return "C"
Case 13
Return "D"
Case 14
Return "E"
Case 15
Return "F"
End Select
End Function
Ham chuyen ma asc
Function asc(ByVal a As Char)
Select Case a
Case "0"
asc = 0
Case "1"
asc = 1
Case "2"

NGO TH TUYET HA T012825

http://thuviendientu.org

asc = 2
Case "3"
asc = 3
Case "4"
asc = 4
Case "5"
asc = 5
Case "6"
asc = 6
Case "7"
asc = 7
Case "8"
asc = 8
Case "9"
asc = 9
Case "A"
asc = 10
Case "B"
asc = 11
Case "C"
asc = 12
Case "D"
asc = 13
Case "E"
asc = 14
Case "F"
asc = 15
End Select

NGO TH TUYET HA T012825

O AN BAO MAT THONG TIN


HE MA DES

http://thuviendientu.org

O AN BAO MAT THONG TIN


HE MA DES

End Function
Ham chon khoa
Sub chonkhoadung()
Dim i As Integer
Dim x As String = banro(0)
'For i = 0 To banro(0).Length - 1
'x += binary(asc(banro(0).Substring(i, 1)), hebit)
'Next
For i = 0 To 255
taodaykhoa(chuoikhoa(i))
Dim banma1 As String = doirachu(mahoa(x))
If banma1 = doirachu(banma(0)) Then
chuoikhoa(i) = chuoikhoa(i).Replace("#", "0")
txtkhoa.Text = doirachu(chuoikhoa(i))
Exit Sub
End If
Next
MessageBox.Show("khong tim duoc khoa")
End Sub
Private Sub txtbanro_TextChanged(ByVal sender As System.Object,
ByVal e As System.EventArgs) Handles txtbanro.TextChanged
btthamma.Enabled = True
lbthongbao.Text = ""
End Sub
Private
Sub
txtbanma_TextChanged(ByVal
sender
As
System.Object,
ByVal
e
As
System.EventArgs)
Handles
txtbanma.TextChanged

NGO TH TUYET HA T012825

http://thuviendientu.org

O AN BAO MAT THONG TIN


HE MA DES

btthamma.Enabled = True
lbthongbao.Text = ""
End Sub

End Class

NGO TH TUYET HA T012825

You might also like