Obfuscating images - delphi

I want to distribute a few images and not allow others to see them unless they are using my program. My intention will be to use JPG files in which I will alter the header so other image viewers cannot read them anymore. For example I can delete the bytes 7-10 which are the magic signature for JPG. Later, my program will reconstruct the header and show the JPG file.
Question: how do I do this on the fly, without reading the “broken” JPG file, restoring the header, saving the good file to disk and then re-loading it as a “good” JPG file?

Load the "broken" file into a TMemoryStream, patch the bytes in-memory, and use TGraphic.LoadFromStream() to load the fixed JPG file.

Encrypt them. Load the encrypted image, decrypt in memory and then do the loadfromstream like mghie suggested.

Why not just encrypt the images with a private key and distribute your public key to the people you want to view the images? much easier to distribute a public key than writing some custom software and distributing that. Don't forget; anything displayed on screen can be screen captured. The fact a custom-mangled JPEG can only be displayed with your app is no protection. Also don't forget; people can simply distribute your software with the mangled image.

Mghie's answer is about as good as you'll find, but it's not likely to be too effective. If someone wants to look at your images and they know anything about image formats, they'll open it in a hex editor and most likely recognize what they see as a JPEG with the magic header removed.
If you really want to keep someone from viewing your images, construct your own image format, (it's not as hard as it sounds, really,) and put as little metadata in as possible, and then hope that works. Or encrypt them, or put them into an archive, (construct your own archive format for best results,) and hope that works.
Thing is, ultimately, anything that's encoded has to be decoded before it can be shown, and any sufficiently-talented hacker can trace their way through your decoding routine and figure out how it works. Why are you trying to hide things from your users anyway?

You could make it more difficult for them by byte packing the images as encrypted resources. But like anything else if they have access to the files that could get the images out. It just depends on how much effort the are willing to use.

Depending on how secure you need it to be you could do something as simple as obfuscate the file extension to an extension that is only opened with your application. This will only work if its not super secret images that you are changing.

Related

What are some options for handling image uploading/compression in ASP?

please bear with me as I'm not trying to frustrate anyone with inane questions, and I did google search this but I couldn't really find anything recent or helpful.
I am a novice programmer and I am using a classic asp web application. I just enabled the users to upload and download images, but I'm quickly regretting it as it's eating up all of the router bandwidth. I am finding my solution inadequate, so I wanted to start over.
My desire is threefold with this functionality:
Compression. I understand that this is impossible to do BEFORE uploading without some kind of Java/Silverlight/Flash portion of the application to handle uploads, correct? What is the common way most places go about this? Just allow regular file uploads and compress once they are on the server?
Resizing. I want to resize all images before they are uploaded to a reasonable size, instead of just telling users that try and upload huge camera images that they can't upload. I figure I just want to let them upload and have it resize for them before uploading. Does this functionality exist already?
Changing filetype. I want to allow users to upload all image file types but make them .jpg on the server after the upload.
With these three requirements, how hard is it to implement something like this in just pure code and libraries? Would it be better to just use a 3rd party plugin, such as ASPjpeg or ASPupload? Have you encountered something similar, and what was your solution?
Thanks.
Take a look at ASPJpeg and ASPUpload from Persits. We use these components to upload a full size image (can be png even though the library is "ASPJpeg"), resize it to several different sizes we need on our site, then store the resized images on the server in a variety of folders. The ASPUpload component is a little tricky but if you follow their sample code you'll be fine.
I never found a good component for decompressing uploaded zip files and had to write my own, which I've since abandoned. In the end with upload speeds increasing and storage getting so cheap, it started to matter less and less that the files were compressed before being uploaded.
EDIT: Just noticed you mentioned these components in your question. Consider this an endorsement of your idea to use them. :-)

Where to store decrypted files?

I am encrypting downloaded files and saving them locally in app's documents directory.
To read them you must decrypt those file and store some where temporarily.
My concerns are:
1.if I store them in doc directory for time they are being used, for that time window one can get those files using tools like iExplorer.
2.My idea is to store them in memory for the time they are being used and flush the vault after use.
This option is good for small files but for large files say 50 MB or video of 100 MB, I am afraid that app will receive memory warning in result will terminate abruptly.
I want to know the best approach for doing this.
There is no perfect security storing local files in a safe way. If a person has full access to the device, he can always find a way to decrypt the files, as long as your application is able to decrypt it.
The only question is: How much effort is necessary to decrypt the files?
If your only concern is that a person may use iExplorer to copy and open these files, a simple local symmetric encryption will do the trick.
Just embed a random symmetric key in your application and encrypt the data block by block while you download it.
You can use the comfortable "Security Transforms" framework to do the symmetric encryption. There are some good examples in the Apple Documentation.
When you load the files, you can use the same key to decrypt them while you load them from the file system.
Just to make things clear: This is not a perfect protection of the files. But to decrypt the files, one has access to your app binary. Analyse this binary in a debugger and searching for the decryption part to extract your symmetric key. This is a lot effort necessary just to decrypt the files.
Split your files into smaller sizes before saving them, then decrypt on load.
Later edit: I noticed this is mentioned in the comments. I agree splitting files isn't the easiest thing in the world, but presumably you'll only need this for video. About 100MB is a lot of text or audio. If your PDF weights as much, it's probably scanned text, and you can change it into a series if images.
And yes, splitting better be done server-side, don't want the user waste battery in video processing.
Decrypt them, obfuscate them with a toy algorithm (e. g. XOR with a constant block), and store them in documents. When needed, load and decrypt.
Since the problem has no solution in theory (a determined enough attacker can read your process memory after all), it's as good a solution as any.

