1

This question is quite related to this one, but I am looking for more details.

I am writing a very basic implementation to demonstrate how RSA works. My current implementation generates key pairs using 64-bit integers.

I am looking for the proper method to encrypt a string. I guess the bytes are packed together (32-bits) (smaller than the modulus value), then each packet is encrypted.

I would like to know if each packet is encrypted independently or if they are chained together.

Maarten Bodewes
  • 88,868
  • 12
  • 146
  • 304
nowox
  • 111
  • 3
  • Why don't you use GNU GMP library? Most of the functions that you write can be found on that library. modpow, mpz_probab_prime_p, extgcd, etc.. – kelalaka Dec 27 '19 at 23:22
  • Because I would like to remain very simple. – nowox Dec 27 '19 at 23:23
  • Notes: A) the C code shown is limited to 32-bit public modulus $n$, dues to the 64-bit limitation of `res * a` in `modpow`. B) 512-bit RSA was [unsafe 20 years ago](https://crypto.stackexchange.com/a/1982/555). C) The [RSA paper](https://people.csail.mit.edu/rivest/Rsapaper.pdf) shows a method to directly encipher text, but since it is deterministic, it allows a guess to be verified, which is a disaster in many common cases (names of a guy on the class roll..). D) Text encryption using RSA should RSA-encipher a random symmetric key used to encipher the text. – fgrieu Dec 27 '19 at 23:56
  • Could you have a look at [the following answer](https://crypto.stackexchange.com/a/32922/1172) about using RSA as a block cipher and see if it answers your question? Basically chaining is not performed, not even for textbook RSA. Note that asking for a code review is not on topic for this site. As there is no specific question about the code in the post either, I cannot migrate to SO. I'll remove the code from the question (you can still see or copy it if you look at the edits). – Maarten Bodewes Dec 28 '19 at 09:54
  • @Maarten-reinstateMonica Indeed the answer you mentioned helped me to understand that what I am looking for is not related to RSA but to the "Block cipher mode of operation" and therefore to ECB, CBC, CFB and OFB... – nowox Dec 28 '19 at 10:00
  • Right. And nobody performs any of them in real life protocols (I hope), you'd use a hybrid cryptosystem. But for RSA you could just encrypt and concatenate (closest to ECB mode) using integers encoded to a static size (the byte size of the modulus) if you want to encrypt larger messages for practice purposes. – Maarten Bodewes Dec 28 '19 at 10:05
  • The question _"How does RSA encrypt a message?"_ is akin to _how does wheel moves a load?_: it has many answers. In _"a very basic implementation (made) to demonstrate how RSA works"_, without pretending to be an illustration of what's made in actual use of RSA, what about the method in [section VIII _A Small Example_ of the original RSA paper](https://people.csail.mit.edu/rivest/Rsapaper.pdf#page=10)? There's no chaining (and chaining is uncommon). – fgrieu Dec 28 '19 at 15:26

0 Answers0