private void btnBrute_Click(object sender, EventArgs e) { var key = parseByteArray(txtSearch.Text); if (key == null) { MessageBox.Show(@"Error with search string!"); } else { try { var fs = File.OpenRead(_filePath); var blockSize = Int32.Parse(txtSize.Text); var blocks = (int)fs.Length / blockSize; var block = new byte[blockSize]; setHashAlgorithm(); progressBar.Maximum = blocks * blockSize; progressBar.Value = 0; var sb = new StringBuilder(); for (var i = 0; i < blockSize; i++) // Each iteration the starting offset is different { fs.Seek(i, SeekOrigin.Begin); var readBytes = 0; var blockCount = blocks; do { var pos = fs.Position; readBytes = fs.Read(block, 0, blockSize); var hash = _ha != null?_ha.ComputeHash(block) : CRC16.GetCRC(block); if (memcmp(key, hash, key.Length) == 0) //are equal { sb.Append("@" + pos.ToString("X7") + Environment.NewLine); } blockCount--; progressBar.PerformStep(); } while (readBytes == blockSize && blockCount != 0); } // Show results txtList.Text = sb.Length == 0 ? @"Search Key not found!" : sb.ToString(); fs.Close(); } catch (Exception ex) { MessageBox.Show(ex.Message); } } }
// ReSharper restore AccessToStaticMemberViaDerivedType private void btnCompute_Click(object sender, EventArgs e) { try { var fs = File.OpenRead(_filePath); var blockSize = chkEntireFile.Checked ? fs.Length : Int32.Parse(cbComputeBlockSize.Text); var blocks = chkEntireFile.Checked ? 1 : Int32.Parse(txtBlocks.Text); var block = new byte[blockSize]; setHashAlgorithm(); progressBar.Maximum = (blocks > 0 ? blocks : (int)(fs.Length / blockSize)); progressBar.Value = 0; var sb = new StringBuilder(); fs.Seek(Int32.Parse(txtOffset.Text), SeekOrigin.Begin); int readBytes; do { var pos = fs.Position; readBytes = fs.Read(block, 0, (int)blockSize); var hash = _ha != null?_ha.ComputeHash(block) : CRC16.GetCRC(block); sb.Append("@" + pos.ToString("X7") + ": " + byteArrayToString(hash) + Environment.NewLine); blocks--; progressBar.PerformStep(); } while (readBytes == blockSize && blocks != 0); // Show results txtList.Text = sb.ToString(); fs.Close(); } catch (Exception ex) { MessageBox.Show(ex.Message); } }
private void superBruteForce_DoWork(object sender, DoWorkEventArgs e) { var worker = (BackgroundWorker)sender; try { var fileBuffer = File.ReadAllBytes(_filePath); worker.ReportProgress(0, new ValueObject(fileBuffer.Length)); for (var blockSize = 64; blockSize <= fileBuffer.Length; blockSize += 4) { worker.ReportProgress(1, new ValueObject(fileBuffer.Length - blockSize)); for (var offset = 0; offset < fileBuffer.Length - blockSize; offset += 4) { if (worker.CancellationPending) { e.Cancel = true; return; } var hash = _ha != null?_ha.ComputeHash(fileBuffer, offset, blockSize) : CRC16.GetCRC(fileBuffer, offset, blockSize); if (_searchKey[0] == hash[0]) // 1:256 probability { if (memcmp(_searchKey, hash, _searchKey.Length) == 0) //key found!!! { e.Result = "@" + offset.ToString("X7") + " of " + blockSize + " : " + byteArrayToString(hash); return; } } if (!chkHighCPU.Checked && (offset % 64) == 0) { System.Threading.Thread.Sleep(1); //let the cpu cool off } worker.ReportProgress(11, new ValueObject(offset)); } worker.ReportProgress(10, new ValueObject(blockSize)); } e.Result = "Search key not found."; } catch (Exception ex) { MessageBox.Show(ex.Message); } }