Encrypting an image using a Caesar cipher.

C#

MIT License

Two simple methods to encrypt/decrypt images.

Download (right click, save as, rename as appropriate)

Embed

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.IO;
using System.Windows.Forms;

        private void EncryptFile()
        {            
            OpenFileDialog dialog = new OpenFileDialog();
            dialog.Filter = "JPEG Files (*.jpeg)|*.jpeg|PNG Files (*.png)|*.png|JPG Files (*.jpg)|*.jpg|GIF Files (*.gif)|*.gif";
            dialog.InitialDirectory = @"C:\";
            dialog.Title = "Please select an image file to encrypt.";
            byte[] ImageBytes;
            if (dialog.ShowDialog() == DialogResult.OK)
            {
                ImageBytes = File.ReadAllBytes(dialog.FileName);

                for (int i = 0; i < ImageBytes.Length; i++)
                {
                    ImageBytes[i] = (byte)(ImageBytes[i] + 5);
                }

                File.WriteAllBytes(dialog.FileName, ImageBytes);
            }            
        }

        private void DecryptFile()
        {
            OpenFileDialog dialog = new OpenFileDialog();
            dialog.Filter = "JPEG Files (*.jpeg)|*.jpeg|PNG Files (*.png)|*.png|JPG Files (*.jpg)|*.jpg|GIF Files (*.gif)|*.gif";
            dialog.InitialDirectory = @"C:\";
            dialog.Title = "Please select an image file to decrypt.";
            byte[] ImageBytes;
            if (dialog.ShowDialog() == DialogResult.OK)
            {
                ImageBytes = File.ReadAllBytes(dialog.FileName);

                for (int i = 0; i < ImageBytes.Length; i++)
                {
                    ImageBytes[i] = (byte)(ImageBytes[i] - 5);
                }

                File.WriteAllBytes(dialog.FileName, ImageBytes);
            }            
        }