Hier ein Paar Zeilen Code:
'Key schedule arrays
Private Nr As Long 'Number of rounds [For 128 bit block, Nr = {10,
' 12, 14} for 128, 192, 256 bit cipher key]
Private fkey(59) As Long 'Nb*(Nr + 1)
Private rkey(59) As Long 'Nb*(Nr + 1)
'For file encryption, this is the maximum amount of memory (in bytes) allowed
' for file data
Private Const MaxFileChunkSize As Long = 4000000
Private Declare Sub CopyMemory Lib "kernel32" Alias "RtlMoveMemory" ( _
Destination As Any, Source As Any, ByVal Length As Long)
'Decryption Key Scheduler. Calculate rkey() decryption keys based on fkey()
' and Nr.
'Nb is the number of 32 bit units in the block size.
Private Sub CreateDecryptionKeys(Nb As Long)
Dim i As Long
Dim j As Long
Dim k As Long
Dim s(3) As Byte
'Invert the order of the round keys
i = 0
j = Nb * Nr
For k = 0 To Nr
CopyMemory rkey(i), fkey(j), Nb * 4&
i = i + Nb
j = j - Nb
Next k
'Apply the inverse MixColumn transform to all round keys except the first
' and the last
For i = Nb To Nb * Nr - 1
CopyMemory s(0), rkey(i), 4&
rkey(i) = Td0(Te4(s(0)) And &HFF&) Xor _
Td1(Te4(s(1)) And &HFF&) Xor _
Td2(Te4(s(2)) And &HFF&) Xor _
Td3(Te4(s(3)) And &HFF&)
Next i
End Sub
'Key Scheduler. Expand the cipher key into the encryption key schedule.
'pass(0 ... n-1) contains the cipher key, where n = {16, 20, 24, 28, 32} ,
' depending on KeyBits.
'If SUPPORT_LEVEL is non-zero, you must specify BlockBits for the block size
' you will be using.
'Returns zero on success.
Public Function SetCipherKey(pass() As Byte, KeyBits As Long) As Long
Dim i As Long
Dim j As Long
Dim s(3) As Byte
[...]
i = 8
j = 0
CopyMemory fkey(0), pass(0), 4& * i
Do
CopyMemory s(0), fkey(i - 1), 4&
fkey(i) = fkey(i - 8) Xor (Te4(s(0)) And &HFF000000) _
Xor (Te4(s(3)) And &HFF0000) _
Xor (Te4(s(2)) And &HFF00&) _
Xor (Te4(s(1)) And &HFF&) _
Xor rco(j)
fkey(i + 1) = fkey(i - 7) Xor fkey(i)
fkey(i + 2) = fkey(i - 6) Xor fkey(i + 1)
fkey(i + 3) = fkey(i - 5) Xor fkey(i + 2)
If j = 6 Then Exit Do
CopyMemory s(0), fkey(i + 3), 4&
fkey(i + 4) = fkey(i - 4) Xor (Te4(s(3)) And &HFF000000) _
Xor (Te4(s(2)) And &HFF0000) _
Xor (Te4(s(1)) And &HFF00&) _
Xor (Te4(s(0)) And &HFF&)
fkey(i + 5) = fkey(i - 3) Xor fkey(i + 4)
fkey(i + 6) = fkey(i - 2) Xor fkey(i + 5)
fkey(i + 7) = fkey(i - 1) Xor fkey(i + 6)
i = i + 8
j = j + 1
Loop
Nr = 14
CreateDecryptionKeys 4
End Function
Public Function SetCipherKeyString(PassPhrase As String, KeyBits As Long) As _
Long
Dim pass() As Byte
pass = StrConv(PassPhrase, vbFromUnicode)
ReDim Preserve pass(31)
SetCipherKeyString = SetCipherKey(pass, KeyBits)
End Function bis später
Tomy |