(原创)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 contextusing (JavascriptContext context = new JavascriptContext()) {// Setting the externals parameters of the contextcontext.SetParameter("console", new SystemConsole());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"));}}}是不是很強大,竟然可以和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,usercodecontext.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
轉載于:https://www.cnblogs.com/coky/p/6765405.html
總結
以上是生活随笔為你收集整理的(原创)VS2017 C# 运行 Javasrcipt RSA 加密用户名登录 Java开发的服务器的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 信用卡申请条件有哪些
- 下一篇: Spring mvc注解方式使用事务回滚