Posted: Mon Nov 10, 2003 8:35 pm
** Tip **
Exit Procedure = ProcedureReturn
Exit Procedure = ProcedureReturn
http://www.purebasic.com
https://www.purebasic.fr/english/
Code: Select all
;*******************************************************************************
; MODULE: CRijndael
; FILENAME: CRijndael.cls
; AUTHOR: Phil Fresle
; CREATED: 16-Feb-2001
; COPYRIGHT: COPYRIGHT 2001 Phil Fresle
; EMAIL: phil@frez.co.uk
; WEB: http://www.frez.co.uk
;
; DESCRIPTION:
; Implementation of the AES Rijndael Block Cipher. Inspired by Mike Scott's
; implementation in C. Permission For free direct Or derivative use is granted
; subject To compliance with Any conditions that the originators of the
; algorithm place On its exploitation.
;
; MODIFICATION HISTORY:
; 16-Feb-2001 Phil Fresle Initial Version
; 03-Apr-2001 Phil Fresle Added EncryptData And DecryptData functions To
; make it easier To use by VB developers For
; encrypting And decrypting strings. These procs
; take large Byte arrays, the resultant encoded
; Data includes the message Length inserted On
; the front four bytes prior To encryption.
; 19-Apr-2001 Phil Fresle Thanks To Paolo Migliaccio For finding a bug
; with 256 bit KEY. Problem was in the gkey
; Function. Now properly matches NIST values.
;*******************************************************************************
; Partially Converted to PB by Shannara (shannara@unformed.com)
m_lOnBits(30).l
m_l2Power(30).l
m_bytOnBits(7).b
m_byt2Power(7).b
m_InCo(3).b
m_fbsub(255).b
m_rbsub(255).b
m_ptab(255).b
m_ltab(255).b
m_ftable(255).l
m_rtable(255).l
m_rco(29).l
m_Nk.l
m_Nb.l
m_Nr.l
m_fi(23).b
m_ri(23).b
m_fkey(119).l
m_rkey(119).l
;CONVERT: Private Declare Sub CopyMemory Lib "kernel32" Alias "RtlMoveMemory" (ByVal Destination As Any, ByVal Source As Any, ByVal Length As Long)
;*******************************************************************************
; Class_Initialize (Sub)
;*******************************************************************************
Procedure Class_Initialize()
m_InCo(0) = &HB
m_InCo(1) = &HD
m_InCo(2) = &H9
m_InCo(3) = &HE
; Could have done this with a Loop calculating each value, but simply
; assigning the values is quicker - BITS SET FROM RIGHT
m_bytOnBits(0) = 1 ; 00000001
m_bytOnBits(1) = 3 ; 00000011
m_bytOnBits(2) = 7 ; 00000111
m_bytOnBits(3) = 15 ; 00001111
m_bytOnBits(4) = 31 ; 00011111
m_bytOnBits(5) = 63 ; 00111111
m_bytOnBits(6) = 127 ; 01111111
m_bytOnBits(7) = 255 ; 11111111
; Could have done this with a Loop calculating each value, but simply
; assigning the values is quicker - POWERS OF 2
m_byt2Power(0) = 1 ; 00000001
m_byt2Power(1) = 2 ; 00000010
m_byt2Power(2) = 4 ; 00000100
m_byt2Power(3) = 8 ; 00001000
m_byt2Power(4) = 16 ; 00010000
m_byt2Power(5) = 32 ; 00100000
m_byt2Power(6) = 64 ; 01000000
m_byt2Power(7) = 128 ; 10000000
; Could have done this with a Loop calculating each value, but simply
; assigning the values is quicker - BITS SET FROM RIGHT
m_lOnBits(0) = 1 ; 00000000000000000000000000000001
m_lOnBits(1) = 3 ; 00000000000000000000000000000011
m_lOnBits(2) = 7 ; 00000000000000000000000000000111
m_lOnBits(3) = 15 ; 00000000000000000000000000001111
m_lOnBits(4) = 31 ; 00000000000000000000000000011111
m_lOnBits(5) = 63 ; 00000000000000000000000000111111
m_lOnBits(6) = 127 ; 00000000000000000000000001111111
m_lOnBits(7) = 255 ; 00000000000000000000000011111111
m_lOnBits(8) = 511 ; 00000000000000000000000111111111
m_lOnBits(9) = 1023 ; 00000000000000000000001111111111
m_lOnBits(10) = 2047 ; 00000000000000000000011111111111
m_lOnBits(11) = 4095 ; 00000000000000000000111111111111
m_lOnBits(12) = 8191 ; 00000000000000000001111111111111
m_lOnBits(13) = 16383 ; 00000000000000000011111111111111
m_lOnBits(14) = 32767 ; 00000000000000000111111111111111
m_lOnBits(15) = 65535 ; 00000000000000001111111111111111
m_lOnBits(16) = 131071 ; 00000000000000011111111111111111
m_lOnBits(17) = 262143 ; 00000000000000111111111111111111
m_lOnBits(18) = 524287 ; 00000000000001111111111111111111
m_lOnBits(19) = 1048575 ; 00000000000011111111111111111111
m_lOnBits(20) = 2097151 ; 00000000000111111111111111111111
m_lOnBits(21) = 4194303 ; 00000000001111111111111111111111
m_lOnBits(22) = 8388607 ; 00000000011111111111111111111111
m_lOnBits(23) = 16777215 ; 00000000111111111111111111111111
m_lOnBits(24) = 33554431 ; 00000001111111111111111111111111
m_lOnBits(25) = 67108863 ; 00000011111111111111111111111111
m_lOnBits(26) = 134217727 ; 00000111111111111111111111111111
m_lOnBits(27) = 268435455 ; 00001111111111111111111111111111
m_lOnBits(28) = 536870911 ; 00011111111111111111111111111111
m_lOnBits(29) = 1073741823 ; 00111111111111111111111111111111
m_lOnBits(30) = 2147483647 ; 01111111111111111111111111111111
; Could have done this with a Loop calculating each value, but simply
; assigning the values is quicker - POWERS OF 2
m_l2Power(0) = 1 ; 00000000000000000000000000000001
m_l2Power(1) = 2 ; 00000000000000000000000000000010
m_l2Power(2) = 4 ; 00000000000000000000000000000100
m_l2Power(3) = 8 ; 00000000000000000000000000001000
m_l2Power(4) = 16 ; 00000000000000000000000000010000
m_l2Power(5) = 32 ; 00000000000000000000000000100000
m_l2Power(6) = 64 ; 00000000000000000000000001000000
m_l2Power(7) = 128 ; 00000000000000000000000010000000
m_l2Power(8) = 256 ; 00000000000000000000000100000000
m_l2Power(9) = 512 ; 00000000000000000000001000000000
m_l2Power(10) = 1024 ; 00000000000000000000010000000000
m_l2Power(11) = 2048 ; 00000000000000000000100000000000
m_l2Power(12) = 4096 ; 00000000000000000001000000000000
m_l2Power(13) = 8192 ; 00000000000000000010000000000000
m_l2Power(14) = 16384 ; 00000000000000000100000000000000
m_l2Power(15) = 32768 ; 00000000000000001000000000000000
m_l2Power(16) = 65536 ; 00000000000000010000000000000000
m_l2Power(17) = 131072 ; 00000000000000100000000000000000
m_l2Power(18) = 262144 ; 00000000000001000000000000000000
m_l2Power(19) = 524288 ; 00000000000010000000000000000000
m_l2Power(20) = 1048576 ; 00000000000100000000000000000000
m_l2Power(21) = 2097152 ; 00000000001000000000000000000000
m_l2Power(22) = 4194304 ; 00000000010000000000000000000000
m_l2Power(23) = 8388608 ; 00000000100000000000000000000000
m_l2Power(24) = 16777216 ; 00000001000000000000000000000000
m_l2Power(25) = 33554432 ; 00000010000000000000000000000000
m_l2Power(26) = 67108864 ; 00000100000000000000000000000000
m_l2Power(27) = 134217728 ; 00001000000000000000000000000000
m_l2Power(28) = 268435456 ; 00010000000000000000000000000000
m_l2Power(29) = 536870912 ; 00100000000000000000000000000000
m_l2Power(30) = 1073741824 ; 01000000000000000000000000000000
EndProcedure
;*******************************************************************************
; LShift (Function)
;*******************************************************************************
Procedure LShift.l(lValue.l, iShiftBits.w)
If iShiftBits = 0 Then
ProcedureReturn lValue
ElseIf iShiftBits = 31 Then
If lValue And 1 Then
ProcedureReturn &H80000000
Else
ProcedureReturn 0
End If
ProcedureReturn 0
ElseIf iShiftBits < 0 Or iShiftBits > 31 Then
;CONVERT: Err.Raise 6
End If
If (lValue And m_l2Power(31 - iShiftBits)) Then
ProcedureReturn ((lValue And m_lOnBits(31 - (iShiftBits + 1))) * _
m_l2Power(iShiftBits)) Or &H80000000
Else
ProcedureReturn = ((lValue And m_lOnBits(31 - iShiftBits)) * _
m_l2Power(iShiftBits))
End If
EndProcedure
;*******************************************************************************
; RShift (Function)
;*******************************************************************************
Procedure RShift.l(lValue.l, iShiftBits.w)
If iShiftBits = 0 Then
ProcedureReturn lValue
ElseIf iShiftBits = 31 Then
If lValue And &H80000000 Then
ProcedureReturn 1
Else
ProcedureReturn 0
End If
ProcedureReturn 0
ElseIf iShiftBits < 0 Or iShiftBits > 31 Then
;CONVERT: Err.Raise 6
End If
ProcedureReturn (lValue And &H7FFFFFFE) \ m_l2Power(iShiftBits)
If (lValue And &H80000000) Then
R.l
R.l = (R.l Or (&H40000000 \ m_l2Power(iShiftBits - 1)))
ProcedureReturn R.l
End If
EndProcedure
;*******************************************************************************
; LShiftByte (Function)
;*******************************************************************************
Procedure LShiftByte.b(bytValue.b, bytShiftBits.b)
If bytShiftBits = 0 Then
ProcedureReturn bytValue
ElseIf bytShiftBits = 7 Then
If bytValue And 1 Then
ProcedureReturn &H80
Else
ProcedureReturn 0
End If
ProcedureReturn 0
ElseIf bytShiftBits < 0 Or bytShiftBits > 7 Then
;CONVERT: Err.Raise 6
End If
ProcedureReturn ((bytValue And m_bytOnBits(7 - bytShiftBits)) * m_byt2Power(bytShiftBits))
EndProcedure
;*******************************************************************************
; RShiftByte (Function)
;*******************************************************************************
Procedure RShiftByte.b(bytValue.b, bytShiftBits.b)
If bytShiftBits = 0 Then
ProcedureReturn bytValue
ElseIf bytShiftBits = 7 Then
If bytValue And &H80 Then
ProcedureReturn 1
Else
ProcedureReturn 0
End If
ProcedureReturn 0
ElseIf bytShiftBits < 0 Or bytShiftBits > 7 Then
;CONVERT: Err.Raise 6
End If
ProcedureReturn bytValue \ m_byt2Power(bytShiftBits)
EndProcedure
;*******************************************************************************
; RotateLeft (Function)
;*******************************************************************************
Procedure RotateLeft.l(lValue.l, iShiftBits.i)
ProcedureReturn LShift(lValue, iShiftBits) Or RShift(lValue, (32 - iShiftBits))
EndProcedure
;'*******************************************************************************
;' RotateLeftByte (Function)
;*******************************************************************************
Procedure RotateLeftByte.b(bytValue.b, bytShiftBits.b)
ProcedureReturn LShiftByte(bytValue, bytShiftBits) Or RShiftByte(bytValue, (8 - bytShiftBits))
EndProcedure
;*******************************************************************************
; Pack (Function)
;*******************************************************************************
Procedure Pack.l(b().b)
lCount.l
lTemp.l
For lCount = 0 To 3
lTemp = b(lCount)
p.l
p.l = p.l Or LShift(lTemp, (lCount * 8))
ProcedureReturn p.l
Next
EndProcedure
;*******************************************************************************
; PackFrom (Function)
;*******************************************************************************
Procedure PackFrom.l(b().b, k.b)
lCount.l
lTemp.l
For lCount = 0 To 3
lTemp = b(lCount + k)
pf.l
pf.l = pf Or LShift(lTemp, (lCount * 8))
ProcedureReturn = pf
Next
EndProcedure
;*******************************************************************************
; Unpack (Sub)
;*******************************************************************************
Procedure Unpack(ByVal a.l, b().b)
b(0) = a And m_lOnBits(7)
b(1) = RShift(a, 8) And m_lOnBits(7)
b(2) = RShift(a, 16) And m_lOnBits(7)
b(3) = RShift(a, 24) And m_lOnBits(7)
EndProcedure
;*******************************************************************************
; UnpackFrom (Sub)
;*******************************************************************************
Procedure UnpackFrom(a.l, b().b, k.l)
b(0 + k) = a And m_lOnBits(7)
b(1 + k) = RShift(a, 8) And m_lOnBits(7)
b(2 + k) = RShift(a, 16) And m_lOnBits(7)
b(3 + k) = RShift(a, 24) And m_lOnBits(7)
EndProcedure
;*******************************************************************************
; xtime (Function)
;*******************************************************************************
Procedure xtime.b(a.b)
b.b
If (a And &H80) Then
b = &H1B
Else
b = 0
End If
a = LShiftByte(a, 1)
a = a XOr b
ProcedureReturn a
EndProcedure
;*******************************************************************************
; bmul (Function)
;*******************************************************************************
Procedure bmul.b(x.b, y.b)
If x <> 0 And y <> 0 Then
ProcedureReturn m_ptab((CLng(m_ltab(x)) + CLng(m_ltab(y))) Mod 255)
Else
ProcedureReturn 0
End If
EndProcedure
;*******************************************************************************
; SubByte (Function)
;*******************************************************************************
Procedure SubByte.l(a.l)
Dim b.b(3)
Unpack a, b
b(0) = m_fbsub(b(0))
b(1) = m_fbsub(b(1))
b(2) = m_fbsub(b(2))
b(3) = m_fbsub(b(3))
ProcedureReturn Pack(b)
EndProcedure
;*******************************************************************************
; product (Function)
;*******************************************************************************
Procedure product.l(x.l, y .l)
xb.b(3)
yb.b(3)
Unpack x, xb
Unpack y, yb
ProcedureReturn bmul(xb(0), yb(0)) XOr bmul(xb(1), yb(1)) XOr bmul(xb(2), yb(2)) XOr bmul(xb(3), yb(3))
EndProcedure
;*******************************************************************************
; InvMixCol (Function)
;*******************************************************************************
Procedure InvMixCol.l(x.l)
y.l
m.l
b.b(3)
m = Pack(m_InCo)
b(3) = product(m, x)
m = RotateLeft(m, 24)
b(2) = product(m, x)
m = RotateLeft(m, 24)
b(1) = product(m, x)
m = RotateLeft(m, 24)
b(0) = product(m, x)
y = Pack(b)
ProcedureReturn y
EndProcedure
;*******************************************************************************
; ByteSub (Function)
;*******************************************************************************
Procedure ByteSub.b(x.b)
y.b
y = m_ptab(255 - m_ltab(x))
x = y
x = RotateLeftByte(x, 1)
y = y XOr x
x = RotateLeftByte(x, 1)
y = y XOr x
x = RotateLeftByte(x, 1)
y = y XOr x
x = RotateLeftByte(x, 1)
y = y XOr x
y = y XOr &H63
ProcedureReturn y
EndProcedure
;*******************************************************************************
; gentables (Sub)
;*******************************************************************************
Procedure gentables()
i.l
y.b
b.b(3)
ib.b
m_ltab(0) = 0
m_ptab(0) = 1
m_ltab(1) = 0
m_ptab(1) = 3
m_ltab(3) = 1
For i = 2 To 255
m_ptab(i) = m_ptab(i - 1) XOr xtime(m_ptab(i - 1))
m_ltab(m_ptab(i)) = i
Next
m_fbsub(0) = &H63
m_rbsub(&H63) = 0
For i = 1 To 255
ib = i
y = ByteSub(ib)
m_fbsub(i) = y
m_rbsub(y) = i
Next
y = 1
For i = 0 To 29
m_rco(i) = y
y = xtime(y)
Next
For i = 0 To 255
y = m_fbsub(i)
b(3) = y XOr xtime(y)
b(2) = y
b(1) = y
b(0) = xtime(y)
m_ftable(i) = Pack(b)
y = m_rbsub(i)
b(3) = bmul(m_InCo(0), y)
b(2) = bmul(m_InCo(1), y)
b(1) = bmul(m_InCo(2), y)
b(0) = bmul(m_InCo(3), y)
m_rtable(i) = Pack(b)
Next
EndProcedure
;*******************************************************************************
; gkey (Sub)
;*******************************************************************************
Procedure gkey(nb.l, nk.l, KEY.b())
i.l
j.l
k.l
m.l
N.l
C1.l
C2.l
C3.l
CipherKey.l(7)
m_Nb = nb
m_Nk = nk
If m_Nb >= m_Nk Then
m_Nr = 6 + m_Nb
Else
m_Nr = 6 + m_Nk
End If
C1 = 1
If m_Nb < 8 Then
C2 = 2
C3 = 3
Else
C2 = 3
C3 = 4
End If
For j = 0 To nb - 1
m = j * 3
m_fi(m) = (j + C1) Mod nb
m_fi(m + 1) = (j + C2) Mod nb
m_fi(m + 2) = (j + C3) Mod nb
m_ri(m) = (nb + j - C1) Mod nb
m_ri(m + 1) = (nb + j - C2) Mod nb
m_ri(m + 2) = (nb + j - C3) Mod nb
Next
N = m_Nb * (m_Nr + 1)
For i = 0 To m_Nk - 1
j = i * 4
CipherKey(i) = PackFrom(KEY, j)
Next
For i = 0 To m_Nk - 1
m_fkey(i) = CipherKey(i)
Next
j = m_Nk
k = 0
Do While j < N
m_fkey(j) = m_fkey(j - m_Nk) XOr _
SubByte(RotateLeft(m_fkey(j - 1), 24)) XOr m_rco(k)
If m_Nk <= 6 Then
i = 1
Do While i < m_Nk And (i + j) < N
m_fkey(i + j) = m_fkey(i + j - m_Nk) XOr _
m_fkey(i + j - 1)
i = i + 1
Loop
Else
' Problem fixed here
i = 1
Do While i < 4 And (i + j) < N
m_fkey(i + j) = m_fkey(i + j - m_Nk) XOr _
m_fkey(i + j - 1)
i = i + 1
Loop
If j + 4 < N Then
m_fkey(j + 4) = m_fkey(j + 4 - m_Nk) XOr _
SubByte(m_fkey(j + 3))
End If
i = 5
Do While i < m_Nk And (i + j) < N
m_fkey(i + j) = m_fkey(i + j - m_Nk) XOr _
m_fkey(i + j - 1)
i = i + 1
Loop
End If
j = j + m_Nk
k = k + 1
Loop
For j = 0 To m_Nb - 1
m_rkey(j + N - nb) = m_fkey(j)
Next
i = m_Nb
Do While i < N - m_Nb
k = N - m_Nb - i
For j = 0 To m_Nb - 1
m_rkey(k + j) = InvMixCol(m_fkey(i + j))
Next
i = i + m_Nb
Loop
j = N - m_Nb
Do While j < N
m_rkey(j - N + m_Nb) = m_fkey(j)
j = j + 1
Loop
EndProcedure
;*******************************************************************************
; Encrypt (Sub)
;*******************************************************************************
Procedure Encrypt(buff.b())
i.l
j.l
k.l
m.l
a(7).l
b(7).l
x().l
y().l
t().l
For i = 0 To m_Nb - 1
j = i * 4
a(i) = PackFrom(buff, j)
a(i) = a(i) XOr m_fkey(i)
Next
k = m_Nb
x = a
y = b
For i = 1 To m_Nr - 1
For j = 0 To m_Nb - 1
m = j * 3
y(j) = m_fkey(k) XOr m_ftable(x(j) And m_lOnBits(7)) XOr _
RotateLeft(m_ftable(RShift(x(m_fi(m)), 8) And m_lOnBits(7)), 8) XOr _
RotateLeft(m_ftable(RShift(x(m_fi(m + 1)), 16) And m_lOnBits(7)), 16) XOr _
RotateLeft(m_ftable(RShift(x(m_fi(m + 2)), 24) And m_lOnBits(7)), 24)
k = k + 1
Next
t = x
x = y
y = t
Next
For j = 0 To m_Nb - 1
m = j * 3
y(j) = m_fkey(k) XOr m_fbsub(x(j) And m_lOnBits(7)) XOr _
RotateLeft(m_fbsub(RShift(x(m_fi(m)), 8) And m_lOnBits(7)), 8) XOr _
RotateLeft(m_fbsub(RShift(x(m_fi(m + 1)), 16) And m_lOnBits(7)), 16) XOr _
RotateLeft(m_fbsub(RShift(x(m_fi(m + 2)), 24) And m_lOnBits(7)), 24)
k = k + 1
Next
For i = 0 To m_Nb - 1
j = i * 4
UnpackFrom y(i), buff, j
x(i) = 0
y(i) = 0
Next
EndProcedure
;*******************************************************************************
; Decrypt (Sub)
;*******************************************************************************
Procedure Decrypt(buff.b())
i.l
j.l
k.l
m.l
a.l(7)
b.l(7)
x.l()
y.l()
t.l()
For i = 0 To m_Nb - 1
j = i * 4
a(i) = PackFrom(buff, j)
a(i) = a(i) XOr m_rkey(i)
Next
k = m_Nb
x = a
y = b
For i = 1 To m_Nr - 1
For j = 0 To m_Nb - 1
m = j * 3
y(j) = m_rkey(k) XOr m_rtable(x(j) And m_lOnBits(7)) XOr RotateLeft(m_rtable(RShift(x(m_ri(m)), 8) And m_lOnBits(7)), 8) XOr RotateLeft(m_rtable(RShift(x(m_ri(m + 1)), 16) And m_lOnBits(7)), 16) XOr RotateLeft(m_rtable(RShift(x(m_ri(m + 2)), 24) And m_lOnBits(7)), 24)
k = k + 1
Next
t = x
x = y
y = t
Next
For j = 0 To m_Nb - 1
m = j * 3
y(j) = m_rkey(k) XOr m_rbsub(x(j) And m_lOnBits(7)) XOr RotateLeft(m_rbsub(RShift(x(m_ri(m)), 8) And m_lOnBits(7)), 8) XOr RotateLeft(m_rbsub(RShift(x(m_ri(m + 1)), 16) And m_lOnBits(7)), 16) XOr RotateLeft(m_rbsub(RShift(x(m_ri(m + 2)), 24) And m_lOnBits(7)), 24)
k = k + 1
Next
For i = 0 To m_Nb - 1
j = i * 4
UnpackFrom y(i), buff, j
x(i) = 0
y(i) = 0
Next
EndProcedure
;;*******************************************************************************
;; CopyBytesASP (Sub)
;;
;; Slower non-API Function you can use To copy array Data
;;*******************************************************************************
;Private Sub CopyBytesASP(bytDest() As Byte, _
; lDestStart As Long, _
; bytSource() As Byte, _
; lSourceStart As Long, _
; lLength As Long)
; Dim lCount As Long
;
; lCount = 0
; Do
; bytDest(lDestStart + lCount) = bytSource(lSourceStart + lCount)
; lCount = lCount + 1
; Loop Until lCount = lLength
;End Sub
;*******************************************************************************
; IsInitialized (Function)
;*******************************************************************************
;CONVERT: BOOLEAN? VARIANT? Procedure IsInitialized(ByRef vArray As Variant) As Boolean
;On Error Resume Next
;IsInitialized = IsNumeric(UBound(vArray))
;End Function
;*******************************************************************************
; EncryptData (Function)
;
; Takes the message, whatever the size, And password in one call And does
; everything For you To Return an encoded/encrypted message
;*******************************************************************************
Procedure EncryptData.b(bytMessage.b(), bytPassword.b())
bytKey.b(31)
bytIn.b()
bytOut.b()
bytTemp.b(31)
lCount.l
lLength.l
lEncodedLength.l
bytLen.l(3)
lPosition.l
If Not IsInitialized(bytMessage) Then
ProcedureReturn
End If
If Not IsInitialized(bytPassword) Then
ProcedureReturn
End If
; use first 32 bytes of the password For the KEY
For lCount = 0 To UBound(bytPassword)
bytKey(lCount) = bytPassword(lCount)
If lCount = 31 Then
Exit For
End If
Next
; Prepare the KEY; assume 256 bit block and key size
gentables
gkey 8, 8, bytKey
; We are going To put the message size On the front of the message
; in the first 4 bytes. If the Length is more than a max int we are
; in trouble
lLength = UBound(bytMessage) + 1
lEncodedLength = lLength + 4
; The encoded Length includes The 4 bytes stuffed On The front
; And is padded out To be modulus 32
If lEncodedLength Mod 32 <> 0 Then
lEncodedLength = lEncodedLength + 32 - (lEncodedLength Mod 32)
End If
ReDim bytIn(lEncodedLength - 1)
ReDim bytOut(lEncodedLength - 1)
; Put the Length On the front
;* Unpack lLength, bytIn
CopyMemory_ VarPtr(bytIn(0)), VarPtr(lLength), 4
; Put the rest of the message after it
;* CopyBytesASP bytIn, 4, bytMessage, 0, lLength
CopyMemory_ VarPtr(bytIn(4)), VarPtr(bytMessage(0)), lLength
; Encrypt a block at a time
For lCount = 0 To lEncodedLength - 1 Step 32
;* CopyBytesASP bytTemp, 0, bytIn, lCount, 32
CopyMemory_ VarPtr(bytTemp(0)), VarPtr(bytIn(lCount)), 32
Encrypt bytTemp
;* CopyBytesASP bytOut, lCount, bytTemp, 0, 32
CopyMemory_ VarPtr(bytOut(lCount)), VarPtr(bytTemp(0)), 32
Next
ProcedureReturn bytOut
EndProcedure
;*******************************************************************************
; DecryptData (Function)
;
; Opposite of EncryptData
;*******************************************************************************
Procedure DecryptData.b(bytIn.b(), bytPassword.b())
bytMessage.b()
bytKey.b(31)
bytOut.b()
bytTemp.b(31)
lCount.l
lLength.l
lEncodedLength.l
bytLen.b(3)
lPosition.l
If Not IsInitialized(bytIn) Then
ProcedureReturn
End If
If Not IsInitialized(bytPassword) Then
ProcedureReturn
End If
lEncodedLength = UBound(bytIn) + 1
If lEncodedLength Mod 32 <> 0 Then
Exit Function
End If
; use first 32 bytes of the password For the KEY
For lCount = 0 To UBound(bytPassword)
bytKey(lCount) = bytPassword(lCount)
If lCount = 31 Then
Exit For
End If
Next
; Prepare the KEY; assume 256 bit block and key size
gentables
gkey 8, 8, bytKey
; The output array needs To be The same size As The input array
ReDim bytOut(lEncodedLength - 1)
; Decrypt a block at a time
For lCount = 0 To lEncodedLength - 1 Step 32
;* CopyBytesASP bytTemp, 0, bytIn, lCount, 32
CopyMemory_ VarPtr(bytTemp(0)), VarPtr(bytIn(lCount)), 32
Decrypt bytTemp
;* CopyBytesASP bytOut, lCount, bytTemp, 0, 32
CopyMemory_ VarPtr(bytOut(lCount)), VarPtr(bytTemp(0)), 32
Next
; Get the original Length of the string from the first 4 bytes
;* lLength = Pack(bytOut)
CopyMemory_ VarPtr(lLength), VarPtr(bytOut(0)), 4
; Make sure the Length is consistent with our Data
If lLength > lEncodedLength - 4 Then
ProcedureReturn
End If
; Prepare the output message Byte array
ReDim bytMessage(lLength - 1)
;* CopyBytesASP bytMessage, 0, bytOut, 4, lLength
CopyMemory_ VarPtr(bytMessage(0)), VarPtr(bytOut(4)), lLength
ProcedureReturn bytMessage
EndProcedure
Code: Select all
; DESCRIPTION:
; Implementation of the AES Rijndael Block Cipher. Inspired by Mike Scott's
; implementation in C. Permission For free direct Or derivative use is granted
; subject To compliance with Any conditions that the originators of the
; algorithm place On its exploitation.
@Shannara: for youShannara wrote:Well ... we had the babyRight on thanksgiving day 2003
Now another is due this October ...
![]()