-2

I'm trying to encrypt a short text using openssl but I don't know how to proceed. More specifically: I found a code that seems to work but are instructions that have to be executed from terminal. Yes, I know, there exist the system() function but I'd like to avoid it because it requires the file in the folder, and that means that I have first create it and then encrypt. In less word: is there some c code function that substitute this openssl rsautl -encrypt -inkey public_key.pem -pubin -in -out and this openssl rsautl -decrypt -inkey private_key.pem -in -out ??? NOTE: RSA_public_encrypt() RSA_public_decrypt() cannot be used because they requires RSA structure, ie the key pair, but in my scenario I only have public key

  • 1
    Actually, this is how RSA and any other *asymmetric* cryptography works: Alice encrypts with Bob's public key and Bob is the only one who can decrypt with his private key - or the other way round, Bob encrypts with his private key, if now Alice can decrypt the message with Bob's public key, she knows for sure that the message originated from Bob as only he could have done the proper *en*cryption (--> digital signature). – Aconcagua Jun 15 '18 at 08:53
  • https://stackoverflow.com/questions/108518/rsa-encryption-library-for-c for the library or just consult your favourite search engine with "rsa library"... – Aconcagua Jun 15 '18 at 09:12
  • @Aconcagua ok, but this is not helpful... how can I encypt my message having _only_ Bob's public key? and without first sending IVs, nonces, or encypted symmetric key? Additionally the library that you provided is for C++ but I'm developping my application in C, but the most important thing is that it does not use padding and for me it is totally useless (I'v been thought, and theory confirmed, that plain RSA is vulnerable to several kinds of attack) – mariomura Jun 15 '18 at 09:32
  • Well, that's the entire idea behind asymmetric cryptography: Imagine the message is 7, public key is 1, en-/decrpytion is done by addition (so some kind of Cesar), so I send 8. I don't have to know the private key (-1) for this... – Aconcagua Jun 15 '18 at 09:40
  • You might like [this one](https://stackoverflow.com/questions/14026768/library-for-rsa-implementation-in-pure-c) better then, or simply one of [these](https://duckduckgo.com/html?q=rsa%20library%20c). – Aconcagua Jun 15 '18 at 09:47
  • The openssl RSA structure is usable for both private and public keys, i.e. you can store a public key in it without the private key. – Matt Caswell Jun 15 '18 at 10:02
  • Please read the documentation first: https://linux.die.net/man/3/rsa – Antti Haapala -- Слава Україні Jun 15 '18 at 10:10
  • Now I try to explain myself as clear as I can: I'm looking for an API that has the followning format RSA_PUBK_ENCRYPT(TOENCRYPT, PUBK) Like I said RSA_public_encrypt() RSA_public_decrypt() are not suitable because they need both keys Can you provide me such a function? – mariomura Jun 15 '18 at 10:43
  • @Aconcagua I got lost at the point where you said Bob encrypts with his private key. Surely he needs to encrypt with Alice's public key? OTOH, Bob can *sign* with his private key and Alice (or anyone) can verify the signature with Bob's public key. – Ian Abbott Jun 15 '18 at 12:10
  • @IanAbbott Matter is easy: *Anyone* can use the public key, only one single person the private key. Purpose of encrpytion is that no one else as the recipient can read. So anyone uses the public key of the recipient to encode, and the recipient will be the only one able to decode. Signature is inverse: If I can decode with the public key of the sender, the message must come from the latter as he/she was the only one being able to encode with the private key. All provided that the private key secret has not been compromised, of course... – Aconcagua Jun 15 '18 at 13:48
  • ^^^^ Eve can read the signatures and so knows who sent the messages, but cannot read the message bodies. – Martin James Jun 15 '18 at 13:53
  • @Aconcagua Yes, that's what I thought: it was just an unintentional misunderstanding of your original comment. – Ian Abbott Jun 15 '18 at 15:58

1 Answers1

1

Even though RSA *rsa structure is capable of holding key pair, its not mandatory to have private and public key together to use RSA_public_encrypt()

@Aconcagua has already given good explanation of asymmetric cryptography that would explain practically you would never have to have both keys with you, the one intending to encrypt holds public key and the one who want to decrypt will have corresponding private key

So you only need to load you public key in RSA *rsa and use it with RSA_public_encrypt()

Pras
  • 4,047
  • 10
  • 20
  • To make it clearer (citing from RSA documentation): > In public keys, the private exponent and the related secret values are NULL – Aconcagua Jun 15 '18 at 13:52
  • Now I try to explain this again. Suppose you're the client. You want to encrypt a text file (in my case, symmetric session key). You only have (and by only I mean only) the public server key. Is there a function that I can use for this purpose? RSA_public_encrypt() is not good because i do not have RSA structure. Note this more thing: on the web there are some command that you can give to prompt and do what I'm trying to do by code – mariomura Jun 15 '18 at 14:17
  • @mariomura How do you want to use *symmetric* keys with a (per definition!) **a**symmetric algorithm? Or do you actually intend to implement [hybrid encryption](https://en.wikipedia.org/wiki/Hybrid_cryptosystem), where you will *exchange* the key for further *symmetrically* encrypted data transfer *via* initial RSA? In any case, the RSA structure *definition* comes with the header file; you'll then create an instance of on [your own](https://www.openssl.org/docs/manmaster/man3/RSA_new.html), fill the members appropriately and then pass it on to the function... – Aconcagua Jun 18 '18 at 08:40