Example #1
0
        public void ShouldEncryptXmlWithGost_R3410_2001(TestCertificateInfo testCase)
        {
            // Given

            var certificate = testCase.Certificate;

            // Получатель экспортирует отправителю информацию о своем открытом ключе
            var keyContainer  = certificate.GetPrivateKeyInfo();
            var privateKey    = new Gost_R3410_2001_AsymmetricAlgorithm(keyContainer);
            var publicKeyInfo = privateKey.ExportParameters(false);

            // Отправитель импортирует от получателя информацию о его открытом ключе
            var publicKey = new Gost_R3410_2001_AsymmetricAlgorithm();

            publicKey.ImportParameters(publicKeyInfo);

            var xmlDocument = CreateXmlDocument();
            var expectedXml = xmlDocument.OuterXml;

            // When
            var encryptedXmlDocument = EncryptXmlDocument(xmlDocument, publicKey);
            var decryptedXmlDocument = DecryptXmlDocument(encryptedXmlDocument, privateKey);
            var actualXml            = decryptedXmlDocument.OuterXml;

            // Then
            Assert.AreEqual(expectedXml, actualXml);
        }
Example #2
0
        /// <summary>
        /// Возвращает открытый ключ сертификата.
        /// </summary>
        public static AsymmetricAlgorithm GetPublicKeyAlgorithm(this X509Certificate2 certificate)
        {
            if (certificate.IsGost_R3410_2012_512())
            {
                var publicKey         = new Gost_R3410_2012_512_AsymmetricAlgorithm();
                var encodedParameters = certificate.PublicKey.EncodedParameters.RawData;
                var encodedKeyValue   = certificate.PublicKey.EncodedKeyValue.RawData;
                publicKey.ImportCspBlob(encodedParameters, encodedKeyValue);
                return(publicKey);
            }

            if (certificate.IsGost_R3410_2012_256())
            {
                var publicKey         = new Gost_R3410_2012_256_AsymmetricAlgorithm();
                var encodedParameters = certificate.PublicKey.EncodedParameters.RawData;
                var encodedKeyValue   = certificate.PublicKey.EncodedKeyValue.RawData;
                publicKey.ImportCspBlob(encodedParameters, encodedKeyValue);
                return(publicKey);
            }

            if (certificate.IsGost_R3410_2001())
            {
                var publicKey         = new Gost_R3410_2001_AsymmetricAlgorithm();
                var encodedParameters = certificate.PublicKey.EncodedParameters.RawData;
                var encodedKeyValue   = certificate.PublicKey.EncodedKeyValue.RawData;
                publicKey.ImportCspBlob(encodedParameters, encodedKeyValue);
                return(publicKey);
            }

            return(certificate.PublicKey.Key);
        }
        public void ShouldSignXmlWithGost_R3410_2001(TestCertificateInfo testCase)
        {
            // Given
            var certificate  = testCase.Certificate;
            var keyContainer = certificate.GetPrivateKeyInfo();
            var signingKey   = new Gost_R3410_2001_AsymmetricAlgorithm(keyContainer);
            var xmlDocument  = CreateXmlDocument();

            // When
            var signedXmlDocument = SignXmlDocument(xmlDocument, new Gost_R3410_2001_KeyValue(signingKey));

            // Then
            Assert.IsTrue(VerifyXmlDocumentSignature(signedXmlDocument));
        }
Example #4
0
        public void ShouldSetContainerPassword_R3410_2001(TestCertificateInfo testCase)
        {
            // Given
            var data           = GetSomeData();
            var certificate    = testCase.Certificate;
            var securePassword = CreateSecureString(TestConfig.ContainerPassword);

            // When

            var privateKeyInfo = certificate.GetPrivateKeyInfo();
            var privateKey     = new Gost_R3410_2001_AsymmetricAlgorithm(privateKeyInfo);

            privateKey.SetContainerPassword(securePassword);

            var signature        = CreateSignature(privateKey, data);
            var isValidSignature = VerifySignature(privateKey, data, signature);

            // Then
            Assert.IsTrue(isValidSignature);
        }
Example #5
0
 /// <summary>
 /// Создает экземпляр класса с заданным ключом ГОСТ Р 34.10-2001.
 /// </summary>
 public Gost_R3410_2001_KeyValue(Gost_R3410_2001_AsymmetricAlgorithm publicKey) : base(publicKey)
 {
 }