import java.math.*; import java.util.*; import java.io.*; public class rsa { private static BigInteger one=new BigInteger("1"); private static int c=0; private Random r; private BigInteger n, d, e; public rsa() { BigInteger p, q, phi, phiminusone, dd, ee; c+=1; r=new Random(System.currentTimeMillis()+c); p=new BigInteger(20,40,r); q=new BigInteger(20,40,r); n=p.multiply(q); phi=p.subtract(one).multiply(q.subtract(one)); phiminusone=phi.subtract(one); dd=new BigInteger(18,40,r); ee=dd.modInverse(phi); d=dd.min(ee); e=dd.max(ee); } public rsa(String[] key) { n=new BigInteger(key[0]); d=new BigInteger(key[1]); e=d; } public rsa(String[] public_key, String[] private_key) { BigInteger dd=new BigInteger(public_key[1]), ee=new BigInteger(private_key[1]); n=new BigInteger(private_key[0]); d=dd.min(ee); e=dd.max(ee); } public String[] get_private_key() { String[] s=new String[2]; s[0]=n.toString(); s[1]=e.toString(); return s; } public String[] get_public_key() { String[] s=new String[2]; s[0]=n.toString(); s[1]=d.toString(); return s; } public static void print_key(String[] s) { System.out.print("["+s[0]+","+s[1]+"]"); } public BigInteger public_encrypt(BigInteger msg) { return msg.modPow(d,n); } public BigInteger private_encrypt(BigInteger msg) { return msg.modPow(e,n); } public static String BigIntegerToString(BigInteger n) { return new String(n.toByteArray()); } public static BigInteger StringToBigInteger(String s) { return new BigInteger(s.getBytes()); } public static BigInteger[] StringToBigIntegerArray(String inp) { String s; BigInteger[] res=new BigInteger[(inp.length()-1)/3+1]; int i=0; while (!inp.equals("")) { if (inp.length()>3) { s=inp.substring(0,3); inp=inp.substring(3); } else { s=inp; inp=""; } res[i]=StringToBigInteger(s); i+=1; } return res; } public static String BigIntegerArrayToString(BigInteger[] cod) { String s=""; int i; for (i=0; i126) c='~'; r=r+c; } return r; } public BigInteger[] private_encrypt(BigInteger[] cod) { BigInteger[] res=new BigInteger[cod.length]; int i; for (i=0; i