|
| 1 | +// Package affinecipher contains tools that implement affine-cipher. |
| 2 | +package affinecipher |
| 3 | + |
| 4 | +import ( |
| 5 | + "errors" |
| 6 | + "strings" |
| 7 | +) |
| 8 | + |
| 9 | +type operation int |
| 10 | + |
| 11 | +const ( |
| 12 | + encode operation = iota + 1 |
| 13 | + decode |
| 14 | +) |
| 15 | + |
| 16 | +const _totalAlphabets = 26 // total number of letters in alphabet. |
| 17 | + |
| 18 | +// Encode encodes the provided message with the provided keys, using affine-cipher. |
| 19 | +func Encode(text string, a, b int) (string, error) { |
| 20 | + if gcd(a, _totalAlphabets) != 1 { |
| 21 | + return "", errors.New("affinecipher.Encode: a and b must be co-prime") |
| 22 | + } |
| 23 | + return cipher(encode, text, a, b) |
| 24 | +} |
| 25 | + |
| 26 | +// Decode decodes the provided encoded message with the provided keys, using affine-cipher. |
| 27 | +func Decode(text string, a, b int) (string, error) { |
| 28 | + if gcd(a, _totalAlphabets) != 1 { |
| 29 | + return "", errors.New("affinecipher.Decode: a and b must be co-prime") |
| 30 | + } |
| 31 | + mmi := multiInv(a, _totalAlphabets) |
| 32 | + return cipher(decode, text, mmi, b) |
| 33 | +} |
| 34 | + |
| 35 | +// cipher functions takes a operation, the text and as well as key values, and performs the |
| 36 | +// operation on the text using the provided keys. |
| 37 | +func cipher(op operation, text string, a, b int) (string, error) { |
| 38 | + text = strings.ToLower(text) |
| 39 | + var output strings.Builder |
| 40 | + accum := 0 |
| 41 | + opFunc := operationFunc(op) |
| 42 | + for _, char := range []byte(text) { |
| 43 | + if !(char >= 'a' && char <= 'z') && // if not a letter and |
| 44 | + !(char >= '1' && char <= '9') { // if not a number |
| 45 | + continue |
| 46 | + } |
| 47 | + |
| 48 | + // if its encoding then write a space every 5 letters. |
| 49 | + if op == encode && accum != 0 && (accum%5) == 0 { |
| 50 | + output.WriteByte(' ') |
| 51 | + } |
| 52 | + accum++ |
| 53 | + |
| 54 | + if char >= '1' && char <= '9' { // write numbers as it is. |
| 55 | + output.WriteByte(char) |
| 56 | + continue |
| 57 | + } |
| 58 | + output.WriteByte(opFunc(char, a, b)) |
| 59 | + } |
| 60 | + return output.String(), nil |
| 61 | +} |
| 62 | + |
| 63 | +// gcd function finds the greatest common divisor for the numbers passed into it. |
| 64 | +func gcd(a, b int) int { |
| 65 | + if a < b { |
| 66 | + a, b = b, a |
| 67 | + } |
| 68 | + for b > 0 { |
| 69 | + a, b = b, (a % b) |
| 70 | + } |
| 71 | + return a |
| 72 | +} |
| 73 | + |
| 74 | +// multiInv finds the modular multiple inverse of the passed num and mod. |
| 75 | +func multiInv(num, mod int) int { |
| 76 | + t1, t2 := 0, 1 |
| 77 | + a, b := num, mod |
| 78 | + |
| 79 | + if a < b { |
| 80 | + a, b = b, a |
| 81 | + } |
| 82 | + |
| 83 | + for b > 0 { |
| 84 | + t := t1 - (t2 * (a / b)) |
| 85 | + a, b = b, (a % b) |
| 86 | + t1, t2 = t2, t |
| 87 | + } |
| 88 | + |
| 89 | + return t1 |
| 90 | +} |
| 91 | + |
| 92 | +// operationFunc function returns the appropriate function for the passed operation. |
| 93 | +func operationFunc(op operation) func(byte, int, int) byte { |
| 94 | + if op == encode { |
| 95 | + return encryptionFunc |
| 96 | + } |
| 97 | + return decryptionFunc |
| 98 | +} |
| 99 | + |
| 100 | +// encryptionFunc function is for encrypting text. |
| 101 | +func encryptionFunc(letter byte, num1, num2 int) byte { |
| 102 | + temp := ((num1 * int(letter-'a')) + num2) % _totalAlphabets |
| 103 | + return byte(temp) + 'a' |
| 104 | +} |
| 105 | + |
| 106 | +// decryptionFunc function is for decrypting text. |
| 107 | +func decryptionFunc(letter byte, mmi, num2 int) byte { |
| 108 | + temp := (mmi * (int(letter-'a') - num2)) % _totalAlphabets |
| 109 | + if temp < 0 { |
| 110 | + temp += _totalAlphabets |
| 111 | + } |
| 112 | + return byte(temp) + 'a' |
| 113 | +} |
0 commit comments