Encrypted data size using RSA encryption (RSACryptoServiceProvider)

冷暖自知 提交于 2019-12-22 10:36:56

问题


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:


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.



来源:https://stackoverflow.com/questions/685470/encrypted-data-size-using-rsa-encryption-rsacryptoserviceprovider

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!