Expand description
Cipher and decipher using algorithm based on XOR bitwise operation
§Usage
§For V2:
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 encrypted_buffer = match XORCryptor::encrypt_v2(key.as_bytes(), buffer) {
Ok(enc) => enc,
Err(e) => {
println!("Error: {}", e);
return;
}
};
let encrypted_string = String::from_utf8_lossy(&encrypted_buffer);
println!("Encrypted: {}\n", encrypted_string);
// Never convert encrypted buffer into string
// This encrypted string contains formatted non-utf8 characters
// Do not use this string as vector to decrypt
let decrypted_buffer =
match XORCryptor::decrypt_v2(key.as_bytes(), encrypted_string.as_bytes().to_vec()) {
Ok(d) => d,
Err(e) => {
println!("Error: {}", e);
return;
}
};
println!(
"Decrypted from string : {:?}",
String::from_utf8_lossy(&decrypted_buffer)
);
let decrypted_buffer = match XORCryptor::decrypt_v2(key.as_bytes(), encrypted_buffer) {
Ok(d) => d,
Err(e) => {
println!("Error: {}", e);
return;
}
};
println!(
"Decrypted from vec : {:?}",
String::from_utf8_lossy(&decrypted_buffer)
);
}
§Output
$ cargo run --release --bin main
Compiling xor_cryptor v1.2.3 (/Users/shank/Developer/Projects/XORCryptor-Rust)
Finished `release` profile [optimized] target(s) in 0.42s
Running `target/release/main`
Encrypted: _h�OUrF�bq�h��=������
Decrypted from string : "\u{16}Y�\u{7ac}��|�YCfOe\u{14}\u{11}�yJ �/���\u{6}h\u{15}.sY\u{17}\u{13}�k�9�c��\0�\0�\0�\0\0"
Decrypted from vec : "Hello World !"
§For V1:
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);
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 !"