#TripleDES encryption in rust

3 messages · Page 1 of 1 (latest)

frank condor
#

Hello, I've got a project where i have to implement the TripleDES encryption algoritm. I have found the following crate: https://github.com/RustCrypto/block-ciphers/tree/master/des, and have started implementing it, however I'm not completly sure how to use it, the docs aren't really to much help. I've got this code, that compiles, yet throws a runtime error:

let message = "Test string".to_string();
let slice_key = [0 as u8; 24];
let key: Key<TdesEee3> = GenericArray::clone_from_slice(&slice_key);
let encryptor = TdesEee3::new(&key);

let tranformed_message = message.as_bytes();

let mut block: Block<TdesEee3> = GenericArray::clone_from_slice(tranformed_message);

encryptor.encrypt_block(&mut block);

Here is the error it throws:
thread '<unnamed>' panicked at 'Slice must be the same length as the array'

Any help / suggestions are much appreciated thank you!

hollow bay
#

If the project is an educational assignment, it may not be advisable to use a library for this. If it's for a hobby: remember that triple DES is not secure. All that said, on which line is it panicking? I could see slice_key being the wrong size (24), clone_from_slide causing problems, or block being the wrong size.

frank condor
# hollow bay If the project is an educational assignment, it may not be advisable to use a li...

Hello, thank you for your response, yes this is for a assignment, however the point of it is tcp sockets and a part of it is to use a library to encrypt a message to TripleDES, so there is no concern about using the lib or the security of the algorithm. Anyways i have reduced the code a bit:

let message = "Test"
let out_string: String;

match TdesEee3::new_from_slice(&[1u8; 24]) {
    Ok(alg) => {
        let mut block =    GenericArray::clone_from_slice(message.as_bytes());

        alg.encrypt_block(&mut block);

        out_string = String::from_utf8_lossy(block.as_slice()).to_string();
    }
    Err(err) => out_string = format!("Napaka pri kriptiranju: {}", err.to_string()),
}

It appears that the error is happening at this line:
GenericArray::clone_from_slice(message.split_at(23).0.as_bytes());, becouse if i change it to Default::default() like the lib does in the benchmarks it works.