public override void Decode(Asn1BerDecodeBuffer buffer, bool explicitTagging, int implicitLength)
        {
            var elemLength = explicitTagging ? MatchTag(buffer, Asn1Tag.Sequence) : implicitLength;

            Init();

            var context = new Asn1BerDecodeContext(buffer, elemLength);
            var parsedLen = new IntHolder();

            if (!context.MatchElemTag(0, 0, ObjectIdentifierTypeCode, parsedLen, false))
            {
                throw ExceptionUtility.CryptographicException(Resources.Asn1MissingRequiredException, buffer.ByteCount);
            }

            EncryptionParamSet = new Gost2814789ParamSet();
            EncryptionParamSet.Decode(buffer, true, parsedLen.Value);

            if (context.MatchElemTag(0, 0, OctetStringTypeCode, parsedLen, false))
            {
                Ukm = new Asn1OctetString();
                Ukm.Decode(buffer, true, parsedLen.Value);

                if (Ukm.Length != 8)
                {
                    throw ExceptionUtility.CryptographicException(Resources.Asn1ConsVioException, "Ukm.Length", Ukm.Length);
                }
            }
        }
        public override void Decode(Asn1BerDecodeBuffer buffer, bool explicitTagging, int implicitLength)
        {
            var num = explicitTagging ? MatchTag(buffer, Tag) : implicitLength;
            var str = new Asn1OctetString();
            str.Decode(buffer, false, num);

            Value = Encoding.UTF8.GetString(str.Value, 0, str.Value.Length);

            if (explicitTagging && (num == Asn1Status.IndefiniteLength))
            {
                MatchTag(buffer, Asn1Tag.Eoc);
            }
        }
        private static GostKeyExchangeParameters DecodePublicKey(GostR3410KeyTransport keyTransport)
        {
            var publicKeyInfo = keyTransport.TransportParameters.EphemeralPublicKey;
            var publicKeyAlgOid = Asn1ObjectIdentifier.ToOidString(publicKeyInfo.Algorithm.Algorithm);

            if (!publicKeyAlgOid.Equals(GostR34102001Constants.IdGostR34102001String))
            {
                throw ExceptionUtility.CryptographicException(Resources.Asn1DecodeAlg, publicKeyAlgOid);
            }

            var choice = publicKeyInfo.Algorithm.Parameters as Asn1Choice;

            if (choice == null)
            {
                throw ExceptionUtility.CryptographicException(Resources.Asn1DecodeAlgorithmParameters);
            }

            var publicKeyParams = choice.GetElement() as GostR34102001PublicKeyParameters;

            if (publicKeyParams == null)
            {
                throw ExceptionUtility.CryptographicException(Resources.Asn1DecodeAlgorithmParameters);
            }

            var asnDecoder = new Asn1BerDecodeBuffer(publicKeyInfo.SubjectPublicKey.Value);
            var publicKey = new Asn1OctetString();
            publicKey.Decode(asnDecoder);

            return new GostKeyExchangeParameters
                   {
                       DigestParamSet = Asn1ObjectIdentifier.ToOidString(publicKeyParams.DigestParamSet),
                       PublicKeyParamSet = Asn1ObjectIdentifier.ToOidString(publicKeyParams.PublicKeyParamSet),
                       EncryptionParamSet = Asn1ObjectIdentifier.ToOidString(publicKeyParams.EncryptionParamSet),
                       PublicKey = publicKey.Value,
                       PrivateKey = null
                   };
        }
        private static SubjectPublicKeyInfo EncodePublicKey(GostKeyExchangeParameters transportParameters)
        {
            var asnEncoder = new Asn1BerEncodeBuffer();
            var publicKey = new Asn1OctetString(transportParameters.PublicKey);
            publicKey.Encode(asnEncoder);

            var publicKeyValue = asnEncoder.MsgCopy;

            var publicKeyInfo = new SubjectPublicKeyInfo
                                {
                                    SubjectPublicKey = new Asn1BitString(publicKeyValue.Length * 8, publicKeyValue)
                                };

            var publicKeyParams = new GostR34102001PublicKeyParameters
                             {
                                 PublicKeyParamSet = Asn1ObjectIdentifier.FromOidString(transportParameters.PublicKeyParamSet),
                                 DigestParamSet = Asn1ObjectIdentifier.FromOidString(transportParameters.DigestParamSet),
                                 EncryptionParamSet = CreateEncryptionParamSet(transportParameters.EncryptionParamSet)
                             };

            asnEncoder.Reset();
            publicKeyParams.Encode(asnEncoder);

            var publicKeyAlgOid = new Asn1ObjectIdentifier(GostR34102001Constants.IdGostR34102001);
            publicKeyInfo.Algorithm = new AlgorithmIdentifier(publicKeyAlgOid, new Asn1OpenType(asnEncoder.MsgCopy));

            return publicKeyInfo;
        }
 private void Init()
 {
     EncryptionParamSet = null;
     Ukm = null;
 }
 private void Init()
 {
     EncryptionParamSet = null;
     EphemeralPublicKey = null;
     Ukm = null;
 }