-2

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?

Hassaan Salik
  • 413
  • 4
  • 22
  • Have you checked with the debugger that the bytes in the decrypted byte array match the original bytes? (perhaps with a unit test?) – Rowland Shaw Dec 16 '15 at 17:06
  • Well, decryption is not done yet. But even if if they don't match, something should be displayed as an image. The length of encrypted byte array is around 5k but it doesn't generate any image. – Hassaan Salik Dec 16 '15 at 17:15
  • 2
    Why would you expect a potentially invalid byte array to always generate an image? – Rowland Shaw Dec 16 '15 at 17:17
  • That's the whole pint i guess. Is there any guarantee that correct encryption will always generate a valid byte array? How can I check if byte array is valid? – Hassaan Salik Dec 16 '15 at 17:25
  • 2
    If you encrypt a byte array, you will NOT be able to display it as an image. It's no longer an image, it's a bunch of bytes. – dbugger Dec 16 '15 at 17:58

1 Answers1

0

A bitmap file has some specific header information before the actual image. You will need to correctly create that information as the header to your encrypted bytes. Naturally the encrypted image itself will be a mish-mash of coloured pixels.

See BMP file format for details of the header you will need to construct.

rossum
  • 15,344
  • 1
  • 24
  • 38
  • As JPEG image files begin with `FF D8` and end with `FF D9`. What if I manually add these on start and at the end of byte array? I actually tried this but it didn't help. I tried to check if any exception is thrown while conversion as purposed [here](http://stackoverflow.com/questions/8349693/how-to-check-if-a-byte-array-is-a-valid-image). But no exception was caught. – Hassaan Salik Dec 16 '15 at 18:26
  • A JPEG file is compressed. An encrypted image will not match any compression format. You can experiment if you want to, but you will need some sort of uncompressed image format to work with. If BMP does not suit, then perhaps PNG might be better for you. – rossum Dec 16 '15 at 19:24