Skip to content

DES-ECB encryption does not appear to be standard encryption #1

Description

@XiEdAotonG

Encryption results differ from des = "0.8"
This is the code and result of the minimum implementation
Code:

use des::{
    cipher::{BlockEncrypt, KeyInit},
    Des,
};
use ruscrypt::block::des as ruscryptdes;
fn main() {
    let data = "abcdefg";
    let key = "00000000";
    // use ruscrypt: Encrypt with  DES-ECB
    let encrypted = ruscryptdes::encrypt(data, key, "ECB", "hex").unwrap();
    println!("Encrypt with ruscrypt DES-ECB :{:?}", encrypted);
    // use des: Encrypt with DES-ECB
    let encrypted = encrypt(key, data);
    println!("Encrypt with des DES-ECB :{:?}", encrypted.unwrap());
}
fn pkcs7_pad(data: &[u8], block_size: usize) -> Vec<u8> {
    let padding_len = block_size - (data.len() % block_size);
    let mut padded = data.to_vec();
    padded.extend(vec![padding_len as u8; padding_len]);
    padded
}
fn encrypt(key: &str, text: &str) -> Result<String, String> {
    let key: &[u8] = key.as_bytes();
    let data = text.as_bytes();

    let mut encrypted = Vec::new();
    let cipher = Des::new(key.into());
    let padded_data = pkcs7_pad(data, 8);
    for chunk in padded_data.chunks(8) {
        let mut block = des::cipher::generic_array::GenericArray::clone_from_slice(chunk);
        cipher.encrypt_block(&mut block);
        encrypted.extend_from_slice(&block);
    }
    Ok(hex::encode(encrypted))
}

Result:

Encrypt with ruscrypt DES-ECB :"f6f769f973fb3b1c"
Encrypt with des DES-ECB :"162ab71c485c6c5c"

Activity

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions