Skip to content

Commit c8d63ef

Browse files
committed
minor edits
2 parents bdc2f27 + 8422b48 commit c8d63ef

File tree

2 files changed

+51
-14
lines changed

2 files changed

+51
-14
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: 50 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -4,19 +4,21 @@ import (
44
"fmt"
55
"math/rand"
66
)
7-
func main(){
8-
bit:=30
7+
8+
func main() {
9+
bit := 30
910
/*
10-
p and g are pre-agreed constants
11-
that can be communicated over an insecure channel
12-
p should ideally be a large prime number but any integer works
13-
g should be a small integer, 2,3 works fine
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
1415
15-
PS: Note that the secret keys are never send over the network
16+
PS: Note that the secret keys are never send over
17+
the network
1618
*/
1719

18-
p:=2+rand.Intn(1<<bit)
19-
g:=2+rand.Intn(5)
20+
p := 2 + rand.Intn(1<<bit)
21+
g := 2 + rand.Intn(5)
2022

2123
//Both parties choose a secret key
2224

@@ -29,14 +31,48 @@ func main(){
2931
//Both parties send ((g^secret_key)%p)
3032
//It's not possible to determine the secretkey from the value sent
3133

32-
AliceSends :=modularExponentiation(g,AliceSecret,p)
33-
BobSends :=modularExponentiation(g,BobSecret,p)
34+
AliceSends := modularExponentiation(g, AliceSecret, p)
35+
BobSends := modularExponentiation(g, BobSecret, p)
3436

35-
fmt.Printf("Alice sends to Bob: %v\n",AliceSends)
36-
fmt.Printf("Bob sends to Alice: %v\n",BobSends)
37+
fmt.Printf("Alice sends to Bob: %v \n", AliceSends)
38+
fmt.Printf("Bob sends to Alice: %v \n", BobSends)
3739

3840
//Both parties calculate the shared secret key from the value send
41+
<<<<<<< HEAD
42+
43+
=======
44+
//(value_sent^secret_key)%p
45+
//Both calculations end up with same value despite the different inputs
46+
<<<<<<< HEAD
47+
AliceComputes := modularExponentiation(BobSends, AliceSecret, p)
48+
BobComputes := modularExponentiation(AliceSends, BobSecret, p)
3949

50+
fmt.Printf("Alice Computes the shared secret key as: %v \n", AliceComputes)
51+
fmt.Printf("Bob Computes the shared secret key as: %v \n", BobComputes)
52+
53+
// simply confirms that the values are equal
54+
if AliceComputes == BobComputes {
55+
sharedKey := AliceComputes
56+
fmt.Println("Tada, shared key is", sharedKey)
57+
}
58+
59+
}
60+
func modularExponentiation(b int, e int, mod int) int {
61+
//runs in O(log(n)) where n = e
62+
//uses exponentiation by squaring to speed up the process
63+
if mod == 1 {
64+
return 0
65+
}
66+
r := 1
67+
b = b % mod
68+
for e > 0 {
69+
if e%2 == 1 {
70+
r = (r * b) % mod
71+
}
72+
e = e >> 1
73+
b = (b * b) % mod
74+
=======
75+
>>>>>>> 8422b48c4e335339d2173d32895e907f84510d05
4076
AliceComputes :=modularExponentiation(BobSends,AliceSecret,p)
4177
BobComputes := modularExponentiation(AliceSends,BobSecret,p)
4278

@@ -64,6 +100,7 @@ func modularExponentiation(b int, e int, mod int)int{
64100
}
65101
e =e>>1
66102
b = (b*b)%mod
103+
>>>>>>> 8e01f3d134dcddea9e8ab08b08e45ae06a476ede
67104
}
68105
return r
69106
}

0 commit comments

Comments
 (0)