Page 1 of 1

AESCBCEncrypt(PHP openssl_encrypt for purebasic)

Posted: Fri Dec 13, 2019 1:19 pm
by ynkrc

Code: Select all

Procedure.s AESCBCEncrypt(text.s,password.s,InitializationVector.s,method.l=128,padding.l=1,flag.l=#PB_UTF8)
  ;padding--0表示ZeroPadding;1表示pkcs7padding/pkcs5padding(通过其他软件测试没有发现加密结果不一样)
  ;padding--0 represents "ZeroPadding";1 represents "pkcs7padding"/"pkcs5padding"(It seems that, there is no different encryption results found through other software tests.)
  Protected.i Length, pokelen, slen, i
  Protected *KeyAES, *iv, *InAES, *outAES
  Protected.s rst
  *KeyAES = AllocateMemory(32)
  *iv = AllocateMemory(32)
  If *KeyAES And *iv
    If StringByteLength(password, flag) <= 32 And StringByteLength(InitializationVector, flag) <= 32
      PokeS(*KeyAES, password, -1, flag|#PB_String_NoZero)
      PokeS(*iv, InitializationVector, -1, flag|#PB_String_NoZero)
      slen = StringByteLength(text, flag)
      pokelen=16-(slen%16)
      Length=slen+Pokelen
      *InAES = AllocateMemory(Length)
      PokeS(*InAES, text, -1, flag)
      If *InAES
        For i=0 To pokelen-1
          Select padding
            Case 0
              PokeA(*InAES+slen+i,0)
            Case 1
              PokeA(*InAES+slen+i,Pokelen)
          EndSelect
        Next
        *outAES = AllocateMemory(Length)
        If *outAES
          If AESEncoder(*inAES, *outAES, Length, *KeyAES, method, *iv, #PB_Cipher_CBC)
            rst=Base64Encoder(*outAES, Length)
          EndIf
          FreeMemory(*outAES)
        EndIf
        FreeMemory(*InAES)
      EndIf
    EndIf
    FreeMemory(*KeyAES)
  EndIf
  
  ProcedureReturn rst
EndProcedure
;####测试代码/testCode##########################################
; Define e$
; e$=AESCBCEncrypt("1234567890AESEncrypt","12345678abcdefgh","0102030405060708")
; Debug e$ 
Have fun! :lol:

Re: AESCBCEncrypt(PHP openssl_encrypt for purebasic)

Posted: Sat Dec 14, 2019 11:43 am
by #NULL
Could you explain what you are doing and why? If I understand correctly the point is to handle different target encodings as well as the padding for base64. I don't understand case 1, i.e. filling the padding with the value of its size. When is that necessary?
BTW, you have If *InAES but you are already poking in there beforehand. And you have no FreeMemory() for *iv.

Re: AESCBCEncrypt(PHP openssl_encrypt for purebasic)

Posted: Sat Dec 14, 2019 12:39 pm
by infratec
A bit modified:

Code: Select all

CompilerIf #PB_Compiler_IsMainFile
  EnableExplicit
CompilerEndIf


Procedure.s AESCBCEncrypt(Text$, Password$, InitializationVector$, Method.i=128, Padding.i=1, Flag.i=#PB_UTF8)
  ;padding--0 represents "ZeroPadding";1 represents "pkcs7padding"/"pkcs5padding"(It seems that, there is no different encryption results found through other software tests.)
  
  Protected.i Length, PokeLen, SLen, i
  Protected *KeyAES, *IV, *InAES, *OutAES
  Protected Result$
  
  
  *KeyAES = AllocateMemory(32)
  If *KeyAES
    *IV = AllocateMemory(32)
    If *IV
      If StringByteLength(Password$, Flag) <= 32 And StringByteLength(InitializationVector$, Flag) <= 32
        PokeS(*KeyAES, password$, -1, Flag|#PB_String_NoZero)
        PokeS(*IV, InitializationVector$, -1, Flag|#PB_String_NoZero)
        SLen = StringByteLength(Text$, Flag)
        PokeLen = 16 - (SLen % 16)
        Length = SLen + PokeLen
        *InAES = AllocateMemory(Length)
        If *InAES
          PokeS(*InAES, Text$, -1, Flag)
          For i = 0 To PokeLen - 1
            Select Padding
              Case 0
                PokeA(*InAES + SLen + i, 0)
              Case 1
                PokeA(*InAES + SLen + i, PokeLen)
            EndSelect
          Next
          *OutAES = AllocateMemory(Length)
          If *OutAES
            If AESEncoder(*InAES, *OutAES, Length, *KeyAES, Method, *IV, #PB_Cipher_CBC)
              Result$ = Base64Encoder(*OutAES, Length)
            EndIf
            FreeMemory(*OutAES)
          EndIf
          FreeMemory(*InAES)
        EndIf
      EndIf
      FreeMemory(*IV)
    EndIf
    FreeMemory(*KeyAES)
  EndIf
  
  ProcedureReturn Result$
  
EndProcedure


CompilerIf #PB_Compiler_IsMainFile
  Define e$
  
  e$ = AESCBCEncrypt("1234567890AESEncrypt", "12345678abcdefgh", "0102030405060708")
  Debug e$
CompilerEndIf
Padding:

https://crypto.stackexchange.com/questi ... s7-padding

From RFC2898:
4. Concatenate M and a padding string PS to form an encoded
message EM:

EM = M || PS ,

where the padding string PS consists of 8-(||M|| mod 8) octets
each with value 8-(||M|| mod 8). The padding string PS will
satisfy one of the following statements:

PS = 01, if ||M|| mod 8 = 7 ;
PS = 02 02, if ||M|| mod 8 = 6 ;
...
PS = 08 08 08 08 08 08 08 08, if ||M|| mod 8 = 0.

Re: AESCBCEncrypt(PHP openssl_encrypt for purebasic)

Posted: Sat Dec 14, 2019 1:30 pm
by #NULL
Thanks infratec!

Re: AESCBCEncrypt(PHP openssl_encrypt for purebasic)

Posted: Sat Dec 14, 2019 4:10 pm
by ynkrc
infratec wrote:A bit modified:

Code: Select all

CompilerIf #PB_Compiler_IsMainFile
  EnableExplicit
CompilerEndIf

.......
......
Thank you for correcting the code.
I found "aes-128-cbc" encode result in Purebasic is diffrent from JS and PHP(openssl_encrypt),so, I write these codes for Http request use.
May be helpful if somebody is working on this.