Guest User

Untitled

a guest
Jul 16th, 2018
86
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.96 KB | None | 0 0
  1. import java.math.BigInteger;
  2. import java.util.Arrays;
  3. import java.util.Random;
  4. import java.util.Scanner;
  5.  
  6. public class main {
  7. static BigInteger N;
  8.  
  9. static BigInteger e;
  10.  
  11. static BigInteger d;
  12.  
  13. static int bitLowInterval = 512;
  14. static int bitHighInterval = 2048;
  15.  
  16.  
  17.  
  18. public static void main(String[] args) {
  19. //For RSA
  20. System.out.println("\n CONFIGURANDO RSA!!! \n");
  21.  
  22. Random random = new Random();
  23. int BIT_LENGTH = random.nextInt(bitHighInterval-bitLowInterval) + bitLowInterval;
  24.  
  25. BigInteger p = BigInteger.probablePrime(BIT_LENGTH, random);
  26.  
  27. BigInteger q = BigInteger.probablePrime(BIT_LENGTH, random);
  28.  
  29. // n= p.q
  30. N = p.multiply(q);
  31.  
  32. // z= (p-1).(q-1)
  33. BigInteger z = p.subtract(BigInteger.ONE).multiply(q.subtract(BigInteger.ONE));
  34.  
  35. // Escolha um número inteiro ”e”, tal que 1 < e < ze ”z” e ”e” sejam primos entre si
  36. e = BigInteger.probablePrime(BIT_LENGTH / 2, random);
  37.  
  38. while (z.gcd(e).compareTo(BigInteger.ONE) > 0 && e.compareTo(z) < 0) {
  39. e.add(BigInteger.ONE);
  40. }
  41.  
  42. // ”d” tal que d= e-1mod z
  43. d = e.modInverse(z);
  44.  
  45. System.out.println("BIT LENGTH: " + BIT_LENGTH);
  46. System.out.println("n Primo p: " + p);
  47. System.out.println("n Primo q: " + q);
  48. System.out.println("N: " + N);
  49. System.out.println("Z: " + z);
  50. System.out.println("E: " + e);
  51. System.out.println("D: " + d);
  52.  
  53.  
  54.  
  55. //
  56.  
  57. Scanner scanner = new Scanner(System.in);
  58.  
  59. System.out.println("\n Escreva o texto para encriptar: ");
  60.  
  61. String toEncrypt = scanner.nextLine();
  62. if(toEncrypt.isEmpty()) {
  63. toEncrypt = "vazio";
  64. }
  65. byte[] toEncryptBytes = toEncrypt.getBytes();
  66. //
  67. System.out.println("Para encriptar: " + toEncrypt);
  68. System.out.println("Em bytes: " + Arrays.toString(toEncryptBytes));
  69.  
  70. System.out.println("\n ENCRIPTANDO!!! \n");
  71.  
  72.  
  73. byte[] encrypted = encrypt(toEncrypt.getBytes());
  74.  
  75. System.out.println("Encriptado: " + Arrays.toString(encrypted));
  76.  
  77. byte[] decrypted = decrypt(encrypted);
  78.  
  79. System.out.println("Decriptado: " + Arrays.toString(decrypted));
  80.  
  81. System.out.println("Descriptado em string: " + new String(decrypted));
  82.  
  83. System.out.println("Chave pública: " + Arrays.toString(getPublicKey()));
  84.  
  85. System.out.println("Chave privada: " + Arrays.toString(getPrivateKey()));
  86.  
  87. }
  88.  
  89. public static BigInteger[] getPublicKey() {
  90. // chave pública consiste em {e, n}
  91. return new BigInteger[]{e, N};
  92. }
  93.  
  94. public static BigInteger[] getPrivateKey() {
  95. // chave privada {d, n}
  96. return new BigInteger[]{d, N};
  97. }
  98.  
  99. public static byte[] encrypt(byte[] message) {
  100. // Para cifrar uma mensagem ”m”, onde 1 < m < n-1, usando a chave pública {e, n}, faz-se: –c = me mod n
  101. // ”c” é a mensagem cifrada que pode ser enviada com segurança ao receptor
  102. return (new BigInteger(message)).modPow(e, N).toByteArray();
  103. }
  104.  
  105. public static byte[] decrypt(byte[] message) {
  106. // Para decifrar a mensagem, o receptor utiliza sua chave privada (par {d, n}) fazendo outra potenciação modular: –m = cd mod n
  107. return (new BigInteger(message)).modPow(d, N).toByteArray();
  108. }
  109. }
Add Comment
Please, Sign In to add comment