Code: Select all
;{ bAES64
#paData = "#@!$%^&*()(*&^%$!@#"
Procedure aesNCoder(*inData, *outData, szIn, Bits = 256)
szinData + szIn
If szinData < 18
paDataLen = StringByteLength(#paData, #PB_UTF8)
EndIf
*tmp = AllocateMemory(szinData + SizeOf(byte) + paDataLen)
CopyMemory(*inData, *tmp, szinData)
If szinData < 18
PokeS(*tmp + szinData - 1, #paData, paDataLen, #PB_UTF8)
szinData + paDataLen
EndIf
errMod = Mod(szinData, 16) + 16 ; this errMod variable shows how different data size related to each other
If errMod = 16 ; Observed that errMod with value 16 (and 31) makes encryption invalid
If AESEncoder(*tmp, *outData, szinData + donnowha2nput1, ?Key, Bits, ?InitializationVector)
RET = szinData
EndIf
ElseIf errMod = 31 ; Observed that errMod with value 16 (and 31) makes encryption invalid
If AESEncoder(*tmp, *outData, szinData + donnowha2nput2, ?Key, Bits, ?InitializationVector)
RET = szinData
EndIf
Else ; This is normaly working except 16/31 cases
If AESEncoder(*tmp, *outData, szinData + SizeOf(byte), ?Key, Bits, ?InitializationVector)
RET = szinData
EndIf
EndIf
FreeMemory(*tmp)
ProcedureReturn RET
DataSection
Key:
Data.b $06, $a9, $21, $40, $36, $b8, $a1, $5b, $51, $2e, $03, $d5, $34, $12, $00, $06
InitializationVector:
Data.b $3d, $af, $ba, $42, $9d, $9e, $b4, $30, $b4, $22, $da, $80, $2c, $9f, $ac, $41
EndDataSection
EndProcedure
Procedure aesDCoder(*inData, *outData, szIn, Bits = 256)
szOut + szIn
If AESDecoder(*inData, *outData, szOut + SizeOf(byte), ?Key, Bits, ?InitializationVector)
If PeekA(*outData + szOut - 1) = 35
paDataLen = StringByteLength(#paData, #PB_UTF8)
*tmp = AllocateMemory(paDataLen + SizeOf(byte))
PokeS(*tmp, #paData, paDataLen, #PB_UTF8)
If CompareMemory(*outData + szOut - 19, *tmp, 19)
szOut - 19
EndIf
FreeMemory(*tmp)
Else ; it is strange behaviour that when input data size constantly affects and breakes encoding/decoding
errMod = Mod(szOut, 16) + 16 ; this errMod variable shows how different data size related to each other
If errMod = 30 ; Observed that errMod with value 30 (and 31) makes encryption invalid
If AESDecoder(*inData, *outData, szOut + donnowha2input1, ?Key, Bits, ?InitializationVector)
If PeekA(*outData + szOut - 1) = 35
paDataLen = StringByteLength(#paData, #PB_UTF8)
*tmp = AllocateMemory(paDataLen + SizeOf(byte))
PokeS(*tmp, #paData, paDataLen, #PB_UTF8)
If CompareMemory(*outData + szOut - 19, *tmp, 19)
szOut - 19
EndIf
FreeMemory(*tmp)
EndIf
EndIf
ElseIf errMod = 31 ; Observed that errMod with value 31 (and 30) makes encryption invalid
If AESDecoder(*inData, *outData, szOut + donnowha2input2, ?Key, Bits, ?InitializationVector)
If PeekA(*outData + szOut - 1) = 35
paDataLen = StringByteLength(#paData, #PB_UTF8)
*tmp = AllocateMemory(paDataLen + SizeOf(byte))
PokeS(*tmp, #paData, paDataLen, #PB_UTF8)
If CompareMemory(*outData + szOut - 19, *tmp, 19)
szOut - 19
EndIf
FreeMemory(*tmp)
EndIf
EndIf
EndIf
EndIf
ProcedureReturn szOut
EndIf
ProcedureReturn #False
DataSection
Key:
Data.b $06, $a9, $21, $40, $36, $b8, $a1, $5b, $51, $2e, $03, $d5, $34, $12, $00, $06
InitializationVector:
Data.b $3d, $af, $ba, $42, $9d, $9e, $b4, $30, $b4, $22, $da, $80, $2c, $9f, $ac, $41
EndDataSection
EndProcedure
Procedure bAES64enc(*inData, *outData, szIn, Bits = 256) ;inData - BinaryMem, outData - AESEnc&BasedEncMem
If szIn > 0
*AESenc = AllocateMemory(4096)
szAESenc = aesNCoder(*inData, *AESenc, szIn, Bits)
If szAESenc > 0
encoData$ = Base64Encoder(*AESenc, szAESenc)
szEncodata = StringByteLength(encoData$, #PB_UTF8)
If szEncodata > 0
PokeS(*outData, encoData$, szEncodata, #PB_UTF8)
RET = szEncodata
EndIf
EndIf
FreeMemory(*AESenc)
EndIf
ProcedureReturn RET
EndProcedure
Procedure bAES64dec(*inData, *outData, szIn, Bits = 256) ;inData - BasedMem, outData - BaseDec&AESDecMem
If szIn > 0
inData$ = PeekS(*inData, szIn, #PB_UTF8)
*AESdec = AllocateMemory(4096)
szoutData = Base64Decoder(inData$, *AESdec, szIn)
If szoutData > 0
szoutData - SizeOf(byte)
szAESdec = aesDCoder(*AESdec, *outData, szoutData, Bits)
EndIf
FreeMemory(*AESdec)
EndIf
ProcedureReturn szAESdec
EndProcedure
;{
*BAESenc = AllocateMemory(2048)
*BAESdec = AllocateMemory(2048)
For ii = 1 To 1024
rawstring.s = LSet("",ii,Chr(Random(126,33)))
*rawData = UTF8(rawstring)
szRaw = MemorySize(*rawData)
szBAESenc = bAES64enc(*rawData, *BAESenc, szRaw)
If szBAESenc > 0
BAESenc$ = PeekS(*BAESenc, szBAESenc, #PB_UTF8)
szBAESdec = bAES64dec(*BAESenc, *BAESdec, szBAESenc)
If szBAESdec > 0
BAESdec$ = PeekS(*BAESdec, szBAESdec, #PB_UTF8)
If BAESdec$ <> rawstring ; hmm something went wrong but Y?
i + 1
Debug "lup: " + Str(ii) + " : " + Str(i) + " : NOK : " + rawstring + " sz: " + Str(szBAESenc) + " base64enc: " + BAESenc$ + " sz: " + Str(szBAESdec) + " base64dec: " + BAESdec$
Else ; Everything goes fine
; working codepiece
EndIf
EndIf
EndIf
FreeMemory(*rawData)
Delay(10)
Next
;}
But for unknown reason when input memory size is relative bytes the code begins mulfunctioning.
I used MOD() to relate input bytes causing functional disorder and will be veeeery glad to understand the reason of such behaviour.