📄 clsdes.cls
字号:
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
Public Sub EncryptByte(ByteArray() As Byte, Optional Key As String)
Dim a As Long
Dim Offset As Long
Dim OrigLen As Long
Dim CipherLen As Long
Dim CurrPercent As Long
Dim NextPercent As Long
Dim CurrBlock(0 To 7) As Byte
Dim CipherBlock(0 To 7) As Byte
'Set the key if provided
If (Len(Key) > 0) Then Me.Key = Key
'Get the size of the original array
OrigLen = UBound(ByteArray) + 1
'First we add 12 bytes (4 bytes for the
'length and 8 bytes for the seed values
'for the CBC routine), and the ciphertext
'must be a multiple of 8 bytes
CipherLen = OrigLen + 12
If (CipherLen Mod 8 <> 0) Then
CipherLen = CipherLen + 8 - (CipherLen Mod 8)
End If
ReDim Preserve ByteArray(CipherLen - 1)
Call CopyMem(ByteArray(12), ByteArray(0), OrigLen)
'Store the length descriptor in bytes [9-12]
Call CopyMem(ByteArray(8), OrigLen, 4)
'Store a block of random data in bytes [1-8],
'these work as seed values for the CBC routine
'and is used to produce different ciphertext
'even when encrypting the same data with the
'same key)
Call Randomize
Call CopyMem(ByteArray(0), CLng(2147483647 * Rnd), 4)
Call CopyMem(ByteArray(4), CLng(2147483647 * Rnd), 4)
'Encrypt the data in 64-bit blocks
For Offset = 0 To (CipherLen - 1) Step 8
'Get the next block of plaintext
Call CopyMem(CurrBlock(0), ByteArray(Offset), 8)
'XOR the plaintext with the previous
'ciphertext (CBC, Cipher-Block Chaining)
For a = 0 To 7
CurrBlock(a) = CurrBlock(a) Xor CipherBlock(a)
Next
'Encrypt the block
Call EncryptBlock(CurrBlock())
'Store the block
Call CopyMem(ByteArray(Offset), CurrBlock(0), 8)
'Store the cipherblock (for CBC)
Call CopyMem(CipherBlock(0), CurrBlock(0), 8)
'Update the progress if neccessary
If (Offset >= NextPercent) Then
CurrPercent = Int((Offset / CipherLen) * 100)
NextPercent = (CipherLen * ((CurrPercent + 1) / 100)) + 1
RaiseEvent Progress(CurrPercent)
End If
Next
'Make sure we return a 100% progress
If (CurrPercent <> 100) Then RaiseEvent Progress(100)
End Sub
Public Sub DecryptByte(ByteArray() As Byte, Optional Key As String)
Dim a As Long
Dim Offset As Long
Dim OrigLen As Long
Dim CipherLen As Long
Dim CurrPercent As Long
Dim NextPercent As Long
Dim CurrBlock(0 To 7) As Byte
Dim CipherBlock(0 To 7) As Byte
'Set the new key if provided
If (Len(Key) > 0) Then Me.Key = Key
'Get the size of the ciphertext
CipherLen = UBound(ByteArray) + 1
'Decrypt the data in 64-bit blocks
For Offset = 0 To (CipherLen - 1) Step 8
'Get the next block of ciphertext
Call CopyMem(CurrBlock(0), ByteArray(Offset), 8)
'Decrypt the block
Call DecryptBlock(CurrBlock())
'XOR with the previous cipherblock
For a = 0 To 7
CurrBlock(a) = CurrBlock(a) Xor CipherBlock(a)
Next
'Store the current ciphertext to use
'XOR with the next block plaintext
Call CopyMem(CipherBlock(0), ByteArray(Offset), 8)
'Store the block
Call CopyMem(ByteArray(Offset), CurrBlock(0), 8)
'Update the progress if neccessary
If (Offset >= NextPercent) Then
CurrPercent = Int((Offset / CipherLen) * 100)
NextPercent = (CipherLen * ((CurrPercent + 1) / 100)) + 1
RaiseEvent Progress(CurrPercent)
End If
Next
'Get the size of the original array
Call CopyMem(OrigLen, ByteArray(8), 4)
'Make sure OrigLen is a reasonable value,
'if we used the wrong key the next couple
'of statements could be dangerous (GPF)
If (CipherLen - OrigLen > 19) Or (CipherLen - OrigLen < 12) Then
Call Err.Raise(vbObjectError, , "Incorrect size descriptor in DES decryption")
End If
'Resize the bytearray to hold only the plaintext
'and not the extra information added by the
'encryption routine
Call CopyMem(ByteArray(0), ByteArray(12), OrigLen)
ReDim Preserve ByteArray(OrigLen - 1)
'Make sure we return a 100% progress
If (CurrPercent <> 100) Then RaiseEvent Progress(100)
End Sub
Public Sub EncryptFile(SourceFile As String, DestFile As String, Optional Key As String)
Dim Filenr As Integer
Dim ByteArray() As Byte
'Make sure the source file do exist
If (Not FileExist(SourceFile)) Then
Call Err.Raise(vbObjectError, , "Error in Skipjack EncryptFile procedure (Source file does not exist).")
Exit Sub
End If
'Open the source file and read the content
'into a bytearray to pass onto encryption
Filenr = FreeFile
Open SourceFile For Binary As #Filenr
If LOF(Filenr) > 0 Then
ReDim ByteArray(0 To LOF(Filenr) - 1)
Get #Filenr, , ByteArray()
Close #Filenr
'Encrypt the bytearray
Call EncryptByte(ByteArray(), Key)
'If the destination file already exist we need
'to delete it since opening it for binary use
'will preserve it if it already exist
If (FileExist(DestFile)) Then Kill DestFile
'Store the encrypted data in the destination file
Filenr = FreeFile
Open DestFile For Binary As #Filenr
Put #Filenr, , ByteArray()
Close #Filenr
Else
Close #Filenr
Filenr = FreeFile
Open DestFile For Binary As #Filenr
Close #Filenr
End If
End Sub
Public Sub DecryptFile(SourceFile As String, DestFile As String, Optional Key As String)
Dim Filenr As Integer
Dim ByteArray() As Byte
'Make sure the source file do exist
If (Not FileExist(SourceFile)) Then
Call Err.Raise(vbObjectError, , "Error in Skipjack EncryptFile procedure (Source file does not exist).")
Exit Sub
End If
'Open the source file and read the content
'into a bytearray to decrypt
Filenr = FreeFile
Open SourceFile For Binary As #Filenr
If LOF(Filenr) > 0 Then
ReDim ByteArray(0 To LOF(Filenr) - 1)
Get #Filenr, , ByteArray()
Close #Filenr
'Decrypt the bytearray
Call DecryptByte(ByteArray(), Key)
'If the destination file already exist we need
'to delete it since opening it for binary use
'will preserve it if it already exist
If (FileExist(DestFile)) Then Kill DestFile
'Store the decrypted data in the destination file
Filenr = FreeFile
Open DestFile For Binary As #Filenr
Put #Filenr, , ByteArray()
Close #Filenr
Else
Close #Filenr
Filenr = FreeFile
Open DestFile For Binary As #Filenr
Close #Filenr
End If
End Sub
Public Function EncryptString(Text As String, Optional Key As String) As String
Dim ByteArray() As Byte
'Convert the text into a byte array
ByteArray() = StrConv(Text, vbFromUnicode)
'Encrypt the byte array
Call EncryptByte(ByteArray(), Key)
'Convert the byte array back to a string
EncryptString = StrConv(ByteArray(), vbUnicode)
End Function
Public Function DecryptString(Text As String, Optional Key As String) As String
Dim ByteArray() As Byte
'Convert the text into a byte array
ByteArray() = StrConv(Text, vbFromUnicode)
'Encrypt the byte array
Call DecryptByte(ByteArray(), Key)
'Convert the byte array back to a string
DecryptString = StrConv(ByteArray(), vbUnicode)
End Function
Public Property Let Key(New_Value As String)
Dim a As Long
Dim i As Long
Dim C(0 To 27) As Byte
Dim D(0 To 27) As Byte
Dim K(0 To 55) As Byte
Dim CD(0 To 55) As Byte
Dim Temp(0 To 1) As Byte
Dim KeyBin(0 To 63) As Byte
Dim KeySchedule(0 To 63) As Byte
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -