AES+Base64 encryption

Just starting out? Need help? Post your questions and find answers here.
LiK137
Enthusiast
Enthusiast
Posts: 282
Joined: Wed Jun 23, 2010 5:13 pm

AES+Base64 encryption

Post by LiK137 »

Hi,
below is the problematic code of AES encryption with Base64 output.
The problem seems to be with AES part. when #PB_Cipher_ECB is used then it can be somehow make it nearly correct output by catching header and footer of data but otherwise the output is truncated and in both cases some trash data is added at end.
Here is the code itself:

Code: Select all

;{ bAES64
#hidrPAData = "#@!$%^&*()"
#futrPAData = "(*&^%$!@#"
#bAES64enc = 64

Procedure VectoriSalt(*salt, szalt)
  Protected lup
  If szalt > 0 And MemorySize(*salt) >= szalt
    For lup = 0 To szalt - 1
      PokeA(*salt + lup, Random(255,0))
    Next  
  EndIf  
EndProcedure  
Procedure aesNCoder(*inData, Bits = 256, *K = 0, *Vector = 0)
  Protected szIn = MemorySize(*inData), RET = #False
  
  If *K = 0
    *K = ?Key
  EndIf
  If *Vector = 0
    *Vector = ?InitializationVector
  EndIf  
  
  Define *outData = AllocateMemory(szIn)
  If *outData
    If AESEncoder(*inData, *outData, szIn, *K, Bits, *Vector, #PB_Cipher_ECB) ; *
      Protected *tmp = AllocateMemory(szIn)
      If *tmp 
        If AESDecoder(*outData, *tmp, szIn, *K, Bits, *Vector, #PB_Cipher_ECB) ; *
          If CompareMemory(*inData, *tmp, szIn)
            RET = *outData
            Debug "1 " + szIn
          EndIf  
        EndIf  
        FreeMemory(*tmp)
      EndIf
    EndIf
    If RET = 0
      FreeMemory(*outData)
    EndIf  
  EndIf  
  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, Bits = 256, *K = 0, *Vector = 0)
  Protected szIn = MemorySize(*inData), RET = #False
  
  If *K = 0
    *K = ?Key
  EndIf
  If *Vector = 0
    *Vector = ?InitializationVector
  EndIf  
  
  Define *outData = AllocateMemory(szIn)
  If *outData
    If AESDecoder(*inData, *outData, szIn, *K, Bits, *Vector, #PB_Cipher_ECB) ;*
      *tmp = AllocateMemory(szIn)
      If *tmp 
        If AESEncoder(*outData, *tmp, szIn, *K, Bits, *Vector, #PB_Cipher_ECB) ;*
          If CompareMemory(*inData, *tmp, szIn)
            RET = *outData
            Debug "2 " + szIn
          EndIf  
        EndIf  
        FreeMemory(*tmp)
      EndIf
    EndIf
    If RET = 0
      FreeMemory(*outData)
    EndIf  
  EndIf
  
  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 bAES64enc(*inData, Bits = 256, *K = 0, *Vector = 0) ;inData - BinaryMem, outData - AESEnc&BasedEncMem
  Protected szIn = MemorySize(*inData), RET = #False
  
  Protected paDataLen = StringByteLength(#hidrPAData + #futrPAData, #PB_UTF8), 
            hidrLen = StringByteLength(#hidrPAData, #PB_UTF8), 
            futrLen = StringByteLength(#futrPAData, #PB_UTF8), 
            szinPAData = szIn + paDataLen
  
  Protected *tmp = AllocateMemory(szinPAData)
  If *tmp
    PokeS(*tmp, #hidrPAData, hidrLen, #PB_UTF8|#PB_String_NoZero)
    CopyMemory(*inData, *tmp + hidrLen, szIn)
    PokeS(*tmp + hidrLen + szIn, #futrPAData, futrLen, #PB_UTF8|#PB_String_NoZero)
    ShowMemoryViewer(*tmp, szinPAData)
    CallDebugger
    Define *AESenc = aesNCoder(*tmp, Bits, *K, *Vector)
    If *AESenc
      Protected szAESenc = MemorySize(*AESenc)
      ShowMemoryViewer(*AESenc, szAESenc)
      CallDebugger
      *AESdecmp = aesDCoder(*AESenc, Bits, *K, *Vector)
      If *AESdecmp 
        Protected szAESdecmp = MemorySize(*AESdecmp)
        If szAESdecmp = szAESenc And CompareMemory(*AESdecmp, *tmp, szAESenc)
          Protected BAESData$ = Base64Encoder(*AESenc, szinPAData, #PB_Cipher_NoPadding|#PB_Cipher_URL), szBAESData = StringByteLength(BAESData$, #PB_UTF8)
          Debug BAESData$
          Protected *bAESdecmp =  AllocateMemory(szBAESData)
          If *bAESdecmp
            Protected szoutData = Base64Decoder(BAESData$, *bAESdecmp, szBAESData*2)
            If CompareMemory(*bAESdecmp, *AESenc, szoutData) <> 0
              Protected szbAESenc = StringByteLength(BAESData$, #PB_UTF8)
              Protected *bAESenc =  AllocateMemory(szbAESenc)
              If *bAESenc
                PokeS(*bAESenc, BAESData$, szbAESenc, #PB_UTF8|#PB_String_NoZero)
                ShowMemoryViewer(*bAESenc, szbAESenc)
                CallDebugger
                RET = *bAESenc
                Debug "3 " + szbAESenc
              EndIf  
            EndIf
            FreeMemory(*bAESdecmp)
          EndIf
        EndIf  
        FreeMemory(*AESdecmp)
      EndIf    
      FreeMemory(*AESenc)
    EndIf  
    FreeMemory(*tmp)
  EndIf  
  ProcedureReturn RET
EndProcedure
Procedure bAES64dec(*inData, Bits = 256, *K = 0, *Vector = 0) ;inData - BasedMem, outData - BaseDec&AESDecMem
  Protected szIn = MemorySize(*inData), RET = #False
  
  ShowMemoryViewer(*inData, szIn)
  CallDebugger
  
  Protected paDataLen = StringByteLength(#hidrPAData + #futrPAData, #PB_UTF8), 
            hidrLen = StringByteLength(#hidrPAData, #PB_UTF8), 
            futrLen = StringByteLength(#futrPAData, #PB_UTF8), 
            szinPAData = szIn + paDataLen
  
  Protected inData$ = PeekS(*inData, szIn, #PB_UTF8), szInData = StringByteLength(inData$, #PB_UTF8)
  
  Protected *bAESdec = AllocateMemory(szInData)
  If *bAESdec
    Protected szoutData = Base64Decoder(inData$, *bAESdec, szInData*2)
    If szoutData > 0
      ShowMemoryViewer(*bAESdec, szoutData)
      CallDebugger
      Protected Base64Data$ = Base64Encoder(*bAESdec, szoutData, #PB_Cipher_NoPadding|#PB_Cipher_URL), szBase64Data = StringByteLength(Base64Data$, #PB_UTF8)
      If inData$ = Base64Data$
        Protected *AESdec = aesDCoder(*bAESdec, Bits, *K, *Vector)
        If *AESdec
          Protected szAESdec = MemorySize(*AESdec)
          Protected *AESenc = aesNCoder(*AESdec, Bits, *K, *Vector)
          If *AESenc
            Protected szAESenc = MemorySize(*AESenc)
            If CompareMemory(*AESenc, *bAESdec, szoutData)
              Protected tmpHidr.s = PeekS(*AESdec, hidrLen, #PB_UTF8), tmpFutr.s = PeekS(*AESdec + szAESdec - futrLen, futrLen, #PB_UTF8)
              If tmpHidr = #hidrPAData And tmpFutr = #futrPAData
                Protected szRawData = szAESdec - paDataLen
                Define *rawData = AllocateMemory(szRawData)
                If *rawData
                  CopyMemory(*AESdec + hidrLen, *rawData, szRawData)
                  RET = *rawData
                  Debug "6 " + szRawData
                EndIf  
              EndIf 
              ShowMemoryViewer(*AESdec, szoutData+100)
              CallDebugger
            EndIf
            FreeMemory(*AESenc)
          EndIf  
          FreeMemory(*AESdec)
        EndIf  
      EndIf  
    EndIf
    FreeMemory(*bAESdec)
  EndIf  
  ProcedureReturn RET
EndProcedure
Procedure.s bAES64enxtring(rawString.s, Bits = 256, *K = 0, *Vector = 0) ;inData - BinaryMem, outData - AESEnc&BasedEncMem
  Protected RET.s = ""
  
  If rawString <> ""
    Protected szrawString = StringByteLength(rawString, #PB_UTF8)
    Protected *rawData = AllocateMemory(szrawString)
    If *rawData
      PokeS(*rawData, rawString, szrawString, #PB_UTF8|#PB_String_NoZero)
      Protected szRawData = MemorySize(*rawData)
      Protected *BAESenc = bAES64enc(*rawData, Bits, *K, *Vector)
      If *BAESenc
        Protected szBAESenc = MemorySize(*BAESenc)
        Protected BAESenc$ = PeekS(*BAESenc, szBAESenc, #PB_UTF8)
        Protected *BAESdecmp = bAES64dec(*BAESenc, Bits, *K, *Vector)
        If *BAESdecmp
          Protected szBAESdec = MemorySize(*BAESdecmp)
          If szBAESdec = szRawData And CompareMemory(*BAESdecmp, *rawData, szRawData)
            RET = BAESenc$
          EndIf
          FreeMemory(*BAESdecmp)
        EndIf  
        FreeMemory(*BAESenc)
      EndIf
      FreeMemory(*rawData)
    EndIf
  EndIf  
  
  ProcedureReturn RET  
EndProcedure
Procedure.s bAES64dextring(BAESenc.s, Bits = 256, *K = 0, *Vector = 0) ;inData - BasedMem, outData - BaseDec&AESDecMem
  Protected RET.s = ""
  
  If BAESenc <> ""
    Protected szBAESenc = StringByteLength(BAESenc, #PB_UTF8)
    Protected *BAESenc = AllocateMemory(szBAESenc)
    If *BAESenc
      PokeS(*BAESenc, BAESenc, szBAESenc, #PB_UTF8|#PB_String_NoZero)
      Protected szBAESencData = MemorySize(*BAESenc)      
      Protected *BAESdec = bAES64dec(*BAESenc, Bits, *K, *Vector)
      If *BAESdec
        Protected szBAESdec = MemorySize(*BAESdec)
        BAESdec.s = PeekS(*BAESdec, szBAESdec, #PB_UTF8)
        
        Protected *BAESencmp = bAES64enc(*BAESdec, Bits, *K, *Vector)
        If *BAESencmp
          Protected szBAESencmp = MemorySize(*BAESencmp)
          If szBAESencmp = szBAESencData And CompareMemory(*BAESencmp, *BAESenc, szBAESencmp)
            RET = BAESdec
          EndIf 
          FreeMemory(*BAESencmp)
        EndIf  
        FreeMemory(*BAESdec)
      EndIf
      FreeMemory(*BAESenc)
    EndIf  
  EndIf  
  ProcedureReturn RET
EndProcedure
;}

mystr.s = "somestring"
enxtr.s = bAES64enxtring(mystr)
dextr.s = bAES64dextring(enxtr)
Debug "enx: " + enxtr
End

User avatar
STARGÅTE
Addict
Addict
Posts: 2226
Joined: Thu Jan 10, 2008 1:30 pm
Location: Germany, Glienicke
Contact:

Re: AES+Base64 encryption

Post by STARGÅTE »

I can't fully understand the code, but it has serious issues:
  1. You use StringByteLength() to receive the length in bytes of the string (szrawString) and you use it in PokeS() for the length. However, PokeS expected the length in characters (not bytes). Similar when you read a string with PeekS, you pass again the byte length, but character length is expected.
  2. The output buffer size of Base64Decoder() have to be at least 64 bytes. In your code you allocate *bAESdecmp with a length of szBAESData wich is just 39 bytes, but you pass a length of szBAESData*2 to Base64Decoder. So the buffer is to small and you create a memory overflow.
PB 6.01 ― Win 10, 21H2 ― Ryzen 9 3900X, 32 GB ― NVIDIA GeForce RTX 3080 ― Vivaldi 6.0 ― www.unionbytes.de
Lizard - Script language for symbolic calculations and moreTypeface - Sprite-based font include/module
LiK137
Enthusiast
Enthusiast
Posts: 282
Joined: Wed Jun 23, 2010 5:13 pm

Re: AES+Base64 encryption

Post by LiK137 »

Thank you for your response. I have changed StringBytes to Len, increased Padding data to get at least 64 bytes in input which did not sense or improve.
Then I added showmemory for visually inspection of data and compare memory blocks. Now it is exactly understood that AES1 and AES2 pairs and BASE1 and BASE2 pairs are identical but input raw data and output decrypted data differs in last bytes at end.
Below is the modified code:

Code: Select all

Global *raw1, *raw2, r1, r2,
       *aes1, *aes2, a1, a2,
       *baes1, *baes2, b1, b2


;{ bAES64
#hidrPAData = "~!@#$%^&*()_[{<+|+>}]_()*&^%$#@!~"
#futrPAData = "+>}]_()*&^%$#@!~|~!@#$%^&*()_[{<+"

Procedure VectoriSalt(*salt, szalt)
  Protected lup
  If szalt > 0 And MemorySize(*salt) >= szalt
    For lup = 0 To szalt - 1
      PokeA(*salt + lup, Random(255,0))
    Next  
  EndIf  
EndProcedure  
Procedure aesNCoder(*inData, Bits = 256, *K = 0, *Vector = 0)
  Protected szIn = MemorySize(*inData), RET = #False
  
  If *K = 0
    *K = ?Key
  EndIf
  If *Vector = 0
    *Vector = ?InitializationVector
  EndIf  
  
  Define *outData = AllocateMemory(szIn)
  If *outData
    If AESEncoder(*inData, *outData, szIn, *K, Bits, *Vector);, #PB_Cipher_ECB)
      Protected *tmp = AllocateMemory(szIn)
      If *tmp 
        If AESDecoder(*outData, *tmp, szIn, *K, Bits, *Vector);, #PB_Cipher_ECB)
          If CompareMemory(*inData, *tmp, szIn)
            RET = *outData
            Debug "1 " + szIn
          EndIf  
        EndIf  
        FreeMemory(*tmp)
      EndIf
    EndIf
    If RET = 0
      FreeMemory(*outData)
    EndIf  
  EndIf  
  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, Bits = 256, *K = 0, *Vector = 0)
  Protected szIn = MemorySize(*inData), RET = #False
  
  If *K = 0
    *K = ?Key
  EndIf
  If *Vector = 0
    *Vector = ?InitializationVector
  EndIf  
  
  Define *outData = AllocateMemory(szIn)
  If *outData
    If AESDecoder(*inData, *outData, szIn, *K, Bits, *Vector);, #PB_Cipher_ECB)
      *tmp = AllocateMemory(szIn)
      If *tmp 
        If AESEncoder(*outData, *tmp, szIn, *K, Bits, *Vector);, #PB_Cipher_ECB)
          If CompareMemory(*inData, *tmp, szIn)
            RET = *outData
            Debug "2 " + szIn
          EndIf  
        EndIf  
        FreeMemory(*tmp)
      EndIf
    EndIf
    If RET = 0
      FreeMemory(*outData)
    EndIf  
  EndIf
  
  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 bAES64enc(*inData, Bits = 256, *K = 0, *Vector = 0) ;inData - BinaryMem, outData - AESEnc&BasedEncMem
  Protected szIn = MemorySize(*inData), RET = #False
  
  Protected paDataLen = Len(#hidrPAData + #futrPAData), 
            hidrLen = Len(#hidrPAData), 
            futrLen = Len(#futrPAData), 
            szinPAData = szIn + paDataLen
  
  Protected *tmp = AllocateMemory(szinPAData)
  If *tmp
    PokeS(*tmp, #hidrPAData, hidrLen, #PB_UTF8|#PB_String_NoZero)
    CopyMemory(*inData, *tmp + hidrLen, szIn)
    PokeS(*tmp + hidrLen + szIn, #futrPAData, futrLen, #PB_UTF8|#PB_String_NoZero)
    *raw1 = AllocateMemory(szinPAData) : r1 = szinPAData
    CopyMemory(*tmp, *raw1, szinPAData)
    Define *AESenc = aesNCoder(*tmp, Bits, *K, *Vector)
    If *AESenc
      Protected szAESenc = MemorySize(*AESenc)
      
      *aes1 = AllocateMemory(szAESenc) : a1 = szAESenc
      CopyMemory(*AESenc, *aes1, szAESenc)
      
      *AESdecmp = aesDCoder(*AESenc, Bits, *K, *Vector)
      If *AESdecmp 
        Protected szAESdecmp = MemorySize(*AESdecmp)
        If szAESdecmp = szAESenc And CompareMemory(*AESdecmp, *tmp, szAESenc)
          Protected BAESData$ = Base64Encoder(*AESenc, szinPAData, #PB_Cipher_NoPadding|#PB_Cipher_URL), szBAESData = Len(BAESData$)
          Debug BAESData$
          Protected *bAESdecmp =  AllocateMemory(szBAESData*2)
          If *bAESdecmp
            Protected szoutData = Base64Decoder(BAESData$, *bAESdecmp, szBAESData*2)
            If CompareMemory(*bAESdecmp, *AESenc, szoutData) <> 0
              Protected szbAESenc = Len(BAESData$)
              Protected *bAESenc =  AllocateMemory(szbAESenc)
              If *bAESenc
                PokeS(*bAESenc, BAESData$, szbAESenc, #PB_UTF8|#PB_String_NoZero)
                *baes1 = AllocateMemory(szbAESenc) : b1 = szbAESenc
                CopyMemory(*bAESenc, *baes1, szbAESenc)
                RET = *bAESenc
                Debug "3 " + szbAESenc
              EndIf  
            EndIf
            FreeMemory(*bAESdecmp)
          EndIf
        EndIf  
        FreeMemory(*AESdecmp)
      EndIf    
      FreeMemory(*AESenc)
    EndIf  
    FreeMemory(*tmp)
  EndIf  
  ProcedureReturn RET
EndProcedure
Procedure bAES64dec(*inData, Bits = 256, *K = 0, *Vector = 0) ;inData - BasedMem, outData - BaseDec&AESDecMem
  Protected szIn = MemorySize(*inData), RET = #False
  
  *baes2 = AllocateMemory(szIn) : b2 = szIn
  CopyMemory(*inData, *baes2, szIn)
  
  Protected paDataLen = Len(#hidrPAData + #futrPAData), 
            hidrLen = Len(#hidrPAData), 
            futrLen = Len(#futrPAData), 
            szinPAData = szIn + paDataLen
  
  Protected inData$ = PeekS(*inData, szIn, #PB_UTF8), szInData = Len(inData$)
  
  Protected *bAESdec = AllocateMemory(szInData*2)
  If *bAESdec
    Protected szoutData = Base64Decoder(inData$, *bAESdec, szInData*2)
    If szoutData > 0
      *aes2 = AllocateMemory(szoutData): a2 = szoutData
      CopyMemory(*bAESdec, *aes2, szoutData)
      Protected Base64Data$ = Base64Encoder(*bAESdec, szoutData, #PB_Cipher_NoPadding|#PB_Cipher_URL), szBase64Data = Len(Base64Data$)
      If inData$ = Base64Data$
        Protected *AESdec = aesDCoder(*bAESdec, Bits, *K, *Vector)
        If *AESdec
          Protected szAESdec = MemorySize(*AESdec)
          Protected *AESenc = aesNCoder(*AESdec, Bits, *K, *Vector)
          If *AESenc
            Protected szAESenc = MemorySize(*AESenc)
            If CompareMemory(*AESenc, *bAESdec, szoutData)
              Protected tmpHidr.s = PeekS(*AESdec, hidrLen, #PB_UTF8), tmpFutr.s = PeekS(*AESdec + szAESdec - futrLen, futrLen, #PB_UTF8)
              If tmpHidr = #hidrPAData And tmpFutr = #futrPAData
                Protected szRawData = szAESdec - paDataLen
                Define *rawData = AllocateMemory(szRawData)
                If *rawData
                  CopyMemory(*AESdec + hidrLen, *rawData, szRawData)
                  RET = *rawData
                  Debug "6 " + szRawData
                EndIf  
              EndIf 
              *raw2 = AllocateMemory(szoutData) : r2 = szoutData
              CopyMemory(*AESdec, *raw2, szoutData)
            EndIf
            FreeMemory(*AESenc)
          EndIf  
          FreeMemory(*AESdec)
        EndIf  
      EndIf  
    EndIf
    FreeMemory(*bAESdec)
  EndIf  
  ProcedureReturn RET
EndProcedure
Procedure.s bAES64enxtring(rawString.s, Bits = 256, *K = 0, *Vector = 0) ;inData - BinaryMem, outData - AESEnc&BasedEncMem
  Protected RET.s = ""
  
  If rawString <> ""
    Protected szrawString = Len(rawString)
    Protected *rawData = AllocateMemory(szrawString)
    If *rawData
      PokeS(*rawData, rawString, szrawString, #PB_UTF8|#PB_String_NoZero)
      Protected szRawData = MemorySize(*rawData)
      Protected *BAESenc = bAES64enc(*rawData, Bits, *K, *Vector)
      If *BAESenc
        Protected szBAESenc = MemorySize(*BAESenc)
        Protected BAESenc$ = PeekS(*BAESenc, szBAESenc, #PB_UTF8)
        Protected *BAESdecmp = bAES64dec(*BAESenc, Bits, *K, *Vector)
        If *BAESdecmp
          Protected szBAESdec = MemorySize(*BAESdecmp)
          If szBAESdec = szRawData And CompareMemory(*BAESdecmp, *rawData, szRawData)
            RET = BAESenc$
          EndIf
          FreeMemory(*BAESdecmp)
        EndIf  
        FreeMemory(*BAESenc)
      EndIf
      FreeMemory(*rawData)
    EndIf
  EndIf  
  
  ProcedureReturn RET  
EndProcedure
Procedure.s bAES64dextring(BAESenc.s, Bits = 256, *K = 0, *Vector = 0) ;inData - BasedMem, outData - BaseDec&AESDecMem
  Protected RET.s = ""
  
  If BAESenc <> ""
    Protected szBAESenc = Len(BAESenc)
    Protected *BAESenc = AllocateMemory(szBAESenc)
    If *BAESenc
      PokeS(*BAESenc, BAESenc, szBAESenc, #PB_UTF8|#PB_String_NoZero)
      Protected szBAESencData = MemorySize(*BAESenc)      
      Protected *BAESdec = bAES64dec(*BAESenc, Bits, *K, *Vector)
      If *BAESdec
        Protected szBAESdec = MemorySize(*BAESdec)
        BAESdec.s = PeekS(*BAESdec, szBAESdec, #PB_UTF8)
        
        Protected *BAESencmp = bAES64enc(*BAESdec, Bits, *K, *Vector)
        If *BAESencmp
          Protected szBAESencmp = MemorySize(*BAESencmp)
          If szBAESencmp = szBAESencData And CompareMemory(*BAESencmp, *BAESenc, szBAESencmp)
            RET = BAESdec
          EndIf 
          FreeMemory(*BAESencmp)
        EndIf  
        FreeMemory(*BAESdec)
      EndIf
      FreeMemory(*BAESenc)
    EndIf  
  EndIf  
  ProcedureReturn RET
EndProcedure
;}

       
  For i = 1 To 1000
    k = Random(1000)  
    mystr.s = ""
    For j = 1 To k
      mystr.s +  Chr(Random(98,49))
    Next
    
    enxtr.s = bAES64enxtring(mystr)
    Debug "r1: " + CompareMemory(*raw1, *raw2, r1)
    Debug "r2: " + CompareMemory(*raw1, *raw2, r2)
    ShowMemoryViewer(*raw1, r1)
    CallDebugger
    ShowMemoryViewer(*raw2, r2)
    CallDebugger
    Debug "a1: " + CompareMemory(*aes1, *aes2, a1)
    Debug "a2: " + CompareMemory(*aes1, *aes2, a2)
    ShowMemoryViewer(*aes1, a1)
    CallDebugger
    ShowMemoryViewer(*aes2, a2)
    CallDebugger
    Debug "b1: " + CompareMemory(*baes1, *baes2, b1)
    Debug "b2: " + CompareMemory(*baes1, *baes2, b2)
    ShowMemoryViewer(*baes1, b1)
    CallDebugger
    ShowMemoryViewer(*baes2, b2)
    CallDebugger
    FreeMemory(*raw1)
    FreeMemory(*raw2)
    FreeMemory(*aes1)
    FreeMemory(*aes2)
    FreeMemory(*baes1)
    FreeMemory(*baes2)
    
    dextr.s = bAES64dextring(enxtr)
    Debug "r1: " + CompareMemory(*raw1, *raw2, r1)
    Debug "r2: " + CompareMemory(*raw1, *raw2, r2)
    ShowMemoryViewer(*raw1, r1)
    CallDebugger
    ShowMemoryViewer(*raw2, r2)
    CallDebugger
    Debug "a1: " + CompareMemory(*aes1, *aes2, a1)
    Debug "a2: " + CompareMemory(*aes1, *aes2, a2)
    ShowMemoryViewer(*aes1, a1)
    CallDebugger
    ShowMemoryViewer(*aes2, a2)
    CallDebugger
    Debug "b1: " + CompareMemory(*baes1, *baes2, b1)
    Debug "b2: " + CompareMemory(*baes1, *baes2, b2)
    ShowMemoryViewer(*baes1, b1)
    CallDebugger
    ShowMemoryViewer(*baes2, b2)
    CallDebugger
    FreeMemory(*raw1)
    FreeMemory(*raw2)
    FreeMemory(*aes1)
    FreeMemory(*aes2)
    FreeMemory(*baes1)
    FreeMemory(*baes2)
    
    If dextr <> mystr
      Debug i
      Debug "raw: " + Len(mystr) + " : " + mystr
      Debug "emx: " + Len(enxtr) + " : " + enxtr
      Debug "dex: " + Len(dextr) + " : " + dextr
    EndIf
  Next
End
User avatar
STARGÅTE
Addict
Addict
Posts: 2226
Joined: Thu Jan 10, 2008 1:30 pm
Location: Germany, Glienicke
Contact:

Re: AES+Base64 encryption

Post by STARGÅTE »

Your code still mixed up byte length and character length. I know, for characters between code points 0 and 127 it is the same, but having a correct code prevents later errors when using higher code points.

If you want to allocate memory for an UTF8-encoded string, you have to use:

Code: Select all

*Buffer = AllocateMemory(StringByteLength(String$, #PB_UTF8))     ; Byte Length
PokeS(*Buffer, String$, Len(String$), #PB_UTF8|#PB_String_NoZero) ; Character length
Don't mix up MemorySize() with Len() when calculating a length.
Don't mix up the shifting of a buffer (bytes) by adding a length (Len).
Don't use MemorySize() in PeekS for the length parameter. You can do it, but then you need the flag #PB_ByteLength in PeekS().

A part of your code which is still wrong is the allocation of szInData*2.
In line 161 you write:

Code: Select all

Protected *bAESdec = AllocateMemory(szInData*2)
Then you decode the data with Base64Decoder and receiving the real output length szoutData.
You use this later for Base64Encoder or CompareMemory().
However, in line 169 you pass just *bAESdec without indicating the "real" length.

Code: Select all

Protected *AESdec = aesDCoder(*bAESdec, Bits, *K, *Vector)
So here aesDCoder() uses a buffer *bAESdec which has a length of 2*szInData and not szoutData
PB 6.01 ― Win 10, 21H2 ― Ryzen 9 3900X, 32 GB ― NVIDIA GeForce RTX 3080 ― Vivaldi 6.0 ― www.unionbytes.de
Lizard - Script language for symbolic calculations and moreTypeface - Sprite-based font include/module
LiK137
Enthusiast
Enthusiast
Posts: 282
Joined: Wed Jun 23, 2010 5:13 pm

Re: AES+Base64 encryption

Post by LiK137 »

(However, in line 169 you pass just *bAESdec without indicating the "real" length) - You are absolutely right with those comments but the line 169 is terrible mistake and IMHO it may cause
Post Reply