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

Question about encrypt_to_keys with AES #223

Open
developerx-official opened this issue Mar 21, 2023 · 2 comments
Open

Question about encrypt_to_keys with AES #223

developerx-official opened this issue Mar 21, 2023 · 2 comments
Labels

Comments

@developerx-official
Copy link

I'm trying to encrypt data with public keys, using rsa/ecdh, so I was looking at the function encrypt_to_keys, and I noticed that it uses a symmetric algorithm.

I'm using AES256 currently, but does that mean that anyone with the public key passed into the function will be able to decrypt as well?

I tried the openpgp rsa encryption test and had issues running it, so I'm trying using this example as a base.

If I'm going about this wrong, then my apologies I must have misread the documentation. Any help you can offer I would greatly appreciate.

@dignifiedquire
Copy link
Member

PGP in general uses both public/private key based and symmetric encryption. The rough outline is

  • generate a small shared secret
  • encrypt the actual content symmetrically with the shared secret
  • encrypt the shared secret to the recipients using public/private crypto
  • send over the encrypted secret and the encrypted content

So when using pgp you will always have a choice for both pieces. The encrypt_to_keys method will under the hood do roughly the above, inferring the algorithms for public key crypto, based on the passed in keys.

@developerx-official
Copy link
Author

Thanks for the response! I very much appreciate the information :)
Just to make sure I understand, would the following be secure over an open channel?

pub fn encrypt(msg: &str, pubkey_str: &str) -> Result<String, anyhow::Error> {
    let (pubkey, _) = SignedPublicKey::from_string(pubkey_str)?;
    let msg = composed::message::Message::new_literal("none", msg);
    let mut rng = StdRng::from_entropy();
    let new_msg = msg.encrypt_to_keys(
        &mut rng,
        crypto::sym::SymmetricKeyAlgorithm::AES256,
        &[&pubkey],
    )?;
    Ok(new_msg.to_armored_string(None)?)
}

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

No branches or pull requests

2 participants