Advertisement
Schknheit

Untitled

Nov 12th, 2018
260
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 3.69 KB | None | 0 0
  1. package br.com.rsa;
  2.  
  3. import java.security.*;
  4. import java.io.*;
  5. import java.util.*;
  6. import javax.crypto.*;
  7.  
  8. public class Geracao {
  9.  
  10.     public static void main(String[] args) throws IOException {
  11.  
  12.         //Gerando um arquivo que será encriptado e descriptografado.
  13.         Scanner entrada1 = new Scanner(System.in);
  14.        
  15.         System.out.println("Digite qualquer coisa: ");
  16.         String entrada = entrada1.nextLine();
  17.        
  18.         System.out.println("Arquivo criado.");
  19.        
  20.         FileOutputStream saida = new FileOutputStream("arquivo.txt");
  21.         PrintStream volta_saida = System.out;
  22.         PrintStream imprimir = new PrintStream(saida);
  23.         System.setOut(imprimir);
  24.         System.out.println(entrada);
  25.         saida.close();
  26.         System.setOut(volta_saida);
  27.        
  28.         //Gerando as chaves publica e privada.
  29.         try {      
  30.         KeyPairGenerator chave = KeyPairGenerator.getInstance("RSA");
  31.         chave.initialize(1024);
  32.        
  33.         KeyPair chaves = chave.generateKeyPair();
  34.        
  35.         PrivateKey privada = chaves.getPrivate();
  36.         PublicKey publica = chaves.getPublic();
  37.        
  38.         Base64.Encoder cripto = Base64.getEncoder();
  39.  
  40.         FileOutputStream saida_chaves = new FileOutputStream("chaves.txt");
  41.         PrintStream volta_saida_c = System.out;
  42.         PrintStream imprimir_chaves = new PrintStream(saida_chaves);
  43.         System.setOut(imprimir_chaves);
  44.         System.out.println("Chave privada: " + cripto.encodeToString(privada.getEncoded()));
  45.         System.out.println("");
  46.         System.out.println("Chave publica: " + cripto.encodeToString(publica.getEncoded()));
  47.         System.out.println("");
  48.         saida.close();
  49.         System.setOut(volta_saida_c);
  50.  
  51.             //Salvando as chaves publica e privada.
  52.             try (FileOutputStream s_prv = new FileOutputStream("privada" + ".key")){
  53.            
  54.                 s_prv.write(chaves.getPrivate().getEncoded());
  55.                
  56.             }
  57.            
  58.             try (FileOutputStream s_pub = new FileOutputStream("publica" + ".key")){
  59.                
  60.                 s_pub.write(chaves.getPublic().getEncoded());
  61.             }
  62.            
  63.             Criptografar(chaves, null);
  64.             Descriptografar(chaves, null);
  65.         }
  66.                
  67.         //Qualquer erro dentro da geração das chaves
  68.         catch (Exception e){
  69.            
  70.             System.out.println(e);
  71.         }
  72.     }
  73.  
  74.     //TODO - Comentario
  75.      static private void processFile(Cipher cifra, InputStream entrada_arq_c, OutputStream saida_arq_c){
  76.            try {
  77.             byte[] ibuf = new byte[1024];
  78.             int len;
  79.             while ((len = entrada_arq_c.read(ibuf)) != -1) {
  80.                 byte[] obuf = cifra.update(ibuf, 0, len);
  81.                 if ( obuf != null ) saida_arq_c.write(obuf);
  82.             }
  83.             byte[] obuf = cifra.doFinal();
  84.             if ( obuf != null ) saida_arq_c.write(obuf);
  85.            }
  86.           catch(Exception e) {
  87.               e.printStackTrace();
  88.               System.out.println("Problema no manuseio do arquivo.");
  89.           }
  90.     }
  91.    
  92.     //Metodo para criptografar.
  93.      static private void Criptografar(KeyPair chaves, Cipher ci){
  94.         try {
  95.             PublicKey publica = chaves.getPublic();
  96.             Cipher cifra = Cipher.getInstance("RSA");
  97.             cifra.init(Cipher.ENCRYPT_MODE, publica);
  98.            
  99.             FileInputStream entrada_arq_c = new FileInputStream("arquivo.txt");
  100.             FileOutputStream saida_arq_c = new FileOutputStream("criptografado.txt");
  101.             processFile(cifra, entrada_arq_c, saida_arq_c);
  102.  
  103.         }
  104.         catch(Exception e){
  105.            
  106.             System.out.println("Erro ao criptografar.");
  107.         }
  108.     }
  109.    
  110.     //Metodo para descriptografar.
  111.      static private void Descriptografar(KeyPair chaves, Cipher ci){
  112.        
  113.         try {
  114.             PrivateKey privada = chaves.getPrivate();
  115.             Cipher cifra = Cipher.getInstance("RSA");
  116.             cifra.init(Cipher.DECRYPT_MODE, privada);
  117.            
  118.             FileInputStream entrada_arq_c = new FileInputStream("criptografado.txt");
  119.             FileOutputStream saida_arq_c = new FileOutputStream("descriptografado.txt");
  120.             processFile(cifra, entrada_arq_c, saida_arq_c);
  121.         }
  122.         catch(Exception e){
  123.            
  124.             System.out.println("Erro ao descriptografar.");
  125.         }
  126.        
  127.     }
  128. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement