需要解密SAML响应

时间:2014-05-30 10:48:25

标签: c# encryption saml-2.0

我需要使用C#集成单点登录,我从IdP获得SAML响应,我需要解密SAML响应(加密:TripleDes)。我需要在证书中使用私钥解密。

当我尝试使用证书密钥解密时,它会给我错误:密钥大小无效。有没有一种标准方法可以将私钥转换为TripleDes的有效大小?例如

我应该先服用24个字节还是应该服用MD5哈希?是否有任何开放库可用于解密C#中的SAML响应?

以下是我从IdP收到的XML:

<saml:EncryptedAssertion xmlns:saml="urn:oasis:names:tc:SAML:2.0:assertion">
  <EncryptedData Type="http://www.w3.org/2001/04/xmlenc#Element" xmlns="http://www.w3.org/2001/04/xmlenc#">
    <EncryptionMethod Algorithm="http://www.w3.org/2001/04/xmlenc#tripledes-cbc" />
    <KeyInfo xmlns="http://www.w3.org/2000/09/xmldsig#">
      <EncryptedKey xmlns="http://www.w3.org/2001/04/xmlenc#">
        <EncryptionMethod Algorithm="http://www.w3.org/2001/04/xmlenc#rsa-1_5" />
        <KeyInfo xmlns="http://www.w3.org/2000/09/xmldsig#">
          <X509Data>
            <X509Certificate>Signing certificate goes here==</X509Certificate>
          </X509Data>
        </KeyInfo>
        <CipherData>
          <CipherValue>Cipher value goes here==</CipherValue>
        </CipherData>
      </EncryptedKey>
    </KeyInfo>
    <CipherData>
      <CipherValue>cipher value goes here=</CipherValue>
    </CipherData>
  </EncryptedData>
</saml:EncryptedAssertion>

代码段:

byte[] inputArray = Convert.FromBase64String(EncryptedData);
TripleDESCryptoServiceProvider tripleDES = (TripleDESCryptoServiceProvider)cert.PrivateKey;

异常来自第二行。我目前没有使用任何第三方工具。

1 个答案:

答案 0 :(得分:0)

尝试将代码段切换为:

byte[] inputArray = Convert.FromBase64String(EncryptedData);
var cryptoProvider = (RSACryptoServiceProvider)cert.PrivateKey;