public override byte[] EncodePrivateKey(Gost28147SymmetricAlgorithmBase keyExchangeAlgorithm, GostKeyExchangeExportMethod keyExchangeExportMethod)
        {
            if (keyExchangeAlgorithm == null)
            {
                throw ExceptionUtility.ArgumentNull("keyExchangeAlgorithm");
            }

            int keyExchangeExportAlgId;

            if (keyExchangeExportMethod == GostKeyExchangeExportMethod.GostKeyExport)
            {
                keyExchangeExportAlgId = Constants.CALG_SIMPLE_EXPORT;
            }
            else if (keyExchangeExportMethod == GostKeyExchangeExportMethod.CryptoProKeyExport)
            {
                keyExchangeExportAlgId = Constants.CALG_PRO_EXPORT;
            }
            else
            {
                throw ExceptionUtility.ArgumentOutOfRange("keyExchangeExportMethod");
            }

            var currentSessionKey = keyExchangeAlgorithm as Gost28147SymmetricAlgorithm;

            if (currentSessionKey == null)
            {
                using (var derivedSessinKey = new Gost28147SymmetricAlgorithm())
                {
                    derivedSessinKey.Key = keyExchangeAlgorithm.Key;

                    return EncodePrivateKeyInternal(derivedSessinKey, keyExchangeExportAlgId);
                }
            }

            return EncodePrivateKeyInternal(currentSessionKey, keyExchangeExportAlgId);
        }
        public static byte[] EncryptKey(Gost28147SymmetricAlgorithmBase sessionKey, Gost28147SymmetricAlgorithmBase sharedKey, GostKeyExchangeExportMethod exportMethod)
        {
            if (sessionKey == null)
            {
                throw ExceptionUtility.ArgumentNull("sessionKey");
            }

            if (sharedKey == null)
            {
                throw ExceptionUtility.ArgumentNull("sharedKey");
            }

            return sharedKey.EncodePrivateKey(sessionKey, exportMethod);
        }
        public override SymmetricAlgorithm DecodePrivateKey(byte[] encodedKeyExchangeData, GostKeyExchangeExportMethod keyExchangeExportMethod)
        {
            if (encodedKeyExchangeData == null)
            {
                throw ExceptionUtility.ArgumentNull("encodedKeyExchangeData");
            }

            int keyExchangeExportAlgId;

            if (keyExchangeExportMethod == GostKeyExchangeExportMethod.GostKeyExport)
            {
                keyExchangeExportAlgId = Constants.CALG_SIMPLE_EXPORT;
            }
            else if (keyExchangeExportMethod == GostKeyExchangeExportMethod.CryptoProKeyExport)
            {
                keyExchangeExportAlgId = Constants.CALG_PRO_EXPORT;
            }
            else
            {
                throw ExceptionUtility.ArgumentOutOfRange("keyExchangeExportMethod");
            }

            var providerHandle = CryptoApiHelper.ProviderHandle;

            var keyExchangeInfo = new GostKeyExchangeInfo();
            keyExchangeInfo.Decode(encodedKeyExchangeData);

            using (var keyHandle = CryptoApiHelper.DuplicateKey(InternalKeyHandle))
            {
                CryptoApiHelper.SetKeyParameterInt32(keyHandle, Constants.KP_ALGID, keyExchangeExportAlgId);

                var keyExchangeHandle = CryptoApiHelper.ImportKeyExchange(providerHandle, keyExchangeInfo, keyHandle);

                return new Gost28147SymmetricAlgorithm(providerHandle, keyExchangeHandle);
            }
        }
 /// <summary>
 /// Экспортирует (шифрует) секретный ключ.
 /// </summary>
 /// <param name="keyExchangeAlgorithm">Общий секретный ключ.</param>
 /// <param name="keyExchangeExportMethod">Алгоритм экспорта общего секретного ключа.</param>
 public abstract byte[] EncodePrivateKey(Gost28147SymmetricAlgorithmBase keyExchangeAlgorithm, GostKeyExchangeExportMethod keyExchangeExportMethod);
 /// <summary>
 /// Импортирует (дешифрует) секретный ключ.
 /// </summary>
 /// <param name="encodedKeyExchangeData">Зашифрованный общий секретный ключ.</param>
 /// <param name="keyExchangeExportMethod">Алгоритм экспорта общего секретного ключа.</param>
 public abstract SymmetricAlgorithm DecodePrivateKey(byte[] encodedKeyExchangeData, GostKeyExchangeExportMethod keyExchangeExportMethod);
 /// <summary>
 /// Шифрует сессионный ключ с помощью указанного симметричного ключа ГОСТ 28147.
 /// </summary>
 /// <param name="sessionKey">Шифруемый сессионный ключ.</param>
 /// <param name="sharedKey">Общий симметричный ключ ГОСТ 28147 для шифрования сессионного ключа.</param>
 /// <param name="exportMethod">Алгоритм экспорта сессионного ключа.</param>
 /// <returns>Массив байт, содержащий зашифрованный сессионный ключ.</returns>
 /// <remarks>Как правило сессионный ключ используется для шифрования данных и в свою очередь так же шифруется.</remarks>
 public static byte[] EncryptKey(Gost28147SymmetricAlgorithmBase sessionKey, Gost28147SymmetricAlgorithmBase sharedKey, GostKeyExchangeExportMethod exportMethod = GostKeyExchangeExportMethod.GostKeyExport)
 {
     return GostEncryptedXmlImpl.EncryptKey(sessionKey, sharedKey, exportMethod);
 }
 /// <summary>
 /// Шифрует сессионный ключ с помощью указанного симметричного ключа ГОСТ 28147.
 /// </summary>
 /// <param name="sessionKey">Шифруемый сессионный ключ.</param>
 /// <param name="sharedKey">Общий симметричный ключ ГОСТ 28147 для шифрования сессионного ключа.</param>
 /// <param name="exportMethod">Алгоритм экспорта сессионного ключа.</param>
 /// <returns>Массив байт, содержащий зашифрованный сессионный ключ.</returns>
 /// <remarks>Как правило сессионный ключ используется для шифрования данных и в свою очередь так же шифруется.</remarks>
 public static byte[] EncryptKey(Gost28147SymmetricAlgorithmBase sessionKey, Gost28147SymmetricAlgorithmBase sharedKey, GostKeyExchangeExportMethod exportMethod = GostKeyExchangeExportMethod.GostKeyExport)
 {
     return(GostEncryptedXmlImpl.EncryptKey(sessionKey, sharedKey, exportMethod));
 }