Original VS2017 C # running Javasrcipt RSA encrypted user name login to Java Development Server

Source: Internet
Author: User
Tags base64 modulus

The first time I wrote a blog.

Recently want to do a Web automatic login, the user name and password is RSA encrypted, the background is used in Java, I will only point C #, grab the package everything is done (using the fiddler), but because of C # and RSA Encryption way, I did n days, are uncertain, in the middle asked a lot of people, The people who are willing to help are not many, maybe I am too vegetable. Just to get a certified cookie, I used the WebBrowser control, let the person log in, and then get a cookie, but the feeling is a semi-finished.

However, C # and Java in the middle of the RSA, I encountered 2 problems, the Internet is public key to public key, unfortunately, I only exponent,modulus, to use these 2 to generate a new public key, learned C programmer, Look at the Java code how much to understand some, I saw a lot of Java code generated public key, a lot of conversion, the last generation of RSA encrypted data, always system access failed. I am smattering to RSA, and many ToBase64, from Base64, Tohex, Fromhex,btye[], I went to see the next code, unfortunately, the foundation is too poor, are smattering. I followed, the online code, back and forth, in the generation, but unfortunately still the system access failed.

I just thought, what is encryption, as long as they encode the conversion sequence and steps in the middle, and I am not the same, I went into a dead end. I try to read through JavaScript to generate ciphertext files, but still the foundation is too poor, there are some algorithms, and BigInt type, very complex, I think I can read, But someone changed some code, and the work was wasted. The best way is to run the Javasrcipt file that he provided to generate the ciphertext.

I started looking for C # to run JavaScript, there are 2 classes, one is the use of outdated Sciptcontrol, 2010 years ago technology, and 64 support is not good, are pits ah. There is another way to run a third-party class library. I use VS2017 NuGet The input Javascript found the javascript.net, that's it.

But the online learning material is very few, and login his official website, are Chinese, I this primary school English, is a bit unbearable ah, do not understand English, we Baidu, Google Translate, find the entry, enter the document, look at the code. (http://javascriptdotnet.codeplex.com/documentation)

  classProgram { Public classSystemconsole { PublicSystemconsole () {} Public voidPrint (stringistring)            {Console.WriteLine (istring); }        }        Static voidMain (string[] args) {            //Initialize the context            using(Javascriptcontext context =NewJavascriptcontext ()) {                //Setting The externals parameters of the contextContext. Setparameter ("Console",Newsystemconsole ()); Context. Setparameter ("message","Hello World!"); Context. Setparameter (" Number",1); //Running the scriptContext. Run ("var i; for (i = 0; i < 5; i++) Console. Print (Message + ' (' + i + ') '); number + = i;"); //Getting a parameterConsole.WriteLine ("Number :"+ context. GetParameter (" Number")); }        }    }

is not so powerful that you can interact with C # objects.

But I want to use a file, not a string ah. Continue to look at the code, but the code is very small, read the question inside, find the code. There's A.

Static voidMain (string[] args) {      stringScript ="function Test (A, b) {return a+b;} test (' abc ', ' Def ');"; Try {         using(Javascriptcontext context =NewJavascriptcontext ()) {          stringresult = (string) context. Run (script,"Test");        Console.WriteLine (result); }      } Catch(Exception ex) {Console.WriteLine (ex).        Message); Console.WriteLine (ex.      StackTrace); }    }

This can call the method. Great, change it. Find a half-day find, do not support directly call the file. Change it.

stringScript = File.readalltext (appdomain.currentdomain.basedirectory+@".. \.. \js\base64.js"); Script+ = File.readalltext (AppDomain.CurrentDomain.BaseDirectory +@".. \.. \js\security.js"); Script+ = File.readalltext (AppDomain.CurrentDomain.BaseDirectory +@".. \.. \js\base.js"); Try            {                using(Javascriptcontext context =NewJavascriptcontext ()) {                    //Exponent,modulus,password,usercodeContext. Setparameter ("Console",Newsystemconsole ()); Context. Setparameter ("exponent","10001"); Context. Setparameter ("modulus","89ded116f36bf4e6108f549379f0137661a432e64fa80ae13cf1d0bb9fc957d16ee69a44383e3e4d0195e58f700ee7b4b00fa08f73a0cf6fcb517 E3a772a1d2cfc96d2aa4d1df8b1c3a09f7c4ad4c3e29d427b6f96269d3d15db9da9d63fd2fface9299d63f4f17c1fc2565efcbe64b84e2a029f0a60a8 89106c3287f6a0be07"); Context. Setparameter ("Password","Coky"); Context. Setparameter ("Usercode","1234"); Context. Setparameter ("Usercodersa",""); //context. Setparameter ("window", null);Context. Run (script,"Test"); Console.WriteLine ("Usercodersa:"+ context. GetParameter ("Usercodersa")); Console.WriteLine ("Passwordrsa:"+ context. GetParameter ("Passwordrsa")); }            }            Catch(Exception ex) {Console.WriteLine (ex).                Message); Console.WriteLine (ex.            StackTrace); } console.read ();

Here is the JavaScript file, which I wrote myself, the other 2 files

Base64.js
Security.js
I used strings to concatenate them together. But I think in the front, should be the least dependent, on the JavaScript specific syntax is not very understanding, but I think this insurance.
varUsercodersavarPasswordrsaconsole.print ("Base Start");functionTest (exponent,modulus,password,usercode) {Console. Print ("Exponent:" +exponent); Console. Print ("Modulus:" +modulus); Console. Print ("Password:" +password); Console. Print ("Usercode:" +Usercode); Console. Print ("=============================================================="); Rsautils.setmaxdigits (200); varKey =NewRsautils.getkeypair (Exponent, ", modulus); Console. Print ("Key:" +key); Console. Print ("=============================================================="); varB64 =base64encode (password); Console. Print ("Password Base64Encode:" +b64); Console. Print ("=============================================================="); varReversedpwd = B64.split (""). Reverse (). Join (""); Console. Print ("Password Base64Encode reverse:" +reversedpwd); Console. Print ("=============================================================="); Passwordrsa=rsautils.encryptedstring (key, reversedpwd); Console. Print ("Password Rsaencry:" +Passwordrsa); Console. Print ("=============================================================="); B64=Base64Encode (Usercode); Console. Print ("Usercode Base64Encode:" +b64); Console. Print ("=============================================================="); Reversedpwd= B64.split (""). Reverse (). Join (""); Console. Print ("Usercode Base64Encode Reverse:" +reversedpwd); Console. Print ("=============================================================="); Usercodersa=rsautils.encryptedstring (key, reversedpwd); Console. Print ("Usercode rsaencry:" +Usercodersa); Console. Print ("==============================================================");} Test (Exponent,modulus,password,usercode);

Run a bit.

It's done. First write, write very general. There is no understanding, welcome message or qq:2786771252

Original VS2017 C # running Javasrcipt RSA encrypted user name login to Java Development Server

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.