我正在开发一个使用 RSA 加密的 android 应用程序。
我可以从我的服务器上获取我的私钥和我的朋友的公钥。
私钥是这样的:
{"n":"...","d":"...","p":"...","q":"...","dmp1":"...","dmq1":"...","coeff":"..."} (jsbn json format)
modulus => n
public exponent => e
private exponent => d
prime1 => p
prime2 => q
exponent1 => dmp1
exponent2 => dmq1
coefficient => coeff
使用此密钥,我需要解密从我的服务器接收并已由 javascript 库 (SJCL) 加密的消息。我还需要对消息进行加密,以便能够使用 javascrypt 库对其进行解密。
现在我已经这样做了:
public static String decrypt (String data, String stringKey) throws Exception {
JSONObject jsonKey = new JSONObject(stringKey);
BigInteger n = new BigInteger(Base64.decode(jsonKey.getString("n"), Base64.DEFAULT));
BigInteger e = new BigInteger("10001");
BigInteger d = new BigInteger(Base64.decode(jsonKey.getString("d"), Base64.DEFAULT));
BigInteger p = new BigInteger(Base64.decode(jsonKey.getString("p"), Base64.DEFAULT));
BigInteger q = new BigInteger(Base64.decode(jsonKey.getString("q"), Base64.DEFAULT));
BigInteger dmp1 = new BigInteger(Base64.decode(jsonKey.getString("dmp1"), Base64.DEFAULT));
BigInteger dmq1 = new BigInteger(Base64.decode(jsonKey.getString("dmq1"), Base64.DEFAULT));
BigInteger coeff = new BigInteger(Base64.decode(jsonKey.getString("coeff"), Base64.DEFAULT));
KeySpec privateKeySpec = new RSAPrivateCrtKeySpec(n, e, d, p, q, dmp1, dmq1, coeff);
KeyFactory keyFactory = KeyFactory.getInstance("RSA");
PrivateKey privateKey = keyFactory.generatePrivate(privateKeySpec);
Cipher cipher = Cipher.getInstance("RSA/None/PKCS1Padding");
cipher.init(Cipher.DECRYPT_MODE, privateKey);
byte[] dec = cipher.doFinal(data.getBytes());
return new String(Base64.decode(dec, Base64.DEFAULT));
}
现在我明白了:
javax.crypto.IllegalBlockSizeException:输入必须小于 96 字节