patterncsharpMajor
I'm in your .zips crackin' your passwords
Viewed 0 times
crackinpasswordsyourzips
Problem
As an attempt to learn multithreading better, I wrote a program to crack the password of a ZIP file. It is sort of slow, processing a three-digit password of the 95 printable ASCII characters in about 1:45 minutes. This is my class that actually handles the cracking:
```
class DecryptPassword
{
private readonly List _charList;
private readonly int[] _currentPassword;
private readonly string _endPassword;
public DecryptPassword(List charList, string startPassword, string endPassword)
{
_charList = charList;
_endPassword = endPassword;
var passwordLength = Math.Max(startPassword.Length, endPassword.Length);
_currentPassword = startPassword.Select(c => _charList.IndexOf(c)).ToArray();
while (_currentPassword.Length != passwordLength)
{
_currentPassword = _currentPassword.Concat(new [] {0}).ToArray();
}
}
public string CalculatePassword()
{
while (true)
{
var password = GetPasswordAsString();
try
{
if (ZipFile.CheckZipPassword(Environment.GetFolderPath(Environment.SpecialFolder.Desktop) + "\\CrackMe3.zip", password))
{
return password;
}
}
catch (BadCrcException)
{
// For some reason, sometimes a BadCRCException is thrown.
// I have never had it thrown on the real password,
// but this may be an issue for that.
// My best guess is that the speed of access the file,
// or perhaps accessing it from multiple threads, is the issue
}
if (password == _endPassword) { break; }
CalculateNextPassword();
}
return null;
}
private void CalculateNextPassword()
{
for (var index = _currentPassword.Length - 1; index >= 0; index--)
{
if (_currentPassword[
```
class DecryptPassword
{
private readonly List _charList;
private readonly int[] _currentPassword;
private readonly string _endPassword;
public DecryptPassword(List charList, string startPassword, string endPassword)
{
_charList = charList;
_endPassword = endPassword;
var passwordLength = Math.Max(startPassword.Length, endPassword.Length);
_currentPassword = startPassword.Select(c => _charList.IndexOf(c)).ToArray();
while (_currentPassword.Length != passwordLength)
{
_currentPassword = _currentPassword.Concat(new [] {0}).ToArray();
}
}
public string CalculatePassword()
{
while (true)
{
var password = GetPasswordAsString();
try
{
if (ZipFile.CheckZipPassword(Environment.GetFolderPath(Environment.SpecialFolder.Desktop) + "\\CrackMe3.zip", password))
{
return password;
}
}
catch (BadCrcException)
{
// For some reason, sometimes a BadCRCException is thrown.
// I have never had it thrown on the real password,
// but this may be an issue for that.
// My best guess is that the speed of access the file,
// or perhaps accessing it from multiple threads, is the issue
}
if (password == _endPassword) { break; }
CalculateNextPassword();
}
return null;
}
private void CalculateNextPassword()
{
for (var index = _currentPassword.Length - 1; index >= 0; index--)
{
if (_currentPassword[
Solution
You're not seeing 95 threads because of how Tasks work. The documentation for the Task class says that "tasks typically run asynchronously on a thread pool thread". The thread pool will limit how many tasks will run at one time, so when you call
Since these are compute intensive threads, having more threads running than you've got hardware threads will actually slightly slow things down due to the extra task switches and related cache misses.
task.Start() when the pool has reached its limit that task won't start executing until one of the pool threads finishes running and becomes available.Since these are compute intensive threads, having more threads running than you've got hardware threads will actually slightly slow things down due to the extra task switches and related cache misses.
Context
StackExchange Code Review Q#113556, answer score: 36
Revisions (0)
No revisions yet.