public string Unlock(IDigitalLock digitalLock) { string currentChars = digitalLock.ReadAll(); int lockSize = currentChars.Length; int cipherLength = digitalLock.GetCipherLength(); digitalLock.Reset(); TryAllCombinations(digitalLock, "", 0, lockSize); return(digitalLock.ReadAll()); }
public string Unlock(IDigitalLock digitalLock) { string _lock = String.Empty; int characterCount = 0; char temp = digitalLock.Read(0); digitalLock.Turn(TurnDirection.Forward, 0, 1); characterCount++; while (temp != digitalLock.Read(0)) { digitalLock.Turn(TurnDirection.Forward, 0, 1); characterCount++; } for (int i = 0; i < Math.Pow(characterCount, digitalLock.GetCipherLength()); i++) { string tryValue = Complete(i, digitalLock.GetCipherLength()); digitalLock.Reset(); for (int j = digitalLock.GetCipherLength() - 1; j >= 0; j--) { digitalLock.Turn(TurnDirection.Forward, j, tryValue[j] - '0'); } if (!digitalLock.IsLocked()) { _lock = digitalLock.ReadAll(); } } return(_lock); }
public string Unlock(IDigitalLock digitalLock) { try { int cipherLength = digitalLock.GetCipherLength(); int numberOfDigits = GetNumberOfDigits(digitalLock); int [] a = new int[cipherLength]; for (int i = 0; i < cipherLength; i++) { a[i] = 0; } do { for (int j = 0; j < numberOfDigits; j++) { digitalLock.Lock(true); if (!digitalLock.IsLocked()) { return(digitalLock.ReadAll()); } digitalLock.Turn(TurnDirection.Forward, 0, 1); a[0]++; } for (int i = 0; i < (cipherLength - 1); i++) { if (a[i] == numberOfDigits) { a[i] = 0; a[i + 1]++; digitalLock.Turn(TurnDirection.Forward, i + 1, 1); } } if (a[cipherLength - 1] == numberOfDigits) { break; } } while (true); return("Şifre çözülemedi."); } catch (Exception e) { FileLogger.Log(e.Message + " -- şifre çözme sırasında hata oluştu."); throw; } }
public void TryAllCombinations(IDigitalLock digitalLock, string pwd, int position, int size) { if (position < size) { for (int i = 0; i < digitalLock.GetCipherLength(); i++) { digitalLock.Turn(TurnDirection.Forward, position, 1); if (!digitalLock.IsLocked()) { break; } else { Console.WriteLine(digitalLock.ReadAll()); TryAllCombinations(digitalLock, pwd + i, position + 1, size); } } } }