Problems with PureAES

Just starting out? Need help? Post your questions and find answers here.
AgManiX
User
User
Posts: 10
Joined: Fri May 06, 2011 9:25 pm
Contact:

Problems with PureAES

Post by AgManiX »

Hello!
I have a problem with code (require PureAES library), on the line #17:

Code: Select all

Global SecuredContent, Password.s, Lenght, ReadyContent.s, Filename.s
Procedure GeneratePassword()
  OpenCryptRandom()
  Password.s=Str(CryptRandom(2147483647))
  CloseCryptRandom()
EndProcedure
Procedure Secure(Content.s)
  Lenght = StringByteLength(Content) ; Длина
  Debug Password
  Debug Content
  Debug Lenght
*SecuredContent = PureAES_Encrypt(@Content, @Lenght, Password) ;Засекреченый текст
SecuredContent = *SecuredContent
ReadyContent = PeekS(*SecuredContent, Lenght)
EndProcedure
Procedure Unsecure(SecuredContent.s,Password)
  *SecuredContent=SecuredContent;<----------------------------Problem are here
  *UnsecuredContent = PureAES_Decrypt(*SecuredContent, @Lenght, Password)
  Debug "PureAES_Decrypt : Returned value = " + Str(*UnsecuredContent)
  Debug "PureAES_Decrypt : BufferLen = " + Str(Lenght)
  Lenght / SizeOf(Character) ; ANSI != UNICODE
  ReadyContent=PeekS(*UnsecuredContent, Lenght)
  PureAES_FreeBuffer(*SecuredContent)
  PureAES_FreeBuffer(*UnsecuredContent) 
EndProcedure
Procedure SaveNote(Title.s,Content.s)
GeneratePassword()
Secure(Content)
Filename=CreateFile(0,Title+"."+Password)
WriteStringN(0,ReadyContent)
CloseFile(0)
EndProcedure
Procedure ReadNote(Filename.s)
  Password=GetExtensionPart(Filename)
  OpenFile(0,Filename.s)
  SecuredContent.s=""
  While Eof(0)=0
    SecuredContent=SecuredContent+ReadString(0)
  Wend
  Unsecure(SecuredContent,Password)
  CloseFile(0)
EndProcedure

  SaveNote("Save Test","Wow, it works!")
  ReadNote(Filename)
What is wrong? Help me please!

Best wishes, Max Nixischev, AGMAsoft.
Best regards, Max Nixischev, AGMAsoft.
Multilingual PureBasic Search[/b]
infratec
Always Here
Always Here
Posts: 7662
Joined: Sun Sep 07, 2008 12:45 pm
Location: Germany

Re: Problems with PureAES

Post by infratec »

Hi,

here is a working example

Code: Select all

Procedure Unsecure(SecuredContent.s,Password.s)
  *SecuredContent=@SecuredContent
  Debug PeekS(*SecuredContent)
EndProcedure

Unsecure("Test01", "Test02")
And even if Password is global, if you use Password as parameter it is local and you need .s else it is an integer.

Bernd
AgManiX
User
User
Posts: 10
Joined: Fri May 06, 2011 9:25 pm
Contact:

Re: Problems with PureAES

Post by AgManiX »

