Page 1 of 1
Encrypt error
Posted: Wed Mar 13, 2019 11:57 pm
by Krynn
Hello,
I have a problem I am using the Encrypt and Decrypt command.
But instead of perfectly deciphering the text.
I have random characters randomly.
At first glance it comes from the fact that I write in a file. When I put the text encrypt in a field. I do not have this problem.
I opened the text file. I think it's due to the format of the text file where line breaks are not well converted.
Do you have an idea?
thanks
Krynn
Re: Encrypt error
Posted: Thu Mar 14, 2019 12:07 am
by Klaus
Bonsoir Krynn,
quick guess:
Do you use BINFILE when writing and reading your encrypted stuff to/from file?
If not, do so and try again.
Encryptring text results in BINARY data!
Best
Klaus
Re: Encrypt error
Posted: Fri Mar 15, 2019 10:37 am
by Krynn
no, I do not use binfile
I'm doing the test tonight
Thank you
Re: Encrypt error
Posted: Fri Mar 15, 2019 11:26 am
by AxWald
Hi,
binary data (as in the result from 'encrypt') is a pest to handle/ display/ read. Converted to hex the data become much more amiable - check this:
Code: Select all
function B2H pString -- from libHash-Hmac V 2.3, http://marksmith.on-rev.com/revstuff/
repeat for each byte c in pString
get bytetonum(c)
put baseconvert(it,10,16) into tTemp
if it < 16 then put "0" before tTemp
put tTemp after tHex
end repeat
return tolower(tHex)
end B2H
function H2B pString -- from libHash-Hmac V 2.3, http://marksmith.on-rev.com/revstuff/
repeat with n = 1 to length(pString) - 1 step 2
put numtobyte(baseconvert(byte n to n + 1 of pString, 16, 10)) after tBin
end repeat
return tBin
end H2B
The functions convert ugly binary data:
Code: Select all
Salted__40046503…,^D7ˆMWtCÄ…°‰§î†_žÌ˜Mà‰½_Áë÷
to beautiful hex data (B2H):
Code: Select all
53616c7465645f5f3430303436353033852c5e4437884d7f577443c485b089a7ee865f9ecc980f4de08905bd5fc1ebf7
or back (H2B). You see the difference?
For the encryption/ decryption you may use these functions:
Code: Select all
function encStrg theStrg, thePass
put char -1 of the millisecs into MyChar
set the randomSeed to MyChar & MyChar & MyChar
put random(900000000) + 99999999 into MySalt
delete char 1 of MySalt
encrypt theStrg using "aes-256-cbc" with password thePass and salt MySalt
get B2H(it)
return it
end encStrg
function decStrg theStrg, thePass
put H2B(theStrg) into MyVar
decrypt MyVar using "aes-256-cbc" with password thePass
return it
end decStrg
As a bonus they come with a salt, means the encrypted data will look different each time you'll encrypt the same base data ;-)
Another bonus: There's a citation link in the first examples for Mark Smith's web site. It's strongly recommended to have a look at it!
Have fun!
Re: Encrypt error
Posted: Mon Mar 18, 2019 8:51 am
by Krynn
Hello AxWald,
Your appoche seems very relevant in order to get rid of all the special characters.
So I followed your advice and adapt my code.
And strength is found that it works great. I adopted it.
I have a question, I am not a specialist in the code, and I did not really understand the interest.
Code: Select all
put char-of-the-millisecs into MyChar
set the randomSeed to MyChar & MyChar & MyChar
put random (900000000) + 99999999 into MySalt
delete char 1 of MySalt
Hello Klaus,
I did not test your solution. But I will do it occasionally, I think it will be useful for other functions.
thank you