密码块大小在Java中返回0

问题描述

我必须将我们的Java Enterprise应用程序与一个客户进行集成。我们调用IdP并获取XML。返回的一部分是,详细信息在xenc:CipherDataxenc:CipherValue中。如果我使用https://www.samltool.com/decrypt.php并传递返回的数据并插入我们的私钥,则将清除所有内容,并可以读取解密的XML数据。 没关系,我在Java中无法解密返回的信息。第一块为344字节,第二块为4k以上。

当我阅读私钥时,它告诉我算法是RSA,格式是PKCS#8。创建的密码告诉我,块大小为0。 我该如何处理解密?花了几天的时间搜寻所有作品,但现在我已经堆满了。寻找任何帮助,我什至可以解决这个问题。

public static String testPrivateKeyPEM = "-----BEGIN PRIVATE KEY-----"
            + "MIIEvQIBADANBgkqhkiG9w0BAQEFAASCBKcwggSjAgEAAoIBAQDBzQjFSrnpm8Li"
. . .
. . .
            + "wDk2ZcY6biWqeBnQR8gzUN4="
            + "-----END PRIVATE KEY-----";
private PrivateKey privateKey = null;

try {
    String privKey = testPrivateKeyPEM.replaceAll("-----BEGIN PRIVATE KEY-----","")
    .replaceAll(System.lineSeparator(),"").replaceAll("-----END PRIVATE KEY-----","");
    byte[] encoded = Base64.getDecoder().decode(privKey);
KeyFactory keyFactory = KeyFactory.getInstance("RSA");
PKCS8EncodedKeySpec keySpecPKCS8 = new PKCS8EncodedKeySpec(Base64.getDecoder().decode(privKey));
PrivateKey privKey2 = keyFactory.generatePrivate(keySpecPKCS8);
System.err.println(privKey2.getAlgorithm()); //returns RSA
System.err.println(privKey2.getFormat());
this.privateKey = privKey2;
} catch (NoSuchAlgorithmException e) {
  e.printStackTrace();
} catch (InvalidKeySpecException e) {
    e.printStackTrace();
}
//
//Once I read the private key,I create a cipher:
Cipher cipher =  null;
try
  {
//   cipher = Cipher.getInstance("RSA/ECB/OAEPWithSHA-256AndMGF1Padding");
//   cipher = Cipher.getInstance("RSA/ECB/PKCS1Padding");
   cipher = Cipher.getInstance(privateKey.getAlgorithm());
  }
  catch (NoSuchAlgorithmException e)
  {
   e.printStackTrace();
  }
  catch (NoSuchPaddingException e)
  {
   e.printStackTrace();
  }
        if (cipher == null)
         return;
try
 {
   cipher.init(Cipher.PRIVATE_KEY,privateKey);
  }
  catch (InvalidKeyException e)
  {
   e.printStackTrace();
  }
String myString = new String(cipher.doFinal(encrSecond.getBytes()));

解决方法

暂无找到可以解决该程序问题的有效方法,小编努力寻找整理中!

如果你已经找到好的解决方法,欢迎将解决方案带上本链接一起发送给小编。

小编邮箱:dio#foxmail.com (将#修改为@)