if I encrypt 3000 messages with 2048 bit keys it takes me about 2 seconds while if I decrypt the same messages I need about 15 seconds.
Is it normal for decoding to take so long?
Yes. Decryption involves inverting the encryption operation, which is much more computationally expensive. This is because the private key used for decryption is a large number, and inverting the encryption operation requires performing modular exponentiation to this modulus.
The difference in speed between encryption and decryption is more pronounced for larger key sizes. For example, a 2048-bit RSA key may be 100 times faster to encrypt with than to decrypt.
When encrypting, generate a large random key: Crypto.GenerateRandomBytes(32)
. Use that to AES-256 encrypt all of your messages. Use RSA to encrypt your random key. You’ll get the best of both worlds: the security of RSA, with the speed of AES. Plus, AES does not have the message length restrictions that RSA does.
I wrote a series about different encryption algorithms in the xDev magazine and performed the computations from scratch in Xojo (Feistel Network). If the algorithm uses the Feistel Network then encryption and decryption should take the exact same amount of time.
Although the encryption and decryption time is the same, I am not sure what Xojo does in the background to open, read, write, and close files, which can take a significant amount of time.
If the algorithm is running in memory, then it will be faster when compared with files.