का उपयोग कर SamlAssertions की डिक्रिप्शन
मैं पूर्वी नौसेना कमान का इस्तेमाल किया ryptedXml मेरे दावे को डिक्रिप्ट करने के लिए। यहाँ मेरी कोड चला जाता है
EncryptedXmlWithPreconfiguredAsymmetricKey encXml = new EncryptedXmlWithPreconfiguredAsymmetricKey (_xmlDoc,_certificate);
while (_xmlDoc.GetElementsByTagName("EncryptedData").Count > 0)
{
XmlElement encryptedDataElement = _xmlDoc.GetElementsByTagName("EncryptedData")[0] as XmlElement;
EncryptedData encryptedData = new EncryptedData();
encryptedData.LoadXml(encryptedDataElement);
SymmetricAlgorithm symmKey = encXml.GetDecryptionKey(encryptedData, encryptedData.EncryptionMethod.KeyAlgorithm);
symmKey.IV = encXml.GetDecryptionIV(encryptedData, encryptedData.EncryptionMethod.KeyAlgorithm);
symmKey.Padding = encXml.Padding;
symmKey.Mode = encXml.Mode;
byte[] decryptedData = encXml.DecryptData(encryptedData, symmKey);
encXml.ReplaceData(encryptedDataElement, decryptedData);
}
मैं भी विधि GetDecryptionKey() overrided पूर्वनिर्धारित प्रमाणपत्र का उपयोग करने
public class EncryptedXmlWithPreconfiguredAsymmetricKey : EncryptedXml
{
public readonly X509Certificate2 _encryptionCert;
public EncryptedXmlWithPreconfiguredAsymmetricKey(XmlDocument xmlDoc, X509Certificate2 encryptionCert) : base(xmlDoc)
{
_encryptionCert = encryptionCert;
}
public override SymmetricAlgorithm GetDecryptionKey(EncryptedData encryptedData, string symmetricAlgorithmUri)
{
if (encryptedData == null)
throw new ArgumentNullException("encryptedData");
if (encryptedData.KeyInfo == null)
return null;
IEnumerator keyInfoEnum = encryptedData.KeyInfo.GetEnumerator();
KeyInfoRetrievalMethod kiRetrievalMethod;
KeyInfoName kiName;
KeyInfoEncryptedKey kiEncKey;
EncryptedKey ek = null;
while (keyInfoEnum.MoveNext())
{
kiName = keyInfoEnum.Current as KeyInfoName;
kiRetrievalMethod = keyInfoEnum.Current as KeyInfoRetrievalMethod;
kiEncKey = keyInfoEnum.Current as KeyInfoEncryptedKey;
if (kiEncKey != null)
{
ek = kiEncKey.EncryptedKey;
break;
}
}
// if we have an EncryptedKey, decrypt to get the symmetric key
if (ek != null)
{
// now process the EncryptedKey, loop recursively
// If the Uri is not provided by the application, try to get it from the EncryptionMethod
if (symmetricAlgorithmUri == null)
{
if (encryptedData.EncryptionMethod == null)
throw new CryptographicException("Cryptography_Xml_MissingAlgorithm");
symmetricAlgorithmUri = encryptedData.EncryptionMethod.KeyAlgorithm;
}
byte[] key = ek.CipherData.CipherValue;
if (key == null)
throw new CryptographicException("Cryptography_Xml_MissingDecryptionKey");
// Ignore any information about the asymmetric key in the XML, and just use our predefined certificate
var rsaKey = (RSA)_encryptionCert.PrivateKey;
byte[] symkey = DecryptKey(key, rsaKey, false);
SymmetricAlgorithm symAlg = (SymmetricAlgorithm)CryptoConfig.CreateFromName(symmetricAlgorithmUri);
symAlg.Key = symkey;
return symAlg;
}
return null;
}
}
स्रोत
2016-12-02 22:51:35