HiveBrain v1.2.0
Get Started
← Back to all entries
patterncsharpMinor

RSA calculating e in C#

Submitted by: @import:stackexchange-codereview··
0
Viewed 0 times
calculatingrsastackoverflow

Problem

I would like the have a second opinion on my code I've written to calculate e

e is an integer that you wherefore the following counts gcd(φ(n),e) = 1 with 1 < e < φ(n)

public int calce()
{
    do
    {
        e = priemgen.newKey(1, euler);
    } while (GCD(euler, e) != 1);  

    return e;
}

private int GCD(int value1, int value2)
{
    while (value1 != 0 && value2 != 0)
    {
        if (value1 > value2)
        {
            value1 %= value2;
        }
        else
            value2 %= value1;
    }

    return Math.Max(value1, value2);
}

private int NewPrime(int lowerbound, int upperbound)
{
    return priemgen.newKey(lowerbound, upperbound);
}

Solution

There is not a huge amount of code to review here, just a few things:

-
You have a method NewPrime which I assume provides a prime number between the two bounds provided. Yet you do not use it and call priemgen.newKey directly in calce. You should either get rid of the wrapper method or use it consistently.

-
Braces are used inconsistently:

else
    value2 %= value1;


Should be same as the if block:

else
{
    value2 %= value1;
}


-
The naming conventions are inconsistent. Standard naming convention for C# for methods and fields is PascalCase and local variables and parameters are camelCase:

  • calce -> CalculateE



  • newKey on priemgen should be NewKey



  • priemgen - I assume it's a field so should be PrimGenerator



  • lowerbound -> lowerBound



  • upperbound -> upperBound



  • GCD -> Gcd



-
If priemgen is what it sounds like then the name for nextKey should be NextPrime.

-
What does priemgen.newKey return if there is no prime within the given bounds?

Code Snippets

else
    value2 %= value1;
else
{
    value2 %= value1;
}

Context

StackExchange Code Review Q#36726, answer score: 5

Revisions (0)

No revisions yet.