-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathrail_fence_decrypt.go
85 lines (64 loc) · 2.67 KB
/
rail_fence_decrypt.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
package rail_fence_cipher
import (
"errors"
variable_parameter "github.com/golang-infrastructure/go-variable-parameter"
"strings"
"unicode/utf8"
)
// ------------------------------------------------ ---------------------------------------------------------------------
func Decrypt(encryptText string, options ...*Options) (string, error) {
// 未传递参数的话则设置默认的参数
options = variable_parameter.SetDefaultParamByFunc(options, func() *Options {
return NewOptions()
})
// 参数检查
if options[0].PutEdgeDirection == options[0].TakeEdgeDirection {
return "", errors.New("PutEdgeDirection can not equals TakeEdgeDirection")
}
rowCount := (utf8.RuneCountInString(encryptText) + options[0].Columns - 1) / options[0].Columns
table := NewTable(rowCount, options[0].Columns)
encrypttextConsumer := NewTextConsumer(encryptText, options[0].FillCharacter)
// 按照取的方式放
table.VisitByEdgeDirection(options[0].TakeEdgeDirection, func(table Table, rowIndex, columnIndex int, character rune) {
table[rowIndex][columnIndex] = encrypttextConsumer.Take()
})
//fmt.Println(table.String())
// 按照放的方式取
result := strings.Builder{}
table.VisitByEdgeDirection(options[0].PutEdgeDirection, func(table Table, rowIndex, columnIndex int, character rune) {
result.WriteRune(character)
})
return result.String(), nil
}
// ------------------------------------------------ ---------------------------------------------------------------------
// DecryptW W型的栅栏解密
func DecryptW(ciphertext string, options ...*Options) (string, error) {
// 未传递参数的话则设置默认的参数
options = variable_parameter.SetDefaultParamByFunc(options, func() *Options {
return NewOptions()
})
// 参数检查
if options[0].Rows < 3 {
return "", errors.New("rows min 3")
}
table := NewTable(options[0].Rows, utf8.RuneCountInString(ciphertext))
// 标记W路径上的字符
table.VisitByW(func(table Table, rowIndex, columnIndex int, character rune) {
table[rowIndex][columnIndex] = 1
})
// 遍历设置W路径上的字符
ciphertextConsumer := NewTextConsumer(ciphertext, '.')
table.VisitByEdgeDirection(EdgeDirectionLeftTop2Right, func(table Table, rowIndex, columnIndex int, character rune) {
if character == 1 {
table[rowIndex][columnIndex] = ciphertextConsumer.Take()
}
})
//fmt.Println(table.String())
// 收集W路径上的字符
result := strings.Builder{}
table.VisitByW(func(table Table, rowIndex, columnIndex int, character rune) {
result.WriteRune(character)
})
return result.String(), nil
}
// ------------------------------------------------ ---------------------------------------------------------------------