feat: add Affine cipher implementation#989
Merged
siriak merged 2 commits intoTheAlgorithms:masterfrom Jan 7, 2026
Merged
Conversation
Codecov Report❌ Patch coverage is
Additional details and impacted files@@ Coverage Diff @@
## master #989 +/- ##
==========================================
+ Coverage 95.80% 95.87% +0.07%
==========================================
Files 357 358 +1
Lines 23965 24197 +232
==========================================
+ Hits 22960 23200 +240
+ Misses 1005 997 -8 ☔ View full report in Codecov by Sentry. 🚀 New features to boost your workflow:
|
Contributor
Author
|
@siriak, this is ready to be merged. |
siriak
approved these changes
Jan 7, 2026
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
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
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.
Description
This PR adds a comprehensive implementation of the Affine Cipher to the ciphers module. The affine cipher is a type of mono-alphabetic substitution cipher that uses modular arithmetic to encrypt and decrypt messages.
Implementation Details
The implementation provides three main functions:
affine_encrypt(key: usize, message: &str) -> Result<String, String>E(x) = (ax + b) mod maffine_decrypt(key: usize, message: &str) -> Result<String, String>D(x) = a^(-1)(x - b) mod maffine_generate_key() -> usizekey_a * SYMBOLS.len() + key_bHelper Functions
gcd(a: usize, b: usize) -> usizefind_mod_inverse(a: i64, m: i64) -> Option<i64>Noneif inverse doesn't existcheck_keys(key_a: usize, key_b: usize, is_encrypt: bool) -> Result<(), String>Algorithm Details
Encryption:
Decryption:
Key Requirements:
gcd(key_a, m) = 1(key_a must be coprime with symbol set size)key_a ≠ 1(prevents weak encryption)key_b ≠ 0(prevents weak encryption)0 ≤ key_b < m(must be within valid range)Time Complexity:
Space Complexity:$O(n)$ for output string
Symbol Set
Uses all printable ASCII characters (95 total):
Key Features
Resultfor comprehensive error handlingChanges Made
src/ciphers/affine_cipher.rswith complete implementationsrc/ciphers/mod.rsto include and export the moduleType of Change
Testing
All tests pass successfully:
Test Coverage
✅ 20+ Unit Tests Including:
Examples
Checklist
cargo fmtcargo clippymod.rsAdditional Context
Resulttype instead ofsys.exit()for errorsrandcrate's thread-safe RNGSecurity Considerations:
Mathematical Correctness:
Use Cases:
Note on Security:
The affine cipher is not secure for modern cryptographic use. It's included for:
For actual security needs, use modern cryptographic algorithms.
References