Java  1.0
rsa.java
Go to the documentation of this file.
1 
37 public class rsa {
41  static private long n;
45  static private long d;
49  static private long e;
50 
62  private long fixRemainder (long a, long b) {
63  long r = a % b;
64 
65  if ((r * b) < 0)
66  r += b;
67 
68  return r;
69  }
70 
80  private long egcd(long a, long b, long[]last) {
81  long x = 0;
82  last[0] = 1;
83  long y = 1;
84  last[1] = 0;
85 
86  long quotient, t;
87  while ( b != 0 ) {
88  quotient = a / b; // integer division
89  t = b;
90  b = a % b;
91  a = t;
92  t = x;
93  x = last[0] - quotient * x;
94  last[0] = t;
95  t = y;
96  y = last[1] - quotient * y;
97  last[1] = t;
98  }
99  return a;
100  }
101 
110  private long modinv(long a, long m) {
111  long x, y, g;
112  long[] xy = {0,0};
113  g = egcd(a, m, xy);
114  x = xy[0];
115  if (g != 1)
116  return 0; // None: modular inverse does not exist
117  else
118  return fixRemainder (x, m);
119  }
120 
130  private long modinv2(long a, long b, long c) {
131 
132  long r = b%a;
133 
134  if ( r == 0 )
135  return (c / a) % (b / a);
136 
137  return (modinv2(r, a, -c) * b + c) / a%b;
138  }
139 
148  private long power_mod_n(long c, long d, long n) {
149 
150  long e, r = c % n;
151  long drev = baseConverter.reverseBits(d);
152 
153  drev >>>= 1;
154  while ( drev != 0 ) {
155  e = (drev & 1)==1 ? c : 1; // get the bits of d in reversed order
156  drev >>>= 1;
157  r = (e * r*r) % n;
158  }
159 
160  return r;
161  }
162 
181  public long generateKeys ( long p, long q ) {
182  e = 17;
183 
184  n = p*q;
185  long phi = (p-1)*(q-1);
186  d = modinv(e,phi); // modinv2(e,phi,1)
187  if (d==0) return phi;
188  return 0;
189  }
190 
196  public long encrypt(long val) {
197  return power_mod_n(val,e,n);
198  }
204  public long decrypt(long val) {
205  return power_mod_n(val,d,n);
206  }
207 
213  public static void main ( String[] args ) {
214  long a = 0, b = 0;
215 
216  int psize = 1;
217  if (args.length < 3) {
218  System.out.print ( String.format("usage: %s <number1> <number2> <prime size(small=0,medium=1,large=2)>\n", "rsa"));
219  System.exit(1);
220  }
221  else {
222  a = Integer.parseInt(args[0]);
223  b = Integer.parseInt(args[1]);
224  psize = Integer.parseInt(args[2]);
225  }
226 
227  rsa r = new rsa();
228  long[] xy = new long[2];
229  long gcd = r.egcd(a, b, xy);
230  long x = xy[0]; long y = xy[1];
231  System.out.print(String.format("The GCD of %d and %d is %d\n", a, b, gcd));
232  System.out.print(String.format("%d * %d + %d * %d = %d\n", a, x, b, y, gcd));
233  System.out.print(String.format("The modular inverse of %d and %d is %d\n", a, b, r.modinv(a,b)));
234 
235  long p, q;
236  if (psize == 0) {
237  p = 131;
238  q = 139;
239  }
240  else {
241  p = 1217;
242  q = 1223;
243  }
244 
245  long phi = r.generateKeys ( p, q );
246  if (phi > 0) System.out.print (String.format("gcd(%d,%d) is not 1 (modular inverse does not exist)", r.e, phi));
247 
248  System.out.print(String.format("Public key is: n = %d and e = %d\n", r.n, r.e));
249  System.out.print(String.format("Private key is: n = %d and d = %d\n", r.n, r.d));
250 
251  long tnumber = n;
252  long tmin = -n+2;
253  while ((tnumber >= n) || (tnumber < tmin)) {
254  System.out.print (String.format("Enter number to be encrypted (%d <= n <= %d): ", tmin, n-1));
255  tnumber = Integer.parseInt(System.console().readLine());
256  }
257 
258  long c = r.encrypt(tnumber); // pow(tnumber,e) % n
259  long m = r.decrypt(c); // pow(c,d) % n
260 
261  System.out.print(String.format("%d encrypted is: %d\n", tnumber, c));
262  System.out.print(String.format("%d decrypted is: %d\n", c, m));
263  }
264 }
baseConverter
Class for converting between different numerical bases.
Definition: baseConverter.java:64
rsa.egcd
long egcd(long a, long b, long[]last)
The extended Euclidean algorithm for the Greatest Common Divisor of two integers.
Definition: rsa.java:80
rsa.generateKeys
long generateKeys(long p, long q)
Generates the public and private keys.
Definition: rsa.java:181
rsa.encrypt
long encrypt(long val)
Encrypts a number.
Definition: rsa.java:196
rsa.modinv
long modinv(long a, long m)
Calculates the modular inverse of a long.
Definition: rsa.java:110
rsa.power_mod_n
long power_mod_n(long c, long d, long n)
Calculates c to the power of d mod n.
Definition: rsa.java:148
rsa.n
static long n
p*q.
Definition: rsa.java:41
rsa.fixRemainder
long fixRemainder(long a, long b)
Calculates the correct remainder if (a % b) has the opposite sign of b.
Definition: rsa.java:62
rsa.main
static void main(String[] args)
Main function for testing.
Definition: rsa.java:213
rsa.decrypt
long decrypt(long val)
Decrypts a number.
Definition: rsa.java:204
rsa.modinv2
long modinv2(long a, long b, long c)
Calculates the modular inverse of a long.
Definition: rsa.java:130
rsa
An RSA algorithm for public-key cryptography.
Definition: rsa.java:37
rsa.d
static long d
Private key: (n,d).
Definition: rsa.java:45
baseConverter.reverseBits
static long reverseBits(long x)
Reverses the bits of a long.
Definition: baseConverter.java:180
rsa.e
static long e
Public key: (n,e).
Definition: rsa.java:49