How to read a txt file and encrypt its contents

Asked

Viewed 330 times

0

I’m having trouble with the FileReader and buffered. I’ve seen several tutorials plus some are very different from each other and am unable to read my file to encrypt.

I have my AES code here:

import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.security.InvalidAlgorithmParameterException;
import java.security.InvalidKeyException;
import java.security.NoSuchAlgorithmException;

import javax.crypto.Cipher;
import javax.crypto.CipherInputStream;
import javax.crypto.CipherOutputStream;
import javax.crypto.KeyGenerator;
import javax.crypto.NoSuchPaddingException;
import javax.crypto.SecretKey;
import javax.crypto.spec.IvParameterSpec;
import javax.xml.bind.DatatypeConverter;

public class cripto_Aes {

    public static void Aes(String inputAes,int opcaoAes) throws Exception {
          String plainText = inputAes;
          SecretKey secKey = getSecretEncryptionKey(opcaoAes);
          byte[] cipherText = encryptText(plainText, secKey);
          String decryptedText = decryptText(cipherText, secKey);
          System.out.println("Texto original:" + plainText);
          System.out.println("AES Key (Hex Form):"+bytesToHex(secKey.getEncoded()));
          System.out.println("Texto criptografado (Hex Form):"+bytesToHex(cipherText));
          System.out.println("Texto descodificado:"+decryptedText);
          long tempo = System.currentTimeMillis();
          System.out.println("Tempo Executado : " + tempo);       
    }

    public static SecretKey getSecretEncryptionKey(int opcaoAes) throws Exception{
          KeyGenerator generator = KeyGenerator.getInstance("AES");
          generator.init(opcaoAes); // The AES key size in number of bits
          SecretKey secKey = generator.generateKey();
          return secKey;   
    }

    public static byte[] encryptText(String plainText,SecretKey secKey) throws Exception{
          Cipher aesCipher = Cipher.getInstance("AES");
          aesCipher.init(Cipher.ENCRYPT_MODE, secKey);
          byte[] byteCipherText = aesCipher.doFinal(plainText.getBytes());
          return byteCipherText;

    }

    public static String decryptText(byte[] byteCipherText, SecretKey secKey) throws Exception {

          Cipher aesCipher = Cipher.getInstance("AES");
          aesCipher.init(Cipher.DECRYPT_MODE, secKey);
          byte[] bytePlainText = aesCipher.doFinal(byteCipherText);
          return new String(bytePlainText);

    }

    private static String  bytesToHex(byte[] hash) {
          return DatatypeConverter.printHexBinary(hash);       
    }

}

And my menu where set the type of key it is a little big because I have several encryption options but I only posted the AES above not to get too big:

import java.io.File;
import java.io.FileReader;
import java.util.Scanner;
public class Menu {

    public static void main(String[]args) throws Exception{
        Menu();
    }


    public static void Menu() throws Exception{
        int opcao;
        System.out.println("Informa as opção abaixo\n1.AES\n2.DES\n3.RSA\n4.Has");
        opcao = input.nextInt();

        switch(opcao){
        case 1:
            int opcaoAES;
            System.out.println("Informa as opção abaixo\n1.128\n2.192\n3.256");
            opcao = input.nextInt();
            cripto_Aes aes = new cripto_Aes();

            if(opcao ==1){
                System.out.println("Informa uma frase");
                String inputAes = input.next();
                opcaoAES = 128;
                aes.Aes(inputAes, opcaoAES);    

            }else if(opcao ==2){
                System.out.println("Informa uma frase");
                String inputAes = input.next();
                opcaoAES = 192;
                aes.Aes(inputAes, opcaoAES);    

            }else{
                System.out.println("Informa uma frase");
                String inputAes = input.next();
                opcaoAES = 256;
                aes.Aes(inputAes, opcaoAES);    
            }

            break;

        case 2:
            int opcaoDES;
            System.out.println("Informa as opção abaixo\n1.128\n2.192\n3.256");
            opcao = input.nextInt();
            cripto_Des des = new cripto_Des();

            if(opcao ==1){
                System.out.println("Informa uma frase");
                String inputAes = input.next();
                opcaoDES = 128;
                des.Des(inputAes, opcaoDES);    

            }else if(opcao ==2){
                System.out.println("Informa uma frase");
                String inputAes = input.next();
                opcaoDES = 192;
                des.Des(inputAes, opcaoDES);    

            }else{
                System.out.println("Informa uma frase");
                String inputAes = input.next();
                opcaoDES = 256;
                des.Des(inputAes, opcaoDES);    
            }

            break;
        case 3:
            int opcaoRSA;
            System.out.println("Informa as opção abaixo\n1.1024\n2.2048\n3.4096");
            opcao = input.nextInt();
            //RSA rsa = new RSA();

            if(opcao ==1){
                opcaoRSA = 1024;
                System.out.println("Informa uma frase");
                String inputRsa = input.next();
            //  rsa.RSA(inputRsa,opcao);

            }else if(opcao ==2){
                opcaoRSA = 2048;
                System.out.println("Informa uma frase");
                String inputRsa = input.next();
            //  rsa.RSA(inputRsa,opcao);

            }else{

                opcaoRSA = 4096;
                System.out.println("Informa uma frase");
                String inputRsa = input.next();
            //  rsa.RSA(inputRsa,opcao);
            }
                break;


        case 4:
            int opcaoHash;
            System.out.println("Informa as opção abaixo\n1.MD5\n2.SHA1\n3.SH256");
            opcao = input.nextInt();

            if(opcao ==1){
                System.out.println("Digite por favor");
                String frase = input.next();
            //  MD5 hashMD5 = new MD5();
            //  hashMD5.hashMD5(frase);

            }else if(opcao ==2){

                System.out.println("Digite por favor");
                String frase = input.next();
            //  SHA1 hashSHA1 = new SHA1();
            //  hashSHA1.hashSHA1(frase);

            }else{
                System.out.println("Digite por favor");
                String frase = input.next();
            //  SHA256 hashSHA256 = new SHA256();
            //  hashSHA256.hashSHA256(frase);;

            }
                break;
        }           
    }
}

I’m having trouble reading a txt file and encrypting its contents.

  • Another tip may be to use a void ler() function to read the file data, and when storing and encrypting, use the object file instead of the text file, which has extension . dat via fileOutputStream method. I hope I helped

  • 3

    "I’m having trouble" is pretty vague for the dimension code it has. Try to be descriptive, indicating where the error appears, in which method and/or line (if any). As well as what you expected and what you are getting.

  • 1

    Agreeing with Isac, set "problem". Couldn’t read the file? Couldn’t write the content after AES? Couldn’t call AES?

  • Is there an Exception? What error occurs when you try to read?

  • type, I have never used these classes, I am getting lost how to replace typed entry by reading the file. type swap System.out.println("Informs a phrase"); String inputAes = input.next(); by Filereader, unable to exchange

No answers

Browser other questions tagged

You are not signed in. Login or sign up in order to post.