Crates.io | xor_cryptor |
lib.rs | xor_cryptor |
version | 1.2.3 |
source | src |
created_at | 2022-11-03 08:49:29.380403 |
updated_at | 2024-03-23 08:20:24.41847 |
description | A encryption/decryption library using bitwise xor |
homepage | |
repository | https://github.com/shank03/XORCryptor-Rust |
max_upload_size | |
id | 704143 |
size | 25,245 |
Algorithm for encrypting and decrypting based on XOR bitwise operation
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)
);
}
$ 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 !"
$ 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