Foro de elhacker.net

Programación => Java => Mensaje iniciado por: Tronos154 en 19 Febrero 2016, 18:24 pm



Título: [Aporte] Cifrar texto en RSA en java
Publicado por: Tronos154 en 19 Febrero 2016, 18:24 pm
Bueno,ante todo dar las gracias a los miembros de este foro por ayudarme con algunas dudas que me han surgido a la hora de hacer este programa.  :D :D :D

Código
  1. package RSA;
  2.  
  3. import java.math.BigInteger;
  4. import java.util.Random;
  5. import java.util.ArrayList;
  6. import ClavePublica.ClavePublica;
  7. import ClavePrivada.ClavePrivada;
  8. import Euler.Euler;
  9.  
  10. /**
  11.  * Codificación en RSA
  12.  *
  13.  * Autor: Tronos154
  14.  */
  15.  
  16. public class RSA {
  17.  
  18.    public Random rnd1 = new Random();
  19.    public Random rnd2 = new Random();
  20.    public int bitsLength = (int) Math.random() * 56 + 200;
  21.  
  22.    public BigInteger primo1 = BigInteger.probablePrime(bitsLength, rnd1);
  23.    public BigInteger primo2 = BigInteger.probablePrime(bitsLength, rnd2);
  24.    public BigInteger modulo = primo1.multiply(primo2);
  25.  
  26.    public BigInteger euler = Euler.Euler(primo1, primo2);
  27.  
  28.    public BigInteger clavePublica = ClavePublica.ClavePublica(euler);
  29.    public BigInteger clavePrivada = ClavePrivada.ClavePrivada(clavePublica, euler);
  30.  
  31.    public String cifrar(String texto) {
  32.  
  33.        char letra;
  34.        int letra2 = 0;
  35.        String textoEncriptado = "";
  36.        String letra3 = "";
  37.        int contador = 0;
  38.  
  39.        while (contador < texto.length()) {
  40.            letra = texto.charAt(contador);
  41.            letra2 = letra;
  42.            letra3 = String.valueOf(letra2);
  43.            BigInteger numero = new BigInteger(letra3);
  44.            textoEncriptado = textoEncriptado + (numero.modPow(clavePublica, modulo)).toString() + " ";
  45.            contador++;
  46.        }
  47.        return textoEncriptado;
  48.    }
  49.  
  50.    public String descifrar(String textoEncriptado) {
  51.        String textoDesencriptado = "";
  52.        String textoAlmacen = "";
  53.        char letra = 0;
  54.        for (int i = 0; i < textoEncriptado.length(); i++) {
  55.            if (textoEncriptado.charAt(i) == 32) {
  56.                BigInteger numero = new BigInteger(textoAlmacen);
  57.                letra = (char) (int) (numero.modPow(clavePrivada, modulo).longValue());
  58.                textoDesencriptado = textoDesencriptado + letra;
  59.                textoAlmacen = "";
  60.  
  61.            } else {
  62.                textoAlmacen = textoAlmacen + textoEncriptado.charAt(i);
  63.            }
  64.  
  65.        }
  66.        return textoDesencriptado;
  67.    }
  68.  
  69.  
  70.  
  71. }
  72.  

Librerías que se han creado para el programa:

Clave Publica:
Código
  1. package ClavePublica;
  2.  
  3. import java.math.BigInteger;
  4. import java.util.ArrayList;
  5. import java.util.Random;
  6. /**
  7.  * Generador Clave Publica
  8.  *
  9.  *
  10.  * Autor : Tronos154
  11.  *
  12.  */
  13. public class ClavePublica {
  14.  
  15.    public static BigInteger ClavePublica(BigInteger euler) {
  16.  
  17.  
  18.        BigInteger ONE = BigInteger.ONE;
  19.        BigInteger ZERO = BigInteger.ZERO;
  20.        BigInteger Euler2 = euler.subtract(ONE);
  21.        Boolean enc = false;
  22.  
  23.        while (enc == false && Euler2.compareTo(ONE) == 1) {
  24.  
  25.            if ((euler.gcd(Euler2)).toString().equals("1")) {
  26.                enc = true;
  27.  
  28.  
  29.            } else {
  30.                Euler2 = Euler2.subtract(ONE);
  31.            }
  32.        }
  33.  
  34.       return Euler2;
  35.    }
  36.  
  37. }
  38.  

Clave Privada:

Código
  1. package ClavePrivada;
  2. import java.math.BigInteger;
  3.  
  4. /** Clave Privada
  5.  *
  6.  * Autor : Tronos154
  7.  *
  8.  */
  9. public class ClavePrivada {
  10.    public static BigInteger ClavePrivada (BigInteger clavePublica , BigInteger euler) {
  11.        BigInteger clavePrivada = clavePublica.modInverse(euler);
  12.        return clavePrivada;
  13.    }
  14. }
  15.  

Función Euler :

Código
  1. package Euler;
  2.  
  3. import java.math.BigInteger;
  4. import java.util.ArrayList;
  5.  
  6. /**
  7.  *
  8.  * Autor : Tronos154  
  9.  */
  10. public class Euler {
  11.  
  12.    public static BigInteger Euler(BigInteger primo1, BigInteger primo2) {
  13.        BigInteger ONE = BigInteger.ONE;
  14.        BigInteger Euler;
  15.        Euler = (primo1.subtract(ONE)).multiply(primo2.subtract(ONE));
  16.        return Euler;
  17.    }
  18.  
  19. }
  20.