Just starting out? Need help? Post your questions and find answers here.
AgManiX
User
Posts: 10 Joined: Fri May 06, 2011 9:25 pm
Contact:
Post
by AgManiX » Fri May 24, 2013 7:56 pm
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 .
infratec
Always Here
Posts: 7662 Joined: Sun Sep 07, 2008 12:45 pm
Location: Germany
Post
by infratec » Fri May 24, 2013 8:19 pm
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
Posts: 10 Joined: Fri May 06, 2011 9:25 pm
Contact:
Post
by AgManiX » Fri May 24, 2013 9:00 pm
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
infratec
Always Here
Posts: 7662 Joined: Sun Sep 07, 2008 12:45 pm
Location: Germany
Post
by infratec » Fri May 24, 2013 9:18 pm
What is with your Length parameter???
Code: Select all
PureAES_Encrypt(*Buffer, *BufferLen.Long, EncryptionKey.s)
So you need
and
before you call PureAES_Encrypt()
Bernd
AgManiX
User
Posts: 10 Joined: Fri May 06, 2011 9:25 pm
Contact:
Post
by AgManiX » Fri May 24, 2013 9:36 pm
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
infratec
Always Here
Posts: 7662 Joined: Sun Sep 07, 2008 12:45 pm
Location: Germany
Post
by infratec » Fri May 24, 2013 10:07 pm
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
Posts: 7662 Joined: Sun Sep 07, 2008 12:45 pm
Location: Germany
Post
by infratec » Fri May 24, 2013 10:52 pm
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$
EndIfSince 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