-
Notifications
You must be signed in to change notification settings - Fork 1.9k
/
Copy pathrailfence.go
120 lines (111 loc) · 2.13 KB
/
railfence.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
package main
import "fmt"
func Encrypt(str string, depth int) string {
w := len(str)
grid := makeGrid(depth, w)
dir, i := 1, 0
for j := 0; j < w; j++ {
grid[i][j] = string(str[j])
i += dir * 1
if i == depth-1 || i == 0 {
dir *= -1
}
}
printTable(grid)
return readGrid(grid)
}
func readGrid(g [][]string) string {
s := ""
n := len(g)
for i := 0; i < n; i++ {
m := len(g[i])
for j := 0; j < m; j++ {
s += g[i][j]
}
}
return s
}
func makeGrid(n int, w int) [][]string {
grid := make([][]string, n)
for i := 0; i < n; i++ {
grid[i] = make([]string, w)
}
return grid
}
func printTable(t [][]string) {
fmt.Println("Matrix => ")
n := len(t)
for i := 0; i < n; i++ {
m := len(t[i])
for j := 0; j < m; j++ {
fmt.Printf("%s |", t[i][j])
}
fmt.Println()
}
}
func Decrypt(str string, depth int) string {
s := ""
l := len(str)
cycle := (depth * 2) - 2
extra := l % cycle
rows := make([]int, depth)
for i := 0; i < depth; i++ {
rows[i] = (l / cycle) * 2
if i == 0 || i == depth-1 {
rows[i] = (l / cycle)
}
}
i := 0
for extra != 0 {
rows[i]++
extra--
i = (i + 1) % depth
}
rowVals := make([][]string, depth)
for i, j, k := 0, 0, 0; i < depth; i++ {
rowVals[i] = make([]string, rows[i])
k = 0
for k < rows[i] {
rowVals[i][k] = string(str[j])
k++
j++
}
}
counts := make([]int, depth)
for i := range counts {
counts[i] = 0
}
dir := 1
for i, j := 0, 0; i < l; i++ {
s += rowVals[j][counts[j]]
if counts[j] < rows[j] {
counts[j]++
}
if j+1 == depth {
dir = -1
}
if j == 0 {
dir = 1
}
j = j + (1 * dir)
}
return s
}
func checkDepth(d int) error {
if d < 2 {
return fmt.Errorf("%s", "Depth is too low, use 2 or higher")
}
return nil
}
func main() {
plainText := "helloworld"
depth2 := 2
depth3 := 3
encrypted2 := Encrypt(plainText, depth2)
fmt.Println("Encrypted text (depth 2): ", encrypted2)
fmt.Println("Decrypted text (depth 2): ", Decrypt(encrypted2, depth2))
fmt.Println()
encrypted3 := Encrypt(plainText, depth3)
fmt.Println("Encrypted text (depth 3): ", encrypted3)
fmt.Println("Decrypted text (depth 3): ", Decrypt(encrypted3, depth3))
}