Save image and encrypt it so that no one can access

Currently I'm using this tutorial now but I need some help in saving it to an image. For security purposes, is it possible to encrypt the file. And also, only my app will access the said file. I'm asking for help for saving it as image but encrypted, as well as how can I access the encrypted file. Thanks!
It is possible to encrypt any file. The difficult part of your question is if you really want the resulting output to still be an image, rather than a random seeming array of bytes. Image files have strict formats while encrypted files appear to be random and unformatted.
It is not clear, but you may be wanting something like steganography, where one image is hidden inside another. The hidden file can either be your plain image, or the encrypted version of your image. If you hide the encrypted version, then obviously you will need to decrypt it before you can display it.

How to write or modify EXIF data for an existing image on the filesystem, without loading the image?

I've asked similar questions before, but have not received a definitive answer. Seems that there must be a way to simply add/modify metadata to an image without loading the image into memory, without having to deal with directly reading bits.
Seems like ways exist when using CMSampleBufferRefs, but I need to be able to do this with a regular image already saved to disk.
For instance, given a very large png at /Documents/photo.png, I want to modify its exif metadata without having to load that image.
You can use libexif - I've had success with compiling it for iOS before. With libexif, you can modify any image's EXIF metadata.
If you know how to modify the EXIF, you can modify the binary data directly from the file. Just replace in the image the binary portion with the new one.
I don't know if objective-c permit this, but in ansi c should be simple. The complicate part is to identify the exact part to change.

JPEG built-in checksum / fingerprint?

I'm putting together a script to find remove duplicates in a large library of images. At the moment I'm doing a two pass filter of first finding files of the same size and then doing a sha256 on a 10240 byte piece of the file to get a fingerprint of the files with the same size (code here).
It works well, but I'm guessing there are probably checksums built in to the jpeg format that I could use instead of doing the sha256.
Does anyone know if there are checksums or other components that could act as checksums / fingerprints? If so, is there an efficient way to access them?
I don't think the JPEG specification includes any kind of checksum in the way you're describing.
A JPEG can contain a thumbnail as part of its EXIF metadata, though. It's not a perfect indicator, since it's possible for two different images to have the same thumbnail. There's at least one documented case of a thumbnail not being replaced after the image had undergone substantial modifications, said thumbnail revealing much more than the publisher had intended.
Its been awhile since I've dug into the IJG library, but I don't think there's an easy class member or function call you can use there to check for some type of fingerprint. You could use the built in EXIF tags if you can control the encoding of the images...
I'm just built a very similar script. I don't want to checksum metadata I want to see if the actual images are duplicates even if tags have been modified. Best for that is not to sort by size, but do sort by the checksum istelf. I use jhead to remove metadata and then checksum the whole file (but I also thought about just doing part of it, but actually I don't think it saves much time). jhead doesn't use shared memory (pipes) and does overwrite so I just copy the file to shared memory first. I place the checksum in the ImageDescription field for later faster retrieval. Obviously this also allows to check image integrity later and is part of why I checksum the whole thing. Tip: exiv2 is MUCH faster for reading and writing the metadata than exiftool for one at a time decision based manipulation.
In JPEG standard(ITU-T.81) i believe there isn't any field/syntax element which has a checksum or such, for the whole compressed jpeg image file. Unless a customised application puts such filed in the Application segment, or as meta data for which segments are provided in the standard.
So to serve your purpose, what you are doing is one soln.
Other could be some kind a application wrapper which will call some binary file compare utlitiy (like beyond compare, or even a windows command fc /b) and check the result of that compare utility and take the decision u want to.
-AD
One way you could perform is reduce all images to a fixed size and store that as a thumbnail. Then the image comparison would compare similar sized images and give you a chance of being a duplicate - useful if you have cropped (unless cropped heavily) or resized images and want to find those 'duplicates'.
In the XMP specification there are document ID and version ID which should uniquely identify the version of the image.
The problem with these (and with any other metadata-based identification method) is that it might not be respected by some applications that can change the content of the jpeg updating the metadata accordingly.

Resources