|
| 1 | +/** |
| 2 | + * @description - The affine cipher is a type of monoalphabetic substitution cipher, where each letter in an alphabet is mapped to its numeric equivalent, encrypted using a simple mathematical function, and converted back to a letter |
| 3 | + * @see - [wiki](https://en.wikipedia.org/wiki/Affine_cipher) |
| 4 | + */ |
| 5 | + |
| 6 | +// Default key for Affine Cipher |
| 7 | +const key = 'ABCDEFGHIJKLMNOPQRSTUVWXYZ' |
| 8 | + |
| 9 | +/** |
| 10 | + * Fix result for negative value in modulas equation |
| 11 | + * @param {Number} n - Constant number |
| 12 | + * @param {Number} m - Modulos value |
| 13 | + * @return {Number} Return n mod m |
| 14 | + */ |
| 15 | +function mod (n, m) { |
| 16 | + return ((n % m) + m) % m |
| 17 | +} |
| 18 | + |
| 19 | +/** |
| 20 | + * Modular multiplicative inverse |
| 21 | + * @param {Number} a - A coefficient |
| 22 | + * @param {Number} m - Modulos value |
| 23 | + * @return {Number} Return modular multiplicative inverse of coefficient a and modulos m |
| 24 | + */ |
| 25 | +function inverseMod (a, m) { |
| 26 | + for (let x = 1; x < m; x++) { |
| 27 | + if (mod(a * x, m) === 1) return x |
| 28 | + } |
| 29 | +} |
| 30 | + |
| 31 | +/** |
| 32 | + * Argument validation |
| 33 | + * @param {String} str - String to be checked |
| 34 | + * @param {Number} a - A coefficient to be checked |
| 35 | + * @param {Number} b - B coefficient to be checked |
| 36 | + * @return {Boolean} Result of the checking |
| 37 | + */ |
| 38 | +function isCorrectFormat (str, a, b) { |
| 39 | + if (typeof a !== 'number' || typeof b !== 'number') { |
| 40 | + throw new TypeError('Coefficient a, b should be number') |
| 41 | + } |
| 42 | + |
| 43 | + if (typeof str !== 'string') { |
| 44 | + throw new TypeError('Argument str should be String') |
| 45 | + } |
| 46 | + |
| 47 | + return true |
| 48 | +} |
| 49 | +/** |
| 50 | + * Find character index based on ASCII order |
| 51 | + * @param {String} char - Character index to be found |
| 52 | + * @return {Boolean} Character index |
| 53 | + */ |
| 54 | +function findCharIndex (char) { |
| 55 | + return char.toUpperCase().charCodeAt(0) - ('A').charCodeAt(0) |
| 56 | +} |
| 57 | + |
| 58 | +/** |
| 59 | + * Encrypt a Affine Cipher |
| 60 | + * @param {String} str - String to be encrypted |
| 61 | + * @param {Number} a - A coefficient |
| 62 | + * @param {Number} b - B coefficient |
| 63 | + * @return {String} result - Encrypted string |
| 64 | + */ |
| 65 | +function encrypt (str, a, b) { |
| 66 | + let result = '' |
| 67 | + if (isCorrectFormat(str, a, b)) { |
| 68 | + for (let x = 0; x < str.length; x++) { |
| 69 | + const charIndex = findCharIndex(str[x]) |
| 70 | + if (charIndex < 0) result += '-1' + ' ' |
| 71 | + else result += mod(a * charIndex + b, 26) + ' ' |
| 72 | + } |
| 73 | + } |
| 74 | + return result.trim() |
| 75 | +} |
| 76 | +/** |
| 77 | + * Decrypt a Affine Cipher |
| 78 | + * @param {String} str - String to be decrypted |
| 79 | + * @param {Number} a - A coefficient |
| 80 | + * @param {Number} b - B coefficient |
| 81 | + * @return {String} result - Decrypted string |
| 82 | + */ |
| 83 | +function decrypt (str, a, b) { |
| 84 | + let result = '' |
| 85 | + if (isCorrectFormat(str, a, b)) { |
| 86 | + str = str.split(' ') |
| 87 | + for (let x = 0; x < str.length; x++) { |
| 88 | + if (str[x] === '-1') result += ' ' |
| 89 | + else result += key[mod(inverseMod(a, 26) * (parseInt(str[x]) - b), 26)] |
| 90 | + } |
| 91 | + } |
| 92 | + return result |
| 93 | +} |
| 94 | + |
| 95 | +export { encrypt, decrypt } |
0 commit comments