Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import java.io.DataInputStream;
- import java.io.IOException;
- import java.math.BigInteger;
- import java.util.Random;
- public class RSA{
- private BigInteger p;
- private BigInteger q;
- private BigInteger phi;
- private static BigInteger N;
- private static BigInteger e;
- private static BigInteger d;
- private int bitlength = 1024;
- private Random r;
- // TJм*й—6їZћІрi
- public RSA(){
- r = new Random();
- p = BigInteger.probablePrime(bitlength, r);
- q = BigInteger.probablePrime(bitlength, r);
- N = p.multiply(q);
- // N = new BigInteger("20905928649009455472573861685204215525888921384426216315834047716855481184461763703678878513237722911325646921459473108774285276836688100439840516442272392026347569874035118171848029992048901826701259892065115345103282124493582371132725373346437536057036434815071596680944021724516745225608813887676168557383370799608772469836920728125930575765837124585896055303940449289472637988950580463001282652336755182581269875686133299903901719505427807693058671847523276068364030354589153759544239927384811342453744007220444079594612851785508552825953005590630699314847647028710501993125945340893395807994912852614617973173753");
- // N = new BigInteger("26943512249801908090043884738623877426365067475156364351592814943825240349103797095727693521561847598342899042835654905114223523716802596132264701248836834711763186200640238598746870302669066181082694917225088141564511214340925935889150777241211320127006177956369057692420382581643579990574967172990980432956517881095096253030790067002459254898982300477330264788709739370874034452349183336699963726825743322634243513722475533430257621964636493176351114114490764158412988512791881968805786511084483075838994077270939302697711995789743743693863507529130242520387698044136620579168862247550034080430984288203814120610381");
- phi = p.subtract(BigInteger.ONE).multiply(q.subtract(BigInteger.ONE));
- e = BigInteger.probablePrime(bitlength / 2, r);
- while (phi.gcd(e).compareTo(BigInteger.ONE) > 0 && e.compareTo(phi) < 0){
- e.add(BigInteger.ONE);
- }
- // e = new BigInteger("7605032470227196437622763289736866168805221226516125354656851321717912613732449515133285018086421199172420107146165666639901325366185398206336201703206919");
- // e = new BigInteger("11789234042035339534610070424118531939724557884695234463582832521147341415133700367725983159778378035909647000373865890073398948713079859312589716598302047");
- d = e.modInverse(phi);
- System.out.println("e: "+e.toString());
- System.out.println("N: "+N.toString());
- }
- public RSA(BigInteger e, BigInteger d, BigInteger N) {
- this.e = e;
- this.d = d;
- this.N = N;
- }
- @SuppressWarnings("deprecation")
- public static void main(String[] args) throws IOException {
- RSA rsa = new RSA();
- System.out.println("Enter encrypted text:");
- DataInputStream in = new DataInputStream(System.in);
- String x = in.readLine();
- BigInteger encrypted = new BigInteger(x);
- // decrypt
- byte[] decrypted = rsa.decrypt(encrypted,d,N);
- System.out.println("Decrypting Bytes: " + bytesToString(decrypted));
- System.out.println("Decrypted String: " + new String(decrypted));
- }
- private static String bytesToString(byte[] encrypted){
- String test = "";
- for (byte b : encrypted){
- test += Byte.toString(b);
- }
- return test;
- }
- // Decrypt message
- public byte[] decrypt(BigInteger message, BigInteger d, BigInteger N) {
- return (message).modPow(this.d, this.N).toByteArray();
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement