using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using mshtml;
namespace IMEncryptor
{
public partial class FrmMain : Form
{
#region Variables and Constants
IntPtr hWndForegroundWindow; // Foreground window handler
IntPtr hWndTalkArea; // Talk area window handler
IntPtr hWndWriteArea; // Write area window handler
IntPtr hWndSendButton; // Send button window handler
const String windowTitle = "- Instant Message"; // Yahoo Messenger's window title contains this string
const String writeAreaClassName = "YIMInputWindow"; // The class name of the writeArea zone
const String talkAreaContainerName = "YHTMLContainer"; // The container name of the "talkArea"
const String talkAreaClassName = "Internet Explorer_Server"; // The class name of the "talkArea"
const String sendButtonClassName = "Button"; // The class name of the "sendButton"
const String sendButtonText = "&Send"; // The text of the "sendButton"
const String startTag = "!%"; // Marks the START of an encrypted line of message
const String stopTag = "%!"; // Marks the END of an encrypted line of message
const String decryptedLineMarker = "* "; // This is a decrypted line
static String secretPassword; // The secret password entered by the user
#endregion
#region Constructor, Buttons and Timer methods
public FrmMain()
{
InitializeComponent();
this.hWndForegroundWindow = IntPtr.Zero;
this.hWndTalkArea = IntPtr.Zero;
this.hWndWriteArea = IntPtr.Zero;
this.hWndSendButton = IntPtr.Zero;
secretPassword = "";
}
private void ckbEnableDisable_CheckedChanged(object sender, EventArgs e)
{
if (ckbEnableDisable.CheckState == CheckState.Checked)
if (txtSecretPassword.Text.Length != 0)
{
secretPassword = txtSecretPassword.Text;
txtSecretPassword.Enabled = false;
ckbHideView.Enabled = false;
btnSendEncrypted.Enabled = true;
timerCheck.Start();
}
else
{
MessageBox.Show("Please enter the secret password!", "Warning!", MessageBoxButtons.OK, MessageBoxIcon.Warning);
ckbEnableDisable.CheckState = CheckState.Unchecked;
txtSecretPassword.Focus();
}
else
{
timerCheck.Stop();
txtSecretPassword.Enabled = true;
ckbHideView.Enabled = true;
btnSendEncrypted.Enabled = false;
}
}
private void ckbHideView_CheckedChanged(object sender, EventArgs e)
{
if (ckbHideView.CheckState == CheckState.Checked)
txtSecretPassword.PasswordChar = '*';
else
txtSecretPassword.PasswordChar = '\0';
}
private void btnSendEncrypted_Click(object sender, EventArgs e)
{
timerCheck.Enabled = false;
MessengerYahoo_HandleSend();
timerCheck.Enabled = true;
}
private void timerCheck_Tick(object sender, EventArgs e)
{
timerCheck.Stop();
// Get the foreground window
hWndForegroundWindow = Win32API.GetForegroundWindow();
StringBuilder title = new StringBuilder(256);
Win32API.GetWindowText(hWndForegroundWindow, title, 256);
hWndTalkArea = IntPtr.Zero;
hWndWriteArea = IntPtr.Zero;
hWndSendButton = IntPtr.Zero;
if (title.ToString().Contains(windowTitle))
MessengerYahoo_HandleWindow(hWndForegroundWindow);
timerCheck.Enabled = true;
}
#endregion
#region MessengerYahoo specific methods
private void MessengerYahoo_HandleWindow(IntPtr hWndForegroundWindow)
{
// Move YMEncryptor near the Yahoo Messenger's conversation window
MessengerYahoo_MoveTheWindow();
// Find the hWnd's for the current forreground window
Win32API.EnumChildWindows(hWndForegroundWindow, new Win32API.Win32Callback(this.MessengerYahoo_HandleEnumChild), ref hWndForegroundWindow);
MessengerYahoo_HandleDecryption();
}
private void MessengerYahoo_HandleDecryption()
{
// Read the messages from the conversation area
uint msg = Win32API.RegisterWindowMessage("WM_HTML_GETOBJECT");
IntPtr lRes = IntPtr.Zero;
Win32API.SendMessageTimeout(hWndTalkArea, msg, IntPtr.Zero, IntPtr.Zero, Win32API.SendMessageTimeoutFlags.SMTO_ABORTIFHUNG, 1000, out lRes);
IHTMLDocument2 myHTMLDoc = (mshtml.IHTMLDocument2)Win32API.ObjectFromLresult(lRes, typeof(mshtml.IHTMLDocument).GUID, IntPtr.Zero);
foreach (IHTMLElement el in (IHTMLElementCollection)myHTMLDoc.body.all)
if ((el.GetType().ToString().CompareTo("mshtml.HTMLSpanElementClass") == 0) && (el.className != null))
if ((el.innerText != null) && (el.innerText.IndexOf("!%") >= 0) && (el.innerText.LastIndexOf("%!") > 0))
{
// Decrypt the message
String ts = el.innerText.ToString();
String ts2 = ts.Substring(el.innerText.IndexOf(startTag) + 2, el.innerText.LastIndexOf(stopTag) - 2);
String decryptedText = null;
try
{
SymCrypto sc = new SymCrypto(SymCrypto.ServiceProviderEnum.TripleDES);
decryptedText = decryptedLineMarker + sc.Decrypt(ts2, secretPassword);
}
catch (Exception ex)
{
decryptedText = "\n" + "Received a malformed encrypted string: " + ts2 + "\n" + "Exception generated: " + ex.Message;
}
el.innerText = decryptedText;
}
}
private void MessengerYahoo_MoveTheWindow()
{
Win32API.RECT activeRectangle;
Win32API.GetWindowRect(hWndForegroundWindow, out activeRectangle);
this.Location = new System.Drawing.Point(activeRectangle.Left - this.Size.Width, activeRectangle.Bottom - this.Size.Height);
}
private void MessengerYahoo_HandleSend()
{
// Get the message from the writeArea
int textLength = Win32API.SendMessage(hWndWriteArea, Win32API.WM_GETTEXTLENGTH, 0, IntPtr.Zero);
StringBuilder writingtext = new StringBuilder(textLength + 1);
Win32API.SendMessage(hWndWriteArea, Win32API.WM_GETTEXT, textLength + 1, writingtext);
// Encrypt the message
SymCrypto sc = new SymCrypto(SymCrypto.ServiceProviderEnum.TripleDES);
String encryptedText = startTag + sc.Encrypt(writingtext.ToString(), secretPassword) + stopTag;
// Delete the message from the writeArea
Win32API.SendMessage(hWndWriteArea, Win32API.WM_SETTEXT, 0, System.Text.Encoding.ASCII.GetBytes("".ToCharArray()));
// Write the new encrypted message
byte[] encryptedText_byte = System.Text.Encoding.ASCII.GetBytes(encryptedText.ToCharArray());
Win32API.SendMessage(hWndWriteArea, Win32API.WM_SETTEXT, 0, encryptedText_byte);
// Send the new encrypted message (by simulating a click on the Send button)
Win32API.SendMessage(hWndSendButton, Win32API.BM_CLICK, 0, encryptedText_byte);
// Delete the message from the writeArea
Win32API.SendMessage(hWndWriteArea, Win32API.WM_SETTEXT, 0, System.Text.Encoding.ASCII.GetBytes("".ToCharArray()));
// Set the foreground window = the messeger window
Win32API.SetForegroundWindow(hWndForegroundWindow);
}
private bool MessengerYahoo_HandleEnumChild(IntPtr hWnd, IntPtr lParam)
{
int textLength = Win32API.SendMessage(hWnd, Win32API.WM_GETTEXTLENGTH, 0, IntPtr.Zero);
StringBuilder text = new StringBuilder(textLength + 1);
Win32API.SendMessage(hWnd, Win32API.WM_GETTEXT, textLength + 1, text);
StringBuilder className = new StringBuilder();
Win32API.GetClassName(hWnd, className, className.MaxCapacity);
if (className.ToString().Contains(writeAreaClassName))
hWndWriteArea = hWnd;
if (className.ToString().Contains(talkAreaContainerName))
hWndTalkArea = Win32API.FindWindowEx(hWnd, IntPtr.Zero, talkAreaClassName, IntPtr.Zero);
if (className.ToString().Contains(sendButtonClassName))
if (text.ToString().Contains(sendButtonText))
hWndSendButton = hWnd;
if ((hWndWriteArea == IntPtr.Zero) || (hWndTalkArea == IntPtr.Zero) || (hWndSendButton == IntPtr.Zero))
return true;
else
return false;
}
#endregion
}
}