Code aus Rijndael.bas
Option Explicit
Private Potenz(254) As Long, Logarithmus(255) As Long
Private Substitution(255) As Long, InvSubstitution(255) As Long
Private Shifts(2, 2, 1) As Long
Private key() As Long, rc(10) As Byte
Private Lr As Long, Lb As Long, Lk As Long
Public Sub Init_Rijndael()
Dim i As Long, n As Long, tmp(3) As Long
Logarithmus(0) = -1
Potenz(0) = 1
Logarithmus(1) = 0
For i = 1 To 254
Potenz(i) = shl(Potenz(i - 1), 1) Xor Potenz(i - 1)
If Potenz(i) > 255 Then
Potenz(i) = Potenz(i) Xor &H11B&
End If
Logarithmus(Potenz(i)) = i
Next i
For i = 0 To 255
Substitution(i) = fwd_affine(Inv(i))
InvSubstitution(i) = Inv(inv_affine(i))
Next i
SetLongArray_Row Shifts, 0, 0, 1, 2, 3
SetLongArray_Row Shifts, 1, 0, 1, 2, 3
SetLongArray_Row Shifts, 2, 0, 1, 3, 4
SetLongArray_Row Shifts, 0, 1, 3, 2, 1
SetLongArray_Row Shifts, 1, 1, 5, 4, 3
SetLongArray_Row Shifts, 2, 1, 7, 5, 4
rc(0) = 1
For i = 1 To UBound(rc)
rc(i) = mul(rc(i - 1), 2)
Next i
For i = 0 To 255
If InvSubstitution(Substitution(i)) <> i Then
Stop
End If
Next i
End Sub
Private Function mul(a As Byte, b As Byte) As Long
If a <> 0 And b <> 0 Then
mul = Potenz((Logarithmus(a) + Logarithmus(b)) Mod 255)
Else
mul = 0
End If
End Function
Private Function Inv(a As Long) As Long
Dim i As Long
If a < 2 Then
Inv = a: Exit Function
End If
Inv = Potenz(255 - Logarithmus(a))
End Function
Private Function fwd_affine(a As Long) As Long
Dim tmp As Long
tmp = a Xor shl(a, 1) Xor shl(a, 2) Xor shl(a, 3) Xor shl(a, 4)
tmp = tmp Xor shr(tmp, 8) Xor &H63&
fwd_affine = tmp And &HFF&
End Function
Private Function inv_affine(a As Long) As Long
Dim tmp As Long
tmp = shl(a, 1) Xor shl(a, 3) Xor shl(a, 6)
tmp = tmp Xor shr(tmp, 8) Xor &H5&
inv_affine = tmp And &HFF&
End Function
Public Sub KeyExpansion(cipher() As Byte, blockbits As Long)
Dim i As Long, xi_1(3) As Long
Lk = (UBound(cipher) + 1) / 4
Lb = blockbits / 32
Select Case Min(Lk, Lb)
Case 4: Lr = 10
Case 6: Lr = 12
Case 8: Lr = 14
End Select
ReDim key(Lb * (Lr + 1) - 1, 3)
For i = 0 To Lk - 1
key(i, 0) = cipher(i * 4)
key(i, 1) = cipher(i * 4 + 1)
key(i, 2) = cipher(i * 4 + 2)
key(i, 3) = cipher(i * 4 + 3)
Next i
For i = Lk To Lb * (Lr + 1) - 1
If i Mod Lk = 0 Then
xi_1(0) = Substitution(key(i - 1, 1)) Xor rc(Int(i / Lk))
xi_1(1) = Substitution(key(i - 1, 2))
xi_1(2) = Substitution(key(i - 1, 3))
xi_1(3) = Substitution(key(i - 1, 0))
ElseIf i Mod Lk = 4 And Lk = 8 Then
xi_1(0) = Substitution(key(i - 1, 0))
xi_1(1) = Substitution(key(i - 1, 1))
xi_1(2) = Substitution(key(i - 1, 2))
xi_1(3) = Substitution(key(i - 1, 3))
Else
xi_1(0) = key(i - 1, 0)
xi_1(1) = key(i - 1, 1)
xi_1(2) = key(i - 1, 2)
xi_1(3) = key(i - 1, 3)
End If
key(i, 0) = xi_1(0) Xor key(i - Lk, 0)
key(i, 1) = xi_1(1) Xor key(i - Lk, 1)
key(i, 2) = xi_1(2) Xor key(i - Lk, 2)
key(i, 3) = xi_1(3) Xor key(i - Lk, 3)
Next i
End Sub
Public Sub Rijndael_EncryptBlock(block() As Byte)
Dim i As Long
AddRoundKey block, 0
For i = 1 To Lr - 1
SubBytes block
ShiftRows block, 0
MixColumns block
AddRoundKey block, Lb * i
Next i
SubBytes block
ShiftRows block, 0
AddRoundKey block, Lb * Lr
End Sub
Public Sub Rijndael_DecryptBlock(block() As Byte)
Dim i As Long
AddRoundKey block, Lb * Lr
ShiftRows block, 1
InvSubBytes block
For i = Lr - 1 To 1 Step -1
AddRoundKey block, Lb * i
InvMixColumns block
ShiftRows block, 1
InvSubBytes block
Next i
AddRoundKey block, 0
End Sub
Private Sub SubBytes(block() As Byte)
Dim i As Long, j As Long
For i = 0 To 3
For j = 0 To UBound(block, 1)
block(j, i) = Substitution(block(j, i))
Next j
Next i
End Sub
Private Sub ShiftRows(ByRef block() As Byte, Mode As Long)
Dim j As Long, i As Long, tmp() As Long, ByteMode As Long
ByteMode = shr(Lb - 4, 1)
ReDim tmp(UBound(block, 1))
For j = 1 To 3
For i = 0 To UBound(block, 1)
tmp(i) = block((i + Shifts(j - 1, ByteMode, Mode)) Mod Lb, j)
Next i
For i = 0 To UBound(block, 1)
block(i, j) = tmp(i)
Next i
Next j
End Sub
Private Sub MixColumns(block() As Byte)
Dim i As Long, j As Long, tmp(3) As Long
For j = 0 To UBound(block, 1)
tmp(0) = mul(block(j, 0), 2) Xor mul(block(j, 1), 3) Xor _
block(j, 2) Xor block(j, 3)
tmp(1) = mul(block(j, 1), 2) Xor mul(block(j, 2), 3) Xor _
block(j, 3) Xor block(j, 0)
tmp(2) = mul(block(j, 2), 2) Xor mul(block(j, 3), 3) Xor _
block(j, 0) Xor block(j, 1)
tmp(3) = mul(block(j, 3), 2) Xor mul(block(j, 0), 3) Xor _
block(j, 1) Xor block(j, 2)
For i = 0 To 3
block(j, i) = tmp(i)
Next i
Next j
End Sub
Private Sub AddRoundKey(block() As Byte, rk_off As Long)
Dim i As Long, n As Long
For i = 0 To Lb - 1
For n = 0 To 3
block(i, n) = block(i, n) Xor key(rk_off + i, n)
Next n
Next i
End Sub
Private Sub InvSubBytes(block() As Byte)
Dim i As Long, j As Long
For i = 0 To 3
For j = 0 To UBound(block, 1)
block(j, i) = InvSubstitution(block(j, i))
Next j
Next i
End Sub
Private Sub InvMixColumns(block() As Byte)
Dim i As Long, j As Long, tmp(3) As Long
For j = 0 To UBound(block, 1)
tmp(0) = mul(block(j, 0), &HE&) Xor mul(block(j, 1), &HB&) _
Xor mul(block(j, 2), &HD&) Xor mul(block(j, 3), 9)
tmp(1) = mul(block(j, 1), &HE&) Xor mul(block(j, 2), &HB&) _
Xor mul(block(j, 3), &HD&) Xor mul(block(j, 0), 9)
tmp(2) = mul(block(j, 2), &HE&) Xor mul(block(j, 3), &HB&) _
Xor mul(block(j, 0), &HD&) Xor mul(block(j, 1), 9)
tmp(3) = mul(block(j, 3), &HE&) Xor mul(block(j, 0), &HB&) _
Xor mul(block(j, 1), &HD&) Xor mul(block(j, 2), 9)
For i = 0 To 3
block(j, i) = tmp(i)
Next i
Next j
End Sub