I am having this issue, I am making a demo client-server application that include end-to-end encryption for word document. I can send word document without images properly but I can’t with files that contain images. I don’t think it is due to encryptor/decryptor because I can still encrypt and decrypt files that don’t contain images and it’s maybe due to sending and receiving function itself, here my code:
Sending files function:
private void SendFileButton_Click(object sender, RoutedEventArgs e)
{
Microsoft.Win32.OpenFileDialog openFileDialog = new Microsoft.Win32.OpenFileDialog
{
Filter = "Word Documents|*.docx"
};
if (openFileDialog.ShowDialog() == true)
{
string filePath = openFileDialog.FileName;
byte[] encryptedData = EncryptData(filePath);
stream.Write(encryptedData, 0, encryptedData.Length);
MessageBox.Show("File sent!");
}
}`
Receiving files function:
private void ReceiveFileButton_Click(object sender, RoutedEventArgs e)
{
byte[] buffer = new byte[client.ReceiveBufferSize];
int bytesRead = stream.Read(buffer, 0, buffer.Length);
byte[] encryptedData = new byte[bytesRead];
Array.Copy(buffer, encryptedData, bytesRead);
byte[] decryptedData = DecryptData(encryptedData);
Microsoft.Win32.SaveFileDialog saveFileDialog = new Microsoft.Win32.SaveFileDialog
{
Filter = "Word Documents|*.docx"
};
if (saveFileDialog.ShowDialog() == true)
{
string filePath = saveFileDialog.FileName;
File.WriteAllBytes(filePath, decryptedData);
MessageBox.Show("File received and saved!");
}
}`
I tried to check all over the encryptor, decryptor. I want to send encrypted word documents that contains images in order to decrypt it. When I tried to send them, and I click receive button, it say “padding is not invalid and cannot be removed”.
If you still want encryptor, decryptor, here they are:
private byte[] EncryptData(string inputFilePath)
{
byte[] documentBytes;
using (MemoryStream memoryStream = new MemoryStream())
{
using (WordprocessingDocument wordDoc = WordprocessingDocument.Open(inputFilePath, false))
{
wordDoc.Clone(memoryStream);
}
documentBytes= memoryStream.ToArray();
}
byte[] encryptedData;
using (Aes aes = Aes.Create())
{
aes.Key = aesKey;
aes.GenerateIV();
using (MemoryStream memoryStream = new MemoryStream())
{
memoryStream.Write(aes.IV, 0, aes.IV.Length);
using (CryptoStream cryptoStream = new CryptoStream(memoryStream, aes.CreateEncryptor(), CryptoStreamMode.Write))
{
cryptoStream.Write(documentBytes, 0, documentBytes.Length);
}
encryptedData = memoryStream.ToArray();
return encryptedData;
}
}
}
private byte[] DecryptData(byte[] data)
{
byte[] decryptedData;
using (Aes aes = Aes.Create())
{
aes.Key = aesKey;
byte[] iv = new byte[aes.BlockSize / 8];
Array.Copy(data, 0, iv, 0, iv.Length);
aes.IV = iv;
using (MemoryStream memoryStream = new MemoryStream(data, iv.Length, data.Length - iv.Length))
using (CryptoStream cryptoStream = new CryptoStream(memoryStream, aes.CreateDecryptor(), CryptoStreamMode.Read))
using (MemoryStream decryptedStream = new MemoryStream())
{
cryptoStream.CopyTo(decryptedStream);
decryptedData = decryptedStream.ToArray();
return decryptedData;
}
}
}
PIPO PoPI is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.