From e79635de74cec1391e0c60e28c7cc8058734e9aa Mon Sep 17 00:00:00 2001 From: Foo Date: Wed, 27 Jul 2022 17:24:52 +0800 Subject: [PATCH 1/4] Add Affine Cipher --- Ciphers/AffineCipher.js | 88 +++++++++++++++++++++++++++++++ Ciphers/test/AffineCipher.test.js | 27 ++++++++++ 2 files changed, 115 insertions(+) create mode 100644 Ciphers/AffineCipher.js create mode 100644 Ciphers/test/AffineCipher.test.js diff --git a/Ciphers/AffineCipher.js b/Ciphers/AffineCipher.js new file mode 100644 index 0000000000..fde92c930b --- /dev/null +++ b/Ciphers/AffineCipher.js @@ -0,0 +1,88 @@ + +/** + * @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 + * @see - [wiki](https://en.wikipedia.org/wiki/Affine_cipher) + */ + +// Default key for Affine Cipher +const key = 'ABCDEFGHIJKLMNOPQRSTUVWXYZ' + +/** + * Fix result for negative value in modulas equation + * @param {Number} n - Constant number + * @param {Number} m - Modulos value + * @return {Number} Return n mod m + */ +function mod (n, m) { + return ((n % m) + m) % m +} + +/** + * Modular multiplicative inverse + * @param {Number} a - A coefficient + * @param {Number} m - Modulos value + * @return {Number} Return modular multiplicative inverse of coefficient a and modulos m + */ +function inverseMod (a, m) { + for (let x = 1; x < m; x++) { + if (mod(a * x, m) === 1) return x + } +} + +/** + * Argument validation + * @param {String} str - String to be checked + * @param {Number} a - A coefficient to be checked + * @param {Number} b - B coefficient to be checked + * @return {Boolean} Result of the checking + */ +function isCorrectFormat (str, a, b) { + if (typeof a !== 'number' || typeof b !== 'number') { + throw new TypeError('Coefficient a, b should be number') + } + + if (typeof str !== 'string') { + throw new TypeError('Argument str should be String') + } + + return true +} + +/** + * Encrypt a Affine Cipher + * @param {String} str - String to be encrypted + * @param {Number} a - A coefficient + * @param {Number} b - B coefficient + * @return {String} result - Encrypted string + */ +function encrypt (str, a, b) { + let result = '' + if (isCorrectFormat(str, a, b)) { + for (let x = 0; x < str.length; x++) { + if (key.indexOf(str[x].toUpperCase()) === -1) result += '-1' + ' ' + else result += mod(a * key.indexOf(str[x].toUpperCase()) + b, 26) + ' ' + } + } + return result.trim() +} + +/** + * Decrypt a Affine Cipher + * @param {String} str - String to be decrypted + * @param {Number} a - A coefficient + * @param {Number} b - B coefficient + * @return {String} result - Decrypted string + */ +function decrypt (str, a, b) { + let result = '' + if (isCorrectFormat(str, a, b)) { + str = str.split(' ') + for (let x = 0; x < str.length; x++) { + if (str[x] === '-1') result += ' ' + else result += key[mod(inverseMod(a, 26) * (parseInt(str[x]) - b), 26)] + } + } + return result +} + +export { encrypt, decrypt } diff --git a/Ciphers/test/AffineCipher.test.js b/Ciphers/test/AffineCipher.test.js new file mode 100644 index 0000000000..632704bce2 --- /dev/null +++ b/Ciphers/test/AffineCipher.test.js @@ -0,0 +1,27 @@ +import { encrypt, decrypt } from '../AffineCipher' + +describe('Test Affine Cipher', () => { + it('Test - 1, Pass invalid input to encrypt function', () => { + expect(() => encrypt(null, null, null)).toThrow() + expect(() => encrypt('null', null, null)).toThrow() + expect(() => encrypt('null', 1, null)).toThrow() + expect(() => encrypt('null', null, 1)).toThrow() + }) + + it('Test - 2, Pass invalid input to decrypt function', () => { + expect(() => decrypt(null, null, null)).toThrow() + expect(() => decrypt('null', null, null)).toThrow() + expect(() => decrypt('null', 1, null)).toThrow() + expect(() => decrypt('null', null, 1)).toThrow() + }) + + it('Test - 3 Pass string value to encrypt and ecrypt function', () => { + const a = 5 + const b = 8 + expect(decrypt(encrypt('HELLO WORLD', a, b), a, b)).toBe('HELLO WORLD') + expect(decrypt(encrypt('ABC DEF', a, b), a, b)).toBe('ABC DEF') + expect(decrypt(encrypt('Brown fox jump over the fence', a, b), a, b)).toBe( + 'BROWN FOX JUMP OVER THE FENCE' + ) + }) +}) From e60da79caaad490363a9ed17402cfe0177030083 Mon Sep 17 00:00:00 2001 From: shidian00 <81557908+shidian00@users.noreply.github.com> Date: Wed, 27 Jul 2022 18:40:25 +0800 Subject: [PATCH 2/4] Replace IndexOf function to improve performance --- Ciphers/AffineCipher.js | 14 +++++++++++--- 1 file changed, 11 insertions(+), 3 deletions(-) diff --git a/Ciphers/AffineCipher.js b/Ciphers/AffineCipher.js index fde92c930b..1ba871eeb2 100644 --- a/Ciphers/AffineCipher.js +++ b/Ciphers/AffineCipher.js @@ -47,6 +47,14 @@ function isCorrectFormat (str, a, b) { return true } +/** + * Find character index based on ASCII order + * @param {String} char - Character index to be found + * @return {Boolean} Chracter index + */ +function findCharIndex (char) { + return char.toUpperCase().charCodeAt(0) - ('A').charCodeAt(0) +} /** * Encrypt a Affine Cipher @@ -59,13 +67,13 @@ function encrypt (str, a, b) { let result = '' if (isCorrectFormat(str, a, b)) { for (let x = 0; x < str.length; x++) { - if (key.indexOf(str[x].toUpperCase()) === -1) result += '-1' + ' ' - else result += mod(a * key.indexOf(str[x].toUpperCase()) + b, 26) + ' ' + const charIndex = findCharIndex(str[x]) + if (charIndex < 0) result += '-1' + ' ' + else result += mod(a * charIndex + b, 26) + ' ' } } return result.trim() } - /** * Decrypt a Affine Cipher * @param {String} str - String to be decrypted From cd8a8167a156b51b3eacd722bd8823699c20c262 Mon Sep 17 00:00:00 2001 From: shidian00 <81557908+shidian00@users.noreply.github.com> Date: Wed, 27 Jul 2022 18:43:24 +0800 Subject: [PATCH 3/4] remove leading spacing and fixed typo --- Ciphers/AffineCipher.js | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/Ciphers/AffineCipher.js b/Ciphers/AffineCipher.js index 1ba871eeb2..6d633e54af 100644 --- a/Ciphers/AffineCipher.js +++ b/Ciphers/AffineCipher.js @@ -50,7 +50,7 @@ function isCorrectFormat (str, a, b) { /** * Find character index based on ASCII order * @param {String} char - Character index to be found - * @return {Boolean} Chracter index + * @return {Boolean} Character index */ function findCharIndex (char) { return char.toUpperCase().charCodeAt(0) - ('A').charCodeAt(0) From 55c4b8b87a5070637c4d8831eb0986b7c719bf6b Mon Sep 17 00:00:00 2001 From: shidian00 <81557908+shidian00@users.noreply.github.com> Date: Wed, 27 Jul 2022 18:46:26 +0800 Subject: [PATCH 4/4] Remove leading spacing in first row. --- Ciphers/AffineCipher.js | 1 - 1 file changed, 1 deletion(-) diff --git a/Ciphers/AffineCipher.js b/Ciphers/AffineCipher.js index 6d633e54af..367cb18597 100644 --- a/Ciphers/AffineCipher.js +++ b/Ciphers/AffineCipher.js @@ -1,4 +1,3 @@ - /** * @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 * @see - [wiki](https://en.wikipedia.org/wiki/Affine_cipher)