Algorithm used to find 'd'

Algorithm used to find 'd'

The variable 'd' must obey the equation
d.e % phi = 1
This can be re-written as :
d.e = 1 + k.phi

And further

d = (1+k.phi)/e

Iterating though with values for k, a correct value for d is found when a whole number result for d (no fractional part) is found.

The algorithm used is to first test the user entered value in the form. If this satisfies the equation, it is the d value used. If it does not, it is used as the seed value for 'k'. If the 'd' value is left blank in the form, the algorithm will iterate using k==1 as its start value.

Note that if the value 'e' is not co-prime to phi, this script may never find a valid value for 'd'. A limit of 10,000 iterations has been placed on this for the time being. It has been left this way to show the importance of these numbers being co-prime, for the RSA system.