xor_cryptor

Crates.ioxor_cryptor
lib.rsxor_cryptor
version1.2.3
sourcesrc
created_at2022-11-03 08:49:29.380403
updated_at2024-03-23 08:20:24.41847
descriptionA encryption/decryption library using bitwise xor
homepage
repositoryhttps://github.com/shank03/XORCryptor-Rust
max_upload_size
id704143
size25,245
Shashank Verma (shank03)

documentation

README

XORCryptor Lib

Algorithm for encrypting and decrypting based on XOR bitwise operation

crates.io docs.rs

About algorithm

image

Usage

use xor_cryptor::XORCryptor;

fn main() {
    let sample_text = String::from("Hello World !");
    let key = String::from("secret_key");
    let buffer = sample_text.as_bytes().to_vec();

    let res = XORCryptor::new(&key);
    // or
    let res = XORCryptor::new_bytes(key.as_bytes());
    if res.is_err() {
        return;
    }
    let xrc = res.unwrap();

    let encrypted_buffer = xrc.encrypt_vec(buffer);
    let encrypted_string = String::from_utf8_lossy(&encrypted_buffer);
    println!("Encrypted: {}\n", encrypted_string);

    // This encrypted string contains formatted non-utf8 characters
    // Do not use this string as vector to decrypt
    let decrypted_buffer = xrc.decrypt_vec(encrypted_string.as_bytes().to_vec());
    println!(
        "Decrypted from string : {:?}",
        String::from_utf8_lossy(&decrypted_buffer)
    );

    let decrypted_buffer = xrc.decrypt_vec(encrypted_buffer);
    println!(
        "Decrypted from vec    : {:?}",
        String::from_utf8_lossy(&decrypted_buffer)
    );
}

Output

$ cargo run
   Compiling xor_cryptor v1.0.0 (XYZ)
    Finished dev [unoptimized + debuginfo] target(s) in 0.21s
     Running `target/debug/xor_cryptor.exe`

Encrypted: W"♣'"�jMLQ�-

Decrypted from string: "Hell:4u��D6S\u{c}\u{1e}��K"
Decrypted from vec   : "Hello World !"

Benchmark

System Configuration

  • OS: Windows 11
  • Processor: Intel(R) Core(TM) i5-1035G1 CPU @ 1.00 GHz 1.19 GHz
  • Installed RAM: 16.0 GB (2666 MHz - DDR4)
  • System type: 64-bit operating system, x64-based processor
  • Rustc: 1.75.0

Results

$ cargo test --release --package xor_cryptor --lib -- test::benchmark --exact --nocapture
    Finished release [optimized] target(s) in 0.02s
     Running unittests src/lib.rs (target/release/deps/xor_cryptor-9b9862a430980841)

running 1 test
Allocate Buff - 1.62 GB: 2054 ms
Encrypted: 502 ms - 3.24 GBps
Decrypted: 594 ms - 2.74 GBps
test test::benchmark ... ok

test result: ok. 1 passed; 0 failed; 0 ignored; 0 measured; 3 filtered out; finished in 3.26s
Commit count: 23

cargo fmt