patterncsharpMinor
BouncyCastle RSA Implementation
Viewed 0 times
implementationbouncycastlersa
Problem
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();
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();
}
}Solution
Style
In the
_engine.Init(!key.IsPrivate, key);
will be obvious.
Otherwise your code seems to look good.
Regarding your
The RSA key generation requires prime numbers. However, it's impossible to generate absolute prime numbers. Like any other crypto libraries, BC uses probable prime numbers. The certainty indicate how certain you want the number to be prime. Anything above 80 will slow down key generation considerably.
- Almost all the naming of the parameters and methods are good.
RsaProcessor()sounds like a noun. Based on the naming guidlines you should use verbs or verb phrases to name your methods. Maybe a simple name likeProcess()would be sufficient here.
In the
RsaProcessor() method I would suggest to introduce as Boolean forEncryption. In this way the meaning of this line_engine.Init(!key.IsPrivate, key);
will be obvious.
private byte[] Process(byte[] data, int offset, int length, AsymmetricKeyParameter key)
{
Boolean forEncryption = !key.IsPrivate;
_engine.Init(forEncryption , key);
var blockSize = _engine.GetInputBlockSize();
var result = new List();
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();
}Otherwise your code seems to look good.
Regarding your
Unsure about the certinaty parameter See https://stackoverflow.com/a/3087161/2655508 The RSA key generation requires prime numbers. However, it's impossible to generate absolute prime numbers. Like any other crypto libraries, BC uses probable prime numbers. The certainty indicate how certain you want the number to be prime. Anything above 80 will slow down key generation considerably.
Code Snippets
private byte[] Process(byte[] data, int offset, int length, AsymmetricKeyParameter key)
{
Boolean forEncryption = !key.IsPrivate;
_engine.Init(forEncryption , 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();
}Context
StackExchange Code Review Q#61860, answer score: 3
Revisions (0)
No revisions yet.