示例#1
0
            public void ThenGetCipherDataSucceeds()
            {
                // Arrange
                var cipherData = new CipherData {
                    CipherValue = new byte[] { 20 }
                };
                var encryptedKey = new EncryptedKey {
                    CipherData = cipherData
                };

                AS4EncryptedKey as4EncryptedKey = AS4EncryptedKey.FromEncryptedKey(encryptedKey);

                // Act
                CipherData as4CipherData = as4EncryptedKey.GetCipherData();

                // Assert
                Assert.Equal(cipherData, as4CipherData);
            }
        private static byte[] DecryptEncryptedKey(AS4EncryptedKey encryptedKey, X509Certificate2 certificate)
        {
            OaepEncoding encoding = EncodingFactory.Instance
                                    .Create(encryptedKey.GetDigestAlgorithm(), encryptedKey.GetMaskGenerationFunction());

            // We do not look at the KeyInfo element in here, but rather decrypt it with the certificate provided as argument.
            // Call GetRSAPrivateKey to avoid KeySet does not exist exceptions that might be thrown.
            RSA privateKey = certificate.GetRSAPrivateKey();

            if (privateKey == null)
            {
                throw new CryptographicException("The decryption certificate does not contain a private key.");
            }

            AsymmetricCipherKeyPair encryptionCertificateKeyPair =
                DotNetUtilities.GetRsaKeyPair(privateKey);

            encoding.Init(false, encryptionCertificateKeyPair.Private);

            CipherData cipherData = encryptedKey.GetCipherData();

            return(encoding.ProcessBlock(
                       inBytes: cipherData.CipherValue, inOff: 0, inLen: cipherData.CipherValue.Length));
        }