Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import java.math.BigInteger;
- import java.util.Arrays;
- import java.util.Random;
- import java.util.Scanner;
- public class main {
- static BigInteger N;
- static BigInteger e;
- static BigInteger d;
- static int bitLowInterval = 512;
- static int bitHighInterval = 2048;
- public static void main(String[] args) {
- //For RSA
- System.out.println("\n CONFIGURANDO RSA!!! \n");
- Random random = new Random();
- int BIT_LENGTH = random.nextInt(bitHighInterval-bitLowInterval) + bitLowInterval;
- BigInteger p = BigInteger.probablePrime(BIT_LENGTH, random);
- BigInteger q = BigInteger.probablePrime(BIT_LENGTH, random);
- // n= p.q
- N = p.multiply(q);
- // z= (p-1).(q-1)
- BigInteger z = p.subtract(BigInteger.ONE).multiply(q.subtract(BigInteger.ONE));
- // Escolha um número inteiro ”e”, tal que 1 < e < ze ”z” e ”e” sejam primos entre si
- e = BigInteger.probablePrime(BIT_LENGTH / 2, random);
- while (z.gcd(e).compareTo(BigInteger.ONE) > 0 && e.compareTo(z) < 0) {
- e.add(BigInteger.ONE);
- }
- // ”d” tal que d= e-1mod z
- d = e.modInverse(z);
- System.out.println("BIT LENGTH: " + BIT_LENGTH);
- System.out.println("n Primo p: " + p);
- System.out.println("n Primo q: " + q);
- System.out.println("N: " + N);
- System.out.println("Z: " + z);
- System.out.println("E: " + e);
- System.out.println("D: " + d);
- //
- Scanner scanner = new Scanner(System.in);
- System.out.println("\n Escreva o texto para encriptar: ");
- String toEncrypt = scanner.nextLine();
- if(toEncrypt.isEmpty()) {
- toEncrypt = "vazio";
- }
- byte[] toEncryptBytes = toEncrypt.getBytes();
- //
- System.out.println("Para encriptar: " + toEncrypt);
- System.out.println("Em bytes: " + Arrays.toString(toEncryptBytes));
- System.out.println("\n ENCRIPTANDO!!! \n");
- byte[] encrypted = encrypt(toEncrypt.getBytes());
- System.out.println("Encriptado: " + Arrays.toString(encrypted));
- byte[] decrypted = decrypt(encrypted);
- System.out.println("Decriptado: " + Arrays.toString(decrypted));
- System.out.println("Descriptado em string: " + new String(decrypted));
- System.out.println("Chave pública: " + Arrays.toString(getPublicKey()));
- System.out.println("Chave privada: " + Arrays.toString(getPrivateKey()));
- }
- public static BigInteger[] getPublicKey() {
- // chave pública consiste em {e, n}
- return new BigInteger[]{e, N};
- }
- public static BigInteger[] getPrivateKey() {
- // chave privada {d, n}
- return new BigInteger[]{d, N};
- }
- public static byte[] encrypt(byte[] message) {
- // Para cifrar uma mensagem ”m”, onde 1 < m < n-1, usando a chave pública {e, n}, faz-se: –c = me mod n
- // ”c” é a mensagem cifrada que pode ser enviada com segurança ao receptor
- return (new BigInteger(message)).modPow(e, N).toByteArray();
- }
- public static byte[] decrypt(byte[] message) {
- // Para decifrar a mensagem, o receptor utiliza sua chave privada (par {d, n}) fazendo outra potenciação modular: –m = cd mod n
- return (new BigInteger(message)).modPow(d, N).toByteArray();
- }
- }
Add Comment
Please, Sign In to add comment