RSA 加密,长度变差 [英] RSA Encryption, getting bad length

查看:30
本文介绍了RSA 加密,长度变差的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

调用以下函数时:

byte[] bytes = rsa.Encrypt(System.Text.UTF8Encoding.UTF8.GetBytes(stringToEncrypt), true);

我现在收到错误:长度错误.

I am now getting the error: bad length.

使用较小的字符串它可以工作,任何想法问题可能是我传递的字符串少于 200 个字符.

With a smaller string it works, any ideas what the problem could be the string I am passing is under 200 characters.

推荐答案

RSA 加密仅适用于少量数据,您可以加密的数据量取决于您使用的密钥的大小,例如1024位RSA密钥,PKCS#1 V1.5填充,最多可以加密117个字节,2048个RSA密钥,可以加密245个字节.

RSA encryption is only mean for small amounts of data, the amount of data you can encrypt is dependent on the size of the key you are using, for example for 1024 bit RSA keys, and PKCS # 1 V1.5 padding, you can encrypt 117 bytes at most, with a 2048 RSA key, you can encrypt 245 bytes.

这是有充分理由的,非对称加密的计算成本很高.如果要加密大量数据,则应使用对称加密.但是如果你想要不可否认性呢?那么你要做的就是同时使用两者.您创建一个对称密钥并使用非对称加密来交换它,然后安全地交换对称密钥来加密您的大量数据.这就是 SSL 和 WS-Secure 在幕后使用的内容.

There's a good reason for this, asymmetric encryption is computationally expensive. If you want to encrypt large amounts of data you should be using symmetric encryption. But what if you want non-repudiation? Well what you then do is use both. You create a symmetric key and exchange it using asymmetric encryption, then that safely exchanged symmetric key to encrypt your large amounts of data. This is what SSL and WS-Secure use underneath the covers.

这篇关于RSA 加密,长度变差的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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