title: Calculating modulo inverse in java
body: <p>I was trying to figure out a way to calculate modulo inverse using java so I wrote this function to do it:</p>
  <pre class="lang-java prettyprint-override"><code>public static int privatekey(int primeprod, int e, int lambda)
  {
      Random random = new Random();
      float d = 0;
      //extended euler's theorem is
      //d = (1 + x*lambda) / e
      //d is smaller than primeprod
      while(true)
      {
          int d = random.nextInt(200) + 1;
          System.out.println(&quot;seed: &quot;+x);
          var = (1 + (x*lambda)) / e;
          System.out.println(&quot;var: &quot;+d);
          if(isInteger(d) &amp;&amp; d &lt; primeprod)
          {
              break;
          }
      }
      int finalvar = (int)d;
      return finalvar;
  }
  </code></pre>
  <p>I felt that it was going wrong so I reversed euclidean theorem extension I used above as follows</p>
  <pre><code>1 + x.lambda
  ------------- = d (this one is the equation to find out d when x is any integer)
       e
       
  
  de = 1 + x.lambda
  
  de - 1 = x.lambda
  
  de - 1
  ------- = x (this one is to check whether the value obtained by the above equation is true or not by checking if x is the same value we had chosen for our calculation in the first place)
  lambda
  </code></pre>
  <p>After doing this check I found that the value of x I obtained in the reversed equation I had solved to check for mistakes is not equal but approximate to the original random value which I had generated.</p>
  <p>For example taking these values:</p>
  <pre><code>e = 327
  lambda = 484
  x = 76
  
  We get d as 112.0
  
  later We reverse The equation to find the value of x to confirm
  
  we get:
  x = 75.667355372 (Which is approximate to the original value)
  </code></pre>
  <p>I wasn't able to figure out where it was going wrong.</p>
  <p>To look at the full program please visit <a href="https://github.com/theEndurance-del/faulty_rsaencryption_code/blob/main/smallenc.java" rel="nofollow noreferrer">this</a> link.</p>
  <p>Please tell me If I have done something wrong here.</p>
  <p>Thank You in advance!</p>