Skip to content

Commit

Permalink
Merge pull request #40 from ghimiresdp/feature/ruscrypt
Browse files Browse the repository at this point in the history
feature/ruscrypt
  • Loading branch information
ghimiresdp authored Sep 30, 2024
2 parents 6ceaa2d + 91bd03a commit 15255f3
Show file tree
Hide file tree
Showing 7 changed files with 128 additions and 6 deletions.
23 changes: 17 additions & 6 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -11,4 +11,5 @@ members = [
"problem-solving",
# projects
"projects/pandas",
"projects/ruscrypt",
]
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -107,6 +107,7 @@ cargo test --bin huffman
## [5. Projects](./projects/)

1. [Python's `Pandas` like dataframe container](projects/pandas/README.md) `cargo run --bin pandas`
2. [`ruscrypt` basic encryption](projects/ruscrypt/README.md) `cargo run --bin ruscrypt`

> **Note**: Topics that do not contain hyperlinks are work in progress and will
> be updated as soon as the solution gets completed.
Expand Down
6 changes: 6 additions & 0 deletions projects/ruscrypt/Cargo.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
[package]
name = "ruscrypt"
version = "0.1.0"
edition = "2021"

[dependencies]
8 changes: 8 additions & 0 deletions projects/ruscrypt/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
# Ruscrypt

`ruscrypt` is a basic encryption and decryption project that can encrypt and
decrypt messages with passwords.

This library demonstrates the basics of encrypting the data which is not safe
in production use, however you can use it as a fun project and encrypt and
decrypt your basic messages.
30 changes: 30 additions & 0 deletions projects/ruscrypt/src/main.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
mod ruscrypt;

use ruscrypt::Crypto;

/// A basic implementation of `ruscrypt::Crypto` structure
/// to run/test the solution, please run the following:
/// * `cargo run --bin ruscrypt`
/// * `cargo test --bin ruscrypt`
fn main() {
// please check the Crypto struct for documentation of how it works
let crypto = Crypto::new("test".to_owned());
let encrypted = crypto.encrypt("This is a original text".to_owned());
println!("Encrypted: {encrypted}");
let decrypted = crypto.decrypt(encrypted);
println!("Decrypted: {decrypted}");
}

#[cfg(test)]
mod tests {
use crate::ruscrypt::Crypto;

#[test]
fn test_encrypt_decrypt() {
let key = String::from("My Encryption Key");
let message = String::from("This is my Secret Message");

let crypt = Crypto::new(key);
assert_eq!(crypt.decrypt(crypt.encrypt(message.clone())), message);
}
}
65 changes: 65 additions & 0 deletions projects/ruscrypt/src/ruscrypt/mod.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,65 @@
pub(crate) struct Crypto {
key: Vec<u8>,
}

/// Structure `Crypto` is a cryptographic feature that allows us to perform a
/// simple encryption and decryption of our messages.
///
/// This algorithm uses one of the simplest ways to encrypt and decrypt the data
/// using basic XOR operation with the key provided.
///
/// To add more security, we perform XOR operation with different key with
/// character at key which is get using modulus of the index of the message.
impl Crypto {
pub(crate) fn new(key: String) -> Self {
Self {
key: key.as_bytes().to_owned(),
}
}

/// the `encrypt` method is used to encrypt the message using the key
/// provided during initialization of the Crypto structure.
/// the algorithm will first convert the key into a vector of bytes and then
/// performs XOR operation with each byte that is enumerated with the index
/// of the byte in the message string.
///
/// for example:
/// If key is ABC, it's bytes are [0x41, 0x42, and 0x43]
///
/// If the message string is Hello, then it's bytes are
/// [0x48, 0x65, 0x6C, 0x6C, 0x6F]
///
/// so to encrypt the message, we perform XOR operation with corresponding
/// byte of the key. if the index is larger, we perform modulus operation to
/// get the byte for that index.
///
/// the final output might contain non-printable characters, so these can
/// also be saved as hex strings.
pub(crate) fn encrypt(&self, message: String) -> String {
let len = self.key.len();
let enc = message
.bytes() // convert to bytes
.enumerate() // enumerate with index
.map(|(idx, ch)| {
let target = self.key[idx % len]; // find encrypting character
(target ^ ch) as char // perform XOR operation
})
.collect::<String>();
return enc;
}

/// the decrypt operation is exactly the same as that of encryption
/// since we performed the XOR operation on the message, performing the XOR
/// operation on the encrypted data will revert back to the original message
/// for example:
/// 0 0 1 0 1 1 0 1 (original message)
/// XOR 1 0 1 1 0 1 0 1 (encryption key)
/// ---------------------
/// = 1 0 0 1 1 0 0 0 (encrypted message)
/// XOR 1 0 1 1 0 1 0 1 (encryption key)
/// ---------------------
/// = 0 0 1 0 1 1 0 1 (original message)
pub(crate) fn decrypt(&self, message: String) -> String {
return self.encrypt(message);
}
}

0 comments on commit 15255f3

Please sign in to comment.