4
So, guys, I’m having a little problem with one of the work exercises that I have to do.
The teacher provided a .doc
with the instructions for a "Cryptographer" that you should apply a mask to the alphabet and encrypt a text (Enigma style). See the exercise statement:
Your program must have a method to encrypt and decrypt a document.
In both methods, the text file must be loaded and the key (integer value from 2 to 5) requested from the user.
Let’s imagine that each letter has an integer value. We start with the letter A
, that holds value 0
. Next B
worthwhile 1
, C
with the value 2
and so on 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 we encrypt a document we will do the following:
We convert the letter we want to encrypt, putting in its place and letter that is in its position + key.
For example:
We want to convert the lyrics B
. Let’s assume that the key entered by the user is 3
.
B
is in the position 1
. The position of B
+ the key results in 4
. Soon, we will replace the letter B
, by the letter E
.
If the word is BANANA
, using the key 3
, she will be converted to EDQDQD
.
If the value obtained by the sum of position and key is greater than 25 (letter Z), then it should continue at the beginning of the alphabet.
For example:
We want to convert the lyrics X
. Let’s assume that the key entered by the user is 5
.
X
is in the position 23
. The position of X
+ the key results in 28
. If we go back to the beginning of the alphabet, the number 28
will fall to the letter C
. Soon, we will replace the letter X
, by the letter C
.
If the word is XUXU
, using the key 5
, she will be converted to CZCZ
.
In addition white spaces must be converted to a sharp (#).
When decrypting, we will do the reverse process."
I’ve done most of the code, but some problems arise: in the output text, instead of appearing "#
" he breaks a line between words.
If you have any questions regarding the clarity of my explanation of the problem, please comment.
Follows 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;
}
}
See if the problem is time to save the file or its encryption, make it appear on the console before saving and see where the problem is.
– Roberto Araujo