3
I am developing a routine that decrypts information from a ". txt" document using Tripledes.
But when decrypting it generates the following error:
Invalid data.
Stacktrace is like this:
in System.Security.Cryptography.CryptographicException.Throwcryptographicexception(Int32 hr) in System.Security.Cryptography.Utils. _Decryptdata(Safekeyhandle hKey, Byte[] date, Int32 Ib, Int32 cb, Byte[]& outputBuffer, Int32 outputOffset, Paddingmode Paddingmode, Boolean fDone) in System.Security.Cryptography.CryptoAPITransform.Transformfinalblock(Byte[] inputBuffer, Int32 inputOffset, Int32 inputCount) in System.Security.Cryptography.CryptoStream.Read(Byte[] buffer, Int32 offset, Int32 Count) in Encryptionportabilitieste.Encryptionxml_3des.Decrypttextfrommemory(Byte[] Data, Byte[] Key, Byte[] IV) na d: Internal Systems Projects Cecresp Cryptologythis Encryptionthis Encryptionthis Encryptionxml_3des.Cs:line 180
Follow code for better understanding, event:
private void btnDescriptografar_Click(object sender, EventArgs e)
{
try
{
TripleDES tripleDESalg = TripleDES.Create();
// Nome do arquivo completo.
string FileName = txtbArquivo.Text;
using (StreamReader sr = File.OpenText(FileName))
{
string path = @"c:\testeDescriptografado3DES.txt";
using (StreamWriter sw = File.CreateText(path))
{
string s = "";
while ((s = sr.ReadLine()) != null)
{
byte[] linhaBytes = new byte[s.Split('-').Count()];
int i = 0;
foreach (var item in s.Split('-'))
{
linhaBytes[i] = Convert.ToByte(item);
i++;
}
//Criptografia da string para dentro da memória buffer
string resultado = DecryptTextFromMemory(linhaBytes, tripleDESalg.Key, tripleDESalg.IV);
//Escrita para um novo arquivo.
sw.WriteLine(resultado);
}
}
}
MessageBox.Show("Arquivo descriptografado");
}
catch (Exception exc)
{
MessageBox.Show(exc.Message);
}
}
Method "Decrypttextfrommemory()":
public string DecryptTextFromMemory(byte[] Data, byte[] Key, byte[] IV)
{
try
{
MemoryStream msDecrypt = new MemoryStream(Data);
TripleDES tripleDESalg = TripleDES.Create();
CryptoStream csDecrypt = new CryptoStream(msDecrypt,tripleDESalg.CreateDecryptor(Key, IV),CryptoStreamMode.Read);
byte[] fromEncrypt = new byte[Data.Length];
//AQUI DISPARA-SE O ERRO!!!
csDecrypt.Read(fromEncrypt, 0, fromEncrypt.Length);
return new ASCIIEncoding().GetString(fromEncrypt);
}
catch (CryptographicException e)
{
Console.WriteLine("A Cryptographic error occurred: {0}", e.Message);
return null;
}
}
What is the reason for this error?
I don’t understand this line here:
csDecrypt.Read(fromEncrypt, 0, fromEncrypt.Length);
.fromEncrypt
at this point is empty in theory. It was not to read fromData
?– Leonel Sanches da Silva
Could send the lines of the file in question?
– Reiksiel
Gypsy, in case the "csDecrypt" holds the "msDecrypt", which holds the "Date". Understood?
– Matheus Bessa
@Reiksiel, the file I’m using is test. It has 3 lines with 3 random names. Nothing too much
– Matheus Bessa
@Matheusbessa, I found this link https://go4answers.webhost4life.com/Example/cryptographicexception-bad-data-209425.aspx. I could check, I think it might help?
– Reiksiel