forked from TheAlgorithms/Go
-
Notifications
You must be signed in to change notification settings - Fork 0
/
polybius.go
87 lines (79 loc) · 2.46 KB
/
polybius.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
// Package polybius is encrypting method with polybius square
// ref: https://en.wikipedia.org/wiki/Polybius_square#Hybrid_Polybius_Playfair_Cipher
package polybius
import (
"fmt"
"math"
"strings"
)
// Polybius is struct having size, characters, and key
type Polybius struct {
size int
characters string
key string
}
// NewPolybius returns a pointer to object of Polybius.
// If the size of "chars" is longer than "size",
// "chars" are truncated to "size".
func NewPolybius(key string, size int, chars string) (*Polybius, error) {
key = strings.ToUpper(key)
chars = strings.ToUpper(chars)[:size]
for idx, ch := range chars {
if strings.Contains(chars[idx+1:], string(ch)) {
return nil, fmt.Errorf("\"chars\" contains same character: %c", ch)
}
}
if len(key) != size*size {
return nil, fmt.Errorf("len(key): %d must be as long as size squared: %d", len(key), size*size)
}
return &Polybius{size, chars, key}, nil
}
// Encrypt encrypts with polybius encryption
func (p *Polybius) Encrypt(text string) (string, error) {
encryptedText := ""
for _, char := range strings.ToUpper(text) {
encryptedChar, err := p.encipher(char)
if err != nil {
return "", fmt.Errorf("failed encipher: %w", err)
}
encryptedText += encryptedChar
}
return encryptedText, nil
}
// Decrypt decrypts with polybius encryption
func (p *Polybius) Decrypt(text string) (string, error) {
chars := []rune(strings.ToUpper(text))
decryptedText := ""
for i := 0; i < len(chars); i += 2 {
decryptedChar, err := p.decipher(chars[i:int(math.Min(float64(i+2), float64(len(chars))))])
if err != nil {
return "", fmt.Errorf("failed decipher: %w", err)
}
decryptedText += decryptedChar
}
return decryptedText, nil
}
func (p *Polybius) encipher(char rune) (string, error) {
index := strings.IndexRune(p.key, char)
if index < 0 {
return "", fmt.Errorf("%c does not exist in keys", char)
}
row := index / p.size
col := index % p.size
chars := []rune(p.characters)
return string([]rune{chars[row], chars[col]}), nil
}
func (p *Polybius) decipher(chars []rune) (string, error) {
if len(chars) != 2 {
return "", fmt.Errorf("the size of \"chars\" must be even")
}
row := strings.IndexRune(p.characters, chars[0])
if row < 0 {
return "", fmt.Errorf("%c does not exist in characters", chars[0])
}
col := strings.IndexRune(p.characters, chars[1])
if col < 0 {
return "", fmt.Errorf("%c does not exist in characters", chars[1])
}
return string([]rune(p.key)[row*p.size+col]), nil
}