My requirement is to encrypt an image using AES. My approach is to convert image to Byte Array and apply encryption. After encryption convert the encrypted byte array back to image.
But in the last part no image is generated from byte encrypted array.
Image is loaded in byte array here
RandomAccessStreamReference rasr = RandomAccessStreamReference.CreateFromFile(storageFile);
var streamWithContent = await rasr.OpenReadAsync();
byte[] buffer = new byte[streamWithContent.Size];
await streamWithContent.ReadAsync(buffer.AsBuffer(), (uint)streamWithContent.Size, InputStreamOptions.None);
Encryption is done in the following code
IBuffer Buffer = CryptographicBuffer.CreateFromByteArray(buffer);
byte[] encrypted = null;
CryptographicBuffer.CopyToByteArray(CryptographicEngine.Encrypt(AES, Buffer, null),out encrypted);
return encrypted;
While byte array is converted back to image as
var pic = new BitmapImage();
var imageBytes = returned_value;
using (InMemoryRandomAccessStream ms = new InMemoryRandomAccessStream())
{
using (DataWriter writer = new DataWriter(ms.GetOutputStreamAt(0)))
{
writer.WriteBytes((byte[])imageBytes);
writer.StoreAsync().GetResults();
}
pic.SetSource(ms);
}
I tried to check the dimensions of resulted image by using pic.PixelWidth
and it simply 0.
Is this possible or any suggestion?