-
-
Notifications
You must be signed in to change notification settings - Fork 5.7k
Add Affine Cipher #1067
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Add Affine Cipher #1067
Changes from all commits
Commits
Show all changes
4 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,95 @@ | ||
/** | ||
* @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 | ||
} | ||
/** | ||
* Find character index based on ASCII order | ||
* @param {String} char - Character index to be found | ||
* @return {Boolean} Character index | ||
*/ | ||
function findCharIndex (char) { | ||
return char.toUpperCase().charCodeAt(0) - ('A').charCodeAt(0) | ||
} | ||
|
||
/** | ||
* 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++) { | ||
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 | ||
* @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 } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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' | ||
) | ||
}) | ||
}) |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.