Click here to Skip to main content
14,920,539 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I want to encrypt the image so that it is as an image file that has not changed or changed its header
But I have a problem with encryption is that the image does not open and I want it to open but on condition that it is confused

Can anyone help me with the appropriate code so that it is a cryptographic code and not an image processing

What I have tried:

//This code was used by an algorithm DES 

pictureBox1.Image = image;
           MemoryStream ms = new MemoryStream();
           image.Save(ms, System.Drawing.Imaging.ImageFormat.Jpeg);

           byte[] bytes = ms.ToArray();
           BitArray bitArray = new BitArray(bytes);
           for (var x = 5000; x < 9000; x++)
               textBox1.Text += Convert.ToInt16(bitArray[x]);
           MD5CryptoServiceProvider mds = new MD5CryptoServiceProvider();
           //UTF8Encoding utf8 = new UTF8Encoding();
           TripleDESCryptoServiceProvider tdes = new TripleDESCryptoServiceProvider();
           //tdes.Key = mds.ComputeHash(utf8.GetBytes(textBox1.Text));
           tdes.Mode = CipherMode.CBC;
           tdes.Padding = PaddingMode.PKCS7;
           ICryptoTransform tram = tdes.CreateEncryptor();
           encrypted = (tram.TransformFinalBlock(bytes, 0, (bytes.Length)));
           File.WriteAllBytes("E:/u.jpg", encrypted);
Updated 31-May-18 13:29pm
Gerry Schmitz 31-May-18 17:17pm
Zip it with a passord; simpler.
raddevus 31-May-18 18:33pm
But if I get your password will it still be secure? Will image still be encrypted? Sounds like a single point of failure to me.

Please, please, please know that everything I've just said it meant as comic relief. Also, please note that if you cannot understand this humor, it is only because you haven't applied the proper decryption algorithm. Try ROT-13 first and other super-secure algos first. :D
Gerry Schmitz 1-Jun-18 11:37am
What's the difference between "zip and password protected" and "encrypted" .... from a USER point of view?

Decryption implies a "decryption key". Does that have any semblance to a "password" that you can think of?

You find humour in this?

(Reverse the bytes; drop the extension; then zip and pw it; for all it matters).
raddevus 1-Jun-18 12:48pm
I was actually finding humor in the original question because it was basically a jumble of sentences. I'm still not sure what the original question was.
Zipping with a password sounds good.
Gerry Schmitz 1-Jun-18 13:00pm
And I was teasing you ... he, he.
raddevus 1-Jun-18 14:59pm
You definitely got me. Haha :)
Member 13780784 1-Jun-18 13:35pm
Did you mean that you did not understand the question or do you mean the language if you mean the language, I do not speak English very well because it is not my language
If you do not understand the question I will explain to you at the bottom and excuse me I'm still new in cryptography but I do not mean zip technology
raddevus 1-Jun-18 14:58pm
oh, no sorry, I meant the technology. I totally understand that others do not necessarily speak English as their first language. I wish I could read/write/understand another language well enough to ask tech questions in that language. THe original question just sounded interesting to me because of the encryption of an image. Nothing more. Thanks
Member 13780784 3-Jun-18 7:15am
no problem thank you very much

Basically, it's not worth the effort to make the image "all message up" visually.

It's virtually impossible to encrypt the image and have it still have the exact same dimensions as the original image. To do that, you'd be limited to weak encryption methods that result in a one-for-one byte, like a substitution cipher.
I will shorten my question all I wanted to show me the picture after the encryption like this

Block cipher mode of operation - Wikipedia[^]
Eric Lynch 1-Jun-18 14:54pm
The images in the referenced Wikipedia article are deceptive. There, they encrypted only the image data.

Almost all image file formats, including JPEG files, consist of a mix of data: image data, structural data, and meta data (e.g. the model of camera). The structural data identifies the file format and differentiates its parts.

To be able to open a file, following encryption, you would need to selectively avoid encrypting any structural data.

There is no built-in mechanism (of which I am aware) for avoiding structural data during encryption. This would be a fairly complicated program to write and would be dependent on the exact image file format.
Member 13780784 3-Jun-18 7:10am
You understood me and understood what I mean completely and conveyed my idea to all thank you
I understand that there is structural data that I have to output before encrypting the image but I did not succeed in that
Yes it is a complicated thing and I tried it a lot
Dave Kreskowiak 1-Jun-18 15:55pm
All of those ciphers are byte-for-byte replacements, meaning for exactly 1 byte that goes into the cipher, exactly 1 byte comes out. These ciphers are also the weakest forms of encryption there is, making them easily breakable.
Richard Deeming 4-Jun-18 10:56am
If you want to update your question, then click the green "Improve question" link and edit your question.

DO NOT post your update as a "solution".

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900