在CSDN上找的,谁知道怎么来调用?麻烦给出例子!
哪位高手给研究研究!
实在看不懂!下面三篇是源代码!
http://dev.csdn.net/develop/article/20/20658.shtm
http://dev.csdn.net/develop/article/20/20659.shtm
http://dev.csdn.net/develop/article/20/20660.shtm
哪位高手给研究研究!
实在看不懂!下面三篇是源代码!
http://dev.csdn.net/develop/article/20/20658.shtm
http://dev.csdn.net/develop/article/20/20659.shtm
http://dev.csdn.net/develop/article/20/20660.shtm
DES_EncodeDES_Encode(ByRef sCode() As Byte, ByVal sKey As String, ByRef bReturn() As Byte)
DES_Decode(ByRef sCode() As Byte, ByVal sKey As String, ByRef bReturn() As Byte)ByVal传进一个字符串即可
ByRef是返回的加密或者解密前后后的Byte数组
'------------------------------------
'
'Information concerning the DES
'algorithm can be found at:
'http://csrc.nist.gov/fips/fips46-3.pdf
'
'(c) 2000, Fredrik Qvarfort
'Option Explicit'For progress notifications
Event Progress(Percent As Long)'Key-dependant
Private m_Key(0 To 47, 1 To 16) As Byte'Buffered key value
Private m_KeyValue As String'Values given in the DES standard
Private m_E(0 To 63) As Byte
Private m_P(0 To 31) As Byte
Private m_IP(0 To 63) As Byte
Private m_PC1(0 To 55) As Byte
Private m_PC2(0 To 47) As Byte
Private m_IPInv(0 To 63) As Byte
Private m_EmptyArray(0 To 63) As Byte
Private m_LeftShifts(1 To 16) As Byte
Private m_sBox(0 To 7, 0 To 1, 0 To 1, 0 To 1, 0 To 1, 0 To 1, 0 To 1) As LongPrivate Declare Sub CopyMem Lib "kernel32" Alias "RtlMoveMemory" (Destination As Any, Source As Any, ByVal Length As Long)
Private Static Sub Byte2Bin(ByteArray() As Byte, ByteLen As Long, BinaryArray() As Byte) Dim a As Long
Dim ByteValue As Byte
Dim BinLength As Long
'Clear the destination array, faster than
'setting the data to zero in the loop below
Call CopyMem(BinaryArray(0), m_EmptyArray(0), ByteLen * 8)
'Add binary 1's where needed
BinLength = 0
For a = 0 To (ByteLen - 1)
ByteValue = ByteArray(a)
If (ByteValue And 128) Then BinaryArray(BinLength) = 1
If (ByteValue And 64) Then BinaryArray(BinLength + 1) = 1
If (ByteValue And 32) Then BinaryArray(BinLength + 2) = 1
If (ByteValue And 16) Then BinaryArray(BinLength + 3) = 1
If (ByteValue And 8) Then BinaryArray(BinLength + 4) = 1
If (ByteValue And 4) Then BinaryArray(BinLength + 5) = 1
If (ByteValue And 2) Then BinaryArray(BinLength + 6) = 1
If (ByteValue And 1) Then BinaryArray(BinLength + 7) = 1
BinLength = BinLength + 8
NextEnd Sub
Private Static Sub Bin2Byte(BinaryArray() As Byte, ByteLen As Long, ByteArray() As Byte) Dim a As Long
Dim ByteValue As Byte
Dim BinLength As Long
'Calculate byte values
BinLength = 0
For a = 0 To (ByteLen - 1)
ByteValue = 0
If (BinaryArray(BinLength) = 1) Then ByteValue = ByteValue + 128
If (BinaryArray(BinLength + 1) = 1) Then ByteValue = ByteValue + 64
If (BinaryArray(BinLength + 2) = 1) Then ByteValue = ByteValue + 32
If (BinaryArray(BinLength + 3) = 1) Then ByteValue = ByteValue + 16
If (BinaryArray(BinLength + 4) = 1) Then ByteValue = ByteValue + 8
If (BinaryArray(BinLength + 5) = 1) Then ByteValue = ByteValue + 4
If (BinaryArray(BinLength + 6) = 1) Then ByteValue = ByteValue + 2
If (BinaryArray(BinLength + 7) = 1) Then ByteValue = ByteValue + 1
ByteArray(a) = ByteValue
BinLength = BinLength + 8
Next
End Sub
Dim i As Long
Dim L(0 To 31) As Byte
Dim R(0 To 31) As Byte
Dim RL(0 To 63) As Byte
Dim sBox(0 To 31) As Byte
Dim LiRi(0 To 31) As Byte
Dim ERxorK(0 To 47) As Byte
Dim BinBlock(0 To 63) As Byte
'Convert the block into a binary array
'(I do believe this is the best solution
'in VB for the DES algorithm, but it is
'still slow as xxxx)
Call Byte2Bin(BlockData(), 8, BinBlock())
'Apply the IP permutation and split the
'block into two halves, L[] and R[]
For a = 0 To 31
L(a) = BinBlock(m_IP(a))
R(a) = BinBlock(m_IP(a + 32))
Next
'Apply the 16 subkeys on the block
For i = 1 To 16
'E(R[i]) xor K[i]
ERxorK(0) = R(31) Xor m_Key(0, i)
ERxorK(1) = R(0) Xor m_Key(1, i)
ERxorK(2) = R(1) Xor m_Key(2, i)
ERxorK(3) = R(2) Xor m_Key(3, i)
ERxorK(4) = R(3) Xor m_Key(4, i)
ERxorK(5) = R(4) Xor m_Key(5, i)
ERxorK(6) = R(3) Xor m_Key(6, i)
ERxorK(7) = R(4) Xor m_Key(7, i)
ERxorK(8) = R(5) Xor m_Key(8, i)
ERxorK(9) = R(6) Xor m_Key(9, i)
ERxorK(10) = R(7) Xor m_Key(10, i)
ERxorK(11) = R(8) Xor m_Key(11, i)
ERxorK(12) = R(7) Xor m_Key(12, i)
ERxorK(13) = R(8) Xor m_Key(13, i)
ERxorK(14) = R(9) Xor m_Key(14, i)
ERxorK(15) = R(10) Xor m_Key(15, i)
ERxorK(16) = R(11) Xor m_Key(16, i)
ERxorK(17) = R(12) Xor m_Key(17, i)
ERxorK(18) = R(11) Xor m_Key(18, i)
ERxorK(19) = R(12) Xor m_Key(19, i)
ERxorK(20) = R(13) Xor m_Key(20, i)
ERxorK(21) = R(14) Xor m_Key(21, i)
ERxorK(22) = R(15) Xor m_Key(22, i)
ERxorK(23) = R(16) Xor m_Key(23, i)
ERxorK(24) = R(15) Xor m_Key(24, i)
ERxorK(25) = R(16) Xor m_Key(25, i)
ERxorK(26) = R(17) Xor m_Key(26, i)
ERxorK(27) = R(18) Xor m_Key(27, i)
ERxorK(28) = R(19) Xor m_Key(28, i)
ERxorK(29) = R(20) Xor m_Key(29, i)
ERxorK(30) = R(19) Xor m_Key(30, i)
ERxorK(31) = R(20) Xor m_Key(31, i)
ERxorK(32) = R(21) Xor m_Key(32, i)
ERxorK(33) = R(22) Xor m_Key(33, i)
ERxorK(34) = R(23) Xor m_Key(34, i)
ERxorK(35) = R(24) Xor m_Key(35, i)
ERxorK(36) = R(23) Xor m_Key(36, i)
ERxorK(37) = R(24) Xor m_Key(37, i)
ERxorK(38) = R(25) Xor m_Key(38, i)
ERxorK(39) = R(26) Xor m_Key(39, i)
ERxorK(40) = R(27) Xor m_Key(40, i)
ERxorK(41) = R(28) Xor m_Key(41, i)
ERxorK(42) = R(27) Xor m_Key(42, i)
ERxorK(43) = R(28) Xor m_Key(43, i)
ERxorK(44) = R(29) Xor m_Key(44, i)
ERxorK(45) = R(30) Xor m_Key(45, i)
ERxorK(46) = R(31) Xor m_Key(46, i)
ERxorK(47) = R(0) Xor m_Key(47, i)
'Apply the s-boxes
Call CopyMem(sBox(0), m_sBox(0, ERxorK(0), ERxorK(1), ERxorK(2), ERxorK(3), ERxorK(4), ERxorK(5)), 4)
Call CopyMem(sBox(4), m_sBox(1, ERxorK(6), ERxorK(7), ERxorK(8), ERxorK(9), ERxorK(10), ERxorK(11)), 4)
Call CopyMem(sBox(8), m_sBox(2, ERxorK(12), ERxorK(13), ERxorK(14), ERxorK(15), ERxorK(16), ERxorK(17)), 4)
Call CopyMem(sBox(12), m_sBox(3, ERxorK(18), ERxorK(19), ERxorK(20), ERxorK(21), ERxorK(22), ERxorK(23)), 4)
Call CopyMem(sBox(16), m_sBox(4, ERxorK(24), ERxorK(25), ERxorK(26), ERxorK(27), ERxorK(28), ERxorK(29)), 4)
Call CopyMem(sBox(20), m_sBox(5, ERxorK(30), ERxorK(31), ERxorK(32), ERxorK(33), ERxorK(34), ERxorK(35)), 4)
Call CopyMem(sBox(24), m_sBox(6, ERxorK(36), ERxorK(37), ERxorK(38), ERxorK(39), ERxorK(40), ERxorK(41)), 4)
Call CopyMem(sBox(28), m_sBox(7, ERxorK(42), ERxorK(43), ERxorK(44), ERxorK(45), ERxorK(46), ERxorK(47)), 4)
'L[i] xor P(R[i])
LiRi(0) = L(0) Xor sBox(15)
LiRi(1) = L(1) Xor sBox(6)
LiRi(2) = L(2) Xor sBox(19)
LiRi(3) = L(3) Xor sBox(20)
LiRi(4) = L(4) Xor sBox(28)
LiRi(5) = L(5) Xor sBox(11)
LiRi(6) = L(6) Xor sBox(27)
LiRi(7) = L(7) Xor sBox(16)
LiRi(8) = L(8) Xor sBox(0)
LiRi(9) = L(9) Xor sBox(14)
LiRi(10) = L(10) Xor sBox(22)
LiRi(11) = L(11) Xor sBox(25)
LiRi(12) = L(12) Xor sBox(4)
LiRi(13) = L(13) Xor sBox(17)
LiRi(14) = L(14) Xor sBox(30)
LiRi(15) = L(15) Xor sBox(9)
LiRi(16) = L(16) Xor sBox(1)
LiRi(17) = L(17) Xor sBox(7)
LiRi(18) = L(18) Xor sBox(23)
LiRi(19) = L(19) Xor sBox(13)
LiRi(20) = L(20) Xor sBox(31)
LiRi(21) = L(21) Xor sBox(26)
LiRi(22) = L(22) Xor sBox(2)
LiRi(23) = L(23) Xor sBox(8)
LiRi(24) = L(24) Xor sBox(18)
LiRi(25) = L(25) Xor sBox(12)
LiRi(26) = L(26) Xor sBox(29)
LiRi(27) = L(27) Xor sBox(5)
LiRi(28) = L(28) Xor sBox(21)
LiRi(29) = L(29) Xor sBox(10)
LiRi(30) = L(30) Xor sBox(3)
LiRi(31) = L(31) Xor sBox(24)
'Prepare for next round
Call CopyMem(L(0), R(0), 32)
Call CopyMem(R(0), LiRi(0), 32)
Next
'Concatenate R[]L[]
Call CopyMem(RL(0), R(0), 32)
Call CopyMem(RL(32), L(0), 32) 'Apply the invIP permutation
For a = 0 To 63
BinBlock(a) = RL(m_IPInv(a))
Next
'Convert the binaries into a byte array
Call Bin2Byte(BinBlock(), 8, BlockData())End Sub
Dim i As Long
Dim L(0 To 31) As Byte
Dim R(0 To 31) As Byte
Dim RL(0 To 63) As Byte
Dim sBox(0 To 31) As Byte
Dim LiRi(0 To 31) As Byte
Dim ERxorK(0 To 47) As Byte
Dim BinBlock(0 To 63) As Byte
'Convert the block into a binary array
'(I do believe this is the best solution
'in VB for the DES algorithm, but it is
'still slow as xxxx)
Call Byte2Bin(BlockData(), 8, BinBlock())
'Apply the IP permutation and split the
'block into two halves, L[] and R[]
For a = 0 To 31
L(a) = BinBlock(m_IP(a))
R(a) = BinBlock(m_IP(a + 32))
Next
'Apply the 16 subkeys on the block
For i = 16 To 1 Step -1
'E(R[i]) xor K[i]
ERxorK(0) = R(31) Xor m_Key(0, i)
ERxorK(1) = R(0) Xor m_Key(1, i)
ERxorK(2) = R(1) Xor m_Key(2, i)
ERxorK(3) = R(2) Xor m_Key(3, i)
ERxorK(4) = R(3) Xor m_Key(4, i)
ERxorK(5) = R(4) Xor m_Key(5, i)
ERxorK(6) = R(3) Xor m_Key(6, i)
ERxorK(7) = R(4) Xor m_Key(7, i)
ERxorK(8) = R(5) Xor m_Key(8, i)
ERxorK(9) = R(6) Xor m_Key(9, i)
ERxorK(10) = R(7) Xor m_Key(10, i)
ERxorK(11) = R(8) Xor m_Key(11, i)
ERxorK(12) = R(7) Xor m_Key(12, i)
ERxorK(13) = R(8) Xor m_Key(13, i)
ERxorK(14) = R(9) Xor m_Key(14, i)
ERxorK(15) = R(10) Xor m_Key(15, i)
ERxorK(16) = R(11) Xor m_Key(16, i)
ERxorK(17) = R(12) Xor m_Key(17, i)
ERxorK(18) = R(11) Xor m_Key(18, i)
ERxorK(19) = R(12) Xor m_Key(19, i)
ERxorK(20) = R(13) Xor m_Key(20, i)
ERxorK(21) = R(14) Xor m_Key(21, i)
ERxorK(22) = R(15) Xor m_Key(22, i)
ERxorK(23) = R(16) Xor m_Key(23, i)
ERxorK(24) = R(15) Xor m_Key(24, i)
ERxorK(25) = R(16) Xor m_Key(25, i)
ERxorK(26) = R(17) Xor m_Key(26, i)
ERxorK(27) = R(18) Xor m_Key(27, i)
ERxorK(28) = R(19) Xor m_Key(28, i)
ERxorK(29) = R(20) Xor m_Key(29, i)
ERxorK(30) = R(19) Xor m_Key(30, i)
ERxorK(31) = R(20) Xor m_Key(31, i)
ERxorK(32) = R(21) Xor m_Key(32, i)
ERxorK(33) = R(22) Xor m_Key(33, i)
ERxorK(34) = R(23) Xor m_Key(34, i)
ERxorK(35) = R(24) Xor m_Key(35, i)
ERxorK(36) = R(23) Xor m_Key(36, i)
ERxorK(37) = R(24) Xor m_Key(37, i)
ERxorK(38) = R(25) Xor m_Key(38, i)
ERxorK(39) = R(26) Xor m_Key(39, i)
ERxorK(40) = R(27) Xor m_Key(40, i)
ERxorK(41) = R(28) Xor m_Key(41, i)
ERxorK(42) = R(27) Xor m_Key(42, i)
ERxorK(43) = R(28) Xor m_Key(43, i)
ERxorK(44) = R(29) Xor m_Key(44, i)
ERxorK(45) = R(30) Xor m_Key(45, i)
ERxorK(46) = R(31) Xor m_Key(46, i)
ERxorK(47) = R(0) Xor m_Key(47, i)
'Apply the s-boxes
Call CopyMem(sBox(0), m_sBox(0, ERxorK(0), ERxorK(1), ERxorK(2), ERxorK(3), ERxorK(4), ERxorK(5)), 4)
Call CopyMem(sBox(4), m_sBox(1, ERxorK(6), ERxorK(7), ERxorK(8), ERxorK(9), ERxorK(10), ERxorK(11)), 4)
Call CopyMem(sBox(8), m_sBox(2, ERxorK(12), ERxorK(13), ERxorK(14), ERxorK(15), ERxorK(16), ERxorK(17)), 4)
Call CopyMem(sBox(12), m_sBox(3, ERxorK(18), ERxorK(19), ERxorK(20), ERxorK(21), ERxorK(22), ERxorK(23)), 4)
Call CopyMem(sBox(16), m_sBox(4, ERxorK(24), ERxorK(25), ERxorK(26), ERxorK(27), ERxorK(28), ERxorK(29)), 4)
Call CopyMem(sBox(20), m_sBox(5, ERxorK(30), ERxorK(31), ERxorK(32), ERxorK(33), ERxorK(34), ERxorK(35)), 4)
Call CopyMem(sBox(24), m_sBox(6, ERxorK(36), ERxorK(37), ERxorK(38), ERxorK(39), ERxorK(40), ERxorK(41)), 4)
Call CopyMem(sBox(28), m_sBox(7, ERxorK(42), ERxorK(43), ERxorK(44), ERxorK(45), ERxorK(46), ERxorK(47)), 4)
'L[i] xor P(R[i])
LiRi(0) = L(0) Xor sBox(15)
LiRi(1) = L(1) Xor sBox(6)
LiRi(2) = L(2) Xor sBox(19)
LiRi(3) = L(3) Xor sBox(20)
LiRi(4) = L(4) Xor sBox(28)
LiRi(5) = L(5) Xor sBox(11)
LiRi(6) = L(6) Xor sBox(27)
LiRi(7) = L(7) Xor sBox(16)
LiRi(8) = L(8) Xor sBox(0)
LiRi(9) = L(9) Xor sBox(14)
LiRi(10) = L(10) Xor sBox(22)
LiRi(11) = L(11) Xor sBox(25)
LiRi(12) = L(12) Xor sBox(4)
LiRi(13) = L(13) Xor sBox(17)
LiRi(14) = L(14) Xor sBox(30)
LiRi(15) = L(15) Xor sBox(9)
LiRi(16) = L(16) Xor sBox(1)
LiRi(17) = L(17) Xor sBox(7)
LiRi(18) = L(18) Xor sBox(23)
LiRi(19) = L(19) Xor sBox(13)
LiRi(20) = L(20) Xor sBox(31)
LiRi(21) = L(21) Xor sBox(26)
LiRi(22) = L(22) Xor sBox(2)
LiRi(23) = L(23) Xor sBox(8)
LiRi(24) = L(24) Xor sBox(18)
LiRi(25) = L(25) Xor sBox(12)
LiRi(26) = L(26) Xor sBox(29)
LiRi(27) = L(27) Xor sBox(5)
LiRi(28) = L(28) Xor sBox(21)
LiRi(29) = L(29) Xor sBox(10)
LiRi(30) = L(30) Xor sBox(3)
LiRi(31) = L(31) Xor sBox(24)
'Prepare for next round
Call CopyMem(L(0), R(0), 32)
Call CopyMem(R(0), LiRi(0), 32)
Next
'Concatenate R[]L[]
Call CopyMem(RL(0), R(0), 32)
Call CopyMem(RL(32), L(0), 32) 'Apply the invIP permutation
For a = 0 To 63
BinBlock(a) = RL(m_IPInv(a))
Next
'Convert the binaries into a byte array
Call Bin2Byte(BinBlock(), 8, BlockData())End Sub