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

0 Members 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")