0

Possible Duplicate:
RSA Encryption, getting bad length

I am trying to encrypt a large string using RSA. .NET's default implementation lets me encrypt easily small strings, but when I pass large one it throws a CryptographyException: Bad length.

I would like to overcome this, but the problem is that I need to pass the encypted string to another application which I cannot modify (because it's not mine ;-)

So is there an RSA implementation which I can use to load X509 Public Key and then encrypt large strings?

Thank you

Community
  • 1
  • 1
goodolddays
  • 2,595
  • 4
  • 34
  • 51
  • Typically, you would use RSA to decrypt an AES key. AES will then allow you to encrypt the large block of data. – Davin Tryon Jul 19 '12 at 13:53
  • @Shai: Yeah I read that question before... but the problem is that I am looking for a workaround – goodolddays Jul 19 '12 at 14:07
  • @hkproj: does the program you're passing them to need to decrypt them, or just store them? You should add some more details, since there might be a simpler way to handle this – Wug Jul 19 '12 at 14:37

2 Answers2

3

You will need to contrive something in order to accomplish this, perhaps by splitting it into blocks and encrypting each block.

It's not working because the plaintext length in RSA encryption is limited by the length of the key, i.e. it is impossible to encrypt a piece of data larger than the key used to encrypt it. Generally, a one-off exchange of a symmetric key is performed with RSA, and an agreed-upon symmetric algorithm is used from there.

Also, RSA is (compared to symmetric algorithms) very computationally expensive and breaking large data into many dozens (perhaps hundreds?) of small blocks would suffer terrible performance problems, you'd probably only be able to encrypt a few megabytes per second.

Strongly consider an alternate approach.

Edit: Also, consult the documentation of the program that must accept these encrypted strings, because it doesn't help you to use some nonstandard way of encrypting them if the program will be unable to decrypt them.

Wug
  • 12,956
  • 4
  • 34
  • 54
1

Look at Bouncy Castle cryptography library: http://www.bouncycastle.org/csharp/index.html

I think this library is more flexible then standard from .Net. I used in scenarios when it can do things that standard can't.

Also as a workaround you can split large strings into smaller with length corresponding to encryption key and then use standard .Net classes.

And as final. Are you sure that it's not a problem with padding? When its padding you should fill some part of string to get right length.

Regfor
  • 8,515
  • 1
  • 38
  • 51