views:

201

answers:

1

Hi this is the same question, that was asked two years ago: Java/JCE: Decrypting “long” message encrypted with RSA

I had a large byte array and rsa keypair, initiated by value 1024. Using rsa encryption and the specified size of the key is strong requirement, I can't change it. So I can't use symmetric encryption with asymetric encryption symmetric key. I can't use any other keys. I had a byte array and need ciphered byte array to be returned. I wonder if there is any ready tool, that can manage with this problem?

Sorry for such an amateurish question, but I really need a help.

+5  A: 

As stated, your question has a single answer, and that's "no". RSA encryption is an algorithm which encrypts messages up to a given size, which depends on the key size; with a 1024-bit RSA key, and RSA as the standard describes it, the maximum size is 117 bytes, no more. There is no way to encrypt a larger message with RSA alone, and that's a definite, mathematical certainty.

If you really need to process longer messages, then you necessarily have to add something else. In that case, please, please, do not try to do anything fancy of your own devising with some oh-so-clever splitting of data into small blocks and the like. That path leads to doom. You might produce something which appears to compile and run, but which will be invariably weak in some way, like almost every other home-made variation on cryptography. That's because security cannot be tested: it is not a case of "works" or "does not work".

The well-trodden path of asymmetric encryption goes thus:

  1. You select a random sequence of bytes of some appropriate length, e.g. 128 bits (that's 16 bytes). Let's call it K.
  2. You encrypt K with the RSA public key; this yields E.
  3. You encrypt the message with K using a symmetric encryption algorithm ("AES/CBC/PKCS5Padding"). Since this is a one-shot key, you can use an all-zeros IV. This yields a bunch of bytes, let's call it F.
  4. The encrypted message is then the concatenation of E and F.

Decryption proceeds 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 a new key K is generated every time (even if you encrypt the same message twice). That's important, do not change that unless you understand what you are doing (and if you do, then you already know that).

Given what you state about your problem, you have to do something else than "just RSA". The procedure I describe above is about the best "something else" that you could come up with, security-wise.

Assembling some cryptographic elements into such a protocol is a process fraught with pitfalls so you may have better luck using an already defined format and support library. Two common formats for asymmetric encryption are CMS and OpenPGP. A library which supports both and has good reputation is Bouncy Castle.

Thomas Pornin
Good answer. Nothing more to add.
St.Shadow
Its a pity, that I can't change the specification.
Denis
It might just be a matter of how you *interpret* the specification. If it just says "The data should be encrypted using a 1024 bit RSA key", then Thomas' answer is *the* standard way to do this, so I would argue that it is completely in agreement with the spec.
caf
+1 for Bouncy Castle!
CoolBeans
Maybe somebody can help me reading password protected private key with java. Here is link to my question:http://stackoverflow.com/questions/2654949/how-to-read-a-password-encrypted-key-with-java
Denis
-1. It is well known that this encryption mode is not secure. It is susceptible to a chosen ciphertext attack. Either use OAEP padding rather than the old PKCS#1 padding or better use some authentication.
Accipitridae
Also Bouncy Castle isn't that great either. It still has significant unpatched flaws in it implementation of RSA.
Accipitridae
If it is "well known" that PKCS#1 v1.5 padding is "insecure" and "susceptible to a chosen ciphertext attack", then maybe you could provide a reference supporting that assertion ?
Thomas Pornin
PKCS#1 version 2.0.
Accipitridae
Thomas Pornin