From 59caca114c3d564f1d644384d0dfb2624232c579 Mon Sep 17 00:00:00 2001 From: Foo Date: Wed, 3 Aug 2022 02:58:34 +0800 Subject: [PATCH 1/3] fix decryt and encryt func --- Ciphers/AffineCipher.js | 38 +++++++++++++++++++++++++++---- Ciphers/test/AffineCipher.test.js | 12 ++++++---- 2 files changed, 41 insertions(+), 9 deletions(-) diff --git a/Ciphers/AffineCipher.js b/Ciphers/AffineCipher.js index 367cb18597..b4f7a04c7e 100644 --- a/Ciphers/AffineCipher.js +++ b/Ciphers/AffineCipher.js @@ -28,6 +28,27 @@ function inverseMod (a, m) { } } +/** + * GetEuclidGCD Euclidean algorithm to determine the GCD of two numbers + * @param {Number} a integer (may be negative) + * @param {Number} b integer (may be negative) + * @returns {Number} Greatest Common Divisor gcd(a, b) + */ +function GetEuclidGCD (a, b) { + if (typeof a !== 'number' || typeof b !== 'number') { + throw new TypeError('Arguments must be numbers') + } + if (a === 0 && b === 0) return undefined // infinitely many numbers divide 0 + a = Math.abs(a) + b = Math.abs(b) + while (b !== 0) { + const rem = a % b + a = b + b = rem + } + return a +} + /** * Argument validation * @param {String} str - String to be checked @@ -44,8 +65,13 @@ function isCorrectFormat (str, a, b) { throw new TypeError('Argument str should be String') } + if (!(GetEuclidGCD(a, 26) === 1)) { + throw new Error(a + ' is not coprime of 26 !!!') + } + return true } + /** * Find character index based on ASCII order * @param {String} char - Character index to be found @@ -62,17 +88,19 @@ function findCharIndex (char) { * @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++) { const charIndex = findCharIndex(str[x]) if (charIndex < 0) result += '-1' + ' ' - else result += mod(a * charIndex + b, 26) + ' ' + else result += key.charAt(mod(a * charIndex + b, 26)) + ' ' } } return result.trim() } + /** * Decrypt a Affine Cipher * @param {String} str - String to be decrypted @@ -86,10 +114,12 @@ function decrypt (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)] + else { + const charIndex = findCharIndex(str[x]) + result += key[mod(inverseMod(a, 26) * (charIndex - b), 26)] + } } + return result } - return result } - export { encrypt, decrypt } diff --git a/Ciphers/test/AffineCipher.test.js b/Ciphers/test/AffineCipher.test.js index 632704bce2..51d9cb700d 100644 --- a/Ciphers/test/AffineCipher.test.js +++ b/Ciphers/test/AffineCipher.test.js @@ -6,6 +6,8 @@ describe('Test Affine Cipher', () => { expect(() => encrypt('null', null, null)).toThrow() expect(() => encrypt('null', 1, null)).toThrow() expect(() => encrypt('null', null, 1)).toThrow() + expect(() => encrypt('null', 2, 1)).toThrow() + expect(() => encrypt('null', 4, 1)).toThrow() }) it('Test - 2, Pass invalid input to decrypt function', () => { @@ -13,14 +15,14 @@ describe('Test Affine Cipher', () => { expect(() => decrypt('null', null, null)).toThrow() expect(() => decrypt('null', 1, null)).toThrow() expect(() => decrypt('null', null, 1)).toThrow() + expect(() => encrypt('null', 2, 1)).toThrow() + expect(() => encrypt('null', 4, 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( + expect(decrypt(encrypt('HELLO WORLD', 5, 8), 5, 8)).toBe('HELLO WORLD') + expect(decrypt(encrypt('ABC DEF', 3, 5), 3, 5)).toBe('ABC DEF') + expect(decrypt(encrypt('Brown fox jump over the fence', 7, 3), 7, 3)).toBe( 'BROWN FOX JUMP OVER THE FENCE' ) }) From b4065bbce945a85e3f122961292edea1895518c1 Mon Sep 17 00:00:00 2001 From: shidian00 <81557908+shidian00@users.noreply.github.com> Date: Wed, 3 Aug 2022 03:22:43 +0800 Subject: [PATCH 2/3] Update AffineCipher.js --- Ciphers/AffineCipher.js | 24 ++---------------------- 1 file changed, 2 insertions(+), 22 deletions(-) diff --git a/Ciphers/AffineCipher.js b/Ciphers/AffineCipher.js index b4f7a04c7e..c087c4d21b 100644 --- a/Ciphers/AffineCipher.js +++ b/Ciphers/AffineCipher.js @@ -3,6 +3,7 @@ * @see - [wiki](https://en.wikipedia.org/wiki/Affine_cipher) */ +import { CoPrimeCheck } from '../Maths/CoPrimeCheck' // Default key for Affine Cipher const key = 'ABCDEFGHIJKLMNOPQRSTUVWXYZ' @@ -28,27 +29,6 @@ function inverseMod (a, m) { } } -/** - * GetEuclidGCD Euclidean algorithm to determine the GCD of two numbers - * @param {Number} a integer (may be negative) - * @param {Number} b integer (may be negative) - * @returns {Number} Greatest Common Divisor gcd(a, b) - */ -function GetEuclidGCD (a, b) { - if (typeof a !== 'number' || typeof b !== 'number') { - throw new TypeError('Arguments must be numbers') - } - if (a === 0 && b === 0) return undefined // infinitely many numbers divide 0 - a = Math.abs(a) - b = Math.abs(b) - while (b !== 0) { - const rem = a % b - a = b - b = rem - } - return a -} - /** * Argument validation * @param {String} str - String to be checked @@ -65,7 +45,7 @@ function isCorrectFormat (str, a, b) { throw new TypeError('Argument str should be String') } - if (!(GetEuclidGCD(a, 26) === 1)) { + if (!CoPrimeCheck(a, 26)) { throw new Error(a + ' is not coprime of 26 !!!') } From 904ef7c50e124783fb50cd3753205255912f9533 Mon Sep 17 00:00:00 2001 From: shidian00 <81557908+shidian00@users.noreply.github.com> Date: Wed, 3 Aug 2022 09:49:20 +0800 Subject: [PATCH 3/3] Update AffineCipher.js --- Ciphers/AffineCipher.js | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/Ciphers/AffineCipher.js b/Ciphers/AffineCipher.js index c087c4d21b..7aa590c4fe 100644 --- a/Ciphers/AffineCipher.js +++ b/Ciphers/AffineCipher.js @@ -1,5 +1,5 @@ /** - * @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 + * @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) */ @@ -46,7 +46,7 @@ function isCorrectFormat (str, a, b) { } if (!CoPrimeCheck(a, 26)) { - throw new Error(a + ' is not coprime of 26 !!!') + throw new Error(a + ' is not coprime of 26') } return true pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy