commit bb241729642c3b0ab9592e223713602bd386f169 Author: Toerd@archlinux Date: Sat Jun 27 02:06:41 2020 +0200 feat: DH-Example in Elisp diff --git a/diffie-hellman.el b/diffie-hellman.el new file mode 100644 index 0000000..2046e4c --- /dev/null +++ b/diffie-hellman.el @@ -0,0 +1,154 @@ +;; author: phga +;; date: 2020-06-27 +;; description: I was bored while preparing for exams so I procrastinated +;; and implemented a shitty DH-Algorithm-Example in Elisp which cannot handle +;; large numbers because I don't know anything about Elisp-Integers + +;; Notes: +;; (with-current-buffer buffer-or-name) +;; (defvar buffer-local-var nil "DOC") +;; (set (make-local-variable 'buffer-local-var) value) +;; (insert "FOO" "BAR") +(defun phga/dh-example (&optional n) + "Prints out a Diffie-Hellman Example. + N is the Cap for the initial random number used to get the primitive root + for the Galois Field" + (interactive) + ;; Random prime number for Galois Field (Finite Field) + (let* ( + (buf-a "*Alice*") + (buf-b "*Bob*") + (buf-p "*Public*") + (n (or n 200)) + ;; get p + (p (phga/dh-get-generator-p (phga/dh-get-random-prime-number n))) + ;; get primitive root in p + (g (phga/dh-get-generator-elem p)) + ;; generate secret in in GF(p) for Alice + (a (+ 1 (random (- p 2)))) + ;; generate secret in in GF(p) for Bob + (b (+ 1 (random (- p 2)))) + ;; generate public alpha for Alice (gets transmitted to Bob) + (aa (mod (expt g a) p)) + ;; generate public beta for Bob (gets transmitted to Alice) + (bb (mod (expt g b) p)) + ;; generate secret key for further symmetric encrytion for Alice and Bob + (ka (mod (expt bb a) p)) + (kb (mod (expt aa b) p)) + ) + ;; Alice + (when (get-buffer buf-a) (kill-buffer buf-a)) + (generate-new-buffer buf-a) + (with-current-buffer buf-a + (progn + (insert (phga/dh-pretty-name "Alice")) + (insert (concat "P for GF(P) = " (number-to-string p) "\n")) + (insert (concat "G = " (number-to-string g) "\n")) + (insert (concat "A = " (number-to-string a) " (only known by Alice!)\n")) + (insert (concat "Alpha = " (number-to-string aa) "\n")) + (insert (concat "Beta = " (number-to-string bb) " (sent by Bob to Alice)\n")) + (insert (concat "Secret Key = " (number-to-string ka) " (Beta^A mod P == (G^B)^A mod P\n")) + )) + ;; Bob + (when (get-buffer buf-b) (kill-buffer buf-b)) + (generate-new-buffer buf-b) + (with-current-buffer buf-b + (progn + (insert (phga/dh-pretty-name "Bob")) + (insert (concat "P for GF(P) = " (number-to-string p) "\n")) + (insert (concat "G = " (number-to-string g) "\n")) + (insert (concat "B = " (number-to-string b) " (only known by Bob!)\n")) + (insert (concat "Beta = " (number-to-string bb) "\n")) + (insert (concat "Alpha = " (number-to-string aa) " (sent by Alice to Bob)\n")) + (insert (concat "Secret Key = " (number-to-string kb) " (Alpha^B mod P == (G^A)^B mod P\n")) + )) + ;; (kill-buffer "*Public*") + (when (get-buffer buf-p) (kill-buffer buf-p)) + (generate-new-buffer buf-p) + (with-current-buffer buf-p + (progn + (insert "XxXxXxXxXxXxXxXxXxXxXxXxXxXxXxXxXxXxXxXxXxXxXxXxXxXxXx\n") + (insert " This is a public transport buffer!\n") + (insert " Mean Hackerbois can wait here\n and sniff the communication!!!\n") + (insert "XxXxXxXxXxXxXxXxXxXxXxXxXxXxXxXxXxXxXxXxXxXxXxXxXxXxXx\n\n") + (insert (concat "P for GF(P) = " (number-to-string p) "\n")) + (insert (concat "G = " (number-to-string g) "\n")) + (insert (concat "Alpha = " (number-to-string aa) "\n")) + (insert (concat "Beta = " (number-to-string bb) "\n")) + )) + + ;; Create Window setup lel that's soooo bad I am SORRY + (delete-other-windows) + (command-execute 'split-window-horizontally) + (command-execute 'split-window-horizontally) + (command-execute 'balance-windows) + + (set-window-buffer (nth 0 (window-list)) buf-a) + (set-window-buffer (nth 1 (window-list)) buf-p) + (set-window-buffer (nth 2 (window-list)) buf-b) + ) + ) + +(defun phga/dh-get-random-prime-number (n) + "Get a random prime number up to n (inclusive)" + (interactive) + (let ( + (q (random n)) + ) + ;; p <= 1 -> not a prime number + (if (and (> q 1) + (phga/dh-is-prime q)) + q + (phga/dh-get-random-prime-number n)) + ) + ) + +;; count from 2 to sqrt(p) +(defun phga/dh-is-prime (p &optional c) + "Prime Number ye or ne?" + (interactive) + ;; default value for c (starting point for recursion) + (let ((c (or c 2))) + ;; stops in case of c <= sqrt(p) && p % c == 0 || c > sqrt(p) + ;; first yields nil second yields t + (or (> c (sqrt p)) + ;; p % c != 0 => is prime (for this recursion) + (and (/= (mod p c) 0) ;; hopefully t every recursion so we get a prime + (phga/dh-is-prime p (+ c 1))) + ))) + +(defun phga/dh-get-generator-p (q) + (interactive) + ;; p = q*r + 1 -> test for prime + (let ( + (p (+ 1 (* q (+ 1 (random 41)))))) + (if (phga/dh-is-prime p) + p + (phga/dh-get-generator-p q)) + )) + +(defun phga/dh-get-generator-elem (p) + (interactive) + "Get the actual primitive root for GF(p)" + (+ 1 (random (- p 2)))) + +(defun phga/dh-pretty-name (name) +"Returns the name in a pretty format" +(format "****************************\n Hi my Name is %s!\n****************************\n\n" name)) + +;; Tests: +;; (phga/dh-get-generator-p (phga/dh-get-random-prime-number 20)) +;; (and nil t) +;; (setq phga/p (phga/dh-get-generator-p (phga/dh-get-random-prime-number 20))) +;; (setq phga/g (phga/dh-get-generator-elem phga/p)) +;; (setq phga/a (+ 1 (random (- phga/p 2)))) +;; (setq phga/b (+ 1 (random (- phga/p 2)))) +;; (setq phga/aa (mod (expt phga/g phga/a) phga/p)) +;; (setq phga/bb (mod (expt phga/g phga/b) phga/p)) +;; (setq phga/ka (mod (expt phga/bb phga/a) phga/p)) +;; (setq phga/kb (mod (expt phga/aa phga/b) phga/p)) +;; ;; Tested if short-circuit and / ors +;; (or nil (and t (message "Hi"))) + +;; (phga/dh-get-random-prime-number 10) +;; (phga/dh-is-prime 0) \ No newline at end of file