AESCBCEncrypt(PHP openssl_encrypt for purebasic)

Just starting out? Need help? Post your questions and find answers here.
ynkrc
User
User
Posts: 55
Joined: Sat Sep 03, 2011 5:28 am

AESCBCEncrypt(PHP openssl_encrypt for purebasic)

Post 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:
#NULL
Addict
Addict
Posts: 1497
Joined: Thu Aug 30, 2007 11:54 pm
Location: right here

Re: AESCBCEncrypt(PHP openssl_encrypt for purebasic)

Post 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.
infratec
Always Here
Always Here
Posts: 7581
Joined: Sun Sep 07, 2008 12:45 pm
Location: Germany

Re: AESCBCEncrypt(PHP openssl_encrypt for purebasic)

Post 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.
#NULL
Addict
Addict
Posts: 1497
Joined: Thu Aug 30, 2007 11:54 pm
Location: right here

Re: AESCBCEncrypt(PHP openssl_encrypt for purebasic)

Post by #NULL »

Thanks infratec!
ynkrc
User
User
Posts: 55
Joined: Sat Sep 03, 2011 5:28 am

Re: AESCBCEncrypt(PHP openssl_encrypt for purebasic)

Post 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.
Post Reply