/**************************************************************************
THIS CODE AND INFORMATION IS PROVIDED 'AS IS' WITHOUT WARRANTY OF
ANY KIND, EITHER EXPRESSED OR IMPLIED, INCLUDING BUT NOT LIMITED TO
THE IMPLIED WARRANTIES OF MERCHANTABILITY AND/OR FITNESS FOR A
PARTICULAR PURPOSE.
Author: Leon Finker 7/2003
**************************************************************************/
#region using
using System;
using System.Resources;
using System.Security.Permissions;
using System.Runtime.InteropServices;
using System.Diagnostics;
using System.Reflection;
using System.Xml;
using System.Text;
using System.IO;
using System.Drawing;
using System.Collections;
using System.Collections.Specialized;
using System.ComponentModel;
using System.Windows.Forms;
using System.Data;
using System.Security.Cryptography;
using System.Collections.Generic;
#endregion
namespace passkeep
{
internal struct UserData
{
internal string user;
internal string pass;
}
[StructLayout(LayoutKind.Sequential)]
internal struct SeqPoint
{
internal int x, y;
}
[StructLayout(LayoutKind.Sequential)]
internal struct SeqRect
{
internal int left, top, right, bottom;
}
/// <summary>
/// Summary description for PassInfoKeep.
/// </summary>
internal sealed class PassInfoKeep : System.Windows.Forms.Form, IMessageFilter
{
#region data members
/// <summary>
/// Required designer variable.
/// </summary>
private System.ComponentModel.IContainer components;
private System.Windows.Forms.Button btnExit;
private System.Windows.Forms.ComboBox cmbKeys;
private System.Windows.Forms.MenuItem fileMenuItem;
private System.Windows.Forms.MenuItem menuFileAdd;
private System.Windows.Forms.MenuItem menuFileExit;
private System.Windows.Forms.MainMenu menuFile;
private System.Windows.Forms.Button btnDND;
private System.Windows.Forms.Button btnSend;
private System.Windows.Forms.Button btnCancel;
private System.Windows.Forms.MenuItem menuDelete;
private System.Windows.Forms.MenuItem menuItem1;
private System.Windows.Forms.ToolTip tipSend;
private System.Windows.Forms.Label lblService;
private System.Windows.Forms.Label lblPass;
private System.Windows.Forms.Label lblUser;
private System.Windows.Forms.Button btnSendUser;
private System.Windows.Forms.Button btnDNDUser;
private System.Windows.Forms.GroupBox groupBox1;
private System.Windows.Forms.GroupBox groupBox2;
private System.Windows.Forms.GroupBox groupBox3;
private Color drawFrameBkColor, drawFrameBkColorPrev;
private bool startupMode;
private string assemblyPath;
private string datafile;
private HybridDictionary id2pass;
private ICryptoTransform encRij;
private ICryptoTransform enc3des;
private ICryptoTransform decRij;
private ICryptoTransform dec3des;
private Rectangle previousRect, previousRect2;
private Cursor cursorCurrent;
private System.Windows.Forms.MenuItem menuAbout;
private System.Windows.Forms.MenuItem menuImport;
private System.Windows.Forms.OpenFileDialog openFileImport;
private Timer timer1;
private DateTime _lastUsed = DateTime.UtcNow;
static private NotifyIcon notifyIcon;
#endregion
#region DLL Imports
[DllImport("gdi32.dll")]
private static extern int GetBkColor(IntPtr hdc);
[DllImport("user32.dll")]
private static extern bool GetWindowRect(IntPtr hWnd, out SeqRect rect);
[DllImport("user32.dll")]
private static extern bool ScreenToClient(IntPtr hWnd, ref SeqPoint pt);
[DllImport("user32.dll")]
private static extern IntPtr GetParent(IntPtr hWnd);
[DllImport("user32.dll")]
private static extern IntPtr WindowFromPoint(SeqPoint pt);
[DllImport("user32.dll")]
private static extern IntPtr RealChildWindowFromPoint(IntPtr hWndParent, SeqPoint Point);
[DllImport("user32.dll", CharSet=CharSet.Auto, ThrowOnUnmappableChar=true)]
private static extern bool SendMessage(IntPtr hWnd, Int32 msg, Int32 wParam, Int32 lParam);
[DllImport("user32.dll")]
private static extern bool RegisterHotKey(IntPtr hWnd, Int32 id, UInt32 fsModifiers, UInt32 vk);
[DllImport("user32.dll")]
private static extern bool UnregisterHotKey(IntPtr hWnd, Int32 id);
#endregion
internal PassInfoKeep(bool startup)
{
//
// Required for Windows Form Designer support
//
InitializeComponent();
id2pass = new HybridDictionary();
datafile = "data";
startupMode = startup;
}
/// <summary>
/// Clean up any resources being used.
/// </summary>
protected override void Dispose( bool disposing )
{
if( disposing )
{
if (components != null)
{
components.Dispose();
}
}
base.Dispose( disposing );
}
#region Windows Form Designer generated code
/// <summary>
/// Required method for Designer support - do not modify
/// the contents of this method with the code editor.
/// </summary>
private void InitializeComponent()
{
this.components = new System.ComponentModel.Container();
System.ComponentModel.ComponentResourceManager resources = new System.ComponentModel.ComponentResourceManager(typeof(PassInfoKeep));
this.btnExit = new System.Windows.Forms.Button();
this.cmbKeys = new System.Windows.Forms.ComboBox();
this.menuFile = new System.Windows.Forms.MainMenu(this.components);
this.fileMenuItem = new System.Windows.Forms.MenuItem();
this.menuFileAdd = new System.Windows.Forms.MenuItem();
this.menuDelete = new System.Windows.Forms.MenuItem();
this.menuImport = new System.Windows.Forms.MenuItem();
this.menuItem1 = new System.Windows.Forms.MenuItem();
this.menuFileExit = new System.Windows.Forms.MenuItem();
this.menuAbout = new System.Windows.Forms.MenuItem();
this.lblService = new System.Windows.Forms.Label();
this.btnDND = new System.Windows.Forms.Button();
this.btnSend = new System.Windows.Forms.Button();
this.btnCancel = new System.Windows.Forms.Button();
this.tipSend = new System.Windows.Forms.ToolTip(this.components);
this.lblPass = new System.Windows.Forms.Label();
this.lblUser = new System.Windows.Forms.Label();
this.btnSendUser = new System.Windows.Forms.Button();
this.btnDNDUser = new System.Windows.Forms.Button();
this.groupBox1 = new System.Windows.Forms.GroupBox();
this.groupBox2 = new System.Windows.Forms.GroupBox();
this.groupBox3 = new System.Windows.Forms.GroupBox();
this.openFileImport = new System.Windows.Forms.OpenFileDialog();
this.timer1 = new System.Windows.Forms.Timer(this.components);
this.SuspendLayout();
//
// btnExit
//
this.btnExit.FlatStyle = System.Windows.Forms.FlatStyle.System;
this.btnExit.Location = new System.Drawing.Point(80, 141);
this.btnExit.Name = "btnExit";
this.btnExit.Size = new System.Drawing.Size(74, 23);
this.btnExit.TabIndex = 12;
this.btnExit.Text = "&Exit";
this.btnExit.Click += new System.EventHandler(this.btnExit_Click);
//
// cmbKeys
//
this.cmbKeys.DropDownStyle = System.Windows.Forms.ComboBoxStyle.DropDownList;
this.cmbKeys.Location = new System.Drawing.Point(79, 15);
this.cmbKeys.Name = "cmbKeys";
this.cmbKeys.Size = new System.Drawing.Size(152, 24);
this.cmbKeys.Sorted = true;
this.cmbKeys.TabIndex = 3;
//
// menuFile
//
this.menuFile.MenuItems.AddRange(new System.Windows.Forms.MenuItem[] {
this.fileMenuItem,
this.menuAbout});
//
// fileMenuItem
//
this.fileMenuItem.Index = 0;
this.fileMenuItem.MenuItems.AddRange(new System.Windows.Forms.MenuItem[] {
this.menuFileAdd,
this.menuDelete,
this.menuImport,
this.menuItem1,
this.menuFileExit});
this.fileMenuItem.Text = "File";
//
// menuFileAdd
//
this.menuFileAdd.Index = 0;
this.menuFileAdd.Shortcut = System.Windows.Forms.Shortcut.CtrlA;
this.menuFileAdd.Text = "&Add/Replace...";
this.menuFileAdd.Click += new System.EventHandler(this.menuFileAdd_Click);
//
// menuDelete
//
this.menuDelete.Index = 1;
this.menuDelete.Shortcut = System.Windows.Forms.Shortcut.CtrlD;
this.menuDelete.Text = "&Delete";
this.menuDelete.Click += new System.EventHandler(this.menuDelete_Click);
//
// menuImport
//
this.menuImport.Index = 2;
this.menuImport.Shortcut = System.Windows.Forms.Shortcut.CtrlI;
this.menuImport.Text = "&Import...";
this.menuImport.Click += new System.EventHandler(this.menuImport_Click);
//
// menuItem1
//
this.menuItem1.Index = 3;
this.menuItem1.Text = "-";
//
// menuFileExit
//
this.menuFileExit.Index = 4;
this.menuFileExit.Shortcut = System.Windows.Forms.Shortcut.AltF4;
this.menuFileExit.Text = "&Exit";
this.menuFileExit.Click += new System.EventHandler(this.btnExit_Click);
//
// menuAbout
//
this.menuAbout.Index = 1;
this.menuAbout.Text = "&About";
this.menuAbout.Click += new System.EventHandler(this.menuAbout_Click);
//
// lblService
//
this.lblService.FlatStyle = System.Windows.Forms.FlatStyle.System;
this.lblService.Location = new System.Drawing.Point(13, 15);
this.lblService.Name = "lblService";
this.lblService.Size = new System.Drawing.Size(59, 23);
this.lblService.TabIndex = 2;
this.lblService.Text = "&Service:";
//
// btnDND
//
this.btnDND.FlatStyle = System.Windows.Forms.FlatStyle.Popup;
this.btnDND.Image = ((System.Drawing.Image)(resources.GetObject("btnDND.Image")));
this.btnDND.Location = new System.Drawing.Point(79, 99);
this.btnDND.Name = "btnDND";
this.btnDND.Size = new System.Drawing.Size(26, 23);
this.btnDND.TabIndex = 10;
this.btnDND.MouseDown += new System.Windows.Forms.MouseEventHandler(this.btnDND_MouseDown);
//
// btnSend
//
this.btnSend.FlatStyle = System.Windows.Forms.FlatStyle.Popup;
this.btnSend.Image = ((System.Drawing.Image)(resources.GetObject("btnSend.Image")));
this.btnSend.Location = new System.Drawing.Point(125, 99);
this.btnSend.Name = "btnSend";
this.btnSend.Size = new System.Drawing.Size(26, 23);
this.btnSend.TabIndex = 11;
this.btnSend.MouseMove += new System.Windows.Forms.MouseEventHandler(this.btnSend_MouseMove);
this.btnSend.MouseDown += new System.Windows.Forms.MouseEventHandler(this.btnSend_MouseDown);
this.btnSend.MouseUp += new System.Windows.Forms.MouseEventHandler(this.btnSend_MouseUp);
//
// btnCancel
//
this.btnCancel.DialogResult = System.Windows.Forms.DialogResult.Cancel;
this.btnCancel.Location = new System.Drawing.Point(0, -15);
this.btnCancel.Name = "btnCancel";
this.btnCancel.Size = new System.Drawing.Size(0, 15);
this.btnCancel.TabIndex = 0;
this.btnCancel.TabStop = false;
this.btnCancel.Click += new System.EventHandler(this.btnCancel_Click);
//
// lblPass
//
this.lblPass.FlatStyle = System.Windows.Forms.FlatStyle.System;
this.lblPass.Location = new System.Drawing.Point(16, 99);
this.lblPass.Name = "lblPass";
this.lblPass.Size = new System.Drawing.Size(39, 23);
this.lblPass.TabIndex = 9;
this.lblPass.Text = "&Pass:";
//
// lblUser
//
this.lblUser.FlatStyle = System.Windows.Forms.FlatStyle.System;
this.lblUser.Location = new System.Drawing.Point(17, 55);
this.lblUser.Name = "lblUser";
this.lblUser.Size = new System.Drawing.Size(39, 23);
this.lblUser.TabIndex = 5;
this.lblUser.Text = "&User:";
//
// btnSendUser
//
this.btnSendUser.FlatStyle = System.Windows.Forms.FlatStyle.Popup;
this.btnSendUser.Image = ((System.Drawing.Image)(resources.GetObject("btnSendUser.Image")));
this.btnSendUser.Location = new System.Drawing.Point(126, 55);
this.btnSendUser.Name = "btnSendUser";
this.btnSendUser.Size = new System.Drawing.Size(26, 23);
this.btnSendUser.TabIndex = 7;
this.btnSendUser.MouseMove += new System.Windows.Forms.MouseEventHandler(this.btnSendUser_MouseMove);
this.btnSendUser.MouseDown += new System.Windows.Forms.MouseEventHandler(this.btnSendUser_MouseDown);
this.btnSendUser.MouseUp += new System.Windows.Forms.MouseEventHandler(this.btnSendUser_MouseUp);
//
// btnDNDUser
//
this.btnDNDUser.FlatStyle = System.Windows.Forms.FlatStyle.Popup;
this.btnDNDUser.Image = ((System.Drawing.Image)(resources.GetObject("btnDNDUser.Image")));
this.btnDNDUser.Location = new System.Drawing.Point(80, 55);
this.btnDNDUser.Name = "btnDNDUser";
this.btnDNDUser.Size = new System.Drawing.Size(26, 23);
this.btnDNDUser.TabIndex = 6;
this.btnDNDUser.MouseDown += new System.Windows.Forms.MouseEventHandler(this.btnDNDUser_MouseDown);
//
// groupBox1
//
this.groupBox1.FlatStyle = System.Windows.Forms.FlatStyle.System;
this.groupBox1.Location = new System.Drawing.Point(10, -6);
this.groupBox1.Name = "groupBox1";
this.groupBox1.Size = new System.Drawing.Size(239, 51);
this.groupBox1.TabIndex = 1;
this.groupBox1.TabStop = false;
//
// groupBox2
//
this.groupBox2.FlatStyle = System.Windows.Forms.FlatStyle.System;
this.groupBox2.Location = new System.Drawing.Point(10, 80);
this.groupBox2.Name = "groupBox2";
this.groupBox2.Size = new System.Drawing.Size(239, 51);
this.groupBox2.TabIndex = 8;
this.groupBox2.TabStop = false;
//
// groupBox3
//
this.groupBox3.FlatStyle = System.Windows.Forms.FlatStyle.System;
this.groupBox3.Location = new System.Drawing.Point(10, 37);
this.groupBox3.Name = "groupBox3";
this.groupBox3.Size = new System.Drawing.Size(239, 51);
this.groupBox3.TabIndex = 4;
this.groupBox3.TabStop = false;
//
// openFileImport
//
this.openFileImport.Filter = "All Files|*.*";
this.openFileImport.RestoreDirectory = true;
this.openFileImport.Title = "File Import";
//
// timer1
//
this.timer1.Enabled = true;
this.timer1.Interval = 60000;
this.timer1.Tick += new System.EventHandler(this.timer1_Tick);
//
// PassInfoKeep
//
this.AcceptButton = this.btnCancel;
this.AutoScaleBaseSize = new System.Drawing.Size(6, 15);
this.CancelButton = this.btnCancel;
this.ClientSize = new System.Drawing.Size(260, 172);
this.Controls.Add(this.lblUser);
this.Controls.Add(this.btnSendUser);
this.Controls.Add(this.btnDNDUser);
this.Controls.Add(this.lblPass);
this.Controls.Add(this.btnCancel);
this.Controls.Add(this.btnSend);
this.Controls.Add(this.btnDND);
this.Controls.Add(this.lblService);
this.Controls.Add(this.cmbKeys);
this.Controls.Add(this.btnExit);
this.Controls.Add(this.groupBox1);
this.Controls.Add(this.groupBox3);
this.Controls.Add(this.groupBox2);
this.FormBorderStyle = System.Windows.Forms.FormBorderStyle.FixedDialog;
this.MaximizeBox = false;
this.Menu = this.menuFile;
this.Name = "PassInfoKeep";
this.SizeGripStyle = System.Windows.Forms.SizeGripStyle.Hide;
this.StartPosition = System.Windows.Forms.FormStartPosition.CenterScreen;
this.Text = "PassInfoKeep";
this.TopMost = true;
this.Load += new System.EventHandler(this.PassInfoKeep_Load);
this.Activated += new System.EventHandler(this.PassInfoKeep_Activated);
this.Closing += new System.ComponentModel.CancelEventHandler(this.PassInfoKeep_Closing);
this.Resize += new System.EventHandler(this.PassInfoKeep_Resize);
this.ResumeLayout(false);
}
#endregion
/// <summary>
/// The main entry point for the application.
/// </summary>
[STAThread]
static void Main(string[] args)
{
Application.EnableVisualStyles();
notifyIcon = new NotifyIcon();
notifyIcon.Icon = new Icon(Assembly.GetExecutingAssembly().GetManifestResourceStream("passkeep.trayicon.ico"), 16, 16);
notifyIcon.Text = "PassInfoKeep";
notifyIcon.Visible = true;
bool startup = false;
if(args.Length > 0 && args[0] == "/autostart")
startup = true;
PassInfoKeep mainForm = new PassInfoKeep(startup);
mainForm.Icon = new Icon(Assembly.GetExecutingAssembly().GetManifestResourceStream("passkeep.trayicon.ico"), 16, 16);
try
{
mainForm.ShowDialog();
}
finally
{
notifyIcon.Dispose();
}
}
private void TrayClick(object sender, EventArgs e)
{
Visible = true;
WindowState = FormWindowState.Normal;
Activate();
}
private void PassInfoKeep_Load(object sender, System.EventArgs e)
{
Application.AddMessageFilter(this);
assemblyPath = Assembly.GetExecutingAssembly().Location;
int pos = assemblyPath.LastIndexOf('\\');
if(pos == -1)
return;
int len = assemblyPath.Length;
assemblyPath = assemblyPath.Remove(pos + 1, len - pos -1);
tipSend.SetToolTip(btnSendUser, "Drag over the target password field.\nThis will use SendMessage(WM_SETTEXT,...)");
tipSend.SetToolTip(btnDNDUser, "Drag over the target password field.\nThis will use Drag and Drop operation");
tipSend.SetToolTip(btnSend, "Drag over the target password field.\nThis will use SendMessage(WM_SETTEXT,...)");
tipSend.SetToolTip(btnDND, "Drag over the target password field.\nThis will use Drag and Drop operation");
notifyIcon.Click += new EventHandler(TrayClick);
CheckPass dlg = new CheckPass();
if(dlg.ShowDialog() == DialogResult.OK)
{
RegisterHotKey(Handle, 0x988B, 0x0002/*control*/, 0x7B/*F12*/);
RijndaelManaged aes = new RijndaelManaged();
TripleDESCryptoServiceProvider des3 = new TripleDESCryptoServiceProvider();
encRij = aes.CreateEncryptor(dlg.KeyRij, dlg.IVRij);
enc3des = des3.CreateEncryptor(dlg.Key3des, dlg.IV3des);
decRij = aes.CreateDecryptor(dlg.KeyRij, dlg.IVRij);
dec3des = des3.CreateDecryptor(dlg.Key3des, dlg.IV3des);
des3.Clear();
aes.Clear();
dlg.Clear();
FileIOPermission perm = new FileIOPermission(FileIOPermissionAccess.Read, assemblyPath + datafile);
perm.Demand();
if(File.Exists(assemblyPath + datafile))
{
using (Stream fileStream = new FileStream(assemblyPath + datafile, FileMode.OpenOrCreate, FileAccess.Read, FileShare.None))
{
MemoryStream memStream = new MemoryStream();
byte[] buf = new byte[4096];
try
{
using (CryptoStream cryptStream = new CryptoStream(fileStream, decRij, CryptoStreamMode.Read))
{
int read = 0;
while (true)
{
read = cryptStream.Read(buf, 0, buf.Length);
if (read == 0)
break;
memStream.Write(buf, 0, read);
}
cryptStream.Clear();
}
memStream.Position = 0;
using (XmlTextReader xmlr = new XmlTextReader(memStream))
{
while (xmlr.Read())
{
if (xmlr.NodeType == XmlNodeType.Element && xmlr.LocalName == "Entry")
{
UserData data = new UserData();
data.user = xmlr.GetAttribute("user");
data.pass = xmlr.GetAttribute("data");
id2pass[xmlr.GetAttribute("id")] = data;
}
}
buf = memStream.GetBuffer();
Array.Clear(buf, 0, (int)memStream.Length);
}
memStream.Close();
}
catch (Exception)
{
MessageBox.Show("Unable to decrypt", "Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
Close();
}
finally
{
fileStream.Close();
}
}
}
foreach (DictionaryEntry entry in id2pass)
cmbKeys.Items.Add(entry.Key);
if (cmbKeys.Items.Count > 0)
cmbKeys.SelectedIndex = 0;
}
else
{
Close();
}
}
protected override void WndProc(ref Message m)
{
if (m.Msg == 0x0312) //WM_HOTKEY
{
Visible = true;
WindowState = FormWindowState.Normal;
Activate();
}
base.WndProc(ref m);
}
private void btnExit_Click(object sender, System.EventArgs e)
{
Close();
}
private void btnCancel_Click(object sender, System.EventArgs e)
{
WindowState = FormWindowState.Minimized;
Visible = false;
}
private void menuFileAdd_Click(object sender, System.EventArgs e)
{
AddNew dlg = new AddNew();
if (dlg.ShowDialog() == DialogResult.OK)
{
UserData data = new UserData();
data.user = EncryptAndBase64(dlg.User, enc3des);
data.pass = EncryptAndBase64(dlg.Password, enc3des);
id2pass[dlg.TxtID] = data;
if (!cmbKeys.Items.Contains(dlg.TxtID))
{
int itemIndex = cmbKeys.Items.Add(dlg.TxtID);
if (cmbKeys.SelectedIndex == -1)
cmbKeys.SelectedIndex = itemIndex;
}
using (MemoryStream memStream = new MemoryStream())
{
using (XmlTextWriter xmlw = XmlWriteSettings(memStream))
{
int memLen = EncryptSaveStream(memStream);
byte[] buf = memStream.GetBuffer();
Array.Clear(buf, 0, memLen);
dlg.Clear();
}
}
}
}
private void menuDelete_Click(object sender, System.EventArgs e)
{
if(MessageBox.Show("Continue?", "Question", MessageBoxButtons.YesNo, MessageBoxIcon.Question) != DialogResult.Yes)
return;
int itemIndex = cmbKeys.SelectedIndex;
if(itemIndex >= 0)
{
id2pass.Remove(cmbKeys.Items[itemIndex]);
cmbKeys.Items.RemoveAt(itemIndex);
}
if(cmbKeys.Items.Count > 0)
cmbKeys.SelectedIndex = 0;
using (MemoryStream memStream = new MemoryStream())
{
using (XmlTextWriter xmlw = XmlWriteSettings(memStream))
{
int memLen = EncryptSaveStream(memStream);
byte[] buf = memStream.GetBuffer();
Array.Clear(buf, 0, memLen);
}
}
}
private void btnDND_MouseDown(object sender, System.Windows.Forms.MouseEventArgs e)
{
int itemIndex = cmbKeys.SelectedIndex;
if (itemIndex >= 0)
{
object obj = id2pass[cmbKeys.Items[itemIndex]];
Debug.Assert(obj != null && obj is UserData);
string pass = (string)DecryptFromBase64(((UserData)obj).pass, dec3des);
DoDragDrop(pass, DragDropEffects.Copy);
CheckPass.ClearString(pass);
}
}
private void btnDNDUser_MouseDown(object sender, System.Windows.Forms.MouseEventArgs e)
{
int itemIndex = cmbKeys.SelectedIndex;
if (itemIndex >= 0)
{
object obj = id2pass[cmbKeys.Items[itemIndex]];
Debug.Assert(obj != null && obj is UserData);
string user = (string)DecryptFromBase64(((UserData)obj).user, dec3des);
DoDragDrop(user, DragDropEffects.Copy);
CheckPass.ClearString(user);
}
}
private void btnSend_MouseDown(object sender, System.Windows.Forms.MouseEventArgs e)
{
if(cmbKeys.SelectedIndex < 0)
return;
cursorCurrent = Cursor;
Cursor = Cursors.Hand;
btnSend.Capture = true;
}
private void btnSendUser_MouseDown(object sender, System.Windows.Forms.MouseEventArgs e)
{
if(cmbKeys.SelectedIndex < 0)
return;
cursorCurrent = Cursor;
Cursor = Cursors.Hand;
btnSendUser.Capture = true;
}
private void btnSendUser_MouseMove(object sender, System.Windows.Forms.MouseEventArgs e)
{
if(!btnSendUser.Capture)
return;
FrameTargetWnd();
}
private void btnSend_MouseMove(object sender, System.Windows.Forms.MouseEventArgs e)
{
if(!btnSend.Capture)
return;
FrameTargetWnd();
}
private void btnSend_MouseUp(object sender, System.Windows.Forms.MouseEventArgs e)
{
btnSend.Capture = false;
Cursor = cursorCurrent;
if(previousRect.Height > 0 || previousRect.Width > 0)
{
ControlPaint.DrawReversibleFrame(previousRect, drawFrameBkColorPrev, FrameStyle.Dashed);
ControlPaint.DrawReversibleFrame(previousRect2, drawFrameBkColorPrev, FrameStyle.Dashed);
previousRect.Size = new Size(0, 0);
}
SendUserPassToWnd(true);
}
private void btnSendUser_MouseUp(object sender, System.Windows.Forms.MouseEventArgs e)
{
btnSendUser.Capture = false;
Cursor = cursorCurrent;
if(previousRect.Height > 0 || previousRect.Width > 0)
{
ControlPaint.DrawReversibleFrame(previousRect, drawFrameBkColorPrev, FrameStyle.Dashed);
ControlPaint.DrawReversibleFrame(previousRect2, drawFrameBkColorPrev, FrameStyle.Dashed);
previousRect.Size = new Size(0, 0);
}
SendUserPassToWnd(false);
}
private void PassInfoKeep_Closing(object sender, System.ComponentModel.CancelEventArgs e)
{
Clear();
HandleRef href = new HandleRef(this, Handle);
UnregisterHotKey(Handle, 0x988B);
}
private void PassInfoKeep_Resize(object sender, System.EventArgs e)
{
if(WindowState == FormWindowState.Minimized)
Visible = false;
}
private XmlTextWriter XmlWriteSettings(MemoryStream memStream)
{
XmlTextWriter xmlw = new XmlTextWriter(memStream, null);
xmlw.WriteStartElement("Entries");
foreach (DictionaryEntry entry in id2pass)
{
UserData data = (UserData)entry.Value;
xmlw.WriteStartElement("Entry");
xmlw.WriteAttributeString("id", (string)entry.Key);
xmlw.WriteAttributeString("user", data.user);
xmlw.WriteAttributeString("data", data.pass);
xmlw.WriteEndElement();
}
xmlw.WriteEndElement();
xmlw.Flush();
memStream.Position = 0;
return xmlw;
}
private int EncryptSaveStream(MemoryStream memStream)
{
int retLen = 0;
if(File.Exists(assemblyPath + datafile))
File.Copy(assemblyPath + datafile, assemblyPath + datafile + ".bak", true);
FileIOPermission perm = new FileIOPermission(FileIOPermissionAccess.Write, assemblyPath + datafile);
perm.Demand();
using (Stream fileStream = new FileStream(assemblyPath + datafile, FileMode.Create, FileAccess.Write, FileShare.None))
{
using (CryptoStream cryptStream = new CryptoStream(memStream, encRij, CryptoStreamMode.Read))
{
int read = 0;
byte[] buf = new byte[4096];
while (true)
{
read = cryptStream.Read(buf, 0, buf.Length);
if (read == 0)
break;
fileStream.Write(buf, 0, read);
}
retLen = (int)memStream.Length;
cryptStream.Clear();
}
}
return retLen;
}
private void SendUserPassToWnd(bool ispass)
{
Point pt = Cursor.Position;
IntPtr hWnd = FindTargetWindow(pt);
if(hWnd != this.Handle && hWnd.ToInt64() != 0)
{
int itemIndex = cmbKeys.SelectedIndex;
if (itemIndex >= 0)
{
object obj = id2pass[cmbKeys.Items[itemIndex]];
Debug.Assert(obj != null && obj is UserData);
string val = "";
if(ispass)
val = (string)DecryptFromBase64(((UserData)obj).pass, dec3des);
else
val = (string)DecryptFromBase64(((UserData)obj).user, dec3des);
IntPtr ptr = Marshal.StringToCoTaskMemAuto(val);
int valLen = val.Length;
CheckPass.ClearString(val);
SendMessage(hWnd, 0x000C, 0, ptr.ToInt32());
CheckPass.ClearString(ptr, valLen*Marshal.SystemDefaultCharSize);
Marshal.FreeCoTaskMem(ptr);
}
}
}
private IntPtr FindTargetWindow(Point pt)
{
SeqPoint seqpt = new SeqPoint();
seqpt.x = pt.X;
seqpt.y = pt.Y;
IntPtr hWnd = WindowFromPoint(seqpt);
if(Bounds.Contains(pt))
return this.Handle;
if (hWnd.ToInt64() != 0)
{
IntPtr hParent = GetParent(hWnd);
if(hParent.ToInt64() != 0 && ScreenToClient(hParent, ref seqpt))
{
hParent = RealChildWindowFromPoint(hParent, seqpt);
if(hParent.ToInt64() != 0)
hWnd = hParent;
}
}
return hWnd;
}
private string EncryptAndBase64(string val, ICryptoTransform trans)
{
byte[] buf = UnicodeEncoding.Unicode.GetBytes(val);
using (MemoryStream memStream = new MemoryStream())
{
using (CryptoStream cryptStream = new CryptoStream(memStream, trans, CryptoStreamMode.Write))
{
cryptStream.Write(buf, 0, buf.Length);
cryptStream.FlushFinalBlock();
Array.Clear(buf, 0, buf.Length);
buf = memStream.GetBuffer();
val = Convert.ToBase64String(buf, 0, (int)memStream.Length);
Array.Clear(buf, 0, (int)memStream.Length);
cryptStream.Clear();
return val;
}
}
}
private string DecryptFromBase64(string base64str, ICryptoTransform trans)
{
byte[] buf = Convert.FromBase64String(base64str);
using (MemoryStream memStream = new MemoryStream())
{
using (CryptoStream cryptStream = new CryptoStream(memStream, trans, CryptoStreamMode.Write))
{
cryptStream.Write(buf, 0, buf.Length);
cryptStream.FlushFinalBlock();
Array.Clear(buf, 0, buf.Length);
buf = memStream.GetBuffer();
string ret = UnicodeEncoding.Unicode.GetString(buf, 0, (int)memStream.Length);
Array.Clear(buf, 0, (int)memStream.Length);
cryptStream.Clear();
return ret;
}
}
}
private void menuImport_Click(object sender, System.EventArgs e)
{
CheckPass dlg = new CheckPass();
if(dlg.ShowDialog() != DialogResult.OK)
return;
SymmetricAlgorithm alg = new RijndaelManaged();
ICryptoTransform rij = alg.CreateDecryptor(dlg.KeyRij, dlg.IVRij);
alg.Clear();
alg = new TripleDESCryptoServiceProvider();
ICryptoTransform des3 = alg.CreateDecryptor(dlg.Key3des, dlg.IV3des);
alg.Clear();
dlg.Clear();
if(openFileImport.ShowDialog() == DialogResult.OK)
{
using (Stream fileStream = openFileImport.OpenFile())
{
try
{
byte[] buf = new byte[4096];
MemoryStream memStream = new MemoryStream((int)fileStream.Length);
using (CryptoStream cryptoStream = new CryptoStream(fileStream, rij, CryptoStreamMode.Read))
{
int read = 0;
while (true)
{
read = cryptoStream.Read(buf, 0, buf.Length);
if (read == 0)
break;
memStream.Write(buf, 0, read);
}
cryptoStream.Clear();
fileStream.Close();
memStream.Position = 0;
}
XmlTextReader xmlr = new XmlTextReader(memStream);
HybridDictionary tmpTable = new HybridDictionary();
while (xmlr.Read())
{
if (xmlr.NodeType == XmlNodeType.Element && xmlr.LocalName == "Entry")
{
UserData data = new UserData();
string val = xmlr.GetAttribute("user");
val = (string)DecryptFromBase64(val, des3);
data.user = EncryptAndBase64(val, enc3des);
CheckPass.ClearString(val);
val = xmlr.GetAttribute("data");
val = (string)DecryptFromBase64(val, des3);
data.pass = EncryptAndBase64(val, enc3des);
CheckPass.ClearString(val);
tmpTable[xmlr.GetAttribute("id")] = data;
}
}
buf = memStream.GetBuffer();
Array.Clear(buf, 0, (int)memStream.Length);
xmlr.Close();
memStream.Close();
foreach (DictionaryEntry entry in tmpTable)
{
id2pass[entry.Key] = entry.Value;
if (!cmbKeys.Items.Contains(entry.Key))
cmbKeys.Items.Add(entry.Key);
}
if (cmbKeys.Items.Count > 0 && cmbKeys.SelectedIndex == -1)
cmbKeys.SelectedIndex = 0;
tmpTable.Clear();
using (memStream = new MemoryStream())
{
using (XmlTextWriter xmlw = XmlWriteSettings(memStream))
{
memStream.Position = 0;
int memLen = EncryptSaveStream(memStream);
buf = memStream.GetBuffer();
Array.Clear(buf, 0, memLen);
}
}
}
catch (Exception)
{
MessageBox.Show("Unable to import", "Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
}
finally
{
rij.Dispose();
des3.Dispose();
}
}
}
}
private void Clear()
{
if (encRij != null)
{
encRij.Dispose();
encRij = null;
}
if (enc3des != null)
{
enc3des.Dispose();
enc3des = null;
}
if (decRij != null)
{
decRij.Dispose();
decRij = null;
}
if (dec3des != null)
{
dec3des.Dispose();
dec3des = null;
}
}
private void menuAbout_Click(object sender, System.EventArgs e)
{
About dlg = new About();
dlg.ShowDialog();
}
private void PassInfoKeep_Activated(object sender, System.EventArgs e)
{
if (startupMode)
{
WindowState = FormWindowState.Minimized;
Visible = false;
startupMode = false;
}
}
private void timer1_Tick(object sender, EventArgs e)
{
int totalMs = (int)((DateTime.UtcNow - _lastUsed).TotalMilliseconds);
if (totalMs < timer1.Interval)
return;
if (!timer1.Enabled)
return;
timer1.Enabled = false;
List<Form> forms = new List<Form>();
foreach (Form form in Application.OpenForms)
forms.Add(form);
foreach (Form form in forms)
form.Close();
base.Close();
}
private void FrameTargetWnd()
{
Point pt = Cursor.Position;
IntPtr hWnd = FindTargetWindow(pt);
if (hWnd.ToInt64() != 0)
{
SeqRect rc;
if (GetWindowRect(hWnd, out rc))
{
Rectangle rect = new Rectangle(rc.left, rc.top, rc.right - rc.left, rc.bottom - rc.top);
if (rect != previousRect)
{
Graphics g = Graphics.FromHwnd(hWnd);
IntPtr hDC = g.GetHdc();
if (hDC.ToInt64() != 0)
{
try
{
if (previousRect.Height > 0 || previousRect.Width > 0)
{
ControlPaint.DrawReversibleFrame(previousRect, drawFrameBkColorPrev, FrameStyle.Dashed);
ControlPaint.DrawReversibleFrame(previousRect2, drawFrameBkColorPrev, FrameStyle.Dashed);
}
if (hWnd != this.Handle)
{
drawFrameBkColor = Color.FromArgb(GetBkColor(hDC));
ControlPaint.DrawReversibleFrame(rect, drawFrameBkColor, FrameStyle.Dashed);
previousRect = rect;
rect.Offset(1, 1);
rect.Height -= 2;
rect.Width -= 2;
ControlPaint.DrawReversibleFrame(rect, drawFrameBkColor, FrameStyle.Dashed);
previousRect2 = rect;
drawFrameBkColorPrev = drawFrameBkColor;
}
else
previousRect.Size = new Size(0, 0);
}
finally
{
g.ReleaseHdc(hDC);
g.Dispose();
}
}
}
}
}
}
#region IMessageFilter Members
public bool PreFilterMessage(ref Message m)
{
if ((m.Msg >= 0x0100 && m.Msg <= 0x0109) || (m.Msg >= 0x0200 && m.Msg <= 0x020E))
{
_lastUsed = DateTime.UtcNow;
}
return false;
}
#endregion
}
}