百度等登录RSA加密密码Java版本

本文通过研究百度登录页面的JS,并根据RSA算法,没有使用到第三方库,而是直接使用BigInteger进行计算。

private static String encryptRsa(byte[] source) {
  BigInteger modulus = new BigInteger("126241530253508025781262066619137371803493027496554476063203402310474966019771278419441800007858910595318245186282502104545903062225245500793318133389523584619035060354085823289611677398887819758402850628694295661621985118867551833581272405733949612424645351378772212617779892197765424832405262677735275332143"), exponent = new BigInteger("65537");//使用的是百度统一登录页的modulus和exponent,可以根据实际项目不同来修改
  int chunkSize = modulus.bigLength() / 8 - 1;//算出分批加密每批的最大长度
  StringBuilder result = new StringBuilder();
  for(int i = 0; i < source.length; i += chunkSize) {


    BigInteger c = new BigInteger(arrayReverse(Arrays.copyOfRange(source, i, i + chunkSize)));

    result.append(c.modPow(exponent, modulus).toString(16));

  }

  return result.toString();
}



private static byte[] arrayReverse(byte[] src) {

  if(src == null) {

    return null;
  } else {

    int i = 0, j = src.length;

    byte[] result = new byte[j];

    while(true) {

      j--;

      if(j < 0) {

        return result;

      }

      result[j] = src[i++];

    }

  }
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值