TheSwamp
Code Red => AutoLISP (Vanilla / Visual) => Topic started by: matrix2005in on June 21, 2006, 08:48:54 AM
-
is there anyway to encrypt/decrypt a string using Autolisp/Visual lisp?
thanks
:roll:
mathew
-
As a simple example: :-)
(defun cript (str)
(vl-list->string
(mapcar
(function
(lambda (x)
(Boole 6 x 128)
) ;_ lambda
) ;_ function
(vl-string->list str)
) ;_ mapcar
) ;_ vl-list->string
) ;_ defun
;; Example:
(setq str-cript(cript "www.theswamp.org")) ; => "ччч®фиеучбнр®птз"
(setq str-decript(cript str-cript)) ; => "www.theswamp.org"
-
The routine posted by Evgeniy works well, but it's essentially a simple substitution cypher, so it's not very secure. What Microsoft did many years ago was use double substitution, using two keys. One key was 13 characters long and the other 11. I haven't coded this in lisp, so here's a flow chart. The index for the first character in a string is 0.
1. Initialixe x, y, and z each to 0
2. Get character #x from the string to encrypt or decrypt
3. Get character #y from the first key
4. XOR the characters from steps 2 and 3.
5. Get character #z from the second key
6. XOR the result from step 4 with the character from step 5.
7. Add the result from step 6 to the output steing.
8. Increment x by 1. If x > the length of the input string, you're done.
9. Increment y by 1. if y > the length of the first key reset y to 0.
10. Increment z by 1. if y > the length of the second key reset z to 0.
11. Go to step 2.
Evgeniy's routing uses a single character [128] as a key. By using two keys of 13 and 11 characters you get an effective key length of 143 characters, which is much more secure.
An XOR substitution works here because it's bidirectional:
01000001 XOR 00100011 = 01100010 (that's ASCII codes for 'A' XOR '#', which equals 'b')
01100010 XOR 00100011 = 01000001 (that's ASCII codes for 'b' XOR '#', which equals 'A')
I selected ASCII codes so that everything in the example is normal, printable characters.
If you use two keys it doesn't matter which key is used first:
(01000001 XOR 00100011) XOR 01010101 = 00110111
(01000001 XOR 01010101) XOR 00100011 = 00110111
-
> hmspe
Many thanks for explanations! :-)
The variant of the program is more complicated...
(defun cript (str key)
(mapcar
(function
(lambda (a b)
(Boole 6 a b)
) ;_ lambda
) ;_ function
(vl-string->list str)
(progn
(while (> (strlen str) (strlen key))
(setq key (strcat key key))
) ;_ while
(vl-string->list key)
) ;_ progn
) ;_ mapcar
) ;_ defun
(defun decript (lst key)
(vl-list->string
(mapcar
(function
(lambda (a b)
(Boole 6 a b)
) ;_ lambda
) ;_ function
lst
(progn
(while (> (length lst) (strlen key))
(setq key (strcat key key))
) ;_ while
(vl-string->list key)
) ;_ progn
) ;_ mapcar
) ;_ vl-list->string
) ;_ defun
Example:
(setq
str "\n; Let's encode the program...
(defun cript (str key)
(mapcar
(function
(lambda (a b)
(Boole 6 a b)
) ;_ lambda
) ;_ function
(vl-string->list str)
(progn
(while (> (strlen str) (strlen key))
(setq key (strcat key key))
) ;_ while
(vl-string->list key)
) ;_ progn
) ;_ mapcar
) ;_ defun
(defun decript (lst key)
(vl-list->string
(mapcar
(function
(lambda (a b)
(Boole 6 a b)
) ;_ lambda
) ;_ function
lst
(progn
(while (> (length lst) (strlen key))
(setq key (strcat key key))
) ;_ while
(vl-string->list key)
) ;_ progn
) ;_ mapcar
) ;_ vl-list->string
) ;_ defun"
key "Here there can be any text, anyone are long..."
) ;_ setq
(setq lst-cript(cript str key))
(setq str-decript(decript lst-cript key))
(alert (strcat "You have coded the text:\n\n " str-decript))
-
hi evgeniy
thanks for your gr8 algorithm
but while i am writing encrypted string to a text file i am getting error..can you tell me where i am doing mistake??
(setq
str "this is a test"
key "1234"
)
(defun c:test ()
(setq ab (cript str key))
(setq a (open "c:\\test\\tt.txt" "w"))
(setq g (write-line ab a))
(close a)
(princ)
)
-
(defun c:test (KEY STR / A AB G)
(setq ab (cript str key))
(setq a (open "c:\\tt.txt" "w"))
(setq g (write-line (VL-PRIN1-TO-STRING ab) a))
(close a)
(princ)
)
(c:test "1234" "this is a test")
-
hi Evgeniy
thanks for your instant code :-)
one more question...how i can remove this brackets from encrypted code??
the given below is the one i encrypted
(69 87 64 64 17 83 87 71 90 18 64 85 85)
thanks
-
hi Evgeniy
thanks for your instant code :-)
one more question...how i can remove this brackets from encrypted code??
the given below is the one i encrypted
(69 87 64 64 17 83 87 71 90 18 64 85 85)
thanks
The answer to your problem...
(setq s (VL-PRINC-TO-STRING '(69 87 64 64 17 83 87 71 90 18 64 85 85))
s (substr s 2 (- (strlen s) 2)))
or
((lambda (x)
(substr x 2 (- (strlen x) 2))
) ;_ lambda
(VL-PRINC-TO-STRING '(69 87 64 64 17 83 87 71 90 18 64 85 85))
)
Can you explain a problem bodily? :-)
-
oops actually you are not understood my question>>>
i will explain again after running this code:-(defun c:test (KEY STR / A AB G)
(setq ab (cript str key))
(setq a (open "c:\\tt.txt" "w"))
(setq g (write-line (VL-PRIN1-TO-STRING ab) a))
(close a)
(princ)
)
(c:test "1234" "this is a test")
i opened tt.text file manualy to see the out put...
the result was this "(69 87 64 64 17 83 87 71 90 18 64 85 85)"
what i want is remove bracket from output that means not from the given result... the bracket must be removed globally...
hope you understood
thanks
-
(defun c:test (KEY STR / A AB G)
(setq ab (cript str key))
(setq a (open "d:\\tt.txt" "w"))
(princ (apply (function strcat) (mapcar (function (lambda (x) (strcat (itoa x) " "))) ab)) a)
(close a)
(princ)
) ;_ defun
(c:test "1234" "this is a test")
-
Thats..blasting Evgeniy :-)
thanks
-
Thats..blasting Evgeniy :-)
thanks
:-)
(defun cript (str key)
(mapcar
(function
(lambda (a b)
(Boole 6 a b)
) ;_ lambda
) ;_ function
(vl-string->list str)
(progn
(while (> (strlen str) (strlen key))
(setq key (strcat key key))
) ;_ while
(vl-string->list key)
) ;_ progn
) ;_ mapcar
) ;_ defun
(defun decript (lst key)
(vl-list->string
(mapcar
(function
(lambda (a b)
(Boole 6 a b)
) ;_ lambda
) ;_ function
lst
(progn
(while (> (length lst) (strlen key))
(setq key (strcat key key))
) ;_ while
(vl-string->list key)
) ;_ progn
) ;_ mapcar
) ;_ vl-list->string
) ;_ defun
(defun c:test-cript (f1 f2 KEY / STR)
(setq f1 (open f1 "r")
f2 (open f2 "w")
) ;_ setq
(while (setq str (read-line f1))
(write-line
(apply
(function strcat)
(mapcar
(function
(lambda (x)
(strcat (itoa x) " ")
) ;_ lambda
) ;_ function
(cript str key)
) ;_ mapcar
) ;_ apply
f2
) ;_ write-line
) ;_ while
(close f1)
(close f2)
) ;_ defun
(defun c:test-decript (f1 f2 KEY / STR)
(setq f1 (open f1 "r")
f2 (open f2 "w")
) ;_ setq
(while (setq str (read-line f1))
(write-line (decript (read (strcat "(" str ")")) key) f2)
) ;_ while
(close f1)
(close f2)
) ;_ defun
(c:test-cript "d:\\text-1.txt" "d:\\text-2.txt" "1234")
(c:test-decript "d:\\text-2.txt" "d:\\text-3.txt" "1234")
-
oops actually you are not understood my question>>>
i will explain again after running this code:
Concerning a code - I have understood all! :-)
I at all do not understand, what for to you such program? :?
The code offered(suggested) by me, cannot give a high guard is a flight of fancy... :-(
-
hi Evgeniy
actually this is for a en cryted string comparison between a given file and in registry...well actually a simple protection method..i know there wont be a fool proof program available... :-)
can you tell me how i can put these values in registry?? i tried it with "vl-registry-write" function but no hope... :-(
thanks
-
(setenv "My varname" "My confidential code")
-
You may want to do 2 pass encoding: First pass does simple xor encoding, second pass ensures output will survive to/from the registry (not sure if the registry will support low order (control codes, < 32) or extended ascii (> 127), the frequent product of simple xoring methods).
One common method for ensuring the latter is base16|32|64 encoding (http://www.faqs.org/rfcs/rfc3548.html) which typically encodes to a limited (printable ascii) character set, let me know if you want sample lisp code to do base encoding.
Example:
(setq
text "Welcome to theswamp.org"
mask "Actually, I was talking to the duck."
)
(xortext text mask)
"\026\006\030\026\016\001\tYXOiT\037\004\000W\025\014\034E\006\034\000"
(tobase64 (xortext text mask))
"4VNN4Vs01K_NIq_J6kF-KlJB63J56--"
(xortext (frombase64 (tobase64 (xortext text mask))) mask)
"Welcome to theswamp.org"
Encryption is a vast topic that one could dedicate one's entire career to. Before committing to a solution you consider interogating the usual suspects, wikipedia.org, answers.com ...
-
wow interesting subject...but very complicated...well i will look into it
thanks...
actually my problem is now i gota different encryption methods with me..with the help of Evgeniy
i am able to do encrypt/decrypt as i want...upto this point now i am ok...now what my next move is i want to put encrypted values to registry...i know this simple code...
(vl-registry-write ""HKEY_CURRENT_USER\\Myapp" "app" "test data")
but this doesnt work here...i dont know what to do next :-(
-
>matrix2005in
You note a guard for yours programs?
-
well i am
what ever you are mensioned here is just a methed for me and i am not talking your entire stuff..say a portion..you know i am learning...you people are masters..and very good in lisp...i am just a beginner gotta lotsa ideas..but i am not getting the proper syntax/function to write it down :-(
-
well i am
what ever you are mensioned here is just a methed for me and i am not talking your entire stuff..say a portion..you know i am learning...you people are masters..and very good in lisp...i am just a beginner gotta lotsa ideas..but i am not getting the proper syntax/function to write it down :-(
(vl-registry-write ""HKEY_CURRENT_USER\\Myapp" "app" "test data")
=>
(vl-registry-write "HKEY_CURRENT_USER\\Myapp" "app" "test data")
For me, worked!
-
> matrix2005in
Probably, you have not understood me...
I wanted to you help, but not knowing, that you make, it is difficult for me. :(
Knowing your purpose, I can direct better and more easy you, for me will be more hints!
I have written crypt/decrypt - it was interesting to me - never was occupied with such programs.
Now for you a problem on simple function. (F1)
-
hi Evgeniy
i am really sorry for what ever confusion is created here...you helped me a lot that's true....
i will clear myself....
what i want is put a encrypted data in registry as well as in file..so when i call from another function both should return the same value...i know this for you is very simple...what i thought in the beginning if i will get a code for encrypt/decrypt then will do the rest myself...i failed and that's where the confusion created.... :-(
(vl-registry-write "HKEY_CURRENT_USER\\Myapp" "app" "test data")
This Will create a registry entry in HKEY_CURRENT_USER and the value of that is - test data
........in-spite of "test data" i want to put encrypted value there....
Evgeniy you are a good programmer... i am try to learn from you...
thanks
mathew
-
Matrix, I am going to throw out a caution for you. You should really be careful about writing to the registry except where absolutely necessary. The last thing that you want to do is corrupt your (or worse someone else's) registry.
-
hi
i am ok with windows registry...if anything happens i can roll it back
well my problem is using above encryption method..i cannot directly decrypt from file ....and if some modifications done with the encrypted file..this wont show up using decript method..what to do???
-
I advise, use as a key - one of programs.
Write all programs through EVAL and cipher them. Then with compile!
I did not try, but should work...
If each program will use as a key - different parts of one of programs and at install all will be crypt/decrypt under unique number of a computer - very high degree of protection should turn out!
To decrypt a code, I offer during execution of the program then to crack it is necessary each function is very difficultly!
-
Hi
it's interesting that this threads was calm for now seven years ...
I'm searching and reading and testing - and have a question:
Is the code from Evgeniy related to
https://en.wikipedia.org/wiki/One-time_pad ??
-
:-)