This is my implementation of 1024bit(can be changed) RSA. Is there anything I'm doing wrong?
public class Rsa:IEncryption
{
public AsymmetricCipherKeyPair Keys { get;private set; }
private readonly Pkcs1Encoding _engine;
public Rsa()
{
Keys = GenerateKeys();
_engine = new Pkcs1Encoding(new RsaEngine());
}
public byte[] Encrypt(byte[] buffer)
{
return Encrypt(buffer, 0, buffer.Length);
}
public byte[] Decrypt(byte[] buffer)
{
return Decrypt(buffer, 0, buffer.Length);
}
public byte[] Encrypt(byte[] buffer, int offSet, int length)
{
return RsaProcessor(buffer, offSet, length, Keys.Public);
}
public byte[] Decrypt(byte[] buffer, int offSet, int length)
{
return RsaProcessor(buffer, offSet, length,Keys.Private);
}
private byte[] RsaProcessor(byte[] data,int offset,int length, AsymmetricKeyParameter key)
{
_engine.Init(!key.IsPrivate, key);
var blockSize = _engine.GetInputBlockSize();
var result = new List<byte>();
for (var i = offset; i < offset+length; i += blockSize)
{
var currentSize = Math.Min(blockSize, offset + length - i);
result.AddRange(_engine.ProcessBlock(data, i, currentSize));
}
return result.ToArray();
}
public static AsymmetricCipherKeyPair GenerateKeys()
{
var rsaKeyParams = new RsaKeyGenerationParameters(BigInteger.ProbablePrime(512, new Random()),
new SecureRandom(), 1024, 25); //Unsure about the certinaty parameter
var keyGen = new RsaKeyPairGenerator();
keyGen.Init(rsaKeyParams);
return keyGen.GenerateKeyPair();
}
}