AbdelbakiBoukerche / rsa_encrypt

rsa encrypt package
MIT License
17 stars 16 forks source link

Input too large for RSA cipher #4

Open chandaniBhalala opened 4 years ago

chandaniBhalala commented 4 years ago

Getting this exception "Input too large for RSA cipher" when encrypting string with more than 64 characters. Is there any limit of number of characters for encryption?

flutter doctor output Doctor summary (to see all details, run flutter doctor -v): [✓] Flutter (Channel stable, 1.20.4, on Mac OS X 10.15.6 19G2021, locale en-IN)

[✓] Android toolchain - develop for Android devices (Android SDK version 30.0.2) [✓] Xcode - develop for iOS and macOS (Xcode 11.7) [✓] Android Studio (version 4.0) [✓] VS Code (version 1.49.2) [✓] Connected device (3 available)

mnesarco commented 3 years ago

RSA can only encrypt payloads smaller than the key size, so the usual way to use that is:

Given a payload P and a public key PK:

  1. Generate a random key K
  2. Encrypt the payload P with symmetric encryption (ie AES) using the generated key: EP = AESEncrypt( P, K )
  3. Encrypt the key K with RSA and public key PK: EK = RSAEncrypt( K, PK )
  4. Build a package PKG with encrypted key, encrypted payload and size: PKG = { EK, EP, size_of(P) }

Then to decrypt do the opposite:

Given a package PKG and a private key PRIVK:

  1. Extract parts from package: { EK, EP, SIZE } = PKG
  2. Decrypt the key K from EK with RSA and private key: K = RSADecrypt( EK, PRIVK )
  3. Decrypt the payload P using the key K and symmetric encryption: P = AESDecrypt( EP, K )
  4. Remove Padding: P = Trim_To( P, SIZE )