I'm having trouble with FileReader
and buffered. I've seen several tutorials plus some are very different from each other and I can not read my file to encrypt.
I have here my AES code:
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 options of encryption but I just put the above AES to not 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.