Skip to content

Commit b3b7af2

Browse files
authored
Merge pull request #129 from brayo-pip/master
Created Xor cipher
2 parents 9a7707c + 4db3b08 commit b3b7af2

File tree

6 files changed

+313
-21
lines changed

6 files changed

+313
-21
lines changed

ciphers/RSAcipher.go

Lines changed: 174 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,174 @@
1+
package main
2+
3+
import (
4+
//"math/big"
5+
"math/rand"
6+
"math"
7+
"time"
8+
"fmt"
9+
)
10+
func generatePrimes(limit int)int{
11+
/*
12+
generate primes by factoring
13+
relies on the 30k+i, though better formulae exist
14+
where k >=0 and i = (1,7,11,13,17,13,19,23,29)
15+
*/
16+
primes:= prime(limit)
17+
var choice []int
18+
choice = append(choice, 1,7,11,13,17,19,23,29)
19+
for{
20+
k:=rand.Intn(int(limit/30))
21+
i:=choice[rand.Intn(len(choice))]
22+
c:=30*k+i
23+
found := true
24+
for _,v:= range primes{
25+
if c%v==0{
26+
found = false
27+
break
28+
}
29+
}
30+
if found{
31+
return c
32+
}
33+
}
34+
}
35+
func prime(limit int)(primes []int){
36+
sqrtLimit:=int(math.Ceil(math.Sqrt(float64(limit))))
37+
exit:= false
38+
primes = append(primes,2,3,5)
39+
lastIndex :=2
40+
for ;primes[lastIndex]<sqrtLimit;{
41+
if exit == true{
42+
break
43+
}
44+
for i:=primes[lastIndex]+2;i<primes[lastIndex]*primes[lastIndex];i+=2{
45+
found:= true
46+
for _,v:= range primes {
47+
if i%v==0{
48+
found= false
49+
break
50+
}
51+
}
52+
if found{
53+
primes = append(primes,i)
54+
lastIndex++
55+
if i >=sqrtLimit{
56+
exit = true
57+
break
58+
}
59+
}
60+
61+
}
62+
}
63+
return
64+
}
65+
func lcm (a int, b int)int{
66+
//complexity depende
67+
return int((a*b)/gcd(a,b))
68+
69+
}
70+
func gcd (a int, b int) int{
71+
//complexity not clear
72+
for b != 0{
73+
t:=b
74+
b = a % b
75+
a = t
76+
}
77+
return a
78+
}
79+
func modularMultiplicativeInverse(e int, delta int)int{
80+
//runs in O(n) where n = delta
81+
e= e % delta
82+
for i:=1;i<delta;i++{
83+
if (i*e)%delta==1{
84+
return i
85+
}
86+
}
87+
return 0
88+
}
89+
90+
func modularExponentiation(b int, e int, mod int)int{
91+
//runs in O(log(n)) where n = e
92+
if mod == 1{
93+
return 0
94+
}
95+
r:=1
96+
b = b % mod
97+
for ;e>0;{
98+
if e%2==1{
99+
r=(r*b)%mod
100+
}
101+
e =e>>1
102+
b = (b*b)%mod
103+
}
104+
return r
105+
}
106+
107+
func encryptRSA(message []int,e int,n int)[]int{
108+
//runs in O(k*log(n)) where k = len(message) and n = e
109+
var ciphertext []int
110+
for _,v := range message{
111+
ciphertext = append(ciphertext, modularExponentiation(v,e,n))
112+
}
113+
return ciphertext
114+
}
115+
func decryptRSA(ciphertext []int, d int, n int )[]int{
116+
//runs in O(k*log(n)) where k = len(ciphertext) and n = d
117+
var message []int
118+
for _,v := range ciphertext {
119+
message = append(message, modularExponentiation(v,d,n))
120+
}
121+
return message
122+
}
123+
func toASCII(slice []rune)[]int{
124+
//runs in O(n) where n = len(slice)
125+
var converted []int
126+
for _,v:= range slice{
127+
converted = append(converted, int(v))
128+
}
129+
return converted
130+
}
131+
132+
func toRune(slice []int)string{
133+
//runs in O(n) where n = len(slice)
134+
var str string
135+
for _,v:= range slice{
136+
str += string(v)
137+
}
138+
return str
139+
}
140+
141+
142+
func main(){
143+
rand.Seed(time.Now().UTC().UnixNano())
144+
bits:=17
145+
146+
p:= generatePrimes(1<<bits)
147+
q:= generatePrimes(1<<bits)
148+
for p==q{
149+
q = generatePrimes(1<<bits)
150+
}
151+
152+
n:= p*q
153+
154+
delta:=lcm(p-1,q-1)
155+
156+
e:=generatePrimes(delta)
157+
d:=modularMultiplicativeInverse(e,delta)
158+
159+
fmt.Printf("%v \n%v \n%v \n%v\n",p,q,e,d)
160+
161+
162+
str:="I think RSA is really great"
163+
message := []rune(str)
164+
asciiSlice :=toASCII(message)
165+
166+
fmt.Printf("asciiSlice : %v \n",asciiSlice)
167+
encrypted := encryptRSA(asciiSlice,e,n)
168+
fmt.Printf("encrypted : %v \n",encrypted)
169+
decrypted := decryptRSA(encrypted,d,n)
170+
fmt.Printf("decrypted : %v \n",decrypted)
171+
fmt.Printf("cleartext : %v \n",toRune(decrypted))
172+
//switched to atom
173+
174+
}

