DES加解密算法实现
Option Explicit
Private Declare Sub CopyMemory Lib "kernel32" Alias "RtlMoveMemory" (Destination As Any, Source As Any, ByVal Length As Long)
'置换表
Private IP(63) As Byte 'IP置换码
Private IP_1(63) As Byte 'IP-1置换码
Private E(47) As Byte 'E膨胀码
Private P(31) As Byte 'P变换码
Private S1(3, 15) As Byte 'S盒1
Private S2(3, 15) As Byte 'S盒2
Private S3(3, 15) As Byte 'S盒3
Private S4(3, 15) As Byte 'S盒4
Private S5(3, 15) As Byte 'S盒5
Private S6(3, 15) As Byte 'S盒6
Private S7(3, 15) As Byte 'S盒7
Private S8(3, 15) As Byte 'S盒8
Private PC_1(55) As Byte
Private PC_2(47) As Byte
Private Lsi(16) As Byte '循环左移位
Private Sub Class_Initialize()
Dim i As Integer
IP(0) = 57 ' 58
IP(1) = 49 ' 50
IP(2) = 41 ' 42
IP(3) = 33 ' 34
IP(4) = 25 ' 26
IP(5) = 17 ' 18
IP(6) = 9 ' 10
IP(7) = 1 ' 2
IP(8) = 59 ' 60
IP(9) = 51 ' 52
IP(10) = 43 ' 44
IP(11) = 35 ' 36
IP(12) = 27 ' 28
IP(13) = 19 ' 20
IP(14) = 11 ' 12
IP(15) = 3 ' 4
IP(16) = 61 ' 62
IP(17) = 53 ' 54
IP(18) = 45 ' 46
IP(19) = 37 ' 38
IP(20) = 29 ' 30
IP(21) = 21 ' 22
IP(22) = 13 ' 14
IP(23) = 5 ' 6
IP(24) = 63 ' 64
IP(25) = 55 ' 56
IP(26) = 47 ' 48
IP(27) = 39 ' 40
IP(28) = 31 ' 32
IP(29) = 23 ' 24
IP(30) = 15 ' 16
IP(31) = 7 ' 8
IP(32) = 56 ' 57
IP(33) = 48 ' 49
IP(34) = 40 ' 41
IP(35) = 32 ' 33
IP(36) = 24 ' 25
IP(37) = 16 ' 17
IP(38) = 8 ' 9
IP(39) = 0 ' 1
IP(40) = 58 ' 59
IP(41) = 50 ' 51
IP(42) = 42 ' 43
IP(43) = 34 ' 35
IP(44) = 26 ' 27
IP(45) = 18 ' 19
IP(46) = 10 ' 11
IP(47) = 2 ' 3
IP(48) = 60 ' 61
IP(49) = 52 ' 53
IP(50) = 44 ' 45
IP(51) = 36 ' 37
IP(52) = 28 ' 29
IP(53) = 20 ' 21
IP(54) = 12 ' 13
IP(55) = 4 ' 5
IP(56) = 62 ' 63
IP(57) = 54 ' 55
IP(58) = 46 ' 47
IP(59) = 38 ' 39
IP(60) = 30 ' 31
IP(61) = 22 ' 23
IP(62) = 14 ' 15
IP(63) = 6 ' 7
IP_1(0) = 39 ' 40
IP_1(1) = 7 ' 8
IP_1(2) = 47 ' 48
IP_1(3) = 15 ' 16
IP_1(4) = 55 ' 56
IP_1(5) = 23 ' 24
IP_1(6) = 63 ' 64
IP_1(7) = 31 ' 32
IP_1(8) = 38 ' 39
IP_1(9) = 6 ' 7
IP_1(10) = 46 ' 47
IP_1(11) = 14 ' 15
IP_1(12) = 54 ' 55
IP_1(13) = 22 ' 23
IP_1(14) = 62 ' 63
IP_1(15) = 30 ' 31
IP_1(16) = 37 ' 38
IP_1(17) = 5 ' 6
IP_1(18) = 45 ' 46
IP_1(19) = 13 ' 14
IP_1(20) = 53 ' 54
IP_1(21) = 21 ' 22
IP_1(22) = 61 ' 62
IP_1(23) = 29 ' 30
IP_1(24) = 36 ' 37
IP_1(25) = 4 ' 5
IP_1(26) = 44 ' 45
IP_1(27) = 12 ' 13
IP_1(28) = 52 ' 53
IP_1(29) = 20 ' 21
IP_1(30) = 60 ' 61
IP_1(31) = 28 ' 29
IP_1(32) = 35 ' 36
IP_1(33) = 3 ' 4
IP_1(34) = 43 ' 44
IP_1(35) = 11 ' 12
IP_1(36) = 51 ' 52
IP_1(37) = 19 ' 20
IP_1(38) = 59 ' 60
IP_1(39) = 27 ' 28
IP_1(40) = 34 ' 35
IP_1(41) = 2 ' 3
IP_1(42) = 42 ' 43
IP_1(43) = 10 ' 11
IP_1(44) = 50 ' 51
IP_1(45) = 18 ' 19
IP_1(46) = 58 ' 59
IP_1(47) = 26 ' 27
IP_1(48) = 33 ' 34
IP_1(49) = 1 ' 2
IP_1(50) = 41 ' 42
IP_1(51) = 9 ' 10
IP_1(52) = 49 ' 50
IP_1(53) = 17 ' 18
IP_1(54) = 57 ' 58
IP_1(55) = 25 ' 26
IP_1(56) = 32 ' 33
IP_1(57) = 0 ' 1
IP_1(58) = 40 ' 41
IP_1(59) = 8 ' 9
IP_1(60) = 48 ' 49
IP_1(61) = 16 ' 17
IP_1(62) = 56 ' 57
IP_1(63) = 24 ' 25
E(0) = 31
For i = 1 To 5
E(i) = i - 1
Next
For i = 6 To 11
E(i) = i - 3
Next
For i = 12 To 17
E(i) = i - 5
Next
For i = 18 To 23
E(i) = i - 7
Next
For i = 24 To 29
E(i) = i - 9
Next
For i = 30 To 35
E(i) = i - 11
Next
For i = 36 To 41
E(i) = i - 13
Next
For i = 42 To 46
E(i) = i - 15
Next
E(47) = 30
P(0) = 15 ' 16
P(1) = 6 ' 7
P(2) = 19 ' 20
P(3) = 20 ' 21
P(4) = 28 ' 29
P(5) = 11 ' 12
P(6) = 27 ' 28
P(7) = 16 ' 17
P(8) = 0 ' 1
P(9) = 14 ' 15
P(10) = 22 ' 23
P(11) = 25 ' 26
P(12) = 4 ' 5
P(13) = 17 ' 18
P(14) = 30 ' 31
P(15) = 9 ' 10
P(16) = 1 ' 2
P(17) = 7 ' 8
P(18) = 23 ' 24
P(19) = 13 ' 14
P(20) = 31 ' 32
P(21) = 26 ' 27
P(22) = 2 ' 3
P(23) = 8 ' 9
P(24) = 18 ' 19
P(25) = 12 ' 13
P(26) = 29 ' 30
P(27) = 5 ' 6
P(28) = 21 ' 22
P(29) = 10 ' 11
P(30) = 3 ' 4
P(31) = 24 ' 25
S1(0, 0) = 14
S1(0, 1) = 4
S1(0, 2) = 13
S1(0, 3) = 1
S1(0, 4) = 2
S1(0, 5) = 15
S1(0, 6) = 11
S1(0, 7) = 8
S1(0, 8) = 3
S1(0, 9) = 10
S1(0, 10) = 6
S1(0, 11) = 12
S1(0, 12) = 5
S1(0, 13) = 9
S1(0, 14) = 0
S1(0, 15) = 7
S1(1, 0) = 0
S1(1, 1) = 15
S1(1, 2) = 7
S1(1, 3) = 4
S1(1, 4) = 14
S1(1, 5) = 2
S1(1, 6) = 13
S1(1, 7) = 1
S1(1, 8) = 10
S1(1, 9) = 6
S1(1, 10) = 12
S1(1, 11) = 11
S1(1, 12) = 9
S1(1, 13) = 5
S1(1, 14) = 3
S1(1, 15) = 8
S1(2, 0) = 4
S1(2, 1) = 1
S1(2, 2) = 14
S1(2, 3) = 8
S1(2, 4) = 13
S1(2, 5) = 6
S1(2, 6) = 2
S1(2, 7) = 11
S1(2, 8) = 15
S1(2, 9) = 12
S1(2, 10) = 9
S1(2, 11) = 7
S1(2, 12) = 3
S1(2, 13) = 10
S1(2, 14) = 5
S1(2, 15) = 0
S1(3, 0) = 15
S1(3, 1) = 12
S1(3, 2) = 8
S1(3, 3) = 2
S1(3, 4) = 4
S1(3, 5) = 9
S1(3, 6) = 1
S1(3, 7) = 7
S1(3, 8) = 5
S1(3, 9) = 11
S1(3, 10) = 3
S1(3, 11) = 14
S1(3, 12) = 10
S1(3, 13) = 0
S1(3, 14) = 6
S1(3, 15) = 13
S2(0, 0) = 15
S2(0, 1) = 1
S2(0, 2) = 8
S2(0, 3) = 14
S2(0, 4) = 6
S2(0, 5) = 11
S2(0, 6) = 3
S2(0, 7) = 4
S2(0, 8) = 9
S2(0, 9) = 7
S2(0, 10) = 2
S2(0, 11) = 13
S2(0, 12) = 12
S2(0, 13) = 0
S2(0, 14) = 5
S2(0, 15) = 10
S2(1, 0) = 3
S2(1, 1) = 13
S2(1, 2) = 4
S2(1, 3) = 7
S2(1, 4) = 15
S2(1, 5) = 2
S2(1, 6) = 8
S2(1, 7) = 14
S2(1, 8) = 12
S2(1, 9) = 0
S2(1, 10) = 1
S2(1, 11) = 10
S2(1, 12) = 6
S2(1, 13) = 9
S2(1, 14) = 11
S2(1, 15) = 5
S2(2, 0) = 0
S2(2, 1) = 14
S2(2, 2) = 7
S2(2, 3) = 11
S2(2, 4) = 10
S2(2, 5) = 4
S2(2, 6) = 13
S2(2, 7) = 1
S2(2, 8) = 5
S2(2, 9) = 8
S2(2, 10) = 12
S2(2, 11) = 6
S2(2, 12) = 9
S2(2, 13) = 3
S2(2, 14) = 2
S2(2, 15) = 15
S2(3, 0) = 13
S2(3, 1) = 8
S2(3, 2) = 10
S2(3, 3) = 1
S2(3, 4) = 3
S2(3, 5) = 15
S2(3, 6) = 4
S2(3, 7) = 2
S2(3, 8) = 11
S2(3, 9) = 6
S2(3, 10) = 7
S2(3, 11) = 12
S2(3, 12) = 0
S2(3, 13) = 5
S2(3, 14) = 14
S2(3, 15) = 9