3

I need to use some encryption mechanism in one of the project I am working on. I was exploring RSA encryption and wrote some sample programs to learn.

I understand that block size of RSA encryption is 16 bytes. So I gave the string "12345678" as input to below function:

 public static string Encrypt (string input) { var byteConverter = new UnicodeEncoding (); RSACryptoServiceProvider cruptoEngine = new RSACryptoServiceProvider(); byte[] output = cruptoEngine.Encrypt (byteConverter.GetBytes (input), false); return BytesToString (output); //BytesToString () converts the bytes to hex string } 

Now the encrypted string that I get is of 128 bytes (256 hex characters). This string is too big for me. I was kind of hoping that I would get 16 bytes of encrypted data if I give 16 bytes of plain data. Am I doing something wrong? Is this what is supposed to happen? Can I somehow shorten encrypted data?

1 Answer 1

13

You are mistaken. RSA is not a block cipher, so you cannot really talk about the block size of it.

The output of a RSA encryption will have the same length as the RSA modulus. You have not indicated any RSA key in your code, so the runtime will (as far as I recall) use a default key. That key apparently has a 1024 bit modulus, which explains the output length.

You might want to look into AES encryption instead. For many reasons you should normally only use RSA to encrypt a key and then use AES or a similar symmetric cipher algorithm to encrypt your actual text.

AES is a block cipher with block size 16 bytes, so that will (depending on which padding you use and how you transport your initialization vector) encrypt 16 bytes of plain data to 16 bytes of encrypted data.

Sign up to request clarification or add additional context in comments.

1 Comment

Seems like this is not completely true. I used 4096 size for RSA. However, the result of Encrypt call is 550 bytes (whereas the modulus is 512 bytes or so)... Which causes the exception on decryption... I dunno what to do with that.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.