Methods in calling JavaScript files in C # WinForm

Source: Internet
Author: User
Tags md5 encryption time in milliseconds

There are a lot of SNS community or SNS site, such as happy, 51, school, etc., but found that most of the community in inviting friends when they did not provide QQ mailbox or QQ space Friends list to get the function, but it seems that domestic support, but the online related QQ articles are not many, I hope this article will bring you some help.

QQ Space and Mailbox login check mode:

QQ Space and e-mail, the user entered the password will first be a section of the page JS encryption, and then encrypt the password will be added to the verification code to form a new string, and then the new string is MD5 (32-bit) encryption, encryption after the formation of the final password, That's why we often find that the number of passwords in our password boxes increases as we commit, and then when you commit, The current request will add the Set-cookie value returned from the previous fetch to the cookie in the current request header to keep the CAPTCHA request consistent with the current request, and then post the new password and username to the QQ server.

Well, after looking at the above QQ verification method, we look at how to use the code to login QQ space and email to get the friends and contacts we need.

QQ space and Mailbox automatic login get a contact's solution:

QQ Space and e-mail landing first we need to user input password for JS encryption, but we use the code to log in when we did not use the browser, then how do we drive JS it? Do not be afraid, in Java and. NET have a corresponding way to drive JS on the server code side, here focus on the method of. NET:

First, download Windows Script control to the Microsoft Web site, which is an ActiveX (R) control. After the download installation is complete, create a new C # application project, select the reference node in Solution Explorer, right-click the Select Add Reference menu, pop up the Add Reference dialog box, click Browse to locate the directory where Windows Script control is installed. Select the MSScript.ocx file to determine. Then a Msscriptcontrol component is added under the reference node, and the following is all the objects after his interop.

<!--[if!vml]--><!--[endif]-->

The ScriptControl provides a simple interface to the host script engine that supports ActiveX (TM) script. Next we explain the properties and methods of the ScriptControl that are converted to the Scriptcontrolclass class.

Property

Allowui property: The user interface element that is applied to the ScriptControl itself or the SCIRPT engine is readable and writable.

CodeObject property: Returns the object that is used to invoke the public member of the specified module. Read-only.

Error property: Returns the Error object that contains the details about the last error that occurred. Read-only.

Language Property: Sets or returns the name of the Script language being used. can read and write.

Modules property: Returns a collection of modules for the ScriptControl object. Read-only.

Procedures property: Returns the collection of procedures defined in the specified module. Read-only.

Sitehwnd Property: Sets or returns the HWnd of the window, which is used to display dialog boxes and other user interface elements by executing the Script code. can read and write.

State property: Sets or returns the mode of the ScriptControl object. can read and write.

Timeout Property: Sets or returns the time (in milliseconds) after which the user can choose to abort the execution of the Script code or allow the code to continue executing. can read and write.

Usesafesubset Property: Sets or returns a Boolean value that indicates whether the host application has a privacy requirement. Usesafesubset is True if the host application requires security control, otherwise False. can read and write.

Method

Addcode method: Adds the specified code to the module. The Addcode method can be called multiple times.

AddObject method: Make the Host object model available to the Script engine.

Eval method: Evaluates an expression and returns the result.

Executestatement method: Executes the specified statement.

Reset method: Discards all Script code and objects that have been added to the ScriptControl.

Run method: Runs the specified procedure.

Event

Error Event: This event occurs when a run-time error occurs.

Timeout Event: This event occurs when the time specified by the timeout attribute is exceeded and the user selects End in the Results dialog box.

Description

The Allowui property, if set to false, shows statements such as dialog boxes that do not work, such as MsgBox statements in VBScript, alert in JavaScript, and so on, and if the executed script exceeds the number of milliseconds set by timeout, Also does not jump out of the dialog box that exceeds the time alert, and vice versa; The reset Language property empties the Addcode loaded code, and for the timeout property, ScriptControl checks the Allowui property of the object when a time-out occurs. Determines whether user interface elements are allowed to be displayed.

To make the control easier to use, encapsulate it with the ScriptEngine class, which is the complete code:

Using System;

Using Msscriptcontrol;

Using System.Text;

Namespace Scriptnamespace

