public override void Run(KExplorerNode folder, FileInfo[] files) { while ( true ) { string[] checkName = QuickDialog2.DoQuickDialog( "AccountRepo Check", "UserId","", "Password (optional)", ""); if ( checkName == null ) { return; } string filePath; byte[] ciphertext = null; try { filePath = files[0].FullName; FileStream fileStream = new FileStream(filePath, FileMode.Open, FileAccess.Read, FileShare.Read); using(BinaryReader reader = new BinaryReader(fileStream)) { ciphertext = reader.ReadBytes((int)fileStream.Length); } byte[] plaintext = null; IOlympCryptography olympCryptography = new OlympCryptography(); plaintext = olympCryptography.Decrypt(ciphertext, new CompressStreamReaderDelegate(CompressStreamReader)); string reallyPlain = System.Text.UTF8Encoding.UTF8.GetString( plaintext ); XmlDocument doc = new XmlDocument(); // Seems sometimes in the encoding, we get a garbage character at the beginning. if ( reallyPlain.StartsWith("<")) { doc.LoadXml( reallyPlain ); } else { doc.LoadXml( reallyPlain.Substring(1) ); } // If user enters nothing. Give him a message box of all the user ids. if ( checkName[0].Trim().Length == 0) { StringBuilder names = new StringBuilder(); XmlNodeList nameNodes = doc.SelectNodes("//UserState[@StateKey='PasswordHash']"); foreach ( XmlNode nameNode in nameNodes ) { string userId = nameNode.Attributes["UserId"].Value; XmlNode lockedState = doc.SelectSingleNode("//UserState[@StateKey='Locked' and @UserId='"+userId+"']"); bool locked = lockedState.Attributes["StateValue"].Value.Equals("True"); names.Append( userId + ((locked) ? " (locked);" : "; ")); } if ( MessageBox.Show(names.ToString(), "User ID's", MessageBoxButtons.OKCancel ) == DialogResult.Cancel ) { return; } else { continue; } } // They entered a name and a password. check it. check it for locked. If locked, prompt to unluck. else { XmlNode nameNode = doc.SelectSingleNode("//UserState[@StateKey='PasswordHash' and @UserId='"+checkName[0]+"']"); if ( nameNode == null) { if ( MessageBox.Show( "User Not Found","USer not found"+checkName[0], MessageBoxButtons.OKCancel ) == DialogResult.Cancel ) { return; } else { continue; } } XmlNode userNode = doc.SelectSingleNode("//User[@UserId='"+checkName[0]+"']"); KeyedHashAlgorithm hashAlgorithm = KeyedHashAlgorithm.Create("HMACSHA1"); // we use the user ID as the "secret" key :-) hashAlgorithm.Key = System.Text.Encoding.Default.GetBytes( checkName[0] ); string storedHash = nameNode.Attributes["StateValue"].Value; if ( checkName[1].Trim().Length > 0 ) { // compute the hash code byte[] hash = hashAlgorithm.ComputeHash(System.Text.Encoding.Default.GetBytes( checkName[1] )); string passHash = Convert.ToBase64String(hash); //EndOfValidity="2009-01-09T18:59:00.0000000" if ( userNode.Attributes["EndOfValidity"] != null ) { string validityEnd = userNode.Attributes["EndOfValidity"].Value; DateTime dt = Convert.ToDateTime(validityEnd); if ( dt < DateTime.Now ) { DialogResult dr = MessageBox.Show("Expired:" + validityEnd + ", Extend out one year?" , "AccountRepo Helper", MessageBoxButtons.YesNoCancel); if ( dr == DialogResult.Cancel ) { return; } else if ( dr == DialogResult.Yes ) { DateTime newDT = DateTime.Now.AddYears(1); string newValidity = newDT.ToString("yyyy-MM-dd"); userNode.Attributes["EndOfValidity"].Value = newValidity; MemoryStream ms = new MemoryStream(); doc.Save( ms ); ciphertext = olympCryptography.Encrypt(ms.GetBuffer() , new CompressStreamWriterDelegate(CompressStreamWriter)); fileStream = new FileStream(filePath, FileMode.Create, FileAccess.Write, FileShare.None); using(BinaryWriter writer = new BinaryWriter(fileStream)) { writer.Write(ciphertext, 0, ciphertext.Length); writer.Flush(); } } } } // User entered correct password. Let's go the extra mile and see if the user is locked // expired.. If so, let's force unlock the user. XmlNode lockedState = doc.SelectSingleNode("//UserState[@StateKey='Locked' and @UserId='"+checkName[0]+"']"); bool locked = lockedState.Attributes["StateValue"].Value.Equals("True"); if ( !passHash.Equals( storedHash )) { string newPass = QuickDialog.DoQuickDialog("Force new password", "New Password (blank=no change)", ""); if ( newPass == null ) { return; } else if ( newPass.Trim().Length > 0 ) { hash = hashAlgorithm.ComputeHash(System.Text.Encoding.Default.GetBytes( checkName[1] )); passHash = Convert.ToBase64String(hash); nameNode.Attributes["StateValue"].Value = passHash; MemoryStream ms = new MemoryStream(); doc.Save( ms ); ciphertext = olympCryptography.Encrypt(ms.GetBuffer() , new CompressStreamWriterDelegate(CompressStreamWriter)); fileStream = new FileStream(filePath, FileMode.Create, FileAccess.Write, FileShare.None); using(BinaryWriter writer = new BinaryWriter(fileStream)) { writer.Write(ciphertext, 0, ciphertext.Length); writer.Flush(); } } else { continue; } } if ( !locked ) { if (MessageBox.Show("PW IS GOOD for " + checkName[0] + " and locked=" + locked.ToString(), "AccountRepo help", MessageBoxButtons.OKCancel) == DialogResult.Cancel ) { return; } else { continue; } } else { DialogResult dr = MessageBox.Show("User is locked. unlock?", "AccountRepo Help", MessageBoxButtons.YesNoCancel); if ( dr == DialogResult.Cancel) { return; } else if ( dr == DialogResult.No ) { continue; } else // Yes { lockedState.Attributes["StateValue"].Value = "False"; MemoryStream ms = new MemoryStream(); doc.Save( ms ); ciphertext = olympCryptography.Encrypt(ms.GetBuffer() , new CompressStreamWriterDelegate(CompressStreamWriter)); fileStream = new FileStream(filePath, FileMode.Create, FileAccess.Write, FileShare.None); using(BinaryWriter writer = new BinaryWriter(fileStream)) { writer.Write(ciphertext, 0, ciphertext.Length); writer.Flush(); } } } } else { string[] abc123 = new string[]{"!", "@", "#", "$", "%", "^", "&", "*", "(", ")"}; bool found = false; foreach ( string pCharTest in abc123 ) { // compute the hash code byte[] hash = hashAlgorithm.ComputeHash(System.Text.Encoding.Default.GetBytes( "ABCD123" + pCharTest )); string passHash = Convert.ToBase64String(hash); if ( passHash.Equals( storedHash )) { found = true; string userId = nameNode.Attributes["UserId"].Value; XmlNode lockedState = doc.SelectSingleNode("//UserState[@StateKey='Locked' and @UserId='"+userId+"']"); bool locked = lockedState.Attributes["StateValue"].Value.Equals("True"); if ( MessageBox.Show(pCharTest + ((locked) ? " (locked)" : ""), "Hint Hint", MessageBoxButtons.OKCancel ) == DialogResult.Cancel ) { return; } else { break; } } } if ( !found ) { if (MessageBox.Show("None found", "Hint Hint", MessageBoxButtons.OK) == DialogResult.Cancel ) { return; } else { continue; } } } } } catch (Exception ) { // Exceptions are secret. //Console.WriteLine( e.StackTrace ); //Console.WriteLine( e.Message ); } } }
public override void Run(KExplorerNode folder, FileInfo[] files) { while (true) { string[] checkName = QuickDialog2.DoQuickDialog( "AccountRepo Check", "UserId", "", "Password (optional)", ""); if (checkName == null) { return; } string filePath; byte[] ciphertext = null; try { filePath = files[0].FullName; FileStream fileStream = new FileStream(filePath, FileMode.Open, FileAccess.Read, FileShare.Read); using (BinaryReader reader = new BinaryReader(fileStream)) { ciphertext = reader.ReadBytes((int)fileStream.Length); } byte[] plaintext = null; IOlympCryptography olympCryptography = new OlympCryptography(); plaintext = olympCryptography.Decrypt(ciphertext, new CompressStreamReaderDelegate(CompressStreamReader)); string reallyPlain = System.Text.UTF8Encoding.UTF8.GetString(plaintext); XmlDocument doc = new XmlDocument(); // Seems sometimes in the encoding, we get a garbage character at the beginning. if (reallyPlain.StartsWith("<")) { doc.LoadXml(reallyPlain); } else { doc.LoadXml(reallyPlain.Substring(1)); } // If user enters nothing. Give him a message box of all the user ids. if (checkName[0].Trim().Length == 0) { StringBuilder names = new StringBuilder(); XmlNodeList nameNodes = doc.SelectNodes("//UserState[@StateKey='PasswordHash']"); foreach (XmlNode nameNode in nameNodes) { string userId = nameNode.Attributes["UserId"].Value; XmlNode lockedState = doc.SelectSingleNode("//UserState[@StateKey='Locked' and @UserId='" + userId + "']"); bool locked = lockedState.Attributes["StateValue"].Value.Equals("True"); names.Append(userId + ((locked) ? " (locked);" : "; ")); } if (MessageBox.Show(names.ToString(), "User ID's", MessageBoxButtons.OKCancel) == DialogResult.Cancel) { return; } else { continue; } } // They entered a name and a password. check it. check it for locked. If locked, prompt to unluck. else { XmlNode nameNode = doc.SelectSingleNode("//UserState[@StateKey='PasswordHash' and @UserId='" + checkName[0] + "']"); if (nameNode == null) { if (MessageBox.Show("User Not Found", "USer not found" + checkName[0], MessageBoxButtons.OKCancel) == DialogResult.Cancel) { return; } else { continue; } } XmlNode userNode = doc.SelectSingleNode("//User[@UserId='" + checkName[0] + "']"); KeyedHashAlgorithm hashAlgorithm = KeyedHashAlgorithm.Create("HMACSHA1"); // we use the user ID as the "secret" key :-) hashAlgorithm.Key = System.Text.Encoding.Default.GetBytes(checkName[0]); string storedHash = nameNode.Attributes["StateValue"].Value; if (checkName[1].Trim().Length > 0) { // compute the hash code byte[] hash = hashAlgorithm.ComputeHash(System.Text.Encoding.Default.GetBytes(checkName[1])); string passHash = Convert.ToBase64String(hash); //EndOfValidity="2009-01-09T18:59:00.0000000" if (userNode.Attributes["EndOfValidity"] != null) { string validityEnd = userNode.Attributes["EndOfValidity"].Value; DateTime dt = Convert.ToDateTime(validityEnd); if (dt < DateTime.Now) { DialogResult dr = MessageBox.Show("Expired:" + validityEnd + ", Extend out one year?" , "AccountRepo Helper", MessageBoxButtons.YesNoCancel); if (dr == DialogResult.Cancel) { return; } else if (dr == DialogResult.Yes) { DateTime newDT = DateTime.Now.AddYears(1); string newValidity = newDT.ToString("yyyy-MM-dd"); userNode.Attributes["EndOfValidity"].Value = newValidity; MemoryStream ms = new MemoryStream(); doc.Save(ms); ciphertext = olympCryptography.Encrypt(ms.GetBuffer() , new CompressStreamWriterDelegate(CompressStreamWriter)); fileStream = new FileStream(filePath, FileMode.Create, FileAccess.Write, FileShare.None); using (BinaryWriter writer = new BinaryWriter(fileStream)) { writer.Write(ciphertext, 0, ciphertext.Length); writer.Flush(); } } } } // User entered correct password. Let's go the extra mile and see if the user is locked // expired.. If so, let's force unlock the user. XmlNode lockedState = doc.SelectSingleNode("//UserState[@StateKey='Locked' and @UserId='" + checkName[0] + "']"); bool locked = lockedState.Attributes["StateValue"].Value.Equals("True"); if (!passHash.Equals(storedHash)) { string newPass = QuickDialog.DoQuickDialog("Force new password", "New Password (blank=no change)", ""); if (newPass == null) { return; } else if (newPass.Trim().Length > 0) { hash = hashAlgorithm.ComputeHash(System.Text.Encoding.Default.GetBytes(checkName[1])); passHash = Convert.ToBase64String(hash); nameNode.Attributes["StateValue"].Value = passHash; MemoryStream ms = new MemoryStream(); doc.Save(ms); ciphertext = olympCryptography.Encrypt(ms.GetBuffer() , new CompressStreamWriterDelegate(CompressStreamWriter)); fileStream = new FileStream(filePath, FileMode.Create, FileAccess.Write, FileShare.None); using (BinaryWriter writer = new BinaryWriter(fileStream)) { writer.Write(ciphertext, 0, ciphertext.Length); writer.Flush(); } } else { continue; } } if (!locked) { if (MessageBox.Show("PW IS GOOD for " + checkName[0] + " and locked=" + locked.ToString(), "AccountRepo help", MessageBoxButtons.OKCancel) == DialogResult.Cancel) { return; } else { continue; } } else { DialogResult dr = MessageBox.Show("User is locked. unlock?", "AccountRepo Help", MessageBoxButtons.YesNoCancel); if (dr == DialogResult.Cancel) { return; } else if (dr == DialogResult.No) { continue; } else // Yes { lockedState.Attributes["StateValue"].Value = "False"; MemoryStream ms = new MemoryStream(); doc.Save(ms); ciphertext = olympCryptography.Encrypt(ms.GetBuffer() , new CompressStreamWriterDelegate(CompressStreamWriter)); fileStream = new FileStream(filePath, FileMode.Create, FileAccess.Write, FileShare.None); using (BinaryWriter writer = new BinaryWriter(fileStream)) { writer.Write(ciphertext, 0, ciphertext.Length); writer.Flush(); } } } } else { string[] abc123 = new string[] { "!", "@", "#", "$", "%", "^", "&", "*", "(", ")" }; bool found = false; foreach (string pCharTest in abc123) { // compute the hash code byte[] hash = hashAlgorithm.ComputeHash(System.Text.Encoding.Default.GetBytes( "ABCD123" + pCharTest)); string passHash = Convert.ToBase64String(hash); if (passHash.Equals(storedHash)) { found = true; string userId = nameNode.Attributes["UserId"].Value; XmlNode lockedState = doc.SelectSingleNode("//UserState[@StateKey='Locked' and @UserId='" + userId + "']"); bool locked = lockedState.Attributes["StateValue"].Value.Equals("True"); if (MessageBox.Show(pCharTest + ((locked) ? " (locked)" : ""), "Hint Hint", MessageBoxButtons.OKCancel) == DialogResult.Cancel) { return; } else { break; } } } if (!found) { if (MessageBox.Show("None found", "Hint Hint", MessageBoxButtons.OK) == DialogResult.Cancel) { return; } else { continue; } } } } } catch (Exception) { // Exceptions are secret. //Console.WriteLine( e.StackTrace ); //Console.WriteLine( e.Message ); } } }