/
main.go
442 lines (360 loc) · 11.6 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
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
package main
import (
"bytes"
"crypto/aes"
"crypto/cipher"
"database/sql"
"fmt"
_ "github.com/go-sql-driver/mysql"
//"github.com/opensds/multi-cloud/s3/pkg/utils"
"io"
"io/ioutil"
)
type Tag struct {
key []byte
iv []byte
}
/*func main() {
// Open up our database connection.
// I've set up a database on my local machine using phpmyadmin.
// The database is called testDb
db, err := sql.Open("mysql", "root:@tcp(127.0.0.1:4000)/s3")
// if there is an error opening the connection, handle it
if err != nil {
panic(err.Error())
}
// Execute the query
results, err := db.Query("SELECT sseserverkey, sseiv FROM bucket_sseopts")
if err != nil {
panic(err.Error()) // proper error handling instead of panic in your app
}
var tag Tag
for results.Next() {
// for each row, scan the result into our tag composite object
err = results.Scan(&tag.key, &tag.iv)
if err != nil {
panic(err.Error()) // proper error handling instead of panic in your app
}
// and then print out the tag's Name attribute
fmt.Println(tag)
}
// defer the close till after the main function has finished
// executing
defer db.Close()
// encrypt
// read file to byte[]
allBytes, readErr := ioutil.ReadFile("/home/prakash/Downloads/tigervnc64-1.8.0.exe")
fmt.Println(readErr)
allEncBytes := make([]byte, 0)
thouBytes := make([]byte, 0)
for i := 0; i < 5242880; i++ {
thouBytes = append(thouBytes, allBytes[i])
}
fmt.Println("\t\t thoubytes.len",len(thouBytes))
// encrypt in chunks of 1000, write file to disk
var out bytes.Buffer
block, err := aes.NewCipher(tag.key)
if err != nil {
panic(err)
}
stream := cipher.NewCTR(block, tag.iv)
writer := &cipher.StreamWriter{S: stream, W: &out}
// Copy the input to the output buffer, encrypting as we go.
if _, err := io.Copy(writer, bytes.NewReader(thouBytes)); err != nil {
panic(err)
}
allEncBytes = append(allEncBytes, out.Bytes()...)
// block2
thouBytes = make([]byte, 0)
for i := 5242880; i < 5242880*2; i++ {
thouBytes = append(thouBytes, allBytes[i])
}
fmt.Println("\t\t thoubytes.len",len(thouBytes))
// encrypt in chunks of 1000, write file to disk
var out1 bytes.Buffer
if err != nil {
panic(err)
}
stream1 := cipher.NewCTR(block, tag.iv)
writer1 := &cipher.StreamWriter{S: stream1, W: &out1}
// Copy the input to the output buffer, encrypting as we go.
if _, err := io.Copy(writer1, bytes.NewReader(thouBytes)); err != nil {
panic(err)
}
allEncBytes = append(allEncBytes, out1.Bytes()...)
// block 3
thouBytes = make([]byte, 0)
for i := 5242880*2; i < (5242880*2)+791560; i++ {
thouBytes = append(thouBytes, allBytes[i])
}
fmt.Println("\t\t thoubytes.len",len(thouBytes))
// encrypt in chunks of 1000, write file to disk
var out2 bytes.Buffer
if err != nil {
panic(err)
}
stream2 := cipher.NewCTR(block, tag.iv)
writer2 := &cipher.StreamWriter{S: stream2, W: &out2}
// Copy the input to the output buffer, encrypting as we go.
if _, err := io.Copy(writer2, bytes.NewReader(thouBytes)); err != nil {
panic(err)
}
allEncBytes = append(allEncBytes, out2.Bytes()...)
// write file to disk
writeErr := ioutil.WriteFile("/home/prakash/Downloads/tigervnc64-1.8.0mainenc.exe", allEncBytes, 0777)
if writeErr != nil {
panic(writeErr)
}
// decrypt
fullBytes1, _ := ioutil.ReadFile("/home/prakash/Downloads/tigervnc64-1.8.0mainenc.exe")
fmt.Println("\t\t fullBytes1.len", len(fullBytes1))
fullBytes := make([]byte, 0)
//block, err := aes.NewCipher(tag.key)
if err != nil {
panic(err)
}
//bcountmax := 11277320
bchunk1 := 5242880
bchunk2 := 5242880
bchunk3 := 791560
newBuf := make([]byte, 0)
bindex := 0
for bindex < bchunk1 {
newBuf = append(newBuf, fullBytes1[bindex])
bindex = bindex + 1
}
fmt.Println("\t\t bindex = ", bindex)
fmt.Println("\t\t newBuf.len", len(newBuf))
finalReader := &cipher.StreamReader{S: stream, R: bytes.NewReader(newBuf)}
// Copy the input to the output stream, decrypting as we go.
// now, read out the chunks, decrypting as we read
nonEncBuf, _ := ioutil.ReadAll(finalReader)
fullBytes = append(fullBytes, nonEncBuf...)
fmt.Println("\t\t fullBytes.len", len(fullBytes))
fmt.Println(bytes.Equal(newBuf, nonEncBuf))
newBuf1 := make([]byte, 0)
for bindex < (bchunk1 + bchunk2) {
newBuf1 = append(newBuf1, fullBytes1[bindex])
bindex = bindex + 1
}
fmt.Println("\t\t bindex = ", bindex)
fmt.Println("\t\t newBuf1.len", len(newBuf1))
finalReader1 := &cipher.StreamReader{S: stream, R: bytes.NewReader(newBuf1)}
// Copy the input to the output stream, decrypting as we go.
// now, read out the chunks, decrypting as we read
nonEncBuf1, _ := ioutil.ReadAll(finalReader1)
fullBytes = append(fullBytes, nonEncBuf1...)
fmt.Println("\t\t fullBytes.len", len(fullBytes))
fmt.Println(bytes.Equal(newBuf1, nonEncBuf1))
newBuf2 := make([]byte, 0)
for bindex < (bchunk1 + bchunk2 + bchunk3) {
newBuf2 = append(newBuf2, fullBytes1[bindex])
bindex = bindex + 1
}
fmt.Println("\t\t bindex = ", bindex)
fmt.Println("\t\t newBuf2.len", len(newBuf2))
finalReader2 := &cipher.StreamReader{S: stream, R: bytes.NewReader(newBuf2)}
// Copy the input to the output stream, decrypting as we go.
// now, read out the chunks, decrypting as we read
nonEncBuf2, _ := ioutil.ReadAll(finalReader2)
fullBytes = append(fullBytes, nonEncBuf2...)
fmt.Println("\t\t fullBytes.len", len(fullBytes))
fmt.Println(bytes.Equal(newBuf2, nonEncBuf2))
//newBuf = make([]byte, 791560)
ioutil.WriteFile("/home/prakash/Downloads/tigervnc64-1.8.0fromcloudramain.exe", fullBytes, 0777)
}*/
func main() {
// Open up our database connection.
// I've set up a database on my local machine using phpmyadmin.
// The database is called testDb
db, err := sql.Open("mysql", "root:@tcp(127.0.0.1:4000)/s3")
// if there is an error opening the connection, handle it
if err != nil {
panic(err.Error())
}
// Execute the query
results, err := db.Query("SELECT sseserverkey, sseiv FROM bucket_sseopts")
if err != nil {
panic(err.Error()) // proper error handling instead of panic in your app
}
var tag Tag
for results.Next() {
// for each row, scan the result into our tag composite object
err = results.Scan(&tag.key, &tag.iv)
if err != nil {
panic(err.Error()) // proper error handling instead of panic in your app
}
// and then print out the tag's Name attribute
fmt.Println(tag)
}
// defer the close till after the main function has finished
// executing
defer db.Close()
//keyArr, keyErr := utils.GetRandomNBitKey(32)
//if keyErr != nil {
// panic(keyErr)
//}
//ivArr, ivErr := utils.GetRandomNBitKey(16)
//if ivErr != nil {
// panic(ivErr)
//}
keyArr := tag.key
ivArr := tag.iv
// read file to byte[]
allBytes, readErr := ioutil.ReadFile("/home/prakash/Downloads/tigervnc64-1.8.0.exe")
fmt.Println(readErr)
allEncBytes := make([]byte, 0)
allBytesLen := len(allBytes)
modVal := allBytesLen / 5242880
for i := 0; i < modVal; i++ {
thouBytes := make([]byte, 0)
for j := (i) * 5242880; j < (i+1)*5242880; j++ {
thouBytes = append(thouBytes, allBytes[j])
}
// encrypt in chunks of 1000, write file to disk
var out bytes.Buffer
block, err := aes.NewCipher(keyArr)
if err != nil {
panic(err)
}
stream := cipher.NewCTR(block, ivArr)
writer := &cipher.StreamWriter{S: stream, W: &out}
// Copy the input to the output buffer, encrypting as we go.
if _, err := io.Copy(writer, bytes.NewReader(thouBytes)); err != nil {
panic(err)
}
allEncBytes = append(allEncBytes, out.Bytes()...)
}
{
thouBytes := make([]byte, 0)
for j := (modVal) * 5242880; j < allBytesLen; j++ {
thouBytes = append(thouBytes, allBytes[j])
}
// encrypt in chunks of 1000, write file to disk
var out bytes.Buffer
block, err := aes.NewCipher(keyArr)
if err != nil {
panic(err)
}
stream := cipher.NewCTR(block, ivArr)
writer := &cipher.StreamWriter{S: stream, W: &out}
// Copy the input to the output buffer, encrypting as we go.
if _, err := io.Copy(writer, bytes.NewReader(thouBytes)); err != nil {
panic(err)
}
allEncBytes = append(allEncBytes, out.Bytes()...)
}
// write file to disk
writeErr := ioutil.WriteFile("/home/prakash/Downloads/tigervnc64-1.8.0enc.exe", allEncBytes, 0777)
if writeErr != nil {
panic(writeErr)
}
// read encrypted file, decrypt and write back to disk
// read file to byte[]
readEncBytes, readErr := ioutil.ReadFile("/home/prakash/Downloads/tigervnc64-1.8.0fc.exe")
fmt.Println(readErr)
// decrypt, write file to disk
allDecBytes := make([]byte, 0)
//for i:=0;i<modVal;i++{
//thouBytes := make([]byte, 0)
//for j:=(i)*1000000;j<(i+1)*1000000;j++{
// thouBytes = append(thouBytes, allBytes[j])
//}
for i := 0; i < modVal; i++ {
decThouBytes := make([]byte, 0)
for j := (i) * 5242880; j < (i+1)*5242880; j++ {
decThouBytes = append(decThouBytes, readEncBytes[j])
}
// encrypt in chunks of 1000, write file to disk
var out bytes.Buffer
block, err := aes.NewCipher(keyArr)
if err != nil {
panic(err)
}
stream := cipher.NewCTR(block, ivArr)
writer := &cipher.StreamWriter{S: stream, W: &out}
// Copy the input to the output buffer, encrypting as we go.
if _, err := io.Copy(writer, bytes.NewReader(decThouBytes)); err != nil {
panic(err)
}
allDecBytes = append(allDecBytes, out.Bytes()...)
}
{
decThouBytes := make([]byte, 0)
for j := (modVal) * 5242880; j < allBytesLen; j++ {
decThouBytes = append(decThouBytes, readEncBytes[j])
}
// encrypt in chunks of 1000, write file to disk
var out bytes.Buffer
block, err := aes.NewCipher(keyArr)
if err != nil {
panic(err)
}
stream := cipher.NewCTR(block, ivArr)
writer := &cipher.StreamWriter{S: stream, W: &out}
// Copy the input to the output buffer, encrypting as we go.
if _, err := io.Copy(writer, bytes.NewReader(decThouBytes)); err != nil {
panic(err)
}
allDecBytes = append(allDecBytes, out.Bytes()...)
}
// write file to disk
writeErr = ioutil.WriteFile("/home/prakash/Downloads/tigervnc64-1.8.0dec.exe", allDecBytes, 0777)
if writeErr != nil {
panic(writeErr)
}
fmt.Println("\n\n\t\t", bytes.Equal(allBytes, allDecBytes))
}
/*func main() {
keyArr, keyErr := utils.GetRandomNBitKey(32)
if keyErr != nil {
panic(keyErr)
}
ivArr, ivErr := utils.GetRandomNBitKey(16)
if ivErr != nil {
panic(ivErr)
}
// read file to byte[]
allBytes, readErr := ioutil.ReadFile("/home/prakash/Downloads/nppinstaller.exe")
fmt.Println(readErr)
// encrypt, write file to disk
var out bytes.Buffer
block, err := aes.NewCipher(keyArr)
if err != nil {
panic(err)
}
stream := cipher.NewOFB(block, ivArr)
writer := &cipher.StreamWriter{S: stream, W: &out}
// Copy the input to the output buffer, encrypting as we go.
// Copy the input to the output buffer, encrypting as we go.
if _, err := io.Copy(writer, bytes.NewReader(allBytes)); err != nil {
panic(err)
}
// write file to disk
writeErr := ioutil.WriteFile("/home/prakash/Downloads/nppinstallerenc.exe", out.Bytes(), 0777)
if writeErr != nil {
panic(writeErr)
}
// read encrypted file, decrypt and write back to disk
// read file to byte[]
allEncBytes, readErr := ioutil.ReadFile("/home/prakash/Downloads/nppinstallerenc.exe")
fmt.Println(readErr)
// decrypt, write file to disk
block, err = aes.NewCipher(keyArr)
if err != nil {
panic(err)
}
stream = cipher.NewOFB(block, ivArr)
reader := &cipher.StreamReader{S: stream, R: bytes.NewReader(allEncBytes)}
// Copy the input to the output buffer, decrypting as we go.
decBytes, decErr := ioutil.ReadAll(reader)
if decErr != nil {
panic(decErr)
}
// write file to disk
writeErr = ioutil.WriteFile("/home/prakash/Downloads/nppinstallerdec.exe", decBytes, 0777)
if writeErr != nil {
panic(writeErr)
}
fmt.Println("\n\n\t\t", bytes.Equal(allBytes, decBytes))
}*/