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

chacha20poly1305 decode issue #570

Closed
vorcigernix opened this issue Dec 29, 2023 · 2 comments
Closed

chacha20poly1305 decode issue #570

vorcigernix opened this issue Dec 29, 2023 · 2 comments

Comments

@vorcigernix
Copy link

This is probably my code, I copy pasted the decode function from the internet (my cryptography knowledge is basic). Code looks like this:

fn decrypt(obsf: &[u8], key: &[u8]) -> String {
    type NonceSize = <ChaCha20Poly1305 as AeadCore>::NonceSize;
    let cipher = ChaCha20Poly1305::new(GenericArray::from_slice(key));
    let (nonce, ciphertext) = obsf.split_at(NonceSize::to_usize());
    let nonce = GenericArray::from_slice(nonce);
    let plaintext = match cipher.decrypt(nonce, ciphertext) {
        Ok(v) => v,
        Err(e) => {
            println!("Decryption failed: {:?}", e);
            return e.to_string();
        }
    };
    String::from_utf8(plaintext).unwrap()
}

and obsf/key seems to be correct vectors, example:

[12, 140, 198, 139, 82, 101, 80, 102, 13, 101, 156, 164, 50, 27, 12, 209, 228, 160, 3, 135, 141, 230, 200, 213, 230, 106, 209, 189, 41, 82, 146, 42]
[91, 50, 50, 56, 44, 49, 48, 54, 44, 50, 48, 57, 44, 57, 53, 44, 50, 48, 49, 44, 49, 50, 52, 44, 49, 52, 52, 44, 49, 57, 52, 44, 51, 51, 44, 53, 55, 44, 54, 53, 44, 49, 49, 52, 44, 57, 49, 44, 49, 55, 53, 44, 49, 48, 49, 44, 49, 56, 53, 44, 50, 48, 56, 44, 50, 50, 52, 44, 50, 49, 49, 44, 50, 49, 52, 44, 49, 53, 48, 44, 50, 50, 44, 50, 49, 56, 44, 49, 50, 44, 50, 55, 44, 50, 56, 44, 50, 50, 55, 44, 54, 55, 44, 50, 51, 55, 44, 53, 56, 44, 50, 49, 50, 44, 49, 57, 51, 44, 50, 52, 44, 50, 48, 48, 44, 49, 57, 53, 44, 50, 53, 50, 44, 51, 55, 93]

Any idea what could be wrong? Whole code is here: https://github.com/vorcigernix/tokenshare/blob/main/src/app.rs

@vorcigernix
Copy link
Author

So I tried a lot of things, code looks very different, but the issue persists. I am printing input and output and they look exactly the same, but the decode function fails. Any help would be really appreciated.

@newpavlov
Copy link
Member

I am not sure what exactly is your problem. Have you verified that you get the same key/nonce/ciphertext? Try to minimize your problem. It's likely that you have a problem not with cryptography per se, but with something else in your code. It's probably better to ask such questions on the URLO forum.

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

2 participants