ciphers/diffieHellmanKeyExchange.go

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

ciphers/xorCipher.go

Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
package main
2+
3+
import "fmt"
4+
func encrypt(key int, plaintext []int)(ciphertext []int){
5+
for _,char:= range plaintext {
6+
ciphertext = append(ciphertext,xor(char,key))
7+
}
8+
return
9+
}
10+
func xor(char int,key int)int{
11+
return (char^key)
12+
}
13+
14+
func decrypt(key int, ciphertext []int)(plaintext []int){
15+
for _,char:= range ciphertext {
16+
plaintext = append(plaintext , xor(char,key))
17+
}
18+
return
19+
}
20+
21+
func decodeToString(slice []int)(str string){
22+
for _,v:= range slice{
23+
str+=string(v)
24+
}
25+
return
26+
}
27+
func toASCII(slice []rune)[]int{
28+
var converted []int
29+
for _,v:= range slice{
30+
converted = append(converted, int(v))
31+
}
32+
return converted
33+
}
34+
func main(){
35+
str := "hello world"
36+
key :=97
37+
temp:= []rune(str)
38+
message:=toASCII(temp)
39+
encrypted:=encrypt(key,message)
40+
decrypted:=decrypt(key,encrypted)
41+
plaintext:=decodeToString(decrypted)
42+
fmt.Println(plaintext)
43+
}
44+

sorts/heap_sort.go

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,20 +1,20 @@
11
//Package sorts a package for demonstrating sorting algorithms in Go
22
package sorts
33

4-
type MaxHeap struct {
4+
type maxHeap struct {
55
slice []int
66
heapSize int
77
}
88

9-
func BuildMaxHeap(slice []int) MaxHeap {
10-
h := MaxHeap{slice: slice, heapSize: len(slice)}
9+
func buildMaxHeap(slice []int) maxHeap {
10+
h := maxHeap{slice: slice, heapSize: len(slice)}
1111
for i := len(slice) / 2; i >= 0; i-- {
1212
h.MaxHeapify(i)
1313
}
1414
return h
1515
}
1616

17-
func (h MaxHeap) MaxHeapify(i int) {
17+
func (h maxHeap) MaxHeapify(i int) {
1818
l, r := 2*i+1, 2*i+2
1919
max := i
2020

@@ -31,10 +31,10 @@ func (h MaxHeap) MaxHeapify(i int) {
3131
}
3232
}
3333

34-
func (h MaxHeap) size() int { return h.heapSize } // ???
34+
func (h maxHeap) size() int { return h.heapSize } // ???
3535

3636
func heapSort(slice []int) []int {
37-
h := BuildMaxHeap(slice)
37+
h := buildMaxHeap(slice)
3838
//log.Println(slice)
3939
for i := len(h.slice) - 1; i >= 1; i-- {
4040
h.slice[0], h.slice[i] = h.slice[i], h.slice[0]

sorts/quick_sort.go

Lines changed: 11 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -13,26 +13,26 @@ func quickSort(arr []int) []int {
1313

1414
median := arr[rand.Intn(len(arr))]
1515

16-
low_part := make([]int, 0, len(arr))
17-
high_part := make([]int, 0, len(arr))
18-
middle_part := make([]int, 0, len(arr))
16+
lowPart := make([]int, 0, len(arr))
17+
highPart := make([]int, 0, len(arr))
18+
middlePart := make([]int, 0, len(arr))
1919

2020
for _, item := range arr {
2121
switch {
2222
case item < median:
23-
low_part = append(low_part, item)
23+
lowPart = append(lowPart, item)
2424
case item == median:
25-
middle_part = append(middle_part, item)
25+
middlePart = append(middlePart, item)
2626
case item > median:
27-
high_part = append(high_part, item)
27+
highPart = append(highPart, item)
2828
}
2929
}
3030

31-
low_part = quickSort(low_part)
32-
high_part = quickSort(high_part)
31+
lowPart = quickSort(lowPart)
32+
highPart = quickSort(highPart)
3333

34-
low_part = append(low_part, middle_part...)
35-
low_part = append(low_part, high_part...)
34+
lowPart = append(lowPart, middlePart...)
35+
lowPart = append(lowPart, highPart...)
3636

37-
return low_part
37+
return lowPart
3838
}

strings/naiveStringSearch.go

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -16,14 +16,14 @@ import (
1616

1717
func naivePatternSearch(text string, pattern string) []int {
1818
var positions []int
19-
for i := 0; i < len(text); i++ {
19+
for i := 0; i < len(text)-len(pattern); i++ {
2020
var match bool = true
21-
for j := 0; j < len(pattern); j++ {
22-
if text[i+j] != pattern[j] {
21+
j := i +(len(pattern))
22+
if text[i+j] != pattern[j] {
2323
match = false
2424
break
2525
}
26-
}
26+
2727
if match {
2828
positions = append(positions, i)
2929
}

0 commit comments

Comments
 (0)