第一次写博客.

最近想做一个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", ); // 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", "");
context.SetParameter("modulus", "89ded116f36bf4e6108f549379f0137661a432e64fa80ae13cf1d0bb9fc957d16ee69a44383e3e4d0195e58f700ee7b4b00fa08f73a0cf6fcb517e3a772a1d2cfc96d2aa4d1df8b1c3a09f7c4ad4c3e29d427b6f96269d3d15db9da9d63fd2fface9299d63f4f17c1fc2565efcbe64b84e2a029f0a60a889106c3287f6a0be07");
context.SetParameter("password", "coky");
context.SetParameter("usercode", "");
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开发的服务器的更多相关文章

  1. 你真的了解字典(Dictionary)吗? C# Memory Cache 踩坑记录 .net 泛型 结构化CSS设计思维 WinForm POST上传与后台接收 高效实用的.NET开源项目 .net 笔试面试总结(3) .net 笔试面试总结(2) 依赖注入 C# RSA 加密 C#与Java AES 加密解密

    你真的了解字典(Dictionary)吗?   从一道亲身经历的面试题说起 半年前,我参加我现在所在公司的面试,面试官给了一道题,说有一个Y形的链表,知道起始节点,找出交叉节点.为了便于描述,我把上面 ...

  2. RSA加密&解密【Java&Scala】

    一.简介 RSA加密算法是一种非对称加密算法.在公开密钥加密和电子商业中RSA被广泛使用. RSA公开密钥密码体制.所谓公开密钥密码体制就是使用不同的加密密钥与解密密钥,是一种“由已知加密密钥推导出解 ...

  3. RSA加密算法和SSH远程连接服务器

    服务器端与客户端的密钥系统不一样,称为非对称式密钥系统 RSA算法的基础是模运算x mod n,事实上: [(a mod n) + (b mod n)] mod n = (a+b) mod n [(a ...

  4. LoadRunner 12 模拟 RSA加密 登录的实现(JS)

    LR 12 中 web_js_run API 非常坑,只能调用一个 JS 文件:更坑的是,不能通用 一个JS调用另外一个JS:(可能有,但在网上找了N个国家,都没有找到!如有,还请朋友告之,谢谢.) ...

  5. IOS, Android, Java Web Rest : RSA 加密和解密问题

    IOS, Android, Java Web Rest :  RSA 加密和解密问题 一对公钥私钥可以使用 OpenSSL创建, 通常 1024位长度够了. 注意: 1. 公钥私钥是BASE64编码的 ...

  6. 安全篇-AES/RSA加密机制

    在服务器与终端设备进行HTTP通讯时,常常会被网络抓包.反编译(Android APK反编译工具)等技术得到HTTP通讯接口地址和参数.为了确保信息的安全,我们采用AES+RSA组合的方式进行接口参数 ...

  7. Java开发和运行环境的搭建

    Java开发需要准备的东西? JDK+Eclipse 其中JDK的意思是Java开发工具包,Eclipse是进行用于做Java程序开发的工具(当然你也可以用记事本什么的去做). 其他开发工具:JCre ...

  8. RSA加密和数字签名在Java中常见应用【原创】

    相关术语解释: RSA,参考: https://en.wikipedia.org/wiki/RSA_(cryptosystem) 非对称加密算法 ,参考:https://baike.baidu.com ...

  9. IdentityServer4之JWT签名(RSA加密证书)及验签

    一.前言 在IdentityServer4中有两种令牌,一个是JWT和Reference Token,在IDS4中默认用的是JWT,那么这两者有什么区别呢? 二.JWT与Reference Token ...

随机推荐

  1. Vue学习之路第二篇:插值表达式

    要开始写Vue的功能了,是不是很激动呢!开始吧! 1.首先建立一个html页面,导入Vue js包 <script type="text/javascript" src=&q ...

  2. 发个ZKW线段树板子测试一下代码高亮

    是我,Long time no see          --Jim 先安利 Wolves  歌手:Madilyn Bailey http://music.163.com/song/524149464 ...

  3. [读书笔记] R语言实战 (四) 基本数据管理

    1. 创建新的变量 mydata<-data.frame(x1=c(2,2,6,4),x2=c(3,4,2,8)) #方法一 mydata$sumx<-mydata$x1+mydata$x ...

  4. Laravel源码解析之从入口开始

    前言 提升能力的方法并非使用更多工具,而是解刨自己所使用的工具.今天我们从Laravel启动的第一步开始讲起. 入口文件 laravel是单入口框架,所有请求必将经过index.php define( ...

  5. python_传递任意数量的实参

    '''def name(*args): #python创建一个空元组,将收到的所有值都封装在这个元组中 """打印所有姓名""" for i ...

  6. URAL 1517 Freedom of Choice

    Freedom of Choice Time Limit: 2000ms Memory Limit: 32768KB This problem will be judged on Ural. Orig ...

  7. static方法调用

    Static方法调用,类名.方法名 int number = Integer.ParseInt(String ); 将字符串参数作为有符号的十进制整数进行解析 将数字解析成字节数组 Character ...

  8. Nodejs RESTFul架构实践之api篇(转)

    why token based auth? 此段摘自 http://zhuanlan.zhihu.com/FrontendMagazine/19920223 英文原文 http://code.tuts ...

  9. [HTML5] Inlining images with SVG and data URIs

    The main reason you want to do I"nlining images with SVG and data URIs" is to reduce http ...

  10. 强名称程序集(strong name assembly)——为程序集赋予强名称

    ,唯一标识一个程序集 2,放置程序集被仿冒和被篡改. 3,能够部署到全局程序集缓存(GAC:GlobalAssembly Cache)中:在将强名称程序集不熟在GAC其中以后,强名称程序集也能够称为共 ...