使用RSA加密的加密数据大小(RSACryptoServiceProvider) [英] Encrypted data size using RSA encryption (RSACryptoServiceProvider)

查看:497
本文介绍了使用RSA加密的加密数据大小(RSACryptoServiceProvider)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我需要在我正在开发的项目之一中使用一些加密机制。我正在探索RSA加密,并写了一些示例程序来学习。

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.

我知道RSA加密的块大小是16字节。所以我给字符串12345678作为以下函数的输入:

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
}



现在我获得的加密字符串是128字节256个十六进制字符)。这个字符串对我来说太大了。我希望如果我给16字节的明文数据,我会得到16字节的加密数据。我做错了什么?这是应该发生什么?我可以以某种方式缩短加密的数据吗?

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?

推荐答案

RSA不是块密码,所以你不能真正谈论它的块大小。

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

RSA加密的输出将具有与RSA模数相同的长度。您没有在代码中指示任何RSA密钥,因此运行时将(根据我的记忆)使用默认密钥。那个键显然有一个1024位的模数,这解释了输出长度。

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.

你可能想要研究AES加密。由于许多原因,您通常只应使用RSA加密密钥,然后使用AES或类似的对称加密算法来加密您的实际文本。

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是一种块密码块大小为16字节,因此(取决于您使用的填充以及如何传输初始化向量)将16字节的明文数据加密为16字节的加密数据。

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.

这篇关于使用RSA加密的加密数据大小(RSACryptoServiceProvider)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