Encrypt output transform, help please?

Started by kanen, December 30, 2012, 12:51:27 PM

Previous topic - Next topic

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">https://gist.github.com/a110621fe731facb40ff



The newLisp encrypt response looks like this:
> (setf text_enc (encrypt "My name is Earl." "foobar")
"+22O12003103O0617A7072903L"))


Whereas the JavaScript output looks like this:
> (encrypt("My name is Earl.", "foobar")
043022079012000031003079006017065055007029003076


I can make a few changes, like:
> (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...
(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...
> (join (map (fn (x) (pack (dup "c" 3) x)) (explode js_text 3)))
"Ѐ?0?0016 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[/url] .

cormullion

#1
Perhaps Base64 encode/code it while its' travelling ?

kanen

#2
Quote from: "cormullion"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[/url] .

kanen

#3
Figured it out -- I think.



(edit: spoke too soon...  a text_enc of "90600009" maps to (90 60) and the 9 gets removed. So I have to use the original, very long: (join (map (fn (x) (format "%03d" x)) (unpack (dup "c" (length text_enc)) text_enc)) )

I return the JavaScript happy version this way:
(setf rtn_blob (join (map (fn (x) (format "%03d" x)) (map char (explode text_enc)))))

And, transform the JavaScript version to newLisp this way:
(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[/url] .

Lutz

#4
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:
> (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 "00" decimal notation
("A" "B" "C")
>

kanen

#5
So, the question is...

(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[/url] .

Lutz

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

kanen

#7
Quote from: "Lutz"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 00 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.
(join (map char (map (fn (x) (int x 0 10)) (explode enc_str 3))))
. Kanen Flowers http://kanen.me[/url] .

Lutz

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


(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:
(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.