5
\$\begingroup\$

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();
    }
}
\$\endgroup\$
  • \$\begingroup\$ I don't know much about cryptography, but is there a reason for not using RSACryptoServiceProvider? \$\endgroup\$ – mjolka Sep 3 '14 at 23:19
  • \$\begingroup\$ Not exactly but BC provides a lot of extra features and is highly customizable \$\endgroup\$ – Abdullah Saleem Sep 4 '14 at 16:54
  • \$\begingroup\$ PKCS#1v1.5 padding is not secure. \$\endgroup\$ – CodesInChaos Sep 22 '14 at 14:16
3
\$\begingroup\$

Style

  • 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 like Process() 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<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();
}

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.

\$\endgroup\$

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.