private void btnDecipher_Click(object sender, EventArgs e) { DateTime dt1 = DateTime.Now; MyCypher r = new MyCypher(this.Key, this.Rand); Color clr = Color.White; List <byte> file_bytes = new List <byte>(); Bitmap bmp = (Bitmap)pbOrg.BackgroundImage; byte[] bytes = this.ciphered_bytes; r.Decipher(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)", "Decipher done. Loading results.", totoal_time, performance_mbps); Application.DoEvents(); Bitmap bmp3 = 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]); bmp3.SetPixel(x, y, clr); k += 3; } } pbDeciphered.BackgroundImage = (Image)bmp3; MessageBox.Show(r.GetStat()); //MyCypher mcc = new MyCypher("123", "456"); //mcc.Decipher(ref this.cip_bytes); //File.WriteAllBytes(Application.StartupPath + "\\B.jpg", cip_bytes); }
public bool DoUnpack(string DestRootDirPath, string SourceFileName, 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 uint crc = 0; uint crc_check = 0; Prompt(string.Format("Unpacking...")); if (Directory.Exists(DestRootDirPath) == true) { try { Directory.Delete(DestRootDirPath, true); } catch (Exception ex) { MessageBox.Show("Unable to remove existing directory\n" + ex.Message); return(false); } } else { Directory.CreateDirectory(DestRootDirPath); } byte[] buff_read = new byte[1024 * 1024]; List <string> Log = new List <string>(); BinaryReader br = null; BinaryWriter bw = null; try { // if (File.Exists(SourceFileName) == false) return false; br = new BinaryReader(new FileStream(SourceFileName, FileMode.Open)); // Random seed is written in plain-text (it is not ciphered) ulong rnd_seed = br.ReadUInt64(); crc ^= GetCrc(rnd_seed); //-------------------------------------------------- MyCypher mc = new MyCypher(Psw, rnd_seed); crc_check = br.ReadUInt32(); ulong ciph_test = br.ReadUInt64(); byte[] bytes_cipher_test = BitConverter.GetBytes(ciph_test); crc ^= GetCrc(ref bytes_cipher_test); mc.Decipher(ref bytes_cipher_test); string ciph_test_str = ASCIIEncoding.ASCII.GetString(bytes_cipher_test); if (ciph_test_str != "CIPHEROK") { br.Close(); Log.Add("Invalid Password"); Prompt(string.Format("Invalid Password")); return(false); } ulong header_length = br.ReadUInt64(); byte[] bytes_header_length = BitConverter.GetBytes(header_length); crc ^= GetCrc(ref bytes_header_length); mc.Decipher(ref bytes_header_length); header_length = BitConverter.ToUInt64(bytes_header_length, 0); //-------------------------------------------------- byte[] bytes_header = new byte[header_length]; br.Read(bytes_header, 0, bytes_header.Length); crc ^= GetCrc(ref bytes_header); mc.Decipher(ref bytes_header); string header_str = ASCIIEncoding.ASCII.GetString(bytes_header); //-------------------------------------------------- KeyValPair kvp = new KeyValPair(';', ':'); kvp.Fill(header_str); int dir_cnt = kvp.GetValAsInt("D.CNT"); Prompt(string.Format("Directory Count: {0}", dir_cnt)); string dir = ""; for (int i = 0; i < dir_cnt; i++) { dir = DestRootDirPath + kvp.GetVal(string.Format("D.{0}", i)); Directory.CreateDirectory(dir); } int file_cnt = kvp.GetValAsInt("F.CNT"); Prompt(string.Format("File Count: {0}", file_cnt)); string file_full_name = ""; long file_offset = 0; long file_length = 0; for (int i = 0; i < file_cnt; i++) { string rel_file_name = kvp.GetVal(string.Format("F.{0}", i)); file_full_name = DestRootDirPath + rel_file_name; Prompt(string.Format("Unpack File: {0}", file_full_name)); bw = new BinaryWriter(new FileStream(file_full_name, FileMode.Create)); file_offset = kvp.GetValAsInt64(string.Format("F.{0}.O", i)); file_offset += (long)header_length + 8 + 8 + 8 + 4 /*CRC*/; file_length = kvp.GetValAsInt64(string.Format("F.{0}.L", i)); br.BaseStream.Seek(file_offset, SeekOrigin.Begin); int cnt = 0; long totlal_rem = file_length; int read_cnt = 0; while (true) { read_cnt = buff_read.Length; if (totlal_rem < read_cnt) { read_cnt = (int)totlal_rem; } cnt = br.Read(buff_read, 0, read_cnt); totlal_rem -= cnt; crc ^= GetCrc(ref buff_read, cnt); mc.Decipher(ref buff_read, cnt); bw.Write(buff_read, 0, cnt); if (cnt == 0) { break; } if (totlal_rem == 0) { break; } } bw.Close(); } Prompt("--------------------------------"); if (crc_check != crc) { Prompt("CRC Missmatch!"); return(false); } 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)); return(false); } }