Javascript SJCL和java Bouncy Castle RSA之间的加密兼容性

Javascript SJCL和java Bouncy Castle RSA之间的加密兼容性,javascript,android,rsa,sjcl,jsbn,Javascript,Android,Rsa,Sjcl,Jsbn,我正在开发一个使用RSA加密的android应用程序 我可以从我的服务器,我的私钥和我的朋友的公钥 私钥如下所示: {"n":"...","d":"...","p":"...","q":"...","dmp1":"...","dmq1":"...","coeff":"..."} (jsbn json format) modulus => n public exponent => e private exponent => d prime1 => p prime2 =&g

我正在开发一个使用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字节


对于RSA,私钥返回按照标准编码的ASN.1二进制。

在另一个项目上花费了大量时间后,我重新使用这个应用程序,并解决了我的问题

首先,SJCL/JSBN库生成的私钥json是六个字符串。 所以我只需要在字节数组中转换字符串

BigInteger n = new BigInteger(Utils.hexStringToByteArray(nString));
BigInteger e = new BigInteger("10001", 16); // Public exponent
发送的加密数据也是一个六进制字符串

byte[] dec = cipher.doFinal(Utils.hexStringToByteArray(data));
return new String(dec, "UTF-8");
密码以明文形式返回字节数组

下面是算法:

Cipher cipher = Cipher.getInstance("RSA/None/PKCS1Padding");
以及hexStringToByteArray函数:

public static byte[] hexStringToByteArray(String s) {
  int len = s.length();
  byte[] data = new byte[len/2];

  s = s.toUpperCase();
  for(int i = 0; i < len; i+=2){
    data[i/2] = (byte) ((Character.digit(s.charAt(i), 16) << 4) + Character.digit(s.charAt(i+1), 16));
  }

 return data;
}

public static String byteArrayToHexString(byte[] bytes) {
  char[] hexChars = new char[bytes.length*2];
  int v;

  for(int j=0; j < bytes.length; j++) {
    v = bytes[j] & 0xFF;
    hexChars[j*2] = hexArray[v>>>4];
    hexChars[j*2 + 1] = hexArray[v & 0x0F];
  }

  return new String(hexChars).toLowerCase();
}
公共静态字节[]hexStringToByteArray(字符串s){
int len=s.length();
字节[]数据=新字节[len/2];
s=s.toUpperCase();
对于(int i=0;i>4];
hexChars[j*2+1]=hexArray[v&0x0F];
}
返回新字符串(hexChars.toLowerCase();
}

当时我看错了地方,将我的BigInteger解码为base64编码字符串。

根据SJCL库,我正在寻找使用此json以获取真正私钥的方法。这是否意味着你的问题与BouncyCastle没有任何关系?如果可能,我想使用BouncyCastle库。但是我的主要问题与我在Java中使用SJCL输出的方式有关。你肯定需要澄清你的问题,它非常模糊。数据长度(字节)是多少?还要注意
biginger e=new biginger(“10001”,16);