.Net 简单 RSA 加密
我正在尝试加密一些简单的东西,比如 int 或 long。 我发现的最简单的方法如下:
int num = 2;
RSACryptoServiceProvider rsa = new RSACryptoServiceProvider();
byte[] numBytes = BitConverter.GetBytes(num);
byte[] encryptedBytes = rsa.Encrypt(numBytes, true);
问题是,加密的字节长为 128 字节。 如何加密数据(我以后仍可以解密),其中加密结果的字节长度与输入的字节长度相同?
我知道如果我定义 RSACryptoServiceProvider(512) 或 RSACryptoServiceProvider(384),我可以缩短结果,但这已经是最低限度了。
我需要分别输入 4 或 8 个字节,以及输出 4 或 8 个字节,
谢谢!
*** 说明:
我想加密一些小内容(即 4 或 8 字节)并获得类似大小的结果。 在 C# 中执行此操作的最简单方法是什么,同时仍然使用一些键(使用内置库)而不是一些 mod 和 shift 操作
I'm trying to encrypt something simple, like int or long. Simplest way I found looks like:
int num = 2;
RSACryptoServiceProvider rsa = new RSACryptoServiceProvider();
byte[] numBytes = BitConverter.GetBytes(num);
byte[] encryptedBytes = rsa.Encrypt(numBytes, true);
Problem is, the encryptedBytes is 128 bytes long. How can I encrypt data (which I could still later decrypt) where the encrypted result is the same length in bytes as the input?
I know I can shorten the result if I define RSACryptoServiceProvider(512) or RSACryptoServiceProvider(384), but that's as low as it goes.
I need 4 or 8 bytes going in, and 4 or 8 bytes coming out (respectively)
Thanks!
*** Clarification:
I want to encrypt something small (ie 4 or 8 bytes) and obtain a result of a similar size. What's the simplest way to do it in C# while still using some key (with the built in libraries) rather than some mod and shift operations
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
您可以使用 Blowfish,它的块大小为 64 位/8 字节。
http://www.hotpixel.net/software.html#blowfishnet
You could use Blowfish, it has a block size of 64 bits / 8 bytes.
http://www.hotpixel.net/software.html#blowfishnet
那是不可能的。 RSA 是一种所谓的分组密码(具有可变的分组长度)。
最知名的流密码可能是 RC4,但 .NET 没有内置实现。 像 jonelf 建议的那样,块大小较小的算法可能是最简单的。 如果字节数不重要,我会使用 AES(块大小 128 位)。
您最初考虑非对称算法有什么原因吗?
That's not possible. RSA is a so-called block cipher (with variable block length).
The most well-known stream cipher is probably RC4, but .NET doesn't come with a built-in implementation. An algorithm with a small block size, like jonelf suggested, would probably be easiest. If the number of bytes isn't critical, I would use AES (block size 128 bits).
Is there a reason why you were originally looking at an asymmetric algorithm?