mirror of
https://github.com/TheAlgorithms/JavaScript.git
synced 2025-07-05 16:26:47 +08:00
Fix Affine-Cipher encrypt and drypt func (#1077)
This commit is contained in:
@ -1,8 +1,9 @@
|
|||||||
/**
|
/**
|
||||||
* @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)
|
* @see - [wiki](https://en.wikipedia.org/wiki/Affine_cipher)
|
||||||
*/
|
*/
|
||||||
|
|
||||||
|
import { CoPrimeCheck } from '../Maths/CoPrimeCheck'
|
||||||
// Default key for Affine Cipher
|
// Default key for Affine Cipher
|
||||||
const key = 'ABCDEFGHIJKLMNOPQRSTUVWXYZ'
|
const key = 'ABCDEFGHIJKLMNOPQRSTUVWXYZ'
|
||||||
|
|
||||||
@ -44,8 +45,13 @@ function isCorrectFormat (str, a, b) {
|
|||||||
throw new TypeError('Argument str should be String')
|
throw new TypeError('Argument str should be String')
|
||||||
}
|
}
|
||||||
|
|
||||||
|
if (!CoPrimeCheck(a, 26)) {
|
||||||
|
throw new Error(a + ' is not coprime of 26')
|
||||||
|
}
|
||||||
|
|
||||||
return true
|
return true
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Find character index based on ASCII order
|
* Find character index based on ASCII order
|
||||||
* @param {String} char - Character index to be found
|
* @param {String} char - Character index to be found
|
||||||
@ -62,17 +68,19 @@ function findCharIndex (char) {
|
|||||||
* @param {Number} b - B coefficient
|
* @param {Number} b - B coefficient
|
||||||
* @return {String} result - Encrypted string
|
* @return {String} result - Encrypted string
|
||||||
*/
|
*/
|
||||||
|
|
||||||
function encrypt (str, a, b) {
|
function encrypt (str, a, b) {
|
||||||
let result = ''
|
let result = ''
|
||||||
if (isCorrectFormat(str, a, b)) {
|
if (isCorrectFormat(str, a, b)) {
|
||||||
for (let x = 0; x < str.length; x++) {
|
for (let x = 0; x < str.length; x++) {
|
||||||
const charIndex = findCharIndex(str[x])
|
const charIndex = findCharIndex(str[x])
|
||||||
if (charIndex < 0) result += '-1' + ' '
|
if (charIndex < 0) result += '-1' + ' '
|
||||||
else result += mod(a * charIndex + b, 26) + ' '
|
else result += key.charAt(mod(a * charIndex + b, 26)) + ' '
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
return result.trim()
|
return result.trim()
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Decrypt a Affine Cipher
|
* Decrypt a Affine Cipher
|
||||||
* @param {String} str - String to be decrypted
|
* @param {String} str - String to be decrypted
|
||||||
@ -86,10 +94,12 @@ function decrypt (str, a, b) {
|
|||||||
str = str.split(' ')
|
str = str.split(' ')
|
||||||
for (let x = 0; x < str.length; x++) {
|
for (let x = 0; x < str.length; x++) {
|
||||||
if (str[x] === '-1') result += ' '
|
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 }
|
export { encrypt, decrypt }
|
||||||
|
@ -6,6 +6,8 @@ describe('Test Affine Cipher', () => {
|
|||||||
expect(() => encrypt('null', null, null)).toThrow()
|
expect(() => encrypt('null', null, null)).toThrow()
|
||||||
expect(() => encrypt('null', 1, null)).toThrow()
|
expect(() => encrypt('null', 1, null)).toThrow()
|
||||||
expect(() => encrypt('null', null, 1)).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', () => {
|
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', null, null)).toThrow()
|
||||||
expect(() => decrypt('null', 1, null)).toThrow()
|
expect(() => decrypt('null', 1, null)).toThrow()
|
||||||
expect(() => decrypt('null', null, 1)).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', () => {
|
it('Test - 3 Pass string value to encrypt and ecrypt function', () => {
|
||||||
const a = 5
|
expect(decrypt(encrypt('HELLO WORLD', 5, 8), 5, 8)).toBe('HELLO WORLD')
|
||||||
const b = 8
|
expect(decrypt(encrypt('ABC DEF', 3, 5), 3, 5)).toBe('ABC DEF')
|
||||||
expect(decrypt(encrypt('HELLO WORLD', a, b), a, b)).toBe('HELLO WORLD')
|
expect(decrypt(encrypt('Brown fox jump over the fence', 7, 3), 7, 3)).toBe(
|
||||||
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'
|
'BROWN FOX JUMP OVER THE FENCE'
|
||||||
)
|
)
|
||||||
})
|
})
|
||||||
|
Reference in New Issue
Block a user