0

The formula for generating decryption key for RSA algorithm is ed = 1 mod T where T is generated using the formula (p-1)(q-1). p and q are two non identical prime number. e is the Encryption Key. So as per the formula if I like to implement the ed = 1 mod T in C program the code block should be

 d = (1%T)/e;

However, I found most of the coding websites(coding website) use d*e = 1 + k * T to generate the decryption key.

I can not understand from where they get k?

3
  • 2
    I’m voting to close this question because it's not really about programming. If you need help understanding the basics of RSA, you could perhaps start by learning about modular arithmetic. Commented Mar 30, 2022 at 20:24
  • @r3mainer: Finding multiplicative inverses is an algorithm problem, part of programming. Commented Mar 30, 2022 at 20:26
  • 4
    ed = 1 modulo T indicates that e and d are multiplicative inverses modulo T; multiplying by d inverts a multiplication by e. E.g., if we have x and multiply it by e, then multiplying by d restores x modulo T; xed = x modulo T. An algorithm for finding modular multiplicative inverses is the extended Euclidean algorithm. Commented Mar 30, 2022 at 20:29

1 Answer 1

2

The modular multiplicative inverse can be found with the extended Euclidean algorithm.

Here is a simple demonstration implementation. Cryptographic implementations generally needed extended-precision arithmetic.

#include <stdio.h>
#include <stdlib.h>


//  Return the multiplicative inverse of e modulo t.
static int invert(int e, int t)
{
    /*  We will work with equations in the form:

            x = d*e + s*t

        where e and t are fixed, and x, d, and s change as we go along.

        Initially, we know these two equations:
       
            t = 0*e + 1*t.
            e = 1*e + 0*t.

        We will use the Euclidean algorithm to reduce the left side to the
        greatest common divisor of t and e.  If they are relatively prime,
        this eventually produces an equation with 1 on the left side, giving
        us:

            1 = d*e + s*t

        and then d is the multiplicative inverse of e since d*e is congruent to
        1 modulo t.
    */

    //  Now we start with our first values of x, d, and s:
    int x0 = t, d0 = 0, s0 = 1;
    int x1 = e, d1 = 1, s1 = 0;

    //  Then we iteratively reduce the equations.
    while (1)
    {
        /*  Find the largest q such that we can subtract q*x1 from x0 without
            getting a negative result.
        */
        int q = x0/x1;

        /*  Subtract the equation x1 = d1*e + s1*t from the equation
            x0 = d0*e + s0*t.
        */
        int x2 = x0 - q*x1;
        int d2 = d0 - q*d1;
        int s2 = s0 - q*s1;

        /*  If we found the inverse, return it.

            We could return d2 directly; it is mathematically correct.
            However, if it is negative, the positive equivalent might be
            preferred, so we return that.
        */
        if (x2 == 1)
            return d2 < 0 ? d2+t : d2;
        
        if (x2 == 0)
        {
            fprintf(stderr, "Error, %d is not relatively prime to %d.\n", e, t);
            exit(EXIT_FAILURE);
        }

        /*  Slide equation 1 to equation 0 and equation 2 to equation 1 so we
            can work on a new one.
        */
        x0 = x1; x1 = x2;
        d0 = d1; d1 = d2;
        s0 = s1; s1 = s2;
    }
}


int main(void)
{
    int e = 3, t = 3127, d = invert(e, t);
    printf("The multiplicative inverse of %d modulo %d is %d.\n", e, t, d);
}

Output:

The multiplicative inverse of 3 modulo 3127 is 2085.
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.