Guest User

Untitled

a guest
May 21st, 2018
99
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 3.68 KB | None | 0 0
  1. import java.math.BigInteger;
  2. import java.util.Random;
  3.  
  4. public class RSA {
  5.  
  6.     /**
  7.      * @param args
  8.      */
  9.     public static void main(String[] args) {
  10.         // TODO Auto-generated method stub
  11.         RSA rsa = new RSA(256);
  12.         System.out.println("generating codes ...");
  13.         rsa.generateCodes();
  14.         BigInteger encres = rsa.encryptBigInteger(new BigInteger("12345"));
  15.         System.out.println("Encrypted: " + encres.toString());
  16.         BigInteger decres = rsa.decryptBigInteger(encres);
  17.         System.out.println("Decrypted: " + decres.toString());
  18.         // rsa.extended_euclid(new BigInteger("834"), new BigInteger("543"));
  19.         // rsa.extended_euclid(new BigInteger("253"), new BigInteger("220"));
  20.         // BigInteger a = rsa.bigIntExpMod(new BigInteger("23456"), new
  21.         // BigInteger("1235"), new BigInteger("8326363"));
  22.         // System.out.println(a.toString());
  23.  
  24.     }
  25.  
  26.     private BigInteger d, e, p, n, phi, q;
  27.     private int bitsize;
  28.  
  29.     public RSA(int bitsize) {
  30.         this.bitsize = bitsize;
  31.         generateCodes();
  32.     }
  33.  
  34.     /*
  35.      * Choose D depending on E and phi E * D - 1 is evenly divisible by (p -1) *
  36.      * (q - 1)
  37.      */
  38.     private BigInteger chooseD(BigInteger phi, BigInteger e) {
  39.         System.out.println("choosing d...");
  40.         BigInteger[] result = extended_euclid(e, phi);
  41.         BigInteger res = result[1].abs();
  42.         boolean check = e.multiply(res).subtract(BigInteger.ONE).mod(phi)
  43.                 .equals(BigInteger.ZERO);
  44.         if (check) {
  45.             return result[1];
  46.         } else
  47.             return null;
  48.     }
  49.  
  50.     /*
  51.      * Choose E depending on phi p,q are primes E < n E is Relatively Prime to
  52.      * (p - 1) * (q - 1)=phi This means that E and (p - 1) * (q - 1) have no
  53.      * common factors except 1
  54.      */
  55.     private BigInteger chooseE(BigInteger phi) {
  56.         BigInteger two = new BigInteger("2");
  57.         BigInteger three = new BigInteger("3");
  58.         for (BigInteger i = three; (i.compareTo(phi) < 0); i = i.add(two)) {
  59.             if (extended_euclid(i, phi)[0].equals(BigInteger.ONE)) {
  60.                 return i;
  61.             }
  62.         }
  63.         return new BigInteger("-1");
  64.     }
  65.  
  66.     /*
  67.      * a recursive implementation of the extended euclidian algorithm
  68.      */
  69.     private BigInteger[] extended_euclid(BigInteger a, BigInteger b) {
  70.         BigInteger[] result = new BigInteger[3];
  71.         if (b.equals(BigInteger.ZERO)) {
  72.             result[0] = a;
  73.             result[1] = BigInteger.ONE;
  74.             result[2] = BigInteger.ZERO;
  75.         } else {
  76.             BigInteger[] recres = extended_euclid(b, a.mod(b));
  77.             result[0] = recres[0];
  78.             result[1] = recres[2];
  79.             result[2] = recres[1].subtract(a.divide(b).multiply(recres[2]));
  80.         }
  81.         return result;
  82.     }
  83.     /*
  84.      * Generate codes e and d
  85.      */
  86.     public void generateCodes() {
  87.         do {
  88.             p = BigInteger.probablePrime(bitsize, new Random());
  89.             q = BigInteger.probablePrime(bitsize, new Random());
  90.             n = p.multiply(q);
  91.             phi = q.subtract(BigInteger.ONE).multiply(
  92.                     p.subtract(BigInteger.ONE));
  93.             e = chooseE(phi);
  94.             d = chooseD(phi, e);
  95.         } while (d == null);
  96.  
  97.         System.out.println("p: " + p.toString() + " q: " + q.toString()
  98.                 + " e: " + e.toString() + " d: " + d.toString() + " phi: "
  99.                 + phi.toString() + " n: " + n);
  100.     }
  101.     /*
  102.      * encrypt
  103.      */
  104.     private BigInteger encryptBigInteger(BigInteger msg) {
  105.         return bigIntExpMod(msg, e, n);
  106.     }
  107.     /*
  108.      * decrypt
  109.      */
  110.     private BigInteger decryptBigInteger(BigInteger msg) {
  111.         return bigIntExpMod(msg, d, n);
  112.     }
  113.     /*
  114.      * Exponentation
  115.      */
  116.     private BigInteger bigIntExpMod(BigInteger base, BigInteger exponent,
  117.             BigInteger m) {
  118.         BigInteger result = BigInteger.ONE;
  119.         BigInteger two = new BigInteger("2");
  120.         while (!(e.equals(BigInteger.ZERO))) {
  121.             if (e.mod(two).equals(BigInteger.ZERO)) {
  122.                 base = base.multiply(base).mod(m);
  123.                 e = e.divide(two);
  124.             } else {
  125.                 result = base.multiply(result).mod(m);
  126.                 e = e.subtract(BigInteger.ONE);
  127.             }
  128.         }
  129.         return result;
  130.     }
  131.  
  132. }
Add Comment
Please, Sign In to add comment