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

0 Members and 1 Guest are viewing this topic.

JohnK

  • Administrator
  • Seagull
  • Posts: 10669
(Challenge) Affine substitution cipher
« on: October 30, 2015, 09:24:14 AM »
It's been awhile since I've made a challenge so I wanted to make a fun one (being that it's Friday).

Today's challenge is to create a simple cipher called the: Affine Cipher.

As a check:
1. Using: a = 5
2. Using: b = 8
3. Using: x = hello
E(x) will be: rclla

Here is the wikipedia page: https://en.wikipedia.org/wiki/Affine_cipher

Also, try not to leave too many variables on the stack that "Eve"can checkout and get your text (keep your code clean and tiddy).


If you finish coding up your Affine Cipher quickly, move on to creating the Vingenere Cipher, which is a polyalphabetic cipher (many alphabets...using a keyword).
Here is the wikipedia page: https://en.wikipedia.org/wiki/Vigen%C3%A8re_cipher
TheSwamp.org (serving the CAD community since 2003)
Member location map - Add yourself

Donate to TheSwamp.org

ChrisCarlson

  • Guest
Re: (Challenge) Affine substitution cipher
« Reply #1 on: October 30, 2015, 10:05:48 AM »
Just thought of something, it would be pretty neat if there was a "spoiler" button which would hide the reply if it contained a proposed solution.

JohnK

  • Administrator
  • Seagull
  • Posts: 10669
Re: (Challenge) Affine substitution cipher
« Reply #2 on: October 30, 2015, 10:22:45 AM »
Just thought of something, it would be pretty neat if there was a "spoiler" button which would hide the reply if it contained a proposed solution.
lol

You can just say that you finished or post your results and post yours once everyone starts posting.
or
You can just post your code and let others operate on the honor system.

...The thing I HATE is when people edit their posts in challenge threads. The history is also very fun to read (It's fun to see how people think/work).

TheSwamp.org (serving the CAD community since 2003)
Member location map - Add yourself

Donate to TheSwamp.org

Lee Mac

  • Seagull
  • Posts: 12929
  • London, England
Re: (Challenge) Affine substitution cipher
« Reply #3 on: October 30, 2015, 10:30:00 AM »
Here are a few simple ones to start:
Code: [Select]
;; < SPOILER > ;;




















(defun e ( x 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)
        )
    )
    (vl-string-translate
        (vl-list->string m)
        (vl-list->string n)
        (strcase x t)
    )
)
Code: [Select]
;; < SPOILER > ;;





















(defun e ( x a b )
    (vl-list->string
        (mapcar '(lambda ( c ) (if (< 96 c 123) (+ (rem (+ (* (- c 97) a) b) 26) 97) c))
            (vl-string->list (strcase x t))
        )
    )
)
Code: [Select]
;; < SPOILER > ;;




















(defun e ( x a b / c )
    (if (< 0 (setq c (ascii x)))
        (strcat
            (if (< 96 c 123)
                (chr (+ (rem (+ (* (- c 97) a) b) 26) 97))
                (chr c)
            )
            (e (substr x 2) a b)
        )
        ""
    )
)
Code: [Select]
_$ (e "hello" 5 8)
"rclla"

< EDIT: added another >
« Last Edit: October 30, 2015, 10:35:38 AM by Lee Mac »

JohnK

  • Administrator
  • Seagull
  • Posts: 10669
Re: (Challenge) Affine substitution cipher
« Reply #4 on: October 30, 2015, 10:36:44 AM »
Lee,
Code - Auto/Visual Lisp: [Select]
  1. _$ (d "rclla" 5 8)
  2. "hello"
right?

...I mean, I haven't tested your code or anything (I don't have AutoCAD at the moment) but you built a full test (Dx function) right?
TheSwamp.org (serving the CAD community since 2003)
Member location map - Add yourself

Donate to TheSwamp.org

Lee Mac

  • Seagull
  • Posts: 12929
  • London, England
Re: (Challenge) Affine substitution cipher
« Reply #5 on: October 30, 2015, 10:38:23 AM »
Lee,
Code - Auto/Visual Lisp: [Select]
  1. _$ (d "rclla" 5 8)
  2. "hello"
right?

...I mean, I haven't tested your code or anything (I don't have AutoCAD at the moment) but you built a full test (Dx function) right?

Oh I'm not finished yet  :-)

JohnK

  • Administrator
  • Seagull
  • Posts: 10669
Re: (Challenge) Affine substitution cipher
« Reply #6 on: October 30, 2015, 10:43:04 AM »
Ah. :)
TheSwamp.org (serving the CAD community since 2003)
Member location map - Add yourself

Donate to TheSwamp.org

squirreldip

  • Newt
  • Posts: 114
Re: (Challenge) Affine substitution cipher
« Reply #7 on: October 30, 2015, 12:28:24 PM »
Code: [Select]
;; < SPOILER > ;;



































(defun e (x a b / c res)
 (foreach c (vl-string->list (strcase x t))
  (setq res (append res (list (+ (rem (+ (* (- c 97) a) b) 26) 97))))
 )
 (vl-list->string res)
)

Lee Mac

  • Seagull
  • Posts: 12929
  • London, England
Re: (Challenge) Affine substitution cipher
« Reply #8 on: October 30, 2015, 12:52:04 PM »
There & back again for the first example:

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




















(defun e ( x a b )
    ((lambda ( l ) (vl-string-translate (car l) (cadr l) x)) (getmap a b))
)
(defun d ( x a b )
    ((lambda ( l ) (vl-string-translate (cadr l) (car l) x)) (getmap a b))
)
(defun getmap ( 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))
)
Code: [Select]
_$ (d (e "hello" 5 8) 5 8)
"hello"

Lee Mac

  • Seagull
  • Posts: 12929
  • London, England
Re: (Challenge) Affine substitution cipher
« Reply #9 on: October 30, 2015, 01:05:26 PM »
And another, 'abstracted' method:

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



















(defun e ( x a b )
    (applyfootostring (strcase x t)
       '(lambda ( x ) (applyfootochar x (lambda ( x ) (+ b (* a x)))))
    )
)
(defun d ( x a b )
    (setq a (invert a 26))
    (applyfootostring (strcase x t)
       '(lambda ( x ) (applyfootochar x (lambda ( x ) (* a (- x b)))))
    )
)
(defun applyfootostring ( str foo )
    (vl-list->string (mapcar foo (vl-string->list str)))
)
(defun applyfootochar ( cha foo )
    (if (< 96 cha 123)
        (+ 97 (mod26 (foo (- cha 97))))
        cha
    )
)
(defun mod26 ( x )
    (if (< -1 x) (rem x 26) (mod26 (+ x 26)))
)
(defun invert ( a m / foo )
    (defun foo ( a b c d / q )
        (if (< 0 d)
            (progn
                (setq q (/ b d))
                (foo c d (- a (* q c)) (- b (* q d)))
            )
            (if (< a 0)
                (foo (+ a m) b c d)
                a
            )
        )
    )
    (foo 0 m 1 a)
)

JohnK

  • Administrator
  • Seagull
  • Posts: 10669
