Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Seeking help for encrypt files #613

Closed
niuhuan opened this issue Jul 16, 2024 · 1 comment
Closed

Seeking help for encrypt files #613

niuhuan opened this issue Jul 16, 2024 · 1 comment

Comments

@niuhuan
Copy link

niuhuan commented Jul 16, 2024

I'm sorry for my poor knowledge in AES, I tried for a long time but couldn't succeed. I need some help. Thank you.

I wan't encrypt and decrypt files, such as directly reading an input stream or encrypting with an output stream.

I don't know how these nonce are converted, I need some hints, here is my code

use aead::stream::{EncryptorBE32, EncryptorLE31, NewStream, StreamBE32};
use aead::{Aead, Nonce};
use aes_gcm::{Aes256Gcm, KeyInit};


fn encrypt_file_name(file_name: &str, password: &str) -> anyhow::Result<String> {
    let key_bytes = md5::compute(password.as_bytes()).0;
    let key_hex = hex::encode(key_bytes);
    let key = key_hex.as_bytes();
    let nonce_slice = &key_bytes[0..12];
    let cipher = Aes256Gcm::new_from_slice(key)?;
    let nonce = Nonce::<Aes256Gcm>::from_slice(nonce_slice);
    ////////
    let enc = cipher
        .encrypt(nonce, file_name.as_bytes())
        .map_err(|e| anyhow::anyhow!("decrypt file name failed: {}", e))?;
    /////////
    ///// the trait `AeadCore` is not implemented for `StreamBE32<AesGcm<Aes256, UInt<UInt<UInt<UInt<UTerm, B1>, B1>, B0>, B0>>>`
    // let mut encryptor = EncryptorBE32::from_aead(cipher, nonce);
    ////////
    let final_name = base64::prelude::BASE64_URL_SAFE.encode(enc.as_slice());
    Ok(final_name)
}
@niuhuan
Copy link
Author

niuhuan commented Jul 16, 2024

fn encrypt_file_name(file_name: &str, password: &str) -> anyhow::Result<String> {
    let key_bytes = md5::compute(password.as_bytes()).0;
    let key_hex = hex::encode(key_bytes);
    let key = key_hex.as_bytes();
    let nonce_slice = &key_bytes[0..7];
    let cipher = Aes256Gcm::new_from_slice(key)?;
    let encryptor = EncryptorBE32::from_aead(cipher, nonce_slice.into());
    let final_vec = encryptor
        .encrypt_last(file_name.as_bytes())
        .map_err(|e| anyhow::anyhow!("encrypt file name failed: {}", e))?;
    let final_name = base64::prelude::BASE64_URL_SAFE.encode(final_vec.as_slice());
    Ok(final_name)
}

fn decrypt_file_name(file_name: &str, password: &str) -> anyhow::Result<String> {
    let key_bytes = md5::compute(password.as_bytes()).0;
    let key_hex = hex::encode(key_bytes);
    let key = key_hex.as_bytes();
    let nonce_slice = &key_bytes[0..7];
    let cipher = Aes256Gcm::new_from_slice(key)?;
    let encryptor = DecryptorBE32::from_aead(cipher, nonce_slice.into());
    let final_vec = base64::prelude::BASE64_URL_SAFE.decode(file_name.as_bytes())?;
    let final_name = encryptor
        .decrypt_last(final_vec.as_slice())
        .map_err(|e| anyhow::anyhow!("decrypt file name failed: {}", e))?;
    Ok(String::from_utf8(final_name)?)
}

@niuhuan niuhuan closed this as completed Jul 16, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant