Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commit 390170a

Browse files
Encryption is done using concurrent goroutines.
1 parent 994143d commit 390170a

File tree

3 files changed

+107
-21
lines changed

3 files changed

+107
-21
lines changed

‎FileEncryptionSoftware.exe

2.04 MB
Binary file not shown.

‎S_DES/DESEncryptor.go

Lines changed: 107 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,13 @@
11
package S_DES
22

33
import (
4+
"encoding/json"
45
"io"
6+
"io/ioutil"
57
"log"
68
"os"
9+
"sync"
10+
"time"
711
)
812

913
const (
@@ -16,6 +20,9 @@ type DesEncryptor struct {
1620
cipher DES_8encryption
1721
decryptionFileConnector *os.File
1822
decryptionFilename string
23+
Fastmode bool `json:"fastmode"`
24+
ConcurrentPaths int `json:"threadcount"`
25+
Chunksize int `json:"buffersize"`
1926
}
2027

2128
func (encryptor *DesEncryptor) getBinaryByteArray(byteVal byte) []byte {
@@ -114,6 +121,63 @@ func (engine *DesEncryptor) writeDecryptionBufferToFile(decryptionBuffer *[][]by
114121
}
115122
}
116123

124+
func (encryptor *DesEncryptor) concurrentChunkEncryption(waitGroup *sync.WaitGroup, buffer *[]byte, bufferDataSize int, encryptionBuffer *[][]byte) {
125+
defer waitGroup.Done()
126+
encryptor.encryptChunk(buffer, bufferDataSize, encryptionBuffer)
127+
}
128+
129+
/**
130+
Uses goroutines to encrypt the file in chunks
131+
*/
132+
func (encryptor *DesEncryptor) fastEncrypt(file *os.File) {
133+
// Concurrent encryption of multiple chunks
134+
dataBuffer := make([][]byte, encryptor.ConcurrentPaths) // holds the primary data to be encypted.
135+
for i := 0; i < len(dataBuffer); i++ {
136+
dataBuffer[i] = make([]byte, encryptor.Chunksize)
137+
}
138+
// var encryptionBuffer [][][]byte
139+
encryptionBuffer := make([][][]byte, encryptor.ConcurrentPaths)
140+
for i := 0; i < len(encryptionBuffer); i++ {
141+
encryptionBuffer[i] = make([][]byte, encryptor.Chunksize)
142+
for j := 0; j < len(encryptionBuffer[i][j]); j++ {
143+
encryptionBuffer[i][j] = make([]byte, 8)
144+
}
145+
}
146+
147+
waitGroup := new(sync.WaitGroup)
148+
chunkMap := make([]int, len(dataBuffer)) // stores the count of bytes w.r.t every chunk.
149+
var err error
150+
cnt := 0
151+
for err != io.EOF {
152+
err = nil
153+
bytesread := 0
154+
chunkCount := 0
155+
for i := 0; i < len(dataBuffer); i++ {
156+
bytesread, err = file.Read(dataBuffer[i])
157+
if err != nil {
158+
if err != io.EOF {
159+
log.Println("Problem reading from primary file", err)
160+
}
161+
break
162+
}
163+
waitGroup.Add(1)
164+
go encryptor.concurrentChunkEncryption(waitGroup, &dataBuffer[i], bytesread, &encryptionBuffer[i])
165+
chunkCount += 1
166+
chunkMap[i] = bytesread
167+
}
168+
waitGroup.Wait()
169+
// if chunkCount == len(dataBuffer) {
170+
// fmt.Println("chunkCount and len(dataBuffer) are same")
171+
// }
172+
// Write encryption buffer into the file
173+
for i := 0; i < chunkCount; i++ {
174+
encryptor.writeEncryptionBufferToFile(&encryptionBuffer[i], chunkMap[i], encryptor.encryptionFilename)
175+
}
176+
cnt += 1
177+
// fmt.Println("End of a loop, cnt:", cnt)
178+
}
179+
}
180+
117181
/**
118182
Reads in a chunk of byte-data from file.
119183
Encrypts it.
@@ -136,27 +200,37 @@ func (encryptor *DesEncryptor) runEncryption(filename string) {
136200
}
137201

138202
var buffer []byte
139-
buffer = make([]byte, bufferSize)
203+
buffer = make([]byte, encryptor.Chunksize)
140204
var encryptionBuffer [][]byte
141-
encryptionBuffer = make([][]byte, bufferSize)
142-
for i := 0; i < bufferSize; i++ {
205+
encryptionBuffer = make([][]byte, encryptor.Chunksize)
206+
for i := 0; i < encryptor.Chunksize; i++ {
143207
encryptionBuffer[i] = make([]byte, 8)
144208
}
145209

146-
for {
147-
bytesread, err := file.Read(buffer)
148-
if err != nil {
149-
if err != io.EOF {
150-
log.Fatalln("Problem while reading the contents of the file.")
210+
if encryptor.Fastmode == true {
211+
log.Println("Fastmode enabled encryption...")
212+
concurrentStart := time.Now()
213+
encryptor.fastEncrypt(file)
214+
concurrentElapsed := time.Since(concurrentStart)
215+
log.Println("The total time for fast mode: ", concurrentElapsed)
216+
} else {
217+
log.Println("Using normal mode encryption...")
218+
normalStart := time.Now()
219+
for {
220+
bytesread, err := file.Read(buffer)
221+
if err != nil {
222+
if err != io.EOF {
223+
log.Fatalln("Problem while reading the contents of the file.")
224+
}
225+
break
151226
}
152-
break
227+
encryptor.encryptChunk(&buffer, bytesread, &encryptionBuffer)
228+
// Write encryptionBuffer into file.
229+
encryptor.writeEncryptionBufferToFile(&encryptionBuffer, bytesread, encryptor.encryptionFilename)
230+
// fmt.Println("bytesread:", bytesread, "bytes to string:", string(buffer[:bytesread]))
153231
}
154-
155-
encryptor.encryptChunk(&buffer, bytesread, &encryptionBuffer)
156-
// Write encryptionBuffer into file.
157-
encryptor.writeEncryptionBufferToFile(&encryptionBuffer, bytesread, encryptor.encryptionFilename)
158-
159-
// fmt.Println("bytesread:", bytesread, "bytes to string:", string(buffer[:bytesread]))
232+
normalElapsed := time.Since(normalStart)
233+
log.Println("The total time for normal mode: ", normalElapsed)
160234
}
161235
}
162236

@@ -224,12 +298,12 @@ func (encryptor *DesEncryptor) EncryptFile(filename string) bool {
224298
return false
225299
}
226300

227-
// File encryption is successful. Delete the primary file from disk.
228-
err = os.Remove(filename)
229-
if err != nil {
230-
log.Println("Encryption failure for file:", encryptor.encryptionFilename, err)
231-
return false
232-
}
301+
// // File encryption is successful. Delete the primary file from disk.
302+
// err = os.Remove(filename)
303+
// if err != nil {
304+
// log.Println("Encryption failure for file:", encryptor.encryptionFilename, err)
305+
// return false
306+
// }
233307

234308
log.Println("File-Encryption procedure complete...")
235309
return true
@@ -281,8 +355,20 @@ func (engine *DesEncryptor) DecryptFile(filename string) bool {
281355
return true
282356
}
283357

358+
/**
359+
Reads the DES configuration file. Initializes the struct members.
360+
*/
361+
func (engine *DesEncryptor) readConfigurationFile(configFilename string) {
362+
data, err := ioutil.ReadFile(configFilename)
363+
if err != nil {
364+
log.Fatalln("Problem reading the DES configuration file...", err)
365+
}
366+
err = json.Unmarshal(data, &engine)
367+
}
368+
284369
func (engine *DesEncryptor) Init(filename string) {
285370
log.Println("Initializing FES engine...")
371+
engine.readConfigurationFile("des_config.json")
286372
engine.cipher.Init(filename)
287373
log.Println("FES engine intialization complete...")
288374
}

‎fes.exe

11 KB
Binary file not shown.

0 commit comments

Comments
(0)

AltStyle によって変換されたページ (->オリジナル) /