zoukankan      html  css  js  c++  java
  • (原创)VS2017 C# 运行 Javasrcipt RSA 加密用户名登录 Java开发的服务器

    第一次写博客.

         最近想做一个Web的自动登录,用户名和密码是RSA加密过的,后台是用的JAVA,我只会点C#,抓包什么都搞定了(使用的是Fiddler),不过由于C#和RSA的加密方式不同,我搞了N天,都搞不定,中间问过很多人,愿意帮助的人不多,可能是我太菜了.就是为了得到个认证的cookie,我中间用过Webbrowser控件,让人自己登录,然后得到Cookie,不过感觉终究是个半成品.

         然而,C#和Java中间的RSA互转,我遇到了2个问题,网上都是public key 转 public key ,可惜,我只有exponent,modulus,要用这2个生成新的public key ,学习过C的程序员,看Java代码都多少看懂一些,我看了很多Java代码生成public Key的,很多转换的,最后的生成的RSA加密数据,总是系统访问失败.我对RSA一知半解,而且中间很多 ToBase64 ,From Base64, ToHex,FromHex,Btye[],我又去看了下编码,可惜,基础太差,都是一知半解.我按照,网上的代码,来回转换,在生成,不过可惜还是系统访问失败.

        我就想,什么是加密,只要他们中间几次编码的转换顺序和步骤,和我的不一样,我就走进了死胡同.我试着想通读JavaScript生成密文的文件,不过还是基础太差,中间有一些算法,和 BigInt 类型,很复杂,我想我可以看完,不过人家改几个代码,工作又白费了.最好的办法是运行他提供的JavaSrcipt 文件来生成密文.

       我开始找C#运行JavaScript的办法,有2类,一个是使用 过时的 sciptcontrol ,2010年前的技术,而且64支持不好,都是坑啊.还有一种就是运行第三方类库.我使用VS2017 NuGet 输入Javascript 找到了 Javascript.Net,就是它了.

      不过网上学习资料很少,而且登录他的官网,都是English,我这小学英语,真是有点吃不消啊,看不懂英文,我们百度,谷歌翻译,找到词条,进入文档,看代码.(http://javascriptdotnet.codeplex.com/documentation)

      class Program
        {
            public class SystemConsole
            {
                public SystemConsole() { }
    
                public void Print(string iString)
                {
                    Console.WriteLine(iString);
                }
            }
    
            static void Main(string[] args)
            {
                // Initialize the context
                using (JavascriptContext context = new JavascriptContext()) {
    
                    // Setting the externals parameters of the context
                    context.SetParameter("console", new SystemConsole());
                    context.SetParameter("message", "Hello World !");
                    context.SetParameter("number", 1);
    
                    // Running the script
                    context.Run("var i; for (i = 0; i < 5; i++) console.Print(message + ' (' + i + ')'); number += i;");
    
                    // Getting a parameter
                    Console.WriteLine("number: " + context.GetParameter("number"));
                }
            }
        }

    是不是很强大,竟然可以和C#对象交互.

    不过我想使用的是文件,不是字符串啊.继续看代码,不过代码很少,看了提问里面,找代码.有一个.

    static void Main(string[] args)
        {
          string script = "function test(a,b){return a+b;} test('abc','def');";
    
          try { 
            using (JavascriptContext context = new JavascriptContext()) {
              string result = (string)context.Run(script, "test");
              Console.WriteLine(result);
            }
          } catch (Exception ex) {
            Console.WriteLine(ex.Message);
            Console.WriteLine(ex.StackTrace);
          }
        }

    这可以调用方法.太棒了,自己改下.找了半天发现,不支持直接调用文件.自己改下.

    string script = File.ReadAllText(AppDomain.CurrentDomain.BaseDirectory+ @"....jsBase64.js");
                script += File.ReadAllText(AppDomain.CurrentDomain.BaseDirectory + @"....jssecurity.js");
                script += File.ReadAllText(AppDomain.CurrentDomain.BaseDirectory + @"....jsase.js");
                try
                {
                    using (JavascriptContext context = new JavascriptContext())
                    {
                        //exponent,modulus,password,usercode
                        context.SetParameter("console", new SystemConsole());
                        context.SetParameter("exponent", "10001");
                        context.SetParameter("modulus", "89ded116f36bf4e6108f549379f0137661a432e64fa80ae13cf1d0bb9fc957d16ee69a44383e3e4d0195e58f700ee7b4b00fa08f73a0cf6fcb517e3a772a1d2cfc96d2aa4d1df8b1c3a09f7c4ad4c3e29d427b6f96269d3d15db9da9d63fd2fface9299d63f4f17c1fc2565efcbe64b84e2a029f0a60a889106c3287f6a0be07");
                        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();

    下面是Javascript文件,这个是我自己编写的,其他2个文件

    Base64.js
    security.js
    我用字符串把他们串联起来.不过我觉得在最前面的,应该是依赖最小的,对Javascript具体语法不是很了解,不过我觉得这样保险.
    var usercodeRSA
    var passwordRSA
    console.Print("base start");
    function test(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);
        var key = new RSAUtils.getKeyPair(exponent, '', modulus);
        console.Print("Key:" + key);
        console.Print("==============================================================");
    
        var b64 = base64encode(password);
        console.Print("password base64encode:" + b64);
        console.Print("==============================================================");
        var reversedPwd = 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);

    运行一下.

     

    大功告成.第一次写,写的很一般.有看不懂,欢迎留言 或者QQ:2786771252

  • 相关阅读:
    Flink 1.9 实战:使用 SQL 读取 Kafka 并写入 MySQL
    Spring 社区的唯一一个国产开源项目
    使用DynamoShake从dynamodb迁移到mongodb
    1754-I Hate It 线段树(单点替换,区间最值)
    1166-敌兵布阵 线段树(单点增减,区间求和)
    P1078 文化之旅
    P1107 最大整数
    521. [NOIP2010] 引水入城 cogs
    P1137 旅行计划
    P2678 跳石头(二分答案)
  • 原文地址:https://www.cnblogs.com/coky/p/6765405.html
Copyright © 2011-2022 走看看