Cipher blocksize 返回 0 在 Java 中。

huangapple go评论73阅读模式
英文:

Cipher blocksize return 0 in Java

问题

我必须将我们的Java企业应用程序与我们的一个客户集成。我们调用IdP并获取XML返回。返回的一部分是<e:CipherData><e:CipherValue>,详细信息在<xenc:CipherData><xenc: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()); // 返回 RSA
    System.err.println(privKey2.getFormat());
    this.privateKey = privKey2;
} catch (NoSuchAlgorithmException e) {
    e.printStackTrace();
} catch (InvalidKeySpecException e) {
    e.printStackTrace();
}

// 读取私钥后,创建密码:
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()));

这是您提供的代码的翻译部分。

英文:

I have to do an integration of our Java Enterprise application with one of our clients. We call the IdP and get back the XML. Part of the return, is <e:CipherData><e:CipherValue> and the details are in <xenc:CipherData><xenc:CipherValue>. If I use the https://www.samltool.com/decrypt.php and pass the returned data and insert our private key, then EVERYTHING is cleaned and can read the decrypted XML data.
Doesn’t matter, what I do in Java I can’t decrypt the returned information. The first piece is 344 bytes and the second is over 4k.

When I’m reading the private key it is telling me the algorithm is RSA and the format is PKCS#8. The created cipher tells me that the blocksize is 0.
What can I do to process the decryption? Spent days googling all pieces but now I’m fully stacked. Looking for ANY help and I even can contract this problem out.

public static String testPrivateKeyPEM = &quot;-----BEGIN PRIVATE KEY-----&quot;
+ &quot;MIIEvQIBADANBgkqhkiG9w0BAQEFAASCBKcwggSjAgEAAoIBAQDBzQjFSrnpm8Li&quot;
. . .
. . .
+ &quot;wDk2ZcY6biWqeBnQR8gzUN4=&quot;
+ &quot;-----END PRIVATE KEY-----&quot;;
private PrivateKey privateKey = null;
try {
String privKey = testPrivateKeyPEM.replaceAll(&quot;-----BEGIN PRIVATE KEY-----&quot;, &quot;&quot;)
.replaceAll(System.lineSeparator(), &quot;&quot;).replaceAll(&quot;-----END PRIVATE KEY-----&quot;, &quot;&quot;);
byte[] encoded = Base64.getDecoder().decode(privKey);
KeyFactory keyFactory = KeyFactory.getInstance(&quot;RSA&quot;);
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(&quot;RSA/ECB/OAEPWithSHA-256AndMGF1Padding&quot;);
//   cipher = Cipher.getInstance(&quot;RSA/ECB/PKCS1Padding&quot;);
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()));

答案1

得分: 1

你链接到了一个在线解密服务,在该网站上有关于密钥加密方法和数据加密方法的选项。

正如@dnault已经提到的,有两个步骤:

步骤1:使用您的私钥解密加密的AES密钥

步骤2:使用解密后的AES密钥解密数据(请使用正确的解密模式)。

图片显示了选项字段:

Cipher blocksize 返回 0 在 Java 中。

英文:

You linked to an online service for decryption and on that site you have options for the key encryption method and another option for the data encryption method.

As @dnault already mentioned there are two steps:

step 1: decrypt the encrypted AES-key with your private key

step 2: decrypt the data using the decrypted AES key (please use the correct mode for decryption).

The picture shows the option fields:

Cipher blocksize 返回 0 在 Java 中。

huangapple
  • 本文由 发表于 2020年8月13日 03:42:08
  • 转载请务必保留本文链接:https://go.coder-hub.com/63383680.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定