Author Topic: Encrypt/decrypt given string  (Read 11269 times)

RONALDTP and 1 Guest are viewing this topic.

matrix2005in

  • Guest
Encrypt/decrypt given string
« on: June 21, 2006, 08:48:54 AM »
is there anyway to encrypt/decrypt a string using Autolisp/Visual lisp?

thanks

:roll:
mathew


ElpanovEvgeniy

  • Water Moccasin
  • Posts: 1569
  • Moscow (Russia)
Re: Encrypt/decrypt given string
« Reply #1 on: June 21, 2006, 09:09:38 AM »
As a simple example:  :-)

Code: [Select]
(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"

hmspe

  • Bull Frog
  • Posts: 362
Re: Encrypt/decrypt given string
« Reply #2 on: June 21, 2006, 10:32:55 AM »
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

 
"Science is the belief in the ignorance of experts." - Richard Feynman

ElpanovEvgeniy

  • Water Moccasin
  • Posts: 1569
  • Moscow (Russia)
Re: Encrypt/decrypt given string
« Reply #3 on: June 21, 2006, 10:37:56 AM »
> hmspe
Many thanks for explanations!  :-)

The variant of the program is more complicated...

Code: [Select]
(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:

Code: [Select]
(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))


matrix2005in

  • Guest
Re: Encrypt/decrypt given string
« Reply #4 on: June 21, 2006, 11:15:35 AM »
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??

Code: [Select]
(setq
str "this is a test"
key "1234"
)

Code: [Select]
(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)
  )

ElpanovEvgeniy

  • Water Moccasin
  • Posts: 1569
  • Moscow (Russia)
Re: Encrypt/decrypt given string
« Reply #5 on: June 21, 2006, 11:26:12 AM »
Code: [Select]
(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")

matrix2005in

  • Guest
Re: Encrypt/decrypt given string
« Reply #6 on: June 21, 2006, 01:01:28 PM »
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

ElpanovEvgeniy

  • Water Moccasin
  • Posts: 1569
  • Moscow (Russia)
Re: Encrypt/decrypt given string
« Reply #7 on: June 21, 2006, 01:15:47 PM »
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...

Code: [Select]
(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
Code: [Select]
((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? :-)

matrix2005in

  • Guest
Re: Encrypt/decrypt given string
« Reply #8 on: June 21, 2006, 01:46:34 PM »
oops actually you are not understood my question>>>

i will explain again after running this code:-
Code: [Select]
(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

ElpanovEvgeniy

  • Water Moccasin
  • Posts: 1569
  • Moscow (Russia)
Re: Encrypt/decrypt given string
« Reply #9 on: June 21, 2006, 01:57:39 PM »
Code: [Select]
(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")

matrix2005in

  • Guest
Re: Encrypt/decrypt given string
« Reply #10 on: June 21, 2006, 02:16:45 PM »
Thats..blasting Evgeniy :-)

thanks

ElpanovEvgeniy

  • Water Moccasin
  • Posts: 1569
  • Moscow (Russia)
Re: Encrypt/decrypt given string
« Reply #11 on: June 21, 2006, 02:20:46 PM »
Thats..blasting Evgeniy :-)

thanks

 :-)

Code: [Select]
(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")

ElpanovEvgeniy

  • Water Moccasin
  • Posts: 1569
  • Moscow (Russia)
Re: Encrypt/decrypt given string
« Reply #12 on: June 21, 2006, 02:34:17 PM »
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...  :-(

matrix2005in

  • Guest
Re: Encrypt/decrypt given string
« Reply #13 on: June 22, 2006, 10:04:44 AM »
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


ElpanovEvgeniy

  • Water Moccasin
  • Posts: 1569
  • Moscow (Russia)
Re: Encrypt/decrypt given string
« Reply #14 on: June 22, 2006, 10:39:56 AM »
(setenv "My varname" "My confidential code")

MP

  • Seagull
  • Posts: 17750
  • Have thousands of dwgs to process? Contact me.
Re: Encrypt/decrypt given string
« Reply #15 on: June 22, 2006, 10:40:40 AM »
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 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 ...
Engineering Technologist • CAD Automation Practitioner
Automation ▸ Design ▸ Drafting ▸ Document Control ▸ Client
cadanalyst@gmail.comhttp://cadanalyst.slack.comhttp://linkedin.com/in/cadanalyst

matrix2005in

  • Guest
Re: Encrypt/decrypt given string
« Reply #16 on: June 22, 2006, 11:10:20 AM »
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...
Code: [Select]
(vl-registry-write ""HKEY_CURRENT_USER\\Myapp" "app" "test data")but this doesnt work here...i dont know what to do next  :-(

ElpanovEvgeniy

  • Water Moccasin
  • Posts: 1569
  • Moscow (Russia)
Re: Encrypt/decrypt given string
« Reply #17 on: June 22, 2006, 11:40:35 AM »
>matrix2005in
You note a guard for yours programs?

matrix2005in

  • Guest
Re: Encrypt/decrypt given string
« Reply #18 on: June 22, 2006, 11:56:25 AM »
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 :-(




ElpanovEvgeniy

  • Water Moccasin
  • Posts: 1569
  • Moscow (Russia)
Re: Encrypt/decrypt given string
« Reply #19 on: June 22, 2006, 12:08:08 PM »
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!


ElpanovEvgeniy

  • Water Moccasin
  • Posts: 1569
  • Moscow (Russia)
Re: Encrypt/decrypt given string
« Reply #20 on: June 22, 2006, 12:28:30 PM »
> 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)

matrix2005in

  • Guest
Re: Encrypt/decrypt given string
« Reply #21 on: June 22, 2006, 01:53:25 PM »
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

Bob Wahr

  • Guest
Re: Encrypt/decrypt given string
« Reply #22 on: June 22, 2006, 02:31:45 PM »
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.

matrix2005in

  • Guest
Re: Encrypt/decrypt given string
« Reply #23 on: June 23, 2006, 04:18:06 PM »
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???

ElpanovEvgeniy

  • Water Moccasin
  • Posts: 1569
  • Moscow (Russia)
Re: Encrypt/decrypt given string
« Reply #24 on: June 23, 2006, 05:57:16 PM »
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!

Peter2

  • Swamp Rat
  • Posts: 653
Re: Encrypt/decrypt given string
« Reply #25 on: October 09, 2013, 05:25:44 AM »
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 ??
Peter

AutoCAD Map 3D 2023 German (so some technical terms will be badly retranslated to English)
BricsCAD V23

ElpanovEvgeniy

  • Water Moccasin
  • Posts: 1569
  • Moscow (Russia)
Re: Encrypt/decrypt given string
« Reply #26 on: October 09, 2013, 07:48:29 AM »
 :-)