#encryption-decryption #encryption #xor #decryption #cryptor

xor_cryptor

A encryption/decryption library using bitwise xor

8 stable releases

1.2.3 Mar 23, 2024
1.2.2 Mar 22, 2024
1.1.0 Dec 14, 2022
1.0.2 Nov 29, 2022

#490 in Cryptography

39 downloads per month
Used in xrc_cli

MIT license

25KB
437 lines

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

Dependencies

~1.5MB
~25K SLoC