Uh oh!
There was an error while loading.Please reload this page.
- Notifications
You must be signed in to change notification settings - Fork5.7k
feat: add Substitution Cipher algorithm and tests#1773
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 ourterms of service andprivacy statement. We’ll occasionally send you account related emails.
Already on GitHub?Sign in to your account
Open
mmohamedkhaled wants to merge10 commits intoTheAlgorithms:masterChoose a base branch frommmohamedkhaled:add-substitution-cipher
base:master
Could not load branches
Branch not found:{{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline, and old review comments may become outdated.
Uh oh!
There was an error while loading.Please reload this page.
Open
Changes fromall commits
Commits
Show all changes
10 commits Select commitHold shift + click to select a range
c93cda3
feat: add Substitution Cipher algorithm and tests
mmohamedkhaled9f44c2f
fix: correct import path in SubstitutionCipher test
mmohamedkhaled417fb61
fix: correct casing in import path for CI compatibility
mmohamedkhaled73c15ce
fix: correct import path and folder name for CI
mmohamedkhaled7adf020
fix: update import path in SubstitutionCipher test for correct folder…
mmohamedkhaled56542d6
fix: update import path in SubstitutionCipher test for correct folder…
mmohamedkhaled5f1ed3e
removed
mmohamedkhaledc4035de
added Substitution Cipher and its test
mmohamedkhaledb9432a0
edited the style
mmohamedkhaled25add22
"test: add missing coverage for Substitution Cipher decryption edge c…
mmohamedkhaledFile filter
Filter by extension
Conversations
Failed to load comments.
Loading
Uh oh!
There was an error while loading.Please reload this page.
Jump to
Jump to file
Failed to load files.
Loading
Uh oh!
There was an error while loading.Please reload this page.
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,59 @@ | ||
/** | ||
* Substitution Cipher | ||
* | ||
* A monoalphabetic substitution cipher replaces each letter of the plaintext | ||
* with another letter based on a fixed permutation (key) of the alphabet. | ||
* https://en.wikipedia.org/wiki/Substitution_cipher | ||
*/ | ||
const alphabet = 'ABCDEFGHIJKLMNOPQRSTUVWXYZ' | ||
const defaultKey = 'QWERTYUIOPASDFGHJKLZXCVBNM' | ||
/** | ||
* Encrypts a string using a monoalphabetic substitution cipher | ||
* @param {string} text - The text to encrypt | ||
* @param {string} key - The substitution key (must be 26 uppercase letters) | ||
* @returns {string} | ||
*/ | ||
export function substitutionCipherEncryption(text, key = defaultKey) { | ||
if (key.length !== 26 || !/^[A-Z]+$/.test(key)) { | ||
throw new RangeError('Key must be 26 uppercase English letters.') | ||
} | ||
let result = '' | ||
const textUpper = text.toUpperCase() | ||
for (let i = 0; i < textUpper.length; i++) { | ||
const char = textUpper[i] | ||
const index = alphabet.indexOf(char) | ||
if (index !== -1) { | ||
result += key[index] | ||
} else { | ||
result += char | ||
} | ||
} | ||
return result | ||
} | ||
/** | ||
* Decrypts a string encrypted with the substitution cipher | ||
* @param {string} text - The encrypted text | ||
* @param {string} key - The substitution key used during encryption | ||
* @returns {string} | ||
*/ | ||
export function substitutionCipherDecryption(text, key = defaultKey) { | ||
if (key.length !== 26 || !/^[A-Z]+$/.test(key)) { | ||
throw new RangeError('Key must be 26 uppercase English letters.') | ||
} | ||
let result = '' | ||
const textUpper = text.toUpperCase() | ||
for (let i = 0; i < textUpper.length; i++) { | ||
const char = textUpper[i] | ||
const index = key.indexOf(char) | ||
if (index !== -1) { | ||
result += alphabet[index] | ||
} else { | ||
result += char | ||
} | ||
} | ||
return result | ||
} |
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,44 @@ | ||
import { describe, it, expect } from 'vitest' | ||
import { | ||
substitutionCipherEncryption, | ||
substitutionCipherDecryption | ||
} from '../SubstitutionCipher.js' | ||
describe('Substitution Cipher', () => { | ||
const key = 'QWERTYUIOPASDFGHJKLZXCVBNM' | ||
it('correctly encrypts a message', () => { | ||
const encrypted = substitutionCipherEncryption('HELLO WORLD', key) | ||
expect(encrypted).toBe('ITSSG VGKSR') | ||
}) | ||
it('correctly decrypts a message', () => { | ||
const decrypted = substitutionCipherDecryption('ITSSG VGKSR', key) | ||
expect(decrypted).toBe('HELLO WORLD') | ||
}) | ||
it('handles non-alphabetic characters', () => { | ||
const encrypted = substitutionCipherEncryption('Test! 123', key) | ||
expect(encrypted).toBe('ZTLZ! 123') | ||
}) | ||
it('throws error for invalid key', () => { | ||
expect(() => substitutionCipherEncryption('HELLO', 'BADKEY')).toThrow( | ||
RangeError | ||
) | ||
}) | ||
it('encrypts using default key if none provided', () => { | ||
const encrypted = substitutionCipherEncryption('HELLO WORLD') | ||
expect(encrypted).toBe('ITSSG VGKSR') | ||
}) | ||
it('decrypts using default key if none provided', () => { | ||
const decrypted = substitutionCipherDecryption('ITSSG VGKSR') | ||
expect(decrypted).toBe('HELLO WORLD') | ||
}) | ||
it('throws error for invalid key in decryption', () => { | ||
expect(() => substitutionCipherDecryption('HELLO', 'BADKEY')).toThrow(RangeError) | ||
}) | ||
}) |
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
Oops, something went wrong.
Uh oh!
There was an error while loading.Please reload this page.
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.