Guest User

Untitled

a guest
Mar 24th, 2018
97
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.63 KB | None | 0 0
  1. /*
  2. * To change this license header, choose License Headers in Project Properties.
  3. * To change this template file, choose Tools | Templates
  4. * and open the template in the editor.
  5. */
  6. package RSAkeamanansistem;
  7. import java.io.DataInputStream;
  8.  
  9. import java.io.IOException;
  10.  
  11. import java.math.BigInteger;
  12.  
  13. import java.util.Random;
  14. /**
  15. *
  16. * @author meciwp
  17. */
  18. public class RSAKeamananSistem
  19.  
  20. {
  21.  
  22. private BigInteger a;
  23.  
  24. private BigInteger b;
  25.  
  26. private BigInteger D;
  27.  
  28. private BigInteger phi;
  29.  
  30. private BigInteger f;
  31.  
  32. private BigInteger g;
  33.  
  34. private int bitlength = 724;
  35.  
  36. private Random R;
  37.  
  38.  
  39.  
  40. public RSAKeamananSistem()
  41.  
  42. {
  43.  
  44. R = new Random();
  45.  
  46. a = BigInteger.probablePrime(bitlength, R);
  47.  
  48. b = BigInteger.probablePrime(bitlength, R);
  49.  
  50. D = a.multiply(b);
  51.  
  52. phi = a.subtract(BigInteger.ONE).multiply(b.subtract(BigInteger.ONE));
  53.  
  54. f = BigInteger.probablePrime(bitlength / 2, R);
  55.  
  56. while (phi.gcd(f).compareTo(BigInteger.ONE) > 0 && f.compareTo(phi) < 0)
  57.  
  58. {
  59.  
  60. f.add(BigInteger.ONE);
  61.  
  62. }
  63.  
  64. g = f.modInverse(phi);
  65.  
  66. }
  67.  
  68.  
  69.  
  70. public RSAKeamananSistem(BigInteger f, BigInteger g, BigInteger D)
  71.  
  72. {
  73.  
  74. this.f = f;
  75.  
  76. this.g = g;
  77.  
  78. this.D = D;
  79.  
  80. }
  81.  
  82.  
  83.  
  84. @SuppressWarnings("deprecation")
  85.  
  86. public static void main(String[] args) throws IOException
  87.  
  88. {
  89.  
  90. RSAKeamananSistem rsa = new RSAKeamananSistem();
  91.  
  92. DataInputStream in = new DataInputStream(System.in);
  93.  
  94. String teststring;
  95.  
  96. System.out.println("Masukan kata yang Akan di Enkripsi:");
  97.  
  98. teststring = in.readLine();
  99.  
  100. System.out.println("Kata Yang Anda Masukan Adalah: " + teststring);
  101.  
  102. System.out.println("String in Bytes: "
  103.  
  104. + bytesToString(teststring.getBytes()));
  105.  
  106. // encrypt
  107.  
  108. byte[] encrypted = rsa.encrypt(teststring.getBytes());
  109.  
  110. // decrypt
  111.  
  112. byte[] decrypted = rsa.decrypt(encrypted);
  113.  
  114. System.out.println("Decrypting Bytes: " + bytesToString(decrypted));
  115.  
  116. System.out.println("Decrypted String: " + new String(decrypted));
  117.  
  118. }
  119.  
  120.  
  121.  
  122. private static String bytesToString(byte[] encrypted)
  123.  
  124. {
  125.  
  126. String test = "";
  127.  
  128. for (byte b : encrypted)
  129.  
  130. {
  131.  
  132. test += Byte.toString(b);
  133.  
  134. }
  135.  
  136. return test;
  137.  
  138. }
  139.  
  140.  
  141.  
  142. // Encrypt message
  143.  
  144. public byte[] encrypt(byte[] message)
  145.  
  146. {
  147.  
  148. return (new BigInteger(message)).modPow(f, D).toByteArray();
  149.  
  150. }
  151.  
  152.  
  153.  
  154. // Decrypt message
  155.  
  156. public byte[] decrypt(byte[] message)
  157.  
  158. {
  159.  
  160. return (new BigInteger(message)).modPow(g, D).toByteArray();
  161.  
  162. }
  163.  
  164. }
Add Comment
Please, Sign In to add comment