用jsoup 模拟登陆验证码,rsa 密码加密

这个问题拖了快两个月了,这段时间比较忙,一直都没有解决他,刚好赶上端午,就处理掉,

遇到的问题:jsoup进行爬取,需要用户登陆,首先是有验证码,没什么好说,网上资源一大把,而且我爬取的网站的验证码相对比较简单,无需太大复杂的操作。不做赘叙,比较困难的是密码是用rsa 进行数据加密,我查了很多资料,希望在java 模拟加密,但均失败,后来在网上资料的提醒下,使用java 调用 js 代码,直接使用该网站的加密js进行加密,后成功。

 

 

爬取存储在页面的exponent,modulus,java 调用该网站的加密js进行加密,各网站的加密js可能会有修改,所以建议使用你要爬取的网站的js进行修改,一般名称是Security.js

            // 爬取 rsa 密钥
         Connection rsaConnect = Jsoup.connect("http://www.xxxxx.com");
         rsaConnect.cookies(codeMap);

         Document rsaRes= rsaConnect.get();
         
         Integer starti=rsaRes.html().toString().indexOf("var rsaPublicKey = ");
         Integer endi=rsaRes.html().toString().indexOf("</script>", starti);
         String jscode=rsaRes.html().toString().substring(starti+"var rsaPublicKey = ".length(), endi-1);
         System.out.println(jscode);
         Integer exponentIndex=jscode.indexOf('(');
         String exponent=jscode.substring(exponentIndex+2,jscode.indexOf(',', exponentIndex)-1);
         Integer modulusIndex=jscode.indexOf(',',jscode.indexOf(',', exponentIndex)-1);
         String modulus=jscode.substring(modulusIndex+7,jscode.indexOf(')', exponentIndex)-1);
         ScriptEngineManager engineManager = new ScriptEngineManager();
         ScriptEngine engine = engineManager.getEngineByName("js");
         InputStream ins = new  FileInputStream(new File("C://Users//xxxx//Desktop//test.js"));
         InputStreamReader insReader = new InputStreamReader(ins);
         String password=null;
//         modulus="ddd1a5c26d0578b66d656764ef9b10170afaa6cde8fc6be0332c9c5802d826d44acd8b0a744875559bad2fa925b84162589657348e007495f92ed60bd7c31a2a833ac0736afe4d982192591a649cda541d10b74b4bb62023011e33a9ba6bee240d07ee4b7a0adef67a83adcc03d358783d5bf5f408c0d2792e962a2dfc5db657";
         try {
             engine.eval(insReader);
             engine.eval("var password =encryptPassword('"+exponent+"','"+modulus+"','xxxxxxxxxxx');");
              password = engine.get("password").toString();
             System.out.println(password);
         } catch (ScriptException e) {
             e.printStackTrace();
         
         }

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值