Author Topic: (Challenge) Affine substitution cipher  (Read 5359 times)

0 Members and 1 Guest are viewing this topic.

squirreldip

  • Newt
  • Posts: 114
Re: (Challenge) Affine substitution cipher
« Reply #15 on: October 30, 2015, 04:33:58 PM »
Interesting - I didn't expect a difference between the 'mod' function (from wiki link) vs the "rem' function.  'rem' works differently for negative values...

Here's an update to my previous now with a decryptor:
Code: [Select]
;; < SPOILER > ;;
























(defun mod (a b)
 (rem (+ (rem a b) b) b)
)
(defun e (x a b / c res)
 (foreach c (vl-string->list (strcase x t))
  (setq res (append res (list (+ (mod (+ (* (- c 97) a) b) 26) 97))))
 )
 (vl-list->string res)
)
(defun d (x a b / c res)
 (foreach c (vl-string->list (strcase x t))
  (setq res (append res (list (+ (mod (* (- c 97 b) (- 26 (mod a 26))) 26) 97))))
 )
 (vl-list->string res)
)
« Last Edit: November 01, 2015, 03:46:25 PM by squirreldip »

Lee Mac

  • Seagull
  • Posts: 12929
  • London, England
Re: (Challenge) Affine substitution cipher
« Reply #16 on: October 30, 2015, 07:42:20 PM »
Here's an update to my previous now with a decryptor:
...be careful with the decryption as it is a quirk of the example that 5x%26=1 and x=26-5 (this is not true in general).

As an example, using your code:
Code: [Select]
_$ (d (e "hello" 7 8) 7 8)
"vmhhq"

Lee Mac

  • Seagull
  • Posts: 12929
  • London, England
Re: (Challenge) Affine substitution cipher
« Reply #17 on: October 30, 2015, 08:11:45 PM »
Here's another which caches the character map:

Code: [Select]
;; < SPOILER > ;;

















(defun e ( x a b )
    ((lambda ( l ) (vl-string-translate (car l) (cadr l) x)) (foo a b))
)
(defun d ( x a b )
    ((lambda ( l ) (vl-string-translate (cadr l) (car l) x)) (foo a b))
)
(defun-q foo ( a b / lst )
   '( )
    (if (or (/= a (car lst)) (/= b (cadr lst)))
        (setq lst (list a b (map a b))
              foo (vl-list* (car foo) (list 'setq 'lst (list 'quote lst)) (cddr foo))
        )
    )
    (caddr lst)
)
(defun map ( a b / c m n )
    (repeat (setq c 26)
        (setq c (1- c)
              m (cons (+ c 97) m)
              n (cons (+ (rem (+ (* c a) b) 26) 97) n)
        )
    )
    (mapcar 'vl-list->string (list m n))
)

gile

  • Gator
  • Posts: 2520
  • Marseille, France
Re: (Challenge) Affine substitution cipher
« Reply #18 on: November 09, 2015, 01:06:11 PM »
Hi

I didn't see this one.
My F# attempt on Try F#.

Code: [Select]
e 5 8 "hello" or
Code: [Select]
"hello" |> e 5 8  returns "rclla"

Code: [Select]
"rclla" |> d 5 8 returns "hello"

Code: [Select]
"Hello World !" |> e 7 8 returns "Hkhhc Wcxhd !"

Code: [Select]
"Hello World !" |> e 7 8 |> d 7 8 returns "Hello World !

Speaking English as a French Frog