展开全部
根据已知的公钥m与e生成PublicKey,然后加密,需要用e69da5e6ba903231313335323631343130323136353331333335313838到bouncycastle这个库,大致代码如下:// 生成m与e
byte[] mBytes = Hex.decode("C535AD4F...略");
BigInteger m = new BigInteger(1, mBytes);
BigInteger e = BigInteger.valueOf(0x10001);
// 恢复公钥
KeyFactory keyFactory = KeyFactory.getInstance("RSA");
PublicKey publicKey = keyFactory.generatePublic(new RSAPublicKeySpec(m, e));
/* 根据经验,那个JS加密后的密文用Java解出来是反转的字符串,
* 所以如果想要达到与JS完全一致的效果的话,需要将明文先反转,
* 即将 "admin" 变成 "nimda"
*/
byte[] data = new StringBuilder("admin").reverse().toString().getBytes();
// 现在可以用publicKey加密了
Cipher cipher = Cipher.getInstance("RSA", new BouncyCastleProvider());
cipher.init(Cipher.ENCRYPT_MODE, publicKey);
byte[] enc = cipher.doFinal(data);
System.out.println(Hex.toHexString(enc));
理论上代码大概是这样。加密结果是:
0d42e0ab31391a536346a1a1af7696e21734ab6004b33f4ce1add778d0c3b8b93141cf0422ef3e086e41d0dcf96c9908e30dd9b993c1eea0d0a392f2ecf4347e5ed7d9b3451796ef04203248536ebca02120a9c3fa520f88ca43ec0df30d2210026ac98e0dcd460bd3bd38b4ddd30a5fe0a0c103a5bd02b2e7eb9ae3a2ddab68