本文通过研究百度登录页面的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++];
}
}
}