-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathelgamal.rkt
52 lines (52 loc) · 1.53 KB
/
elgamal.rkt
1
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
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
#lang racket
(require math)
(define (powmod a b n)
(with-modulus n
(define (pow a b)
(if (= b 1)
a
(* a (pow a (- b 1)))))
(mod (pow a b))))
(define (powmodspecial a b m n)
(with-modulus n
(define (pow a b)
(if (= b 1)
a
(* a (pow a (- b 1)))))
(mod (* m (pow a b)))))
(define (getKey)
(let* ([p (nth-prime (random 10000 20000))]
[g (random 1 (- p 1))]
[x (random 1 (- p 1))]
[y (powmod g x p)])
(list p g y x)))
(define (pubKey key)
(reverse (cdr (reverse key))))
(define (message->int m)
(define (helper acc m)
(if (= (string-length m) 0)
acc
(let* ([lst (string->list m)]
[c (first lst)]
[ord (char->integer c)])
(helper (+ ord (* 128 acc)) (substring m 1)))))
(helper 0 m))
(define (int->message i)
(define (helper acc i)
(if (= i 0)
acc
(let* ([num (quotient i 128)]
[rem (with-modulus 128 (mod i))]
[c (integer->char rem)])
(string-append (string c) acc (int->message num)))))
(helper "" i))
(define (string-reverse s)
(list->string (reverse (string->list s))))
(define (encrypt message key) ;encrypt. key is p g y x
(let* ([k (random 1 1000)]
[a (powmod (second key) k (first key))]
[b (powmodspecial (third key) k message (first key))])
(list a b)))
(define (decrypt pair key) ; decrypt
(let* ([pow (- (first key) (fourth key) 1)])
(powmodspecial (first pair) pow (second pair) (first key))))