C:\> Rostislav Persion's Projects

.:: XOR Splitter ::.
C# XOR File Splitter


This program uses the XOR algorithm to break a file into two pieces. You cannot retrieve the old file until you merge the two pieces that were created earlier.

The way this software works is by generating a random byte for each byte of the input file. The random byte and the file byte are bitwise XORed together. The resulting value and and the random byte are both written to a separate file. There is no way to recover the original file without merging the two generated files together using XOR on each byte of each file.

Here is a mathematical example of how the algorithm works...

First byte of file: 125

Randomly generated byte: 212

Value after XOR on both values: 169

To get original value back, you must XOR 169 and 212: 125

This process is done for every byte of the file.





DOWNLOAD GUI C# CODE >>

DOWNLOAD CLASS/CONSOLE C# CODE >>

DOWNLOAD DLL LIBRARY >>



DLL LIBRARY USEAGE



1
2
3
4
using Persion.XOR;

XOR.XOR_SPLIT_FUNCTION(@"D:\phone1.jpg", @"D:\xorout1.xor", @"D:\xorout2.xor");
XOR.XOR_JOIN_FUNCTION(@"D:\xorout1.xor", @"D:\xorout2.xor", @"D:\out_phone_001.jpg");


C# GUI CODE ....


  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
 50
 51
 52
 53
 54
 55
 56
 57
 58
 59
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using System.IO;
using System.Threading;

namespace XOR_FILE_001
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }

        private void button1_Click(object sender, EventArgs e)
        {
            //open file dialog
            OpenFileDialog ofd1 = new OpenFileDialog();
            ofd1.Filter = "All Files (*.*)|*.*";
            ofd1.ShowDialog();
            textBox1.Text = ofd1.FileName;
        }

        private void exitToolStripMenuItem_Click(object sender, EventArgs e)
        {
            
        }

        private void button2_Click(object sender, EventArgs e)
        {
            //save file dialog
            SaveFileDialog sfd1 = new SaveFileDialog();
            sfd1.Filter = "All Files (*.*)|*.*";
            sfd1.ShowDialog();
            textBox2.Text = sfd1.FileName;
        }

        private void button3_Click(object sender, EventArgs e)
        {
            //save file dialog
            SaveFileDialog sfd2 = new SaveFileDialog();
            sfd2.Filter = "All Files (*.*)|*.*";
            sfd2.ShowDialog();
            textBox3.Text = sfd2.FileName;
        }

        private void button4_Click(object sender, EventArgs e)
        {
            //open file dialog
            OpenFileDialog ofd3 = new OpenFileDialog();
            ofd3.Filter = "All Files (*.*)|*.*";
            ofd3.ShowDialog();
            textBox4.Text = ofd3.FileName;
        }

        private void button7_Click(object sender, EventArgs e)
        {


            try
            {
                
                //status change
                status_text.Text = "LOADING FILE...";
                this.Refresh();
                Application.DoEvents();
                Thread.Sleep(1000);
                
                //load input
                byte[] inputArray = File.ReadAllBytes(textBox1.Text);

                //populate and encode output array
                Random rnd1 = new Random(Environment.TickCount);
                Queue<int> line1 = new Queue<int>();
                Queue<int> line2 = new Queue<int>();

                status_text.Text = "ENCODING FILE...";
                for (int i = 0; i < inputArray.Length; i++)
                {
                    byte offset = (byte)rnd1.Next(0, 256);
                    line1.Enqueue(offset);
                    byte encoded = inputArray[i];
                    line2.Enqueue(encoded ^ offset);

                }

                //convert queue to arrays
                int[] ar_line1 = line1.ToArray();
                int[] ar_line2 = line2.ToArray();

                byte[] byte_line1 = new byte[ar_line1.Length];
                byte[] byte_line2 = new byte[ar_line2.Length];

                for (int i = 0; i < ar_line1.Length; i++)
                {
                    byte_line1[i] = (byte)ar_line1[i];
                    byte_line2[i] = (byte)ar_line2[i];
                }

                //write to file
                status_text.Text = "WRITING FILES...";
                this.Refresh();
                Application.DoEvents();
                Thread.Sleep(1000);

                File.WriteAllBytes(textBox2.Text, byte_line1);
                File.WriteAllBytes(textBox3.Text, byte_line2);

                //done
                status_text.Text = "READY.";
                this.Refresh();
                Application.DoEvents();
                Thread.Sleep(1000);
            }
            catch(Exception e1)
            {
                //show error
                status_text.Text = "ERROR.";
                this.Refresh();
                Application.DoEvents();
                Thread.Sleep(1000);
                MessageBox.Show("ERROR SPLITTING FILE.\n\n" + e1.Message);
            }

        }

        private void button5_Click(object sender, EventArgs e)
        {
            //open file dialog
            OpenFileDialog ofd5 = new OpenFileDialog();
            ofd5.Filter = "All Files (*.*)|*.*";
            ofd5.ShowDialog();
            textBox5.Text = ofd5.FileName;
        }

        private void button6_Click(object sender, EventArgs e)
        {
            //save file dialog
            SaveFileDialog sfd6 = new SaveFileDialog();
            sfd6.Filter = "All Files (*.*)|*.*";
            sfd6.ShowDialog();
            textBox6.Text = sfd6.FileName;
        }

        private void button8_Click(object sender, EventArgs e)
        {
            try
            {
                //status change
                status_text.Text = "READING INPUT FILES...";
                this.Refresh();
                Application.DoEvents();
                Thread.Sleep(1000);

                //load input
                byte[] inputArray1 = File.ReadAllBytes(textBox4.Text);
                byte[] inputArray2 = File.ReadAllBytes(textBox5.Text);

                //status change
                status_text.Text = "MERGING FILES...";
                this.Refresh();
                Application.DoEvents();
                Thread.Sleep(1000);

                //validate sizes
                if (inputArray1.Length == inputArray2.Length)
                {

                    // combine and store output
                    Queue<byte> outputArray = new Queue<byte>();
                    for (int i = 0; i < inputArray1.Length; i++)
                    {
                        int outCombine = inputArray1[i] ^ inputArray2[i];
                        outputArray.Enqueue((byte)outCombine);
                    }

                    //load output array
                    byte[] ar_line2 = outputArray.ToArray();

                    //write to file
                    status_text.Text = "WRITING OUTPUT FILE...";
                    this.Refresh();
                    Application.DoEvents();
                    Thread.Sleep(1000);
                    File.WriteAllBytes(textBox6.Text, ar_line2);

                    //done
                    status_text.Text = "READY.";
                    this.Refresh();
                    Application.DoEvents();
                    Thread.Sleep(1000);

                }
                else
                {
                    //show error
                    status_text.Text = "ERROR.";
                    this.Refresh();
                    Application.DoEvents();
                    Thread.Sleep(1000);
                    MessageBox.Show("INPUT FILES ARE DIFFERENT SIZE.");
                }


            }
            catch(Exception e2)
            {
                //show error
                status_text.Text = "ERROR.";
                this.Refresh();
                Application.DoEvents();
                Thread.Sleep(1000);
                MessageBox.Show("ERROR MERGING FILES.\n\n" + e2.Message);
            }
        }

        private void fileToolStripMenuItem_Click(object sender, EventArgs e)
        {

        }

        private void fileToolStripMenuItem_Click_1(object sender, EventArgs e)
        {
            Application.Exit();
        }

        private void Form1_Load(object sender, EventArgs e)
        {

        }

        private void aBOUTToolStripMenuItem_Click(object sender, EventArgs e)
        {
            //load about form
            Form2 myNewForm = new Form2();
            myNewForm.ShowDialog(this);
        }
    }
}


