-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.go
223 lines (188 loc) · 6.49 KB
/
main.go
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
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
package main
import (
"crypto/rand"
"fmt"
"log"
"math"
"math/big"
"strconv"
"strings"
)
func main() {
plaintext := "Alice and Bob want to send message securely"
fromIndexWord := strings.Index(plaintext, "message")
toIndexWord := fromIndexWord + 7
log.Println("Plain Text : ", plaintext)
log.Println("Index Position : " + strconv.Itoa(fromIndexWord) + " to " + strconv.Itoa(toIndexWord) + "")
log.Println("Index Word : ", plaintext[fromIndexWord:toIndexWord])
log.Println("Index Word Length : ", len(plaintext[fromIndexWord:toIndexWord]))
fmt.Println("-------------------------***-------------------------------")
plainbin := binary(plaintext)
log.Println("Binary :", plainbin)
log.Println("Binary Length :", len(plainbin))
log.Println("Plain Length :", len(plaintext))
log.Println("Plain ASCII :", []byte(plaintext))
fmt.Println("-------------------------***-------------------------------")
keyplain, keybin, err := GenerateRandomString(len(plaintext))
if err != nil {
panic(err)
}
log.Println("Key in plain : ", keyplain)
log.Println("Key in binary : ", keybin)
log.Println("Key in binary length : ", len(keybin))
log.Println("Key in plain length : ", len(keyplain))
log.Println("key in ASCII : ", []byte(keyplain))
log.Println("Index key Word : ", keyplain[fromIndexWord:toIndexWord])
log.Println("Index key Length : ", len(keyplain[fromIndexWord:toIndexWord]))
fmt.Println("-------------------------***-------------------------------")
Titxt := produceTi(keyplain)
TiBin := binary(Titxt)
stringTI := Titxt
log.Println("Ti ASCII : ", []byte(Titxt))
log.Println("Ti in Plain : ", Titxt)
log.Println("Ti length : ", len(Titxt))
log.Println("Ti in binary : ", TiBin)
log.Println("Ti in binary length : ", len(TiBin))
log.Println("Index Ti Word : ", stringTI[fromIndexWord:toIndexWord])
log.Println("Index Ti Length : ", len(stringTI[fromIndexWord:toIndexWord]))
fmt.Println("-------------------------***-------------------------------")
chiper := encryptStreamChiper(TiBin, plainbin)
log.Println("Chiper : ", chiper)
log.Println("Chiper in binary length : ", len(chiper))
chiperdecode := binaryToText(chiper)
log.Println("Chiper ASCII : ", []byte(chiperdecode))
log.Println("Chiper text : ", chiperdecode)
log.Println("Chiper text length: ", len(chiperdecode))
log.Println("Index Chiper Word: ", chiperdecode[fromIndexWord:toIndexWord])
log.Println("Index Chiper Length : ", len(chiperdecode[fromIndexWord:toIndexWord]))
plain := decryptStreamChiper(TiBin, chiper)
log.Println("Plain : ", plain)
log.Println("Plain in binary length : ", len(plain))
plaindecode := binaryToText(plain)
log.Println("Plain ASCII : ", []byte(plaindecode))
log.Println("Plain text : ", plaindecode)
log.Println("Plain text length: ", len(plaindecode))
if plain != plainbin {
panic("failed to decrypt")
}
fmt.Println("-------------------------*** Start Scenario SSE ***-------------------------------")
log.Println("Alice give Bob index word : ", plaintext[fromIndexWord:toIndexWord])
log.Println("Alice give Bob index word position : from " + strconv.Itoa(fromIndexWord) + " to " + strconv.Itoa(toIndexWord) + "")
prooftxt := proof(chiperdecode, plaintext[fromIndexWord:toIndexWord], fromIndexWord, toIndexWord)
log.Println("Bob give Alice proof : ", prooftxt)
log.Println("Alice compare proof : " + prooftxt + " and Ti : " + stringTI[fromIndexWord:toIndexWord] + "")
if prooftxt != stringTI[fromIndexWord:toIndexWord] {
panic("failed to proofing")
} else {
log.Println("Proofing Success!")
}
}
func binary(s string) string {
res := ""
for _, c := range s {
res = fmt.Sprintf("%s%.8b", res, c)
}
return res
}
func GenerateRandomString(n int) (string, string, error) {
const letters = "0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz-"
ret := make([]byte, n)
for i := 0; i < n; i++ {
num, err := rand.Int(rand.Reader, big.NewInt(int64(len(letters))))
if err != nil {
return "", "", err
}
ret[i] = letters[num.Int64()]
}
token := binary(string(ret))
return string(ret), token, nil
}
func encryptStreamChiper(key string, message string) string {
// keyarr := []rune(key)
// messagearr := []rune(message)
keyarr := []byte(key)
messagearr := []byte(message)
chipertxt := ""
for i := 0; i < len(messagearr); i++ {
ciphertmp := messagearr[i] ^ keyarr[i]
// log.Println(string(messagearr[i]))
// log.Println(keyarr[i])
chipertxt = fmt.Sprintf("%s%s", chipertxt, fmt.Sprint(ciphertmp))
}
return chipertxt
}
func produceTi(key string) string {
//keyarr := []rune(key)
keyarr := []byte(key) //convert to ASCII
////Ti := (Si,Fki (Si)) == Fki(Si) = 1^1 mod 5
Ti := make([]byte, len(keyarr))
for i := 0; i < len(keyarr); i++ {
// res := math.Pow(float64(5), float64(keyarr[i])) //pangkat
//res := math.Mod(float64(keyarr[i]), 14) //modulo
res := int(keyarr[i]) + 3
// log.Println(int(keyarr[i]))
// log.Println("*************")
// log.Println(res)
copy(Ti[i:], string(int(res)))
}
return string(Ti)
}
func decryptStreamChiper(key string, chiper string) string {
keyarr := []byte(key)
chiperarr := []byte(chiper)
plaintext := ""
for i := 0; i < len(chiperarr); i++ {
plaintexttmp := chiperarr[i] ^ keyarr[i]
plaintext = fmt.Sprintf("%s%s", plaintext, fmt.Sprint(plaintexttmp))
}
return plaintext
}
func binaryToText(binarystring string) string {
plaintext := ""
n := 8
for n <= len(binarystring) {
lass := n - 8
// log.Println(valuex)
// log.Println(n)
x := binarystring[lass:n]
y, e := strconv.Atoi(x)
if e != nil {
panic(e)
}
//fmt.Println(string(binaryToDecimal(y)))
plaintext = fmt.Sprintf("%s%s", plaintext, string(binaryToDecimal(y)))
n += 8
}
return plaintext
}
func binaryToDecimal(num int) int {
var remainder int
index := 0
decimalNum := 0
for num != 0 {
remainder = num % 10
num = num / 10
decimalNum = decimalNum + remainder*int(math.Pow(2, float64(index)))
index++
}
return decimalNum
}
func proof(chiper string, word string, fromindex int, toindex int) string {
chiperword := chiper[fromindex:toindex]
wordarr := []byte(word)
chiperwordarr := []byte(chiperword)
//plaintext := ""
// fmt.Println(word)
// fmt.Println(chiperword)
prooftxt := make([]byte, len(wordarr))
for i := 0; i < len(wordarr); i++ {
plaintexttmp := chiperwordarr[i] ^ wordarr[i]
// fmt.Println(plaintexttmp)
//plaintext = fmt.Sprintf("%s%s", plaintext, fmt.Sprint(plaintexttmp))
copy(prooftxt[i:], string(int(plaintexttmp)))
}
// fmt.Println(plaintext)
// fmt.Println(prooftxt)
// fmt.Println(string(prooftxt))
return string(prooftxt)
}