3

sso を実装しているサード パーティ ベンダーに問題があります。私の署名を確認すると、次のエラーが表示されます。

java.lang.ArithmeticException: BigInteger: モジュラスが正でない - java.math.BigInteger.modPow(BigInteger.java:1556) で

私は彼らの Java コードを制御できません。これが私が今していることです:

次のコードを使用して、C# でキー ペアを作成しました。

        CspParameters csp = new CspParameters();
        csp.KeyNumber = (int)KeyNumber.Signature;
        using (RSACryptoServiceProvider rsa = new RSACryptoServiceProvider(1024, csp))
        {

            File.AppendAllText(path + "PrivateKey.xml", rsa.ToXmlString(true));
            File.AppendAllText(path + "PublicKey.xml", rsa.ToXmlString(false));
        }

署名のコードは次のとおりです。

public string MD5withRSASignature(string encryptedStringToSign)
    {

        byte[] signature;
        using (RSACryptoServiceProvider rsa = new RSACryptoServiceProvider(1024))
        {
            XmlDocument xDoc = new XmlDocument();
            xDoc.Load(PRIVATE_KEY_PATH);
            rsa.FromXmlString(xDoc.OuterXml);
            byte[] bytes = Encoding.UTF8.GetBytes(encryptedStringToSign);
            signature = rsa.SignData(bytes, new MD5CryptoServiceProvider());
        }

        return Convert.ToBase64String(signature);
    }

(はい、秘密鍵はキー ストアにある必要があることはわかっています)。

これは、xml キーを変換するために使用するコードです (これは Java です)。

private static RSAPublicKey ReadXMLKey(String fileName)
{

        DocumentBuilderFactory factory =     DocumentBuilderFactory.newInstance();
        DocumentBuilder builder = factory.newDocumentBuilder();
        Document document = builder.parse( new File(fileName) );
        byte[] modBytes = GetBytesFromElement(document, "Modulus");
        byte[] expBytes = GetBytesFromElement(document, "Exponent");
        RSAPublicKeySpec rsaKeyspec = new RSAPublicKeySpec(new BigInteger(modBytes), new BigInteger(expBytes));
        RSAPublicKey key = (RSAPublicKey)KeyFactory.getInstance("RSA").generatePublic(rsaKeyspec);

        return key;
         }

private static byte[] GetBytesFromElement(Document doc, String tag) throws IOException
{
    BASE64Decoder decoder = new BASE64Decoder();
    NodeList list = doc.getElementsByTagName(tag);
    byte[] results = null;
    if (list.getLength() == 1)
    {
        Element item = (Element)list.item(0);
        Text text = (Text)item.getFirstChild();
        results = decoder.decodeBuffer(text.getNodeValue().trim());
    }
    return results;
}
4

1 に答える 1

4

例外は、Java が使用している RSA 公開鍵に関係しています。あなたのコードはどれもそれを指していません。Java 側はどのようにしてそのキーを取得したのですか?どのような形式が使用されていますか?

エラーの原因となるよくある間違いの 1 つは、モジュラスがバイトの配列に変換されているが、必要なときに先頭のゼロ バイトが存在しない場合です基本的に、この BigInteger コンストラクターは、最初に表示されるよりも使用するのが少しトリッキーです。これは、DER でエンコードされた ASN.1 整数との互換性のために設計されています。つまり、モジュラスの最初のバイトbに上位ビットが設定されている場合、つまり128 <= b < 256先頭にゼロバイトを追加する必要があります。そうしないと、モジュラスが負の数として解釈されます。簡単にするために、先頭にゼロバイトを常に追加できます。必要がなければ害はありません。

于 2010-10-20T11:51:47.547 に答える