码迷,mamicode.com
首页 > 编程语言 > 详细

(原创)VS2017 C# 运行 Javasrcipt RSA 加密用户名登录 Java开发的服务器

时间:2017-04-26 01:12:42      阅读:418      评论:0      收藏:0      [点我收藏+]

标签:hex   mod   工作   加密   test   get   cep   alt   ext   

第一次写博客.

     最近想做一个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+ @"..\..\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 = 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

(原创)VS2017 C# 运行 Javasrcipt RSA 加密用户名登录 Java开发的服务器

标签:hex   mod   工作   加密   test   get   cep   alt   ext   

原文地址:http://www.cnblogs.com/coky/p/6765405.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!