| 81 | } |
| 82 | |
| 83 | func Decrypt(text []rune, keyWord string) ([]rune, error) { |
| 84 | key := getKey(keyWord) |
| 85 | textLength := len(text) |
| 86 | if textLength <= 0 { |
| 87 | return nil, ErrNoTextToEncrypt |
| 88 | } |
| 89 | keyLength := len(key) |
| 90 | if keyLength <= 0 { |
| 91 | return nil, ErrKeyMissing |
| 92 | } |
| 93 | n := textLength % keyLength |
| 94 | for i := 0; i < keyLength-n; i++ { |
| 95 | text = append(text, placeholder) |
| 96 | } |
| 97 | var result []rune |
| 98 | for i := 0; i < textLength; i += keyLength { |
| 99 | transposition := make([]rune, keyLength) |
| 100 | for j := 0; j < keyLength; j++ { |
| 101 | transposition[j] = text[i+key[j]-1] |
| 102 | } |
| 103 | result = append(result, transposition...) |
| 104 | } |
| 105 | result = []rune(strings.TrimRight(string(result), string(placeholder))) |
| 106 | return result, nil |
| 107 | } |