MCPcopy Index your code
hub / github.com/TheAlgorithms/Go / Encrypt

Function Encrypt

cipher/railfence/railfence.go:13–49  ·  view source on GitHub ↗
(text string, rails int)

Source from the content-addressed store, hash-verified

11)
12
13func Encrypt(text string, rails int) string {
14 if rails == 1 {
15 return text
16 }
17
18 // Create a matrix for the rail fence pattern
19 matrix := make([][]rune, rails)
20 for i := range matrix {
21 matrix[i] = make([]rune, len(text))
22 }
23
24 // Fill the matrix
25 dirDown := false
26 row, col := 0, 0
27 for _, char := range text {
28 if row == 0 || row == rails-1 {
29 dirDown = !dirDown
30 }
31 matrix[row][col] = char
32 col++
33 if dirDown {
34 row++
35 } else {
36 row--
37 }
38 }
39 var result strings.Builder
40 for _, line := range matrix {
41 for _, char := range line {
42 if char != 0 {
43 result.WriteRune(char)
44 }
45 }
46 }
47
48 return result.String()
49}
50func Decrypt(cipherText string, rails int) string {
51 if rails == 1 || rails >= len(cipherText) {
52 return cipherText

Callers 1

TestEncryptFunction · 0.70

Calls 1

StringMethod · 0.80

Tested by 1

TestEncryptFunction · 0.56