Question:
The professor asked as the semester's work a code in C# that encrypts a text from a file.
The code isn't polished or anything, I'm just doing the rough stuff then I'll add more stuff, but my problem is that every time the last letter of my text comes out ?
.
For example:
Good night > Encrypts > Decrypts > Good night?
There are two codes, one to encrypt and the other to decrypt:
Encrypt:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.IO;
namespace Cript
{
class Program
{
static void Main(string[] args)
{
//declaração das variáveis
string sDrive, sArquivo = "", sPalavra, sTexto = "",sOpcao;
//método write escreve na tela do usuario
Console.WriteLine("|---------------------------------------------------|");
Console.WriteLine("| Gostaria de Criptografar Uma Mensagem ? (Sim/Não) |");
Console.WriteLine("|---------------------------------------------------|");
Console.Write("--->>>>>>>>");
//Aqui é feito uma conversão, pois a opcao é inicialmente uma string
sOpcao = Console.ReadLine();
//PEDE VALORES DO ARQUIVO A SER CRIADO
Console.Write("Digite em Qual driver o arquivo sera salvo: ");
sDrive = Console.ReadLine();
Console.Write("Digite o Nome do Arquivo(Sem a Extensão .txt): ");
sArquivo = Console.ReadLine();
//depois da conversão o switch verifica a opcao digitada
if ((sOpcao == "s") || (sOpcao == "sim") || (sOpcao == "S" ) || (sOpcao == "SIM") || (sOpcao == "Sim"))
{
Console.Write("Entre com a mensagem para ser criptografada: ");
//sPalavra é a variavel que o usuario vai digitar.
sPalavra = Console.ReadLine();
int[] iAscii = new int[sPalavra.Length];
char[] cChar = new char[sPalavra.Length];
//enquanto a palavra for menor que i
for (int i = 0; i < sPalavra.Length; i++)
{
//Transforma o texto no codigo ASCII dele e retira 5 posições na tabela ASCII (para criptografar)
iAscii[i] = ((int)sPalavra[i]);
iAscii[i] = iAscii[i] + 5;
cChar[i] = ((char)iAscii[i]);
sTexto = sTexto + cChar[i];
}
// Cria e Grava o Arquivo
sArquivo = sDrive + ":" + sArquivo + ".txt";
using (StreamWriter arquivo = File.CreateText(sArquivo))
{
arquivo.WriteLine(sTexto);
arquivo.Close();
}
// Mostra na Tela o Arquivo Criado
Console.WriteLine("Arquivo Criado Como: " + sArquivo);
Console.ReadKey();
}
}
}
}
Decrypt:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.IO;
namespace DeCrypt
{
class Program
{
static void Main(string[] args)
{
//declaração das variáveis
string sDrive, sArquivo, sPalavra = "", sTexto = "",sOpcao;
//método write escreve na tela do prompt do usuario
Console.WriteLine("|---------------------------------------------------|");
Console.WriteLine("| Gostaria de Descriptografar Uma Mensagem ? (S/N) |");
Console.WriteLine("|---------------------------------------------------|");
Console.WriteLine("--->>>>>> ");
//Aqui é feito uma conversão, pois o opcao é inicialmente uma string
sOpcao = Console.ReadLine();
//Aqui, A pessoa digitara o nome e o drive do arquivo
Console.WriteLine("Letra do Drive do Arquivo Instalado: ");
sDrive = Console.ReadLine();
Console.WriteLine("Digite o Nome do Arquivo(Sem Extensão): ");
sArquivo = Console.ReadLine();
sArquivo = sDrive + ":" + sArquivo + ".txt";
if (!File.Exists(sArquivo))
{
Console.WriteLine("Arquivo " + sArquivo + " não Existe." );
}
else
{
Console.WriteLine("O ARQUIVO DESCRIPTOGRAFADO FICOU ASSIM: ");
// ABRE ARQUIVO TEXTO
sPalavra = File.ReadAllText(sArquivo);
//depois da conversão o switch verifica a opcao digitada
switch (sOpcao)
{
//caso a opcao escolhida for 2
case "s":
int[] iAscii = new int[sPalavra.Length];
char[] cChar = new char[sPalavra.Length];
//enquanto a palavra for menor que i
for (int i = 0; i <= sPalavra.Length; i++)
{
//Transforma o texto no codigo ASCII dele e Acrescenta 5 posições na tabela ASCII (para criptografar)
iAscii[i] = ((int)sPalavra[i]);
iAscii[i] = iAscii[i] - 5;
cChar[i] = ((char)iAscii[i]);
sTexto = sTexto + cChar[i];
}
Console.WriteLine(sTexto);
Console.ReadKey();
break;
}
}
}
}
}
Answer:
Your encryption routine is working, as this example in dotNETFiddle can demonstrate:
https://dotnetfiddle.net/WIouyV
using System;
public class Program
{
public static void Main()
{
string sPalavra, sTexto = "";
Console.WriteLine("Criptografando...");
sPalavra = "Boa noite";
int[] iAscii = new int[sPalavra.Length];
char[] cChar = new char[sPalavra.Length];
for (int i = 0; i < sPalavra.Length; i++)
{
iAscii[i] = ((int)sPalavra[i]);
iAscii[i] = iAscii[i] + 5;
cChar[i] = ((char)iAscii[i]);
sTexto = sTexto + cChar[i];
}
Console.WriteLine(sTexto);
Console.WriteLine("Descriptografando...");
sPalavra = "Gtf%stnyj";
sTexto = "";
iAscii = new int[sPalavra.Length];
cChar = new char[sPalavra.Length];
for (int i = 0; i < sPalavra.Length; i++)
{
iAscii[i] = ((int)sPalavra[i]);
iAscii[i] = iAscii[i] - 5;
cChar[i] = ((char)iAscii[i]);
sTexto = sTexto + cChar[i];
}
Console.WriteLine(sTexto);
}
}
Encrypting…
Gtf%stnyj
Decrypting…
Goodnight
What happens is that you are using the TextWriter.WriteLine
method, which automatically adds the end-of-line marker ( Environment.NewLine
) composed of the Carriage Return (ASCII 13) and Line Feed (ASCII 10) characters, thus increasing its string from 9 to 11 characters ( "Boa noite"
+ CR
+ LF
).
I can't test it right now, but I think your file reading routine is including this marker in the 'decryption' loop – what would be a possible cause of the '?' character return.