Page 1 of 1

encrypt/decrypt text

Posted: Sat Apr 09, 2016 1:14 am
by jalz
Hey Guys,
Been doing a lot of reading on the subject and getting my head in a twist. I've wrote the following two functions which encrypt and decrypt text that will go into a database or be retrieved and displayed from a database. I'm experimenting with the AES cipher as I understand not all ciphers in LC work on each platform. I also understand that when I encrypt text, it returns a binary value which could get corrupted when inserted in my database. I therefore need to encode to a hex value and store this. Logic suggests that to retrieve the value back form the database I just need to do the reverse what I did to encrypt to decrypt the hash or are the functions I've written below a one way hash only?

If I'm using H* as the formatsList to binary decode to hex, should I be using B* to binary encode back to binary?

Many thanks
Jalz


function encrypt_text tData
encrypt tData using "aes-128-cbc" with key "FFFFFFFFFFFFDDCC" at 128 bit
get binarydecode("H*",it,eData)
return eData
end encrypt_text

function decrypt_text tData
put binaryencode("B*",tData) into eData
decrypt eData using "aes-128-cbc" with key "FFFFFFFFFFFFDDCC" at 128 bit
return eData
end decrypt_text

Re: encrypt/decrypt text

Posted: Sat Apr 09, 2016 1:21 am
by FourthWorld
You may find using base64encode and base64decode more convenient and producing smaller output.

Re: encrypt/decrypt text

Posted: Sat Apr 09, 2016 1:48 am
by jalz
Thanks Richard,
I've got it working, by simply changing the binary encode format to H*, the same as the decode function.
Still dont get it though, probably need some sleep to think about it but if I'm decoding binary to hex, thought I would have to encode hex back to binary....