2
votes

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

2
Why do you want to encrypt a small object? It may help get you a better answer if we have a better idea of you want to accomplish overallDan McClain

2 Answers

1
votes

You could use Blowfish, it has a block size of 64 bits / 8 bytes. http://www.hotpixel.net/software.html#blowfishnet

0
votes

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?