C# CLASS WITHOUT GUI



  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
 50
 51
 52
 53
 54
 55
 56
 57
 58
 59
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.IO;

namespace Persion.XOR
{

    public class XOR
    {
        public static void XOR_SPLIT_FUNCTION(string input_file1, string output_file1, string output_file2)
        {
            try
            {


                //load input
                byte[] inputArray = File.ReadAllBytes(input_file1);

                //populate and encode output array
                Random rnd1 = new Random(Environment.TickCount);
                Queue<int> line1 = new Queue<int>();
                Queue<int> line2 = new Queue<int>();

                for (int i = 0; i < inputArray.Length; i++)
                {
                    byte offset = (byte)rnd1.Next(0, 256);
                    line1.Enqueue(offset);
                    byte encoded = inputArray[i];
                    line2.Enqueue(encoded ^ offset);

                }

                //convert queue to arrays
                int[] ar_line1 = line1.ToArray();
                int[] ar_line2 = line2.ToArray();

                byte[] byte_line1 = new byte[ar_line1.Length];
                byte[] byte_line2 = new byte[ar_line2.Length];

                for (int i = 0; i < ar_line1.Length; i++)
                {
                    byte_line1[i] = (byte)ar_line1[i];
                    byte_line2[i] = (byte)ar_line2[i];
                }

                File.WriteAllBytes(output_file1, byte_line1);
                File.WriteAllBytes(output_file2, byte_line2);

            }
            catch (Exception e1)
            {
                //show error
                Console.WriteLine("ERROR SPLITTING FILE.\n\n" + e1.Message);
            }

        }

        public static void XOR_JOIN_FUNCTION(string input_file1, string input_file2, string output_file1)
        {
            try
            {

                //load input
                byte[] inputArray1 = File.ReadAllBytes(input_file1);
                byte[] inputArray2 = File.ReadAllBytes(input_file2);

                //validate sizes
                if (inputArray1.Length == inputArray2.Length)
                {

                    // combine and store output
                    Queue<byte> outputArray = new Queue<byte>();
                    for (int i = 0; i < inputArray1.Length; i++)
                    {
                        int outCombine = inputArray1[i] ^ inputArray2[i];
                        outputArray.Enqueue((byte)outCombine);
                    }

                    //load output array
                    byte[] ar_line2 = outputArray.ToArray();

                    //write to file
                    File.WriteAllBytes(output_file1, ar_line2);
                }
                else
                {
                    //show error
                    Console.WriteLine("INPUT FILES ARE DIFFERENT SIZE.");
                }


            }
            catch (Exception e2)
            {
                //show error
                Console.WriteLine("ERROR JOINING FILES.\n\n" + e2.Message);
            }



        }

    }






    class Program
    {
        static void Main(string[] args)
        {
            XOR.XOR_SPLIT_FUNCTION(@"D:\phone1.jpg", @"D:\xorout1.xor",@"D:\xorout2.xor");
            XOR.XOR_JOIN_FUNCTION(@"D:\xorout1.xor", @"D:\xorout2.xor", @"D:\out_phone_001.jpg");
        }
    }




}