RSA: Message Length Limitations
RSA is a public-key cryptography algorithm designed to securely encrypt small amounts of data. Due to its mathematical limitations, the size of data that can be encrypted is restricted by the size of the key's modulus.
RSA is designed to encrypt data that is smaller than the modulus of the public key. For example, with a 1024-bit key, the maximum message size is typically 117 bytes (128 bytes minus 11 bytes used for padding).
If an attempt is made to encrypt data larger than the allowed size, the operation will fail with the following error:
System error: Message too long. (700)
To encrypt larger messages, a hybrid encryption approach is commonly used:
- Generate a symmetric key from a password.
- Use a symmetric algorithm such as AES to encrypt the large message.
- Encrypt the symmetric key using RSA.
Example:
// generate a key and use the key to encrypt the large message
// Note: our components will generate the Key and IV from a given password
aes1.KeyPassword = "mypassword";
aes1.InputMessage = someLargeMessage;
aes1.Encrypt();
// save the key
string myKey = aes1.Key;
// now encrypt the key
rsa1.RecipientCert = new Certificate(cstPEMKeyFile, "C:\\PATH\\TO\\certFile.pem", "certPassword", "*");
rsa1.InputMessage = myKey;
rsa1.Encrypt();
// store the encrypted key
string encryptedKey = rsa1.OutputMessage;
We appreciate your feedback. If you have any questions, comments, or suggestions about this article please contact our support team at support@nsoftware.com.