Advertisement
Guest User

Untitled

a guest
Jan 22nd, 2019
71
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.67 KB | None | 0 0
  1. import java.io.DataInputStream;
  2. import java.io.IOException;
  3. import java.math.BigInteger;
  4. import java.util.Random;
  5.  
  6. public class RSA{
  7. private BigInteger p;
  8. private BigInteger q;
  9. private BigInteger phi;
  10. private static BigInteger N;
  11. private static BigInteger e;
  12. private static BigInteger d;
  13. private int bitlength = 1024;
  14. private Random r;
  15. // TJм*й—6їZћІрi
  16. public RSA(){
  17. r = new Random();
  18. p = BigInteger.probablePrime(bitlength, r);
  19. q = BigInteger.probablePrime(bitlength, r);
  20. N = p.multiply(q);
  21. // N = new BigInteger("20905928649009455472573861685204215525888921384426216315834047716855481184461763703678878513237722911325646921459473108774285276836688100439840516442272392026347569874035118171848029992048901826701259892065115345103282124493582371132725373346437536057036434815071596680944021724516745225608813887676168557383370799608772469836920728125930575765837124585896055303940449289472637988950580463001282652336755182581269875686133299903901719505427807693058671847523276068364030354589153759544239927384811342453744007220444079594612851785508552825953005590630699314847647028710501993125945340893395807994912852614617973173753");
  22. // N = new BigInteger("26943512249801908090043884738623877426365067475156364351592814943825240349103797095727693521561847598342899042835654905114223523716802596132264701248836834711763186200640238598746870302669066181082694917225088141564511214340925935889150777241211320127006177956369057692420382581643579990574967172990980432956517881095096253030790067002459254898982300477330264788709739370874034452349183336699963726825743322634243513722475533430257621964636493176351114114490764158412988512791881968805786511084483075838994077270939302697711995789743743693863507529130242520387698044136620579168862247550034080430984288203814120610381");
  23. phi = p.subtract(BigInteger.ONE).multiply(q.subtract(BigInteger.ONE));
  24. e = BigInteger.probablePrime(bitlength / 2, r);
  25. while (phi.gcd(e).compareTo(BigInteger.ONE) > 0 && e.compareTo(phi) < 0){
  26. e.add(BigInteger.ONE);
  27. }
  28. // e = new BigInteger("7605032470227196437622763289736866168805221226516125354656851321717912613732449515133285018086421199172420107146165666639901325366185398206336201703206919");
  29. // e = new BigInteger("11789234042035339534610070424118531939724557884695234463582832521147341415133700367725983159778378035909647000373865890073398948713079859312589716598302047");
  30. d = e.modInverse(phi);
  31. System.out.println("e: "+e.toString());
  32. System.out.println("N: "+N.toString());
  33. }
  34.  
  35. public RSA(BigInteger e, BigInteger d, BigInteger N) {
  36. this.e = e;
  37. this.d = d;
  38. this.N = N;
  39. }
  40.  
  41. @SuppressWarnings("deprecation")
  42. public static void main(String[] args) throws IOException {
  43. RSA rsa = new RSA();
  44. System.out.println("Enter encrypted text:");
  45. DataInputStream in = new DataInputStream(System.in);
  46. String x = in.readLine();
  47. BigInteger encrypted = new BigInteger(x);
  48. // decrypt
  49. byte[] decrypted = rsa.decrypt(encrypted,d,N);
  50. System.out.println("Decrypting Bytes: " + bytesToString(decrypted));
  51. System.out.println("Decrypted String: " + new String(decrypted));
  52. }
  53.  
  54. private static String bytesToString(byte[] encrypted){
  55. String test = "";
  56. for (byte b : encrypted){
  57. test += Byte.toString(b);
  58. }
  59. return test;
  60. }
  61. // Decrypt message
  62. public byte[] decrypt(BigInteger message, BigInteger d, BigInteger N) {
  63. return (message).modPow(this.d, this.N).toByteArray();
  64. }
  65. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement