Encrypt output transform, help please?

Q&A's, tips, howto's
Locked
kanen
Posts: 145
Joined: Thu Mar 25, 2010 6:24 pm
Contact:

Encrypt output transform, help please?

Post by kanen »

I am using the newLisp (encrypt) function to encrypt something and need to decrypt it somewhere else, in JavaScript. Then, I have to send back a response from JavaScript and decrypt it in newLisp.

My JavaScript is weak-sauce, but here's the gist (cobbled together from the Internets): https://gist.github.com/a110621fe731facb40ff

The newLisp encrypt response looks like this:

Code: Select all

> (setf text_enc (encrypt "My name is Earl." "foobar")
"+\022O\012\000\031\003O\006\017A7\007\029\003L"))
Whereas the JavaScript output looks like this:

Code: Select all

> (encrypt("My name is Earl.", "foobar")
043022079012000031003079006017065055007029003076
I can make a few changes, like:

Code: Select all

> (unpack (dup "c" (length str)) str)
(43 22 79 12 0 31 3 79 6 17 65 55 7 29 3 76)
But, I still end up with something that's not properly JavaScript-y.

If I force everything to be properly formatted for JavaScript, it all works out...

Code: Select all

(join (map (fn (x) (format "%03d" x)) (unpack (dup "c" (length text_enc)) text_enc)) )
> "043022079012000031003079006017065055007029003076"
Until I try to go back to the format (encrypt) expects, then I'm at a loss. Because I can't just say...

Code: Select all

> (join (map (fn (x) (pack (dup "c" 3) x)) (explode js_text 3)))
"Ѐ?0?\000\016 0@P`p???"
I've tried (pack "n" x) and several other options. Nothing seems to get me where I need to be.

Anyone?

P.S. Why doesn't newLisp have SHA256 or AES again? :(
. Kanen Flowers http://kanen.me .

cormullion
Posts: 2038
Joined: Tue Nov 29, 2005 8:28 pm
Location: latiitude 50N longitude 3W
Contact:

Re: Encrypt output transform, help please?

Post by cormullion »

Perhaps Base64 encode/code it while its' travelling ?

kanen
Posts: 145
Joined: Thu Mar 25, 2010 6:24 pm
Contact:

Re: Encrypt output transform, help please?

Post by kanen »

cormullion wrote:Perhaps Base64 encode/code it while its' travelling ?
Doesn't solve the problem that JavaScript only wants it as a string of integers.
. Kanen Flowers http://kanen.me .

kanen
Posts: 145
Joined: Thu Mar 25, 2010 6:24 pm
Contact:

Re: Encrypt output transform, help please?

Post by kanen »

Figured it out -- I think.

(edit: spoke too soon... a text_enc of "\090\060\000\009" maps to (90 60) and the 9 gets removed. So I have to use the original, very long:

Code: Select all

(join (map (fn (x) (format "%03d" x)) (unpack (dup "c" (length text_enc)) text_enc)) )
I return the JavaScript happy version this way:

Code: Select all

(setf rtn_blob (join (map (fn (x) (format "%03d" x)) (map char (explode text_enc)))))
And, transform the JavaScript version to newLisp this way:

Code: Select all

(setf enc_str (join (map char (map (fn (x) (int x 0 10)) (explode enc_str 3)))))
Which I then pass to (encrypt enc_str my_pvt_password).
. Kanen Flowers http://kanen.me .

Lutz
Posts: 5289
Joined: Thu Sep 26, 2002 4:45 pm
Location: Pasadena, California
Contact:

Re: Encrypt output transform, help please?

Post by Lutz »

Keep in mind, that explode works on byte boundaries only in the utf8 version of newLISP. To make it work on both, utf8 and non-utf8, use:

Code: Select all

> (unpack (dup "c" (length str)) str) ; signed -128 to 127
(65 66 67)
> (unpack (dup "b" (length str)) str) ; unsigned 0 to 255
(65 66 67)
> (unpack (dup "s" (length str)) str) ; displayable character or "\000" decimal notation
("A" "B" "C")
> 

kanen
Posts: 145
Joined: Thu Mar 25, 2010 6:24 pm
Contact:

Re: Encrypt output transform, help please?

Post by kanen »

So, the question is...

Code: Select all

(setf enc_str "067020093000014001022091071092094008065017083090010089067030020084002084018094006091029018016000092085067042093051080017023064052051093080")
(setf enc_str (join (map char (map (fn (x) (int x 0 10)) (explode enc_str 3)))))
How do I turn this into something that works with (encrypt) non UTF-8 systems?
I can't seem to get (pack) to work the way I want.

Also, I can't compile newlisp on Android with UTF-8 support, because when I add UTF-8, I get hundreds of compile and link errors, starting with "newlisp.c:3524: undefined reference to `wchar_utf8"
. Kanen Flowers http://kanen.me .

Lutz
Posts: 5289
Joined: Thu Sep 26, 2002 4:45 pm
Location: Pasadena, California
Contact:

Re: Encrypt output transform, help please?

Post by Lutz »

We posted the last at the same second, see the difference of "b" and "c" in pack/unpack.

kanen
Posts: 145
Joined: Thu Mar 25, 2010 6:24 pm
Contact:

Re: Encrypt output transform, help please?

Post by kanen »

Lutz wrote:We posted the last at the same second, see the difference of "b" and "c" in pack/unpack.
Right, but I'm still asking: What the best way to pack enc_str back into \000 format for (encrypt).
I'm using this function, which gets an enc_str like "001003024" but it doesn't work on non UTF-8 systems, sadly.

Code: Select all

(join (map char (map (fn (x) (int x 0 10)) (explode enc_str 3))))
. Kanen Flowers http://kanen.me .

Lutz
Posts: 5289
Joined: Thu Sep 26, 2002 4:45 pm
Location: Pasadena, California
Contact:

Re: Encrypt output transform, help please?

Post by Lutz »

Not sure what problem you are having. Following works for me on both: utf8 and non-utf8 versions:

Code: Select all

(set 'js "043022079012000031003079006017065055007029003076")

(encrypt (join (map char (map (fn (x) (int x 0 10)) (explode js 3)))) "foobar") 
;=> "My name is Earl."
but the following is about double as fast:

Code: Select all

(encrypt (read-expr (append "\"\\" (join (explode js 3) "\\") "\"")) "foobar")
;=> "My name is Earl."

; or using non-escaping {,} as string delimiters

(encrypt (read-expr (append {"\} (join (explode js 3) {\}) {"})) "foobar")
;=> "My name is Earl." 
... and also works on utf8 and non-utf8.

Also when you produce the JavaScript formatted encrypted string use (unpack (dup "b" ...)) not (unpack (dup "c" ...)), which would produce negative integers for numbers > 127.

Locked