Example #1
0
        private void btnCipher_Click(object sender, EventArgs e)
        {
            MyCypher    r          = new MyCypher(this.Key, this.Rand);
            Color       clr        = Color.White;
            List <byte> file_bytes = new List <byte>();
            Bitmap      bmp        = (Bitmap)pbOrg.BackgroundImage;

            if (bmp == null)
            {
                MessageBox.Show("Input file is not loaded.");
                return;
            }

            for (int x = 0; x < bmp.Width; x++)
            {
                for (int y = 0; y < bmp.Height; y++)
                {
                    clr = bmp.GetPixel(x, y);
                    file_bytes.AddRange(new byte[] { clr.R, clr.G, clr.B });
                }
            }
            byte[] bytes = file_bytes.ToArray();

            DateTime dt1 = DateTime.Now;

            r.Cipher(ref bytes);
            DateTime dt2 = DateTime.Now;

            double totoal_time      = dt2.Subtract(dt1).TotalMilliseconds;
            double performance_mbps = 0d;

            performance_mbps  = bytes.Length * 8d;       // total bits
            performance_mbps /= 1024d;                   // total kilo bits
            performance_mbps /= 1024d;                   // total mega bits
            performance_mbps /= (totoal_time / 1000d);   // total mega bits per second
            lblMessage.Text   = string.Format("Message: {0}\nTotal Time(ms): {1:N0}\nPerformance:{2:N3}(Mega bit/Sec)", "Cipher done. Loading results.", totoal_time, performance_mbps);
            Application.DoEvents();

            Bitmap bmp2 = new Bitmap(bmp.Width, bmp.Height);
            int    k    = 0;

            for (int x = 0; x < bmp.Width; x++)
            {
                for (int y = 0; y < bmp.Height; y++)
                {
                    clr = Color.FromArgb(bytes[k], bytes[k + 1], bytes[k + 2]);
                    bmp2.SetPixel(x, y, clr);
                    k += 3;
                }
            }
            pbCiphered.BackgroundImage = (Image)bmp2;
            this.ciphered_bytes        = bytes;

            MessageBox.Show(r.GetStat());

            //MyCypher mcc = new MyCypher("123", "456");
            //mcc.Cipher(ref this.cip_bytes);
        }
Example #2
0
        public bool DoPack(string SourceRootDirPath, string DestFileName, ulong RndSeed, string Psw)
        {
            // File construction:
            // 1- random-seed (64 bit ulong)     bytes 0,1,2,3,4,5,6,7
            // 2- crc (32 bit uint)              bytes 8,9,10,11
            // 3- CIPHEROK (64 bit string)       bytes 12,13,14,15
            // 4- header-length (64 bit ulong)   bytes 16,17,18,19,20,21,22,23
            // 5- header-data (string)           bytes 24,25, ...
            // 6- body


            if (SourceRootDirPath.Trim() == "")
            {
                return(false);
            }
            uint crc = 0;

            Prompt(string.Format("Paking..."));
            Prompt("--------------------------------");

            List <string> Log = new List <string>();
            BinaryWriter  bw  = null;
            BinaryReader  br  = null;

            byte[] buff_read = new byte[1024 * 1024];

            string source_file = null;

            if (File.Exists(SourceRootDirPath))
            {
                source_file = SourceRootDirPath;
            }

            try
            {
                MyCypher mc = new MyCypher(Psw, RndSeed);


                KeyValPair kvp          = ProvideHeader(SourceRootDirPath);
                byte[]     bytes_header = ASCIIEncoding.ASCII.GetBytes(kvp.GetStr());


                bw = new BinaryWriter(new FileStream(DestFileName, FileMode.Create));
                byte[] bytes_rnd_seed = BitConverter.GetBytes(RndSeed);

                byte[] bytes_cipher_test   = ASCIIEncoding.ASCII.GetBytes("CIPHEROK");
                ulong  header_length       = (ulong)bytes_header.Length;
                byte[] bytes_header_length = BitConverter.GetBytes(header_length);


                // Random seed is written in plain-text (it is not ciphered)
                bw.Write(bytes_rnd_seed);
                crc ^= GetCrc(ref bytes_rnd_seed);
                //--------------------------------------------------


                // CRC is written in plain-text (it is not ciphered)
                bw.Write(crc);
                //--------------------------------------------------

                mc.Cipher(ref bytes_cipher_test);
                bw.Write(bytes_cipher_test);
                crc ^= GetCrc(ref bytes_cipher_test);
                //--------------------------------------------------


                mc.Cipher(ref bytes_header_length);
                bw.Write(bytes_header_length);
                crc ^= GetCrc(ref bytes_header_length);
                //--------------------------------------------------


                mc.Cipher(ref bytes_header);
                bw.Write(bytes_header);
                crc ^= GetCrc(ref bytes_header);
                //--------------------------------------------------

                int    file_count     = Int16.Parse(kvp.GetVal("F.CNT"));
                string file_full_name = "";

                for (int i = 0; i < file_count; i++)
                {
                    if (source_file != null)
                    {
                        file_full_name = source_file;
                    }
                    else
                    {
                        file_full_name = SourceRootDirPath + "\\" + kvp.GetVal(string.Format("F.{0}", i));
                    }

                    Prompt(string.Format("Packing File: {0}", file_full_name));

                    FileInfo fi = new FileInfo(file_full_name);
                    if (File.Exists(file_full_name) == false)
                    {
                        Log.Add(string.Format("'{0}' not found", file_full_name));
                        continue;
                    }
                    br = new BinaryReader(new FileStream(file_full_name, FileMode.Open));
                    int cnt = 0;
                    while (true)
                    {
                        cnt = br.Read(buff_read, 0, buff_read.Length);
                        if (cnt == 0)
                        {
                            break;
                        }
                        mc.Cipher(ref buff_read, cnt);
                        bw.Write(buff_read, 0, cnt);
                        crc ^= GetCrc(ref buff_read, cnt);
                    }
                    br.Close();
                }
                bw.Seek(8, SeekOrigin.Begin);
                bw.Write(crc);
                bw.Close();
                Prompt("--------------------------------");
                return(true);
            }
            catch (Exception ex)
            {
                if (bw != null)
                {
                    try
                    {
                        bw.Close();
                    }
                    catch { }
                }
                throw new Exception(string.Format("Unable to pack folder '{0}'.\n{1}", ex.Message));
            }
        }
Example #3
0
        public Pack()
        {
            MyCypher mc = new MyCypher("123456789", this.GetRandomStr(10));

            mc.Cipher(ref ShredArray);
        }