-3

Is there a mod operator in C# for RSA algorithm? I've been using % as I thought this could be used as mod, but the answers I get for for c and m are not correct, so I've realised % doesn't work.

      double e = 13;
      double d; //decryption

      double de = 7; 

      d = ((de * euiler) + 1) / e;

      double message = 25;
      double c = Pow(message, e) % n;
      double m = Pow(c, d) % n;
Rubikted
  • 43
  • 7
  • 5
    why not just use the `RSACryptoServiceProvider` class that's already there? – Sean T Jan 23 '20 at 14:15
  • 1
    Does [this question](https://stackoverflow.com/questions/1082917/mod-of-negative-number-is-melting-my-brain) help you? `%` in C# is the remainder operation and not modulus. – germi Jan 23 '20 at 14:15
  • We are missing the eulier value, but maybe https://stackoverflow.com/questions/8105613/meaning-of-operation-in-c-sharp-for-the-numeric-type-double helps – Athanasios Kataras Jan 23 '20 at 14:16
  • 1
    Maybe you need the [`Math.IEEERemainder()`](https://learn.microsoft.com/en-us/dotnet/api/system.math.ieeeremainder?view=netframework-4.8) operation? – Matthew Watson Jan 23 '20 at 14:17
  • 2
    I don't think I've ever seen an implementation of RSA using floating-point values. Every implementation I've seen uses `BigInteger` or at least `long`. –  Jan 23 '20 at 14:20
  • 1
    What is `euiler`? (Maybe 0.5772156649?). What is `n`? What is the expected result? What is the actual result? – Matthew Watson Jan 23 '20 at 14:21
  • @Rubikted [According to Wikipedia](https://en.wikipedia.org/wiki/Modulo_operation) `modulo` is ambiguous for negative numbers and different languages follow different rules. Your code doesn't use negative numbers though. It *does* use imprecise floating point operations, specifically operations that are susceptible to scaling errors, not just the common precision errors. Errors like this *weaken* encryption and are used to break encrypted messages. That's why you *shouldn't* try to implement your own cryptography algorithms – Panagiotis Kanavos Jan 23 '20 at 14:26
  • @Rubikted in any case, encryption algorithms don't use floating point numbers. Floating point algorithms take the errors into consideration and go to extra lengths to produce *reproducible* results, even if this means they'll have a predetermined error . If you check eg Intel's MKL library you'll find [articles about reproducibility](https://software.intel.com/en-us/mkl-macos-developer-guide-obtaining-numerically-reproducible-results), or descriptions of the errors introduced by different algorithm implementatios – Panagiotis Kanavos Jan 23 '20 at 14:27
  • For learning purposes this is fine but please don't use your own cryptography algorithms in production. – RoadRunner Jan 23 '20 at 14:30
  • @Rubikted So `m=Pow(c,d)` e.g `m=Pow(2561,4549)` is `Infinity`. And why do you thisnk `c` must be 2490? P.S. I'll upvote this question, because I think type specific questions are pretty usefull. – Krivitskiy Grigoriy Jan 23 '20 at 14:38

2 Answers2

5

The confusion lies in double Type.

MSDN:

The modulus operator (%) computes the remainder after dividing its first operand by its second. All numeric types have predefined modulus operators.

Note the round-off errors associated with the double type.

-4

the % is a remainder. You might want to make a static function that uses the % to make a modulo operation.

  • 2
    [Remainder Operator](https://learn.microsoft.com/en-us/dotnet/csharp/language-reference/operators/arithmetic-operators#remainder-operator-). The Microsoft docs don't call it the modulo operator (because it isn't). – Broots Waymb Jan 23 '20 at 14:17