-
Notifications
You must be signed in to change notification settings - Fork 0
/
decryption.go
73 lines (59 loc) · 1.26 KB
/
decryption.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
package localCrypto
import (
"errors"
"math/rand"
)
func getRowColsForDecryption(length int, key int64) (int, int) {
rand.Seed(key)
rows := rand.Intn((length / 2)) + 2
cols := (length / rows)
if cols*rows < length {
cols += 1
}
return rows, cols
}
func (d CipherText) Decrypt() (string, error) {
length := len(d.Entry)
if length == 0 {
return "", errors.New("EmptyEntryFound")
}
plainText, err := DecryptIt(d.Entry, d.key)
if err != nil {
return "", errors.New("UnExpectedErrorOccured")
}
return plainText, nil
}
func DecryptIt(encEntry string, key int64) (string, error) {
length := len(encEntry)
if length == 0 {
return "", errors.New("EmptyEntryFound")
}
rows, cols := getRowColsForDecryption(length, key)
decodeIt := []byte(encEntry)
matrix := make([][]byte, rows)
for i := 0; i < rows; i++ {
matrix[i] = make([]byte, cols)
}
k := 0
for i := 0; i < cols; i++ {
for j := 0; j < rows; j++ {
if k >= length {
break
}
matrix[j][i] = decodeIt[k] ^ 1
k++
}
}
simpleText := []byte{}
for i := 0; i < rows; i++ {
for j := 0; j < cols; j++ {
if matrix[i][j] != '\x00' {
simpleText = append(simpleText, matrix[i][j])
}
}
}
return string(simpleText), nil
}
func (c CipherText) PrintKey() int64 {
return c.key
}