Search code examples
c#encryptionrsaprogress

C# - Get RSA encryption progress


How can I get RSA encryption progress? I want to display it in progress bar. Thanks for help.

    using System.Security.Cryptography; 
RSACryptoServiceProvider RSA = new RSACryptoServiceProvider(4096);

//I want to get percentage process of this function, because it takes a lot of time.
byte[] encrypted = RSA.Encrypt(Encoding.Default.GetBytes("some text", true));

Solution

  • Using a 16k-bit key I can do 1000 Encrypt calls in 2893ms (so ~3ms per), this doesn't seem slow enough to need a progress bar.

    Decrypt, on the other hand, was 458,554ms for 1000 calls (so ~460ms per). But half a second still doesn't really seem to warrant a progress bar. And not very many people have 16k-bit RSA keys (since they take hours to generate)

    Some other quick numbers (time per 1000 calls, in ms):

    Keysize | Encrypt | Decrypt
    --------|---------|--------
      16384 |    2893 | 458554
       4096 |     216 |  14636
       3584 |     182 |  13259
       3072 |     149 |   8370
       2560 |     127 |   4680
       2048 |      94 |   2083
       1536 |      86 |   1236
       1024 |      71 |    473
        512 |      64 |    150
    

    So the best you could do is the fake progress bar of sending text messages... you do some heuristics for how long you think it will take, and artifically move the progress bar, then do an "eep" moment at 80% when it isn't actually done.

    Since the guidance is to only use asymmetric operations to bootstrap symmetric operations (as already suggested in the comments), you can probably get away with using a progress bar on the symmetric operations only, if they're being slow enough for you to warrant it. If you want to throw in a fudge factor for the asymmetric operation, it wouldn't hurt.