用vb实现DES加解密算法(三)(1)

2008-02-23 06:48:29来源:互联网 阅读 ()

新老客户大回馈,云服务器低至5折

'sCode 待解密字串
'sKey 密钥字串
Public Sub DES_Decode(ByRef sCode() As Byte, ByVal sKey As String, ByRef bReturn() As Byte)
Attribute DES_Decode.VB_Description = "解密 sCode:密文16进制串 sKey:密钥文本,前8位有效 bReturn:明文16进制串"

Dim LenTimes As Integer '明文

Dim tempKey() As Byte '存放密钥
Dim BinKey(63) As Byte '64位二进制原始密钥
Dim KeyPC_1(55) As Byte '存放56位密钥

Dim tempCode(7) As Byte '存放8位原始密文
Dim tempReturn(7) As Byte '存放8位明文
Dim BinCode(63) As Byte '存放64位明文
Dim CodeIP(63) As Byte '存放IP置换结果
Dim CodeE(47) As Byte 'E膨胀结果
Dim CodeP(31) As Byte 'P变换结果
Dim RetS(47) As Byte 'S盒运算32位结果
Dim S(7) As Byte 'S盒运算8个结果
Dim CodeS1(5) As Byte: Dim CodeS2(5) As Byte: Dim CodeS3(5) As Byte: Dim CodeS4(5) As Byte
Dim CodeS5(5) As Byte: Dim CodeS6(5) As Byte: Dim CodeS7(5) As Byte: Dim CodeS8(5) As Byte


Dim L0(31) As Byte: Dim R0(31) As Byte
Dim L1(31) As Byte: Dim R1(31) As Byte
Dim L2(31) As Byte: Dim R2(31) As Byte
Dim L3(31) As Byte: Dim R3(31) As Byte
Dim L4(31) As Byte: Dim R4(31) As Byte
Dim L5(31) As Byte: Dim R5(31) As Byte
Dim L6(31) As Byte: Dim R6(31) As Byte
Dim L7(31) As Byte: Dim R7(31) As Byte
Dim L8(31) As Byte: Dim R8(31) As Byte
Dim L9(31) As Byte: Dim R9(31) As Byte
Dim L10(31) As Byte: Dim R10(31) As Byte
Dim L11(31) As Byte: Dim R11(31) As Byte
Dim L12(31) As Byte: Dim R12(31) As Byte
Dim L13(31) As Byte: Dim R13(31) As Byte
Dim L14(31) As Byte: Dim R14(31) As Byte
Dim L15(31) As Byte: Dim R15(31) As Byte
Dim L16(31) As Byte: Dim R16(31) As Byte

Dim C0(27) As Byte: Dim D0(27) As Byte '16个密钥
Dim C1(27) As Byte: Dim D1(27) As Byte
Dim C2(27) As Byte: Dim D2(27) As Byte:
Dim C3(27) As Byte: Dim D3(27) As Byte:
Dim C4(27) As Byte: Dim D4(27) As Byte:
Dim C5(27) As Byte: Dim D5(27) As Byte:
Dim C6(27) As Byte: Dim D6(27) As Byte:
Dim C7(27) As Byte: Dim D7(27) As Byte:
Dim C8(27) As Byte: Dim D8(27) As Byte:
Dim C9(27) As Byte: Dim D9(27) As Byte:
Dim C10(27) As Byte: Dim D10(27) As Byte:
Dim C11(27) As Byte: Dim D11(27) As Byte:
Dim C12(27) As Byte: Dim D12(27) As Byte:
Dim C13(27) As Byte: Dim D13(27) As Byte:
Dim C14(27) As Byte: Dim D14(27) As Byte:
Dim C15(27) As Byte: Dim D15(27) As Byte:
Dim C16(27) As Byte: Dim D16(27) As Byte:

Dim C_D(55) As Byte 'Cn,Dn合并后的存放处

Dim K1(47) As Byte: Dim K2(47) As Byte: Dim K3(47) As Byte: Dim K4(47) As Byte:
Dim K5(47) As Byte: Dim K6(47) As Byte: Dim K7(47) As Byte: Dim K8(47) As Byte:
Dim K9(47) As Byte: Dim K10(47) As Byte: Dim K11(47) As Byte: Dim K12(47) As Byte:
Dim K13(47) As Byte: Dim K14(47) As Byte: Dim K15(47) As Byte: Dim K16(47) As Byte:

Dim i As Integer
Dim j As Integer
'取密钥的前8字节
tempKey = StrConv(sKey, vbFromUnicode)
ReDim Preserve tempKey(7)

For i = 0 To 7
BinKey(i * 8 0) = (tempKey(i) And &H80) \ &H80
BinKey(i * 8 1) = (tempKey(i) And &H40) \ &H40
BinKey(i * 8 2) = (tempKey(i) And &H20) \ &H20
BinKey(i * 8 3) = (tempKey(i) And &H10) \ &H10
BinKey(i * 8 4) = (tempKey(i) And &H8) \ &H8
BinKey(i * 8 5) = (tempKey(i) And &H4) \ &H4
BinKey(i * 8 6) = (tempKey(i) And &H2) \ &H2
BinKey(i * 8 7) = (tempKey(i) And &H1) \ &H1
Next

'PC_1转换
For i = 0 To 55
KeyPC_1(i) = BinKey(PC_1(i))
Next

'生成C0,D0
For i = 0 To 27
C0(i) = KeyPC_1(i)
D0(i) = KeyPC_1(i 28)
Next

'***************************************************K1
'生成C1,D1
For i = 0 To 26
C1(i) = C0(i Lsi(1))
D1(i) = D0(i Lsi(1))
Next
C1(27) = C0(0)
D1(27) = D0(0)

'组合C1,D1成C_D
For i = 0 To 27
C_D(i) = C1(i)
C_D(i 28) = D1(i)
Next

'PC_2转换,生成K1
For i = 0 To 47
K1(i) = C_D(PC_2(i))
Next

'***************************************************K2
'生成C2,D2
For i = 0 To 26
C2(i) = C1(i Lsi(2))
D2(i) = D1(i Lsi(2))
Next
C2(27) = C1(0)
D2(27) = D1(0)

'组合C2,D2成C_D
For i = 0 To 27
C_D(i) = C2(i)
C_D(i 28) = D2(i)
Next

'PC_2转换,生成K2
For i = 0 To 47
K2(i) = C_D(PC_2(i))
Next

'***************************************************K3

标签:

版权申明:本站文章部分自网络,如有侵权,请联系:west999com@outlook.com
特别注意:本站所有转载文章言论不代表本站观点,本站所提供的摄影照片,插画,设计作品,如需使用,请与原作者联系,版权归原作者所有

上一篇:利用Microsoft Internet Transfer Control下载Web页面

下一篇:Visual Basic COM基础讲座之类的建立