Reputation: 471
There are many topics on RSA Encryption and Decryption using BouncyCastle, however I'm encountering some unexpected behaviour.
I'm attempting to encrypt a 64 byte data blocking using a private key of size 64 bytes
I compute the RSA Encryption as followings:
public byte[] Encrypt(byte[] data, AsymmetricKeyParameter key)
{
var engine = new RsaEngine();
engine.Init(true, key);
var blockSize = engine.GetInputBlockSize();
return engine.ProcessBlock(data, 0, blockSize );
}
I compute the decryption using a public key as follows
public byte[] Decrypt(byte[] data, AsymmetricKeyParameter key)
{
var engine = new RsaEngine();
engine.Init(false, key);
var blockSize = engine.GetInputBlockSize();
return engine.ProcessBlock(data, 0, blockSize );
}
What I'm finding is that when I encrypt my 64 data using a 64 byte Private Key I get back a 64 byte encrypted dataBlock.
However when I decode the 64 byte array using a 64 byte public key I get back a data block of size 62 bytes. What is stranger is that the values contained in the 62 byte array equal the values of the 64 byte original array (pre encryption) however the decoded array is missing the first index of the original data and the final index.
I've tried using different keys and different sets of data and the same thing happens.
I must be doing something wrong, but I can't see it.
Cheers.
Upvotes: 4
Views: 8185
Reputation: 108790
You got the essential concepts wrong.
You should take a step back, and describe what you actually want to achieve, so we can find a scheme that fits your needs. Only after that you should worry about implementing it.
Upvotes: 2