rsa_encrypt
rsa_encrypt copied to clipboard
Input too large for RSA cipher
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)
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
:
- Generate a random key
K
- Encrypt the payload
P
with symmetric encryption (ie AES) using the generated key:EP = AESEncrypt( P, K )
- Encrypt the key
K
with RSA and public keyPK
:EK = RSAEncrypt( K, PK )
- 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
:
- Extract parts from package:
{ EK, EP, SIZE } = PKG
- Decrypt the key
K
fromEK
with RSA and private key:K = RSADecrypt( EK, PRIVK )
- Decrypt the payload
P
using the keyK
and symmetric encryption:P = AESDecrypt( EP, K )
- Remove Padding:
P = Trim_To( P, SIZE )