So guys, I have a little problem with one of the workouts I have to do.
The teacher provided .doc
with instructions for a " Encryptor Program " that should mask the alphabet and encrypt a text (Enigma style). See Exercise Statement:
Your program should have a method for encrypting and decrypting a document.
In both methods, the text file should be loaded and the key (integer value 2 to 5) requested from the user.
Let's imagine that each letter has an integer value. We begin with the letter A
, which has 0
value. Then B
with value 1
, C
with value 2
and so on up to the letter Z
.
See the table below:
A B C D E F G H I J K L M N O P Q R S T U V W X Y Z
0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25
When encrypting a document we will do the following:
We convert the letter that we want to encrypt, putting in its place and letter that is in its position + key.
For example:
We want to convert the letter B
. Let's assume that the key entered by the user is 3
.
B
is in 1
position. The position of B
+ key results in 4
. Therefore, we will replace the letter B
with the letter E
.
If the word is BANANA
, using the key 3
, it will be converted to EDQDQD
.
If the value obtained by the sum of the position and the key is greater than 25 (letter Z), then it should continue at the beginning of the alphabet.
For example:
We want to convert the letter X
. Let's assume that the key entered by the user is 5
.
X
is in 23
position. The position of X
+ key results in 28
. If we go back to the beginning of the alphabet, the number 28
will fall in the letter C
. Therefore, we will replace the letter X
with the letter C
.
If the word is XUXU
, using the 5
key, it will be converted to CZCZ
.
In addition blanks should be converted to a sharp (#).
When we decrypt, we will reverse the process. "
I have already done a lot of the code, but some problems arise: in the output text, instead of " #
" it breaks a line between the words.
If you have any questions regarding the clarity of my explanation of the problem, please comment.
Follow the code:
import java.util.Scanner;
import java.io.File;
import java.io.PrintWriter;
import java.io.IOException;
public class trab3{
public static void main(String [] args){
inicio();
}
public static void inicio(){
try{
Scanner in = new Scanner(System.in);
System.out.println("\finforme o nomedoarquivo.txt:");
String arquivo = in.nextLine();
// o arquivo de entrada deve estar previamente colocado na pasta do projeto
File arquivoDeEntrada = new File(arquivo);
Scanner entrada = new Scanner(arquivoDeEntrada);
System.out.println("informe o nomedoarquivodesaida.txt:");
String arquivosaida = in.nextLine();
// para selecionar o nome do arquivo de saida
PrintWriter saida = new PrintWriter(arquivosaida);
int op = 0;
int chave;
do{
System.out.println("Digite 1 para criptografar o arquivo");
System.out.println("Digite 2 para descriptografar o arquivo");
op = in.nextInt();
if(op != 1 && op != 2){
System.out.println("Opção inválida.");
inicio();
} else {
System.out.println("informe a chave de 1 até 5");
chave = in.nextInt();
if(chave != 1 && chave != 2 && chave != 3 && chave != 4 && chave != 5){
System.out.println("Chave inválida");
inicio();
}
leituraEscritaDosArquivos(entrada, saida, op, chave);
}
} while (op != 1 && op != 2);
} catch(IOException e){
System.out.println("Erro com o arquivo. Tente novamente");
inicio();
}
}
public static void leituraEscritaDosArquivos(Scanner entrada, PrintWriter saida, int op, int chave){
while(entrada.hasNext()){
String palavra = entrada.next().toUpperCase();
String resultado = "";
int key = chave;
if(op == 1){
resultado = criptografa(palavra, key);
}
else{
resultado = descriptografa(palavra, key);
}
saida.println(resultado);
}
saida.close();
entrada.close();
System.out.println("Saída gerada com sucesso.");
}
public static String criptografa(String palavra, int chave){
String alfabeto = "ABCDEFGHIJKLMNOPQRSTUVWXYZ";
String resultado = "";
for(int i = 0; i < palavra.length(); i++){
if(palavra.indexOf(palavra.charAt(i)) == ' '){
char novaletra = '#';
resultado = resultado + novaletra;
}else{
int posicaodaletra = alfabeto.indexOf(palavra.charAt(i));
int novaposicao = posicaodaletra + chave;
if(novaposicao > 25){
char novaletra = alfabeto.charAt(posicaodaletra+chave-26);
resultado = resultado + novaletra;
}else{
char novaletra = alfabeto.charAt(novaposicao);
resultado = resultado + novaletra;
}
}
}
return resultado;
}
public static String descriptografa(String palavra , int chave){
String alfabeto = "ABCDEFGHIJKLMNOPQRSTUVWXYZ";
String resultado = "";
for(int i = 0; i < palavra.length(); i++){
if(palavra.indexOf(palavra.charAt(i)) == ' '){
char novaletra = '#';
resultado = resultado + novaletra;
}else{
int posicaodaletra = alfabeto.indexOf(palavra.charAt(i));
int novaposicao = posicaodaletra - chave;
if(novaposicao < 0){
char novaletra = alfabeto.charAt(posicaodaletra-chave+26);
resultado = resultado + novaletra;
}else{
char novaletra = alfabeto.charAt(novaposicao);
resultado = resultado + novaletra;
}
}
}
return resultado;
}
}