AESCipher commands working?

Just starting out? Need help? Post your questions and find answers here.
the.weavster
Posts: 220
Joined: Sat Mar 01, 2014 3:02 pm

AESCipher commands working?

Post by the.weavster »

I'm not sure if the AESCipher commands are not working or if I'm doing something wrong.

I've contrived this little snippet to demonstrate:

Code: Select all

Procedure.i StringByteLength(sdata.s)
  nsize = 0
  !v_nsize = new Blob([v_sdata]).size;
  ProcedureReturn nsize
EndProcedure

Procedure.s EncryptData(sData$,sKey$,sIV$)
  nDataLength = StringByteLength(sData$)
  *key    = AllocateMemory(40)
  *iv     = AllocateMemory(40)
  *bfrIn  = AllocateMemory(nDataLength + 10)
  *bfrOut = AllocateMemory(nDataLength * 2)   
  PokeS(*key,0,sKey$,32)
  PokeS(*iv,0,sIV$,32)
  PokeS(*bfrIn,0,sData$,nDataLength)
  sRes$ = "Didn't work :-("
  nRes = StartAESCipher(#PB_Any,*key,256,*iv,#PB_Cipher_Encode|#PB_Cipher_CBC)
  If nRes
    AddCipherBuffer(nRes,*bfrIn,0,*bfrOut,0,nDataLength)
    FinishCipher(nRes)
    sRes$ = PeekS(*bfrOut,-1)
  EndIf
  FreeMemory(*key)
  FreeMemory(*iv)
  FreeMemory(*bfrIn)
  FreeMemory(*bfrOut)
  ProcedureReturn sRes$
EndProcedure

sData.s = "Something to encrypt"
sKey.s  = "**A32ByteLongPasswordIsUnusual**"
sIV.s   = "ABCDEFGHIJKLMONPQRSTUVWXYZabcdef"

Debug EncryptData(sData,sKey,sIV)
the.weavster
Posts: 220
Joined: Sat Mar 01, 2014 3:02 pm

Re: AESCipher commands working?

Post by the.weavster »

I also can't get AESEncoder() to work unless the length of the data to be encrypted is exactly divisible by the length of the initialisation vector:

Code: Select all

Global *AESInitializationVector1
Global *AESKey1

Procedure.i StringByteLength(sdata.s)
  nsize = 0
  !v_nsize = new Blob([v_sdata]).size;
  ProcedureReturn nsize
EndProcedure

Procedure StringToMemory(sdata.s)
  Size = StringByteLength(sdata)
  *Buffer = AllocateMemory(Size)
  PokeS(*Buffer,0,sdata,Size,#PB_Ascii)
  ProcedureReturn *Buffer
EndProcedure

Procedure TryIt(sdata.s)
  *Text = StringToMemory(sdata)
  nInSize = StringByteLength(sdata)
  nOutsize = nInSize * 2
  *Output = AllocateMemory(nOutsize)
  *DecodedText = AllocateMemory(nOutsize)
  
  AESEncoder(*Text,0,*Output,0,nInSize,*AESKey1,128,*AESInitializationVector1)
  AESDecoder(*Output,0,*DecodedText,0,nOutSize,*AESKey1,128,*AESInitializationVector1)
  Debug PeekS(*DecodedText,0,nInSize,#PB_Ascii)
EndProcedure

*AESInitializationVector1 = StringToMemory("aStringOf16Bytes")
*AESKey1 = StringToMemory("*16BytesOfMagic*")

Text$ = "My length is divisible by 16 ;-)"
TryIt(Text$)

Text$ = "Although I am long and boring my length is also divisible by 16!"
TryIt(Text$)

Text$ = "A string of length not divisible by 16"
TryIt(Text$)
the.weavster
Posts: 220
Joined: Sat Mar 01, 2014 3:02 pm

Re: AESCipher commands working?

Post by the.weavster »

Also using PeekS() with a length of -1 to try and get the contents of the encrypted buffer makes the browser hang.

Code: Select all

PeekS(*Output,0,-1,#PB_Ascii)
Post Reply