{

<summary>

Script type

</summary>

public enum ScriptLanguage

{

<summary>

JScript scripting language

</summary>

Jscript

<summary>

VBScript scripting language

</summary>

VBscript,

<summary>

JavaScript scripting language

</summary>

Javascript

}

<summary>

Script Run Error Agent

</summary>

public delegate void Runerrorhandler ();

<summary>

Script run time-out agent

</summary>

public delegate void Runtimeouthandler ();

<summary>

ScriptEngine class

</summary>

public class ScriptEngine

{

Private ScriptControl MSC;

Defining script Run Error events

public event Runerrorhandler Runerror;

To define a script run timeout event

public event Runtimeouthandler Runtimeout;

<summary>

constructor function

</summary>

Public ScriptEngine ()

: This (scriptlanguage.vbscript)

{ }

<summary>

constructor function

</summary>

<param name= "Language" > Script type </param>

Public ScriptEngine (ScriptLanguage language)

{

This.msc = new Scriptcontrolclass ();

This.msc.UseSafeSubset = true;

This.msc.Language = Language. ToString ();

((dscriptcontrolsource_event) this.msc). Error + = new Dscriptcontrolsource_erroreventhandler (scriptengine_error);

((dscriptcontrolsource_event) this.msc). Timeout + = new Dscriptcontrolsource_timeouteventhandler (scriptengine_timeout);

}

<summary>

Run the Eval method

</summary>

<param name= "expression" > Expressions </param>

<param name= "Codebody" > Function Body </param>

<returns> return value object</returns>

public object Eval (string expression, string codebody)

{

MSc. Addcode (Codebody);

return MSC. Eval (expression);

}

<summary>

Run the Eval method

</summary>

<param name= "Language" > Scripting language </param>

<param name= "expression" > Expressions </param>

<param name= "Codebody" > Function Body </param>

<returns> return value object</returns>

public Object Eval (ScriptLanguage language, string expression, string codebody)

{

if (this. Language! = Language)

This. Language = Language;

return Eval (expression, codebody);

}

<summary>

Running the Run method

</summary>

<param name= "Mainfunctionname" > Entry function name </param>

<param name= "Parameters" > Parameters </param>

<param name= "Codebody" > Function Body </param>

<returns> return value object</returns>

public Object Run (String mainfunctionname, object[] parameters, string codebody)

{

This.msc.AddCode (Codebody);

return MSC. Run (mainfunctionname, ref parameters);

}

<summary>

Running the Run method

</summary>

<param name= "Language" > Scripting language </param>

<param name= "Mainfunctionname" > Entry function name </param>

<param name= "Parameters" > Parameters </param>

<param name= "Codebody" > Function Body </param>

<returns> return value object</returns>

public Object Run (ScriptLanguage language, String mainfunctionname, object[] parameters, string codebody)

{

if (this. Language! = Language)

This. Language = Language;

Return Run (mainfunctionname, parameters, codebody);

}

<summary>

Discard all Script code and objects that have been added to the ScriptControl

</summary>

public void Reset ()

{

This.msc.Reset ();

}

<summary>

Gets or sets the scripting language

</summary>

Public ScriptLanguage Language

{

get {return (scriptlanguage) Enum.parse (typeof (ScriptLanguage), this.msc.Language, false);}

set {This.msc.Language = value. ToString (); }

}

<summary>

Gets or sets the script execution time in milliseconds

</summary>

public int Timeout

{

get {return 0;}

}

<summary>

Set whether user interface elements are displayed

</summary>

public bool Allowui

{

get {return This.msc.AllowUI;}

set {This.msc.AllowUI = value;}

}

<summary>

Whether the host application has privacy requirements

</summary>

public bool Usesafesubset

{

get {return this.msc.UseSafeSubset;}

set {This.msc.UseSafeSubset = true;}

}

<summary>

Runerror event Excitation

</summary>

private void OnError ()

{

if (runerror! = null)

Runerror ();

}

<summary>

OnTimeOut event Excitation

</summary>

private void OnTimeOut ()

{

if (runtimeout! = null)

Runtimeout ();

}

private void Scriptengine_error ()

{

OnError ();

}

private void Scriptengine_timeout ()

{

OnTimeOut ();

}

}

}

On the Find. NET Drive JS Method Later, then we also need to find QQ used to encrypt the JS file, the specific JS file you can go to the QQ login page to download, if you do not know which JS we can find by grasping the package, here will not repeat. Below we can use Msscriptcontrol to drive JS to get the first encrypted password, the method is as follows:

<summary>

Get the password for the first time after encryption

</summary>

<param name= "Jsfilepath" >js file </param>

<param name= "FuncName" > method name for Encryption </param>

<param name= "Paramers" > Encryption methods need to pass in the parameters (one is the password, the other is a code that can be obtained on the page) </param>

<returns> Password after encryption </returns>

Private Object GetPassword (String Jsfilepath, String funcName, params object[] paramers)

{

StreamReader reader = new StreamReader (Jsfilepath);

String sscript = reader. ReadToEnd ();

ScriptEngine se = new ScriptEngine (scriptlanguage.javascript);

Object obj = se. Run (FuncName, Paramers, Sscript);

return obj;

}

With this function above we can encrypt the password for the first time and we need to get the verification code next.

We can use the HttpWebRequest class to get the address: Http://ptlogin2.qq.com/getimage to get the verification code, but be sure to remember to save the cookie returned by this request, We can save it to the Cookiecontainer object so that the returned cookie is added to the header of the login post request to keep the consistency of the request.

Now we will obtain the verification code into a stream or other way for different platform output, we found that the QQ space and email verification code generated by the image is not too complex, so you can consider using image recognition method to automatically obtain the corresponding characters of the image (now there are many third-party verification code recognition software), Of course, for the success rate of consideration, it is best to let the user manually input.

OK, with the verification code, we will now add the verification code character and the password we just added to form a new string, and then through the MD5 encryption

String Pwd=formsauthentication.hashpasswordforstoringinconfigfile (this. Password, "MD5"). ToLower ()

form the password we need in the end.

Finally, we can be user input user name, password and post to the corresponding address QQ, so we successfully landed QQ space or mailbox. After the successful login, you can certainly get contacts and friends and so on.

Well, the article is written here, I hope it will be useful to everyone. ^^ Brave Chen

Methods in calling JavaScript files in C # WinForm

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.