Thank you, Bernd!
But now I have a new one problem :(
When I use "Debug ReadyContent". i see this: This text must b????????d and decoded!
But it must be: This text must be encoded and decoded!
What's wrong whith this code?

Code: Select all

Global SecuredContent, Password.s, Lenght, ReadyContent.s, Filename.s
Procedure GeneratePassword()
  OpenCryptRandom()
  Password.s=Str(CryptRandom(2147483647))
  CloseCryptRandom()
EndProcedure
Procedure Secure(Content.s)
  Lenght = StringByteLength(Content) ; Длина
  Debug Password
  Debug Content
  Debug Lenght
*SecuredContent = PureAES_Encrypt(@Content, @Lenght, Password) ;Засекреченый текст
SecuredContent = *SecuredContent
ReadyContent = PeekS(*SecuredContent, Lenght)
  PureAES_FreeBuffer(*SecuredContent)
EndProcedure
Procedure Unsecure(SecuredContent.s,Password.s)
  *SecuredContent=@SecuredContent
  Debug PeekS(*SecuredContent)
  *UnsecuredContent = PureAES_Decrypt(*SecuredContent, @Lenght, Password)
  Debug "PureAES_Decrypt : Returned value = " + Str(*UnsecuredContent)
  Debug "PureAES_Decrypt : BufferLen = " + Str(Lenght)
  Lenght / SizeOf(Character) ; ANSI != UNICODE
  ReadyContent=PeekS(*UnsecuredContent, Lenght)
  PureAES_FreeBuffer(*UnsecuredContent) 
EndProcedure
Procedure SaveNote(Title.s,Content.s)
GeneratePassword()
Secure(Content)
Filename=Title+"."+Password
CreateFile(0,Filename)
WriteStringN(0,ReadyContent)
CloseFile(0)
EndProcedure
Procedure ReadNote(Filename.s)
  Password=GetExtensionPart(Filename)
  OpenFile(0,Filename.s)
  ReadSecuredContent.s=""
  While Eof(0)=0
    ReadSecuredContent=ReadSecuredContent+ReadString(0)
  Wend
  Unsecure(ReadSecuredContent,Password)
  CloseFile(0)
EndProcedure

  SaveNote("Тестируем сохраняшку","This text must be encoded and decoded!")
  ReadNote(Filename)
  Debug ReadyContent
Max Nixischev
Best regards, Max Nixischev, AGMAsoft.
Multilingual PureBasic Search[/b]
infratec
Always Here
Always Here
Posts: 7662
Joined: Sun Sep 07, 2008 12:45 pm
Location: Germany

Re: Problems with PureAES

Post by infratec »

What is with your Length parameter???

Code: Select all

PureAES_Encrypt(*Buffer, *BufferLen.Long, EncryptionKey.s)
So you need

Code: Select all

Global Length.l
and

Code: Select all

Length = Len(SecuredContent)
before you call PureAES_Encrypt()

Bernd
AgManiX
User
User
Posts: 10
Joined: Fri May 06, 2011 9:25 pm
Contact:

Re: Problems with PureAES

Post by AgManiX »

hm... I replaced Global Lenght -> Lenght.l, but nothing changed - the same text in output
And when I add Len() I have error (bugs), have you any ideas?

Code: Select all

Global SecuredContent, Password.s, Lenght.l, ReadyContent.s, Filename.s
Procedure GeneratePassword()
  OpenCryptRandom()
  Password.s=Str(CryptRandom(2147483647))
  CloseCryptRandom()
EndProcedure
Procedure Secure(Content.s)
  Lenght = StringByteLength(Content) ; Длина
*SecuredContent = PureAES_Encrypt(@Content, @Lenght, Password) ;Засекреченый текст
;SecuredContent = *SecuredContent
ReadyContent = PeekS(*SecuredContent, Lenght)
  PureAES_FreeBuffer(*SecuredContent)
EndProcedure
Procedure Unsecure(SecuredContent.s,Password.s)
  *SecuredContent=@SecuredContent
  Debug PeekS(*SecuredContent)
  *UnsecuredContent = PureAES_Decrypt(*SecuredContent, @Lenght, Password)
  Lenght / SizeOf(Character) ; ANSI != UNICODE
  ReadyContent=PeekS(*UnsecuredContent, Lenght)
  PureAES_FreeBuffer(*UnsecuredContent) 
EndProcedure
Procedure SaveNote(Title.s,Content.s)
GeneratePassword()
Secure(Content)
Filename=Title+"."+Password
CreateFile(0,Filename)
WriteStringN(0,ReadyContent)
CloseFile(0)
EndProcedure
Procedure ReadNote(Filename.s)
  Password=GetExtensionPart(Filename)
  OpenFile(0,Filename.s)
  ReadSecuredContent.s=""
  While Eof(0)=0
    ReadSecuredContent=ReadSecuredContent+ReadString(0)
  Wend
  Unsecure(ReadSecuredContent,Password)
  CloseFile(0)
EndProcedure

  SaveNote("Тестируем сохраняшку","This text must be encoded and decoded!")
  ReadNote(Filename)
  Debug ReadyContent
Best regards, Max Nixischev, AGMAsoft.
Multilingual PureBasic Search[/b]
infratec
Always Here
Always Here
Posts: 7662
Joined: Sun Sep 07, 2008 12:45 pm
Location: Germany

Re: Problems with PureAES

Post by infratec »

Hi,

in my opinion you made it wrong.

The Length is needed before you call PureAES_Decrypt()

Like this:

Code: Select all

Procedure Unsecure(SecuredContent.s,Password.s)
  *SecuredContent=@SecuredContent
  Debug PeekS(*SecuredContent)
  Length = StringByteLength(SecuredContent)
  *UnsecuredContent = PureAES_Decrypt(*SecuredContent, @Lenght, Password)
  ;Lenght / SizeOf(Character) ; ANSI != UNICODE
  ;ReadyContent=PeekS(*UnsecuredContent, Lenght)
  ReadyContent=PeekS(*UnsecuredContent)
  PureAES_FreeBuffer(*UnsecuredContent)
EndProcedure
Bernd
infratec
Always Here
Always Here
Posts: 7662
Joined: Sun Sep 07, 2008 12:45 pm
Location: Germany

Re: Problems with PureAES

Post by infratec »

Maybe this works:

Code: Select all

EnableExplicit


Procedure.s GeneratePassword()
  
  Protected Result$
  
  OpenCryptRandom()
  Result$ = Str(CryptRandom(2147483647))
  CloseCryptRandom()
  
  ProcedureReturn Result$
  
EndProcedure

Procedure.s Secure(Content.s, Password$)
  
  Protected Result$, Length.l, *SecuredContent
   
  Length = StringByteLength(Content)
  Debug Password$
  Debug Content
  Debug Length
  *SecuredContent = PureAES_Encrypt(@Content, @Length, Password)
  Result$ = PeekS(*SecuredContent)
  PureAES_FreeBuffer(*SecuredContent)
  
  ProcedureReturn Result$
  
EndProcedure

Procedure.s Unsecure(SecuredContent.s,Password.s)
  
  Protected Result$, *SecuredContent, *UnsecuredContent, Length.l
  
  Length = StringByteLength(SecuredContent)
  *SecuredContent=@SecuredContent
  Debug PeekS(*SecuredContent)
  *UnsecuredContent = PureAES_Decrypt(*SecuredContent, @Lenght, Password)
  Debug "PureAES_Decrypt : Returned value = " + Str(*UnsecuredContent)
  Debug "PureAES_Decrypt : BufferLen = " + Str(Length)
  Result$ = PeekS(*UnsecuredContent)
  PureAES_FreeBuffer(*UnsecuredContent)
  
  ProcedureReturn Result$
  
EndProcedure

Procedure.s SaveNote(Title.s, Content.s)
  
  Protected Result$, Password$, Filename.s, Encoded$
  
  Password$ = GeneratePassword()
  Encoded$ = Secure(Content, Password$)
  Filename = Title + "." + Password$
  If CreateFile(0, Filename)
    WriteStringN(0, Encoded$)
    CloseFile(0)
    Result$ = Filename
  EndIf
  
  ProcedureReturn Result$
  
EndProcedure

Procedure.s ReadNote(Filename.s)
  
  Protected Password.s, Result$
  
  Password = GetExtensionPart(Filename)
  If ReadFile(0,Filename.s)
    While Not Eof(0)
      Result$ + ReadString(0)
    Wend
    CloseFile(0)
    Result$ = Unsecure(Result$, Password)
  EndIf
  
  ProcedureReturn Result$
  
EndProcedure


Define Filename$, ReadyContent$

Filename$ = SaveNote("Title","This text must be encoded and decoded!")
If Filename$
  ReadyContent$ = ReadNote(Filename$)
  Debug ReadyContent$
EndIf
Since I didn't installed the library I can not test it.

But why don't you use the inbuild AES functions (AESEncoder() and AESDecoder()) :?:
This is crossplatform.

Bernd
Post Reply