Re: (Challenge) Affine substitution cipher
« Reply #10 on: October 30, 2015, 02:59:25 PM »
Here's my submission.
Code - C++: [Select]
  1.  // spoiler
  2.  
  3.  
  4.  
  5.  
  6.  
  7.  
  8.  
  9.  
  10.  
  11.  
  12.  
  13.  
  14.  
  15.  
  16.  
  17.  
  18.  
  19.  
  20.  
  21.  
  22.  
  23.  
  24.  
  25.  
  26.  
  27.  
  28.  
  29.  
  30.  
  31.  
  32.  
  33.  
  34.  
  35.  
  36. #include <iostream>
  37. #include <string>
  38. #include <cstring>
  39. #include <stdlib.h>
  40.  
  41. int mod(int dividend, int divisor) {
  42.     return (dividend % divisor + divisor) % divisor;
  43. }
  44.  
  45. char apply_decryption(char c, int a, int b) {
  46.     int temp = c - 'a';
  47.     temp = ((26 - a) * (temp - b ));
  48.     temp = mod(temp, 26);
  49.     temp += 'a';
  50.     return( temp );
  51. }
  52.  
  53. char apply_cipher(char c, int a, int b) {
  54.     c -= 'a';
  55.     char result = ((a * c) + b) % 26;
  56.     result += 'a';
  57.     return(result);
  58. }
  59.  
  60. char* cipher(const char *answer, int a, int b) {
  61.     char* Dx;
  62.     for (unsigned x = 0; x < strlen(answer); ++x){
  63.         if (isalpha(answer[x])){
  64.             Dx[x] = apply_cipher(answer[x], a, b);
  65.         }
  66.     }
  67.     return Dx;
  68. }
  69.  
  70. void decode(std::string answer, int a, int b) {
  71.     std::cout << "\nDecoded: ";
  72.     for (std::string::iterator it = answer.begin(); it != answer.end(); ++it){
  73.             std::cout << apply_decryption(*it, a, b);
  74.     }
  75. }
  76.  
  77. int main(int argc, const char *argv[]) {
  78.     int a;
  79.     int b;
  80.     char* x;
  81.     std::string Dx;
  82.  
  83.     for (int i = 0; i < argc; i++) {
  84.         if (argv[i] && strlen(argv[i]) > 1) {
  85.             if (argv[i][0] == '-' && argv[i][1] == 'a') { a = atoi(argv[++i]); }
  86.             if (argv[i][0] == '-' && argv[i][1] == 'b') { b = atoi(argv[++i]); }
  87.             if (argv[i][0] == '-' && argv[i][1] == 's') { Dx = cipher(argv[++i], a, b); }
  88.         }
  89.     }
  90.  
  91.     std::cout << "Encoded (Affine cipher): " << Dx;
  92.     decode(Dx, a, b);
  93.     std::cout << std::endl;
  94.     return 1;
  95. }
  96.  


In other news, it sounds like we found a "spoiler mod" for the forum (for things like this). I hope to get that installed at some point in the near future.
TheSwamp.org (serving the CAD community since 2003)
Member location map - Add yourself

Donate to TheSwamp.org

Lee Mac

  • Seagull
  • Posts: 12929
  • London, England
Re: (Challenge) Affine substitution cipher
« Reply #11 on: October 30, 2015, 03:02:08 PM »
For the Vigenθre Cipher:

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


















(defun ev ( s k / l )
    (setq s (vl-string->list s)
          k (vl-string->list k)
          l k
    )
    (repeat (/ (length s) (length k)) (setq k (append k l)))
    (vl-list->string (mapcar '(lambda ( a b ) (if (< 96 a 123) (+ 97 (rem (+ a b -194) 26)) a)) s k))
)
(defun dv ( s k / l )
    (setq s (vl-string->list s)
          k (vl-string->list k)
          l k
    )
    (repeat (/ (length s) (length k)) (setq k (append k l)))
    (vl-list->string (mapcar '(lambda ( a b ) (if (< 96 a 123) (+ 97 (rem (- a b -26) 26)) a)) s k))
)
Code: [Select]
_$ (dv (ev "attackatdawn" "lemon") "lemon")
"attackatdawn"

Lee Mac

  • Seagull
  • Posts: 12929
  • London, England
Re: (Challenge) Affine substitution cipher
« Reply #12 on: October 30, 2015, 03:12:32 PM »
Here's my submission.

Nicely coded John, but 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).

In other news, it sounds like we found a "spoiler mod" for the forum (for things like this). I hope to get that installed at some point in the near future.

Nice  :-)

JohnK

  • Administrator
  • Seagull
  • Posts: 10669
Re: (Challenge) Affine substitution cipher
« Reply #13 on: October 30, 2015, 03:26:18 PM »
Here's my submission.

Nicely coded John, but 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).
...

*UGH* I figured you'd catch that. I was trying to use better math(s) but my head almost exploded.
https://en.wikipedia.org/wiki/Modular_multiplicative_inverse
TheSwamp.org (serving the CAD community since 2003)
Member location map - Add yourself

Donate to TheSwamp.org

JohnK

  • Administrator
  • Seagull
  • Posts: 10669
Re: (Challenge) Affine substitution cipher
« Reply #14 on: October 30, 2015, 03:46:13 PM »
FYI: Sounds like Mark will get the spoiler mod installed sometime soon. :)

Also, I actually coded the Vingenere cipher first and I now see at least one variable left in the code I posted from that solution (I should have posted that and then reworked my code for the Affine cipher post but...*meh*).
TheSwamp.org (serving the CAD community since 2003)
Member location map - Add yourself

Donate to TheSwamp.org