PointyCastle / pointycastle

Moved into the Bouncy Castle project: https://github.com/bcgit/pc-dart
MIT License
270 stars 76 forks source link

Error while encrypt with RSA #226

Open aBuder opened 4 years ago

aBuder commented 4 years ago

If I encrypt data with RSA key. The Data itself is also an RSA key with same length in XML format

VERBOSE-2:ui_dart_state.cc(157)] Unhandled Exception: Invalid argument(s): Input too large for RSA cipher
hoylen commented 4 years ago

RSA encryption only works on blocks that are smaller or equal to the key size.

If you want to use the RSA algorithm to directly encrypt data, you'll have to break up the plaintext into suitably sized blocks. There are some standard methods for doing this (such as RSAES-PKCS1-v1_5 and RSAES-OAEP) which are supported by Pointy Castle. See the "RSA encryption and decryption" section of the tutorial at https://github.com/PointyCastle/pointycastle/blob/master/tutorials/rsa.md. Though RSAES-PKCS1-v1_5 is not considered very secure these days, and is only there for backward compatibility. But directly encrypting data using RSA is not recommended.

The normal practice is to generate a random session key and encrypt the data using a symmetric algorithm (e.g. AES-256) and then encrypt the small session key using RSA. The receiver will use their private key and the RSA algorithm to decrypt the session key, and then use the decrypted session key to decrypt the data. This greatly improves performance, since the bulk data is encrypted/decrypted using the much faster symmetric algorithm rather than the slower RSA algorithm.

P.S. The Pointy Castle project has moved over to https://github.com/bcgit/pc-dart