As already mentioned, your question has one answer, but no. RSA encryption is an algorithm that encrypts messages to a given size, which depends on the size of the key; with a 1024-bit RSA key and RSA as a standard describes it, the maximum size is 117 bytes, no more. It is not possible to encrypt a larger message using only RSA and certain mathematical certainty.
If you really need to handle longer messages, you definitely need to add something else. In this case, please do not try to make any fantasies about your own design with some kind of clever splitting of data into small blocks and the like. This path leads to death. You can create something that seems to compile and run, but that will be consistently weak in some way, like almost all other home cryptography modifications. This is because security cannot be verified: this is not a case of “working” or “not working”.
The well-trodden path of asymmetric encryption goes as follows:
- You choose a random sequence of bytes of some suitable length, for example. 128 bits (this is 16 bytes). Let K. be called
- You encrypt K with the RSA public key; this gives E.
- You encrypt the message using K using the symmetric encryption algorithm (
"AES/CBC/PKCS5Padding" ). Since this is a one-time key, you can use all IV zeros. This gives a bunch of bytes, let's call it F. - The encrypted message is a concatenation of E and F.
Decryption continues in the reverse order: the RSA private key is used to recover K from E, then K is used to decrypt F into the original message. The key K is never stored anywhere, and each time a new key K is generated (even if you encrypt the same message twice). This is important, do not change it if you do not understand what you are doing (and if so, then you already know this).
Given that you are reporting your problem, you need to do something else besides "just RSA." The procedure described above deals with the best “something else” that you could handle from a security point of view.
Assembling some cryptographic elements in such a protocol is a process fraught with traps, so you may be lucky using an already defined format and a support library. Two common asymmetric encryption formats are CMS and OpenPGP . A library that supports both has a good reputation - Bouncy Castle .
source share