Skip to content

Commit 1e503ca

Browse files
committed
minor fixes
1 parent a255920 commit 1e503ca

File tree

2 files changed

+73
-1
lines changed

2 files changed

+73
-1
lines changed

ciphers/RSAcipher.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -63,7 +63,7 @@ func prime(limit int)(primes []int){
6363
return
6464
}
6565
func lcm (a int, b int)int{
66-
//complexity depende
66+
//complexity depends on gcd
6767
return int((a*b)/gcd(a,b))
6868

6969
}

ciphers/diffieHellmanKeyExchange.go

Lines changed: 72 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,72 @@
1+
package main
2+
3+
import (
4+
"fmt"
5+
"math/rand"
6+
)
7+
8+
func main() {
9+
bit := 30
10+
/*
11+
p and g are pre-agreed constants
12+
that can be communicated over an insecure channel
13+
p should ideally be a large prime number but any integer works
14+
g should be a small integer, 2,3 works fine
15+
16+
PS: Note that the secret keys are never send over
17+
the network
18+
*/
19+
20+
p := 2 + rand.Intn(1<<bit)
21+
g := 2 + rand.Intn(5)
22+
23+
//Both parties choose a secret key
24+
25+
AliceSecret := 1 + rand.Intn(1<<bit)
26+
BobSecret := 1 + rand.Intn(1<<bit)
27+
28+
fmt.Printf("Alice's secret key is: %v \n", AliceSecret)
29+
fmt.Printf("Bob's secret key is: %v \n", BobSecret)
30+
31+
//Both parties send ((g^secret_key)%p)
32+
//It's not possible to determine the secretkey from the value sent
33+
34+
AliceSends := modularExponentiation(g, AliceSecret, p)
35+
BobSends := modularExponentiation(g, BobSecret, p)
36+
37+
fmt.Printf("Alice sends to Bob: %v \n", AliceSends)
38+
fmt.Printf("Bob sends to Alice: %v \n", BobSends)
39+
40+
//Both parties calculate the shared secret key from the value send
41+
//(value_sent^secret_key)%p
42+
//Both calculations end up with same value despite the different inputs
43+
AliceComputes := modularExponentiation(BobSends, AliceSecret, p)
44+
BobComputes := modularExponentiation(AliceSends, BobSecret, p)
45+
46+
fmt.Printf("Alice Computes the shared secret key as: %v \n", AliceComputes)
47+
fmt.Printf("Bob Computes the shared secret key as: %v \n", BobComputes)
48+
49+
// simply confirms that the values are equal
50+
if AliceComputes == BobComputes {
51+
sharedKey := AliceComputes
52+
fmt.Println("Tada, shared key is", sharedKey)
53+
}
54+
55+
}
56+
func modularExponentiation(b int, e int, mod int) int {
57+
//runs in O(log(n)) where n = e
58+
//uses exponentiation by squaring to speed up the process
59+
if mod == 1 {
60+
return 0
61+
}
62+
r := 1
63+
b = b % mod
64+
for e > 0 {
65+
if e%2 == 1 {
66+
r = (r * b) % mod
67+
}
68+
e = e >> 1
69+
b = (b * b) % mod
70+
}
71+
return r
72+
}

0 commit comments

Comments
 (0)