Skip to content

Commit 4b07e8a

Browse files
authored
Fix Affine-Cipher encrypt and drypt func (TheAlgorithms#1077)
1 parent cc16cbd commit 4b07e8a

File tree

2 files changed

+22
-10
lines changed

2 files changed

+22
-10
lines changed

Ciphers/AffineCipher.js

Lines changed: 15 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,9 @@
11
/**
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
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
33
* @see - [wiki](https://en.wikipedia.org/wiki/Affine_cipher)
44
*/
55

6+
import { CoPrimeCheck } from '../Maths/CoPrimeCheck'
67
// Default key for Affine Cipher
78
const key = 'ABCDEFGHIJKLMNOPQRSTUVWXYZ'
89

@@ -44,8 +45,13 @@ function isCorrectFormat (str, a, b) {
4445
throw new TypeError('Argument str should be String')
4546
}
4647

48+
if (!CoPrimeCheck(a, 26)) {
49+
throw new Error(a + ' is not coprime of 26')
50+
}
51+
4752
return true
4853
}
54+
4955
/**
5056
* Find character index based on ASCII order
5157
* @param {String} char - Character index to be found
@@ -62,17 +68,19 @@ function findCharIndex (char) {
6268
* @param {Number} b - B coefficient
6369
* @return {String} result - Encrypted string
6470
*/
71+
6572
function encrypt (str, a, b) {
6673
let result = ''
6774
if (isCorrectFormat(str, a, b)) {
6875
for (let x = 0; x < str.length; x++) {
6976
const charIndex = findCharIndex(str[x])
7077
if (charIndex < 0) result += '-1' + ' '
71-
else result += mod(a * charIndex + b, 26) + ' '
78+
else result += key.charAt(mod(a * charIndex + b, 26)) + ' '
7279
}
7380
}
7481
return result.trim()
7582
}
83+
7684
/**
7785
* Decrypt a Affine Cipher
7886
* @param {String} str - String to be decrypted
@@ -86,10 +94,12 @@ function decrypt (str, a, b) {
8694
str = str.split(' ')
8795
for (let x = 0; x < str.length; x++) {
8896
if (str[x] === '-1') result += ' '
89-
else result += key[mod(inverseMod(a, 26) * (parseInt(str[x]) - b), 26)]
97+
else {
98+
const charIndex = findCharIndex(str[x])
99+
result += key[mod(inverseMod(a, 26) * (charIndex - b), 26)]
100+
}
90101
}
102+
return result
91103
}
92-
return result
93104
}
94-
95105
export { encrypt, decrypt }

Ciphers/test/AffineCipher.test.js

Lines changed: 7 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -6,21 +6,23 @@ describe('Test Affine Cipher', () => {
66
expect(() => encrypt('null', null, null)).toThrow()
77
expect(() => encrypt('null', 1, null)).toThrow()
88
expect(() => encrypt('null', null, 1)).toThrow()
9+
expect(() => encrypt('null', 2, 1)).toThrow()
10+
expect(() => encrypt('null', 4, 1)).toThrow()
911
})
1012

1113
it('Test - 2, Pass invalid input to decrypt function', () => {
1214
expect(() => decrypt(null, null, null)).toThrow()
1315
expect(() => decrypt('null', null, null)).toThrow()
1416
expect(() => decrypt('null', 1, null)).toThrow()
1517
expect(() => decrypt('null', null, 1)).toThrow()
18+
expect(() => encrypt('null', 2, 1)).toThrow()
19+
expect(() => encrypt('null', 4, 1)).toThrow()
1620
})
1721

1822
it('Test - 3 Pass string value to encrypt and ecrypt function', () => {
19-
const a = 5
20-
const b = 8
21-
expect(decrypt(encrypt('HELLO WORLD', a, b), a, b)).toBe('HELLO WORLD')
22-
expect(decrypt(encrypt('ABC DEF', a, b), a, b)).toBe('ABC DEF')
23-
expect(decrypt(encrypt('Brown fox jump over the fence', a, b), a, b)).toBe(
23+
expect(decrypt(encrypt('HELLO WORLD', 5, 8), 5, 8)).toBe('HELLO WORLD')
24+
expect(decrypt(encrypt('ABC DEF', 3, 5), 3, 5)).toBe('ABC DEF')
25+
expect(decrypt(encrypt('Brown fox jump over the fence', 7, 3), 7, 3)).toBe(
2426
'BROWN FOX JUMP OVER THE FENCE'
2527
)
2628
})

0 commit comments

Comments
 (0)
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