3
votes

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

1 Answers

13
votes

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.