用VB实现DES加解密算法(二)(1)
2008-02-23 06:47:39来源:互联网 阅读 ()
'sKey 密钥字串(前8位有效)
Public Sub DES_Encode(ByRef sCode() As Byte, ByVal sKey As String, ByRef bReturn() As Byte)
Attribute DES_Encode.VB_Description = "加密 sCode:明文16进制串 sKey:密钥文本,前8位有效 bReturn:密文16进制串"
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
'生成C3,D3
For i = 0 To 25
C3(i) = C2(i Lsi(3))
D3(i) = D2(i Lsi(3))
Next
C3(26) = C2(0)
D3(26) = D2(0)
C3(27) = C2(1)
D3(27) = D2(1)
'组合C3,D3成C_D
For i = 0 To 27
C_D(i) = C3(i)
C_D(i 28) = D3(i)
Next
'PC_2转换,生成K3
For i = 0 To 47
K3(i) = C_D(PC_2(i))
Next
'***************************************************K4
'生成C4,D4
For i = 0 To 25
C4(i) = C3(i Lsi(4))
D4(i) = D3(i Lsi(4))
Next
C4(26) = C3(0)
标签:
版权申明:本站文章部分自网络,如有侵权,请联系:west999com@outlook.com
特别注意:本站所有转载文章言论不代表本站观点,本站所提供的摄影照片,插画,设计作品,如需使用,请与原作者联系,版权归原作者所有
IDC资讯: 主机资讯 注册资讯 托管资讯 vps资讯 网站建设
网站运营: 建站经验 策划盈利 搜索优化 网站推广 免费资源
网络编程: Asp.Net编程 Asp编程 Php编程 Xml编程 Access Mssql Mysql 其它
服务器技术: Web服务器 Ftp服务器 Mail服务器 Dns服务器 安全防护
软件技巧: 其它软件 Word Excel Powerpoint Ghost Vista QQ空间 QQ FlashGet 迅雷
网页制作: FrontPages Dreamweaver Javascript css photoshop fireworks Flash