示例#1
0
        /// <summary>
        /// Wrap ключа Gost28147CryptoServiceProvider на agree
        /// по <see cref="GostKeyWrapMethod.GostKeyWrap"/>.
        /// </summary>
        ///
        /// <param name="prov">Шифруемый ключ.</param>
        ///
        /// <returns>Зашифрованный симметричный ключ.</returns>
        ///
        /// <exception cref="CryptographicException">При ошибках
        /// на managed уровне.</exception>
        private byte[] GostWrap(Gost28147CryptoServiceProvider prov)
        {
            SafeKeyHandle        hSimmKey   = prov.SafeKeyHandle;
            GostWrappedKeyObject wrappedKey = new GostWrappedKeyObject();
            SafeKeyHandle        hExpKey    = SafeKeyHandle.InvalidHandle;

            try
            {
                CapiHelper.ImportAndMakeSharedSecret(_safeProvHandle,
                                                     CspProviderFlags.NoFlags, _publicObject, _safeKeyHandle,
                                                     ref hExpKey, _algType);

                CapiHelper.SetKeyParamDw(hExpKey, GostConstants.KP_ALGID,
                                         GostConstants.CALG_SIMPLE_EXPORT);

                CapiHelper.ExportSessionWrapedKey(hSimmKey,
                                                  hExpKey, wrappedKey);
            }
            finally
            {
                if (!hExpKey.IsClosed)
                {
                    hExpKey.Close();
                }
            }
            return(wrappedKey.GetXmlWrappedKey());
        }
示例#2
0
        /// <summary>
        /// Экспортирует (шифрует) секретный ключ.
        /// </summary>
        /// <param name="prov">Шифруемый ключ.</param>
        /// <param name="method">Алгоритм экспорта ключа.</param>
        /// <returns>Зашифрованный симметричный ключ</returns>
        public override byte[] Wrap(Gost28147 prov, GostKeyWrapMethod method)
        {
            SafeKeyHandle hSimmKey = ((Gost28147CryptoServiceProvider)prov).SafeKeyHandle;
            int           calg     = GostConstants.CALG_SIMPLE_EXPORT;

            if (method == GostKeyWrapMethod.CryptoProKeyWrap)
            {
                calg = GostConstants.CALG_PRO_EXPORT;
            }
            else if (method == GostKeyWrapMethod.CryptoPro12KeyWrap)
            {
                calg = GostConstants.CALG_PRO12_EXPORT;
            }
            else if (method != GostKeyWrapMethod.GostKeyWrap)
            {
                throw new ArgumentOutOfRangeException("method");
            }
            byte[] ret = null;
            // Сохраняем состояние algid GOST12147
            using (SafeKeyHandle hExpKey = CapiHelper.DuplicateKey(
                       SafeKeyHandle.DangerousGetHandle(),
                       SafeProvHandle))
            {
                CapiHelper.SetKeyParameter(hExpKey, GostConstants.KP_ALGID, calg);
                CapiHelper.SetKeyParameter(hExpKey, GostConstants.KP_IV, IV);

                GostWrappedKeyObject wrappedKey = new GostWrappedKeyObject();
                CapiHelper.ExportSessionWrapedKey(hSimmKey,
                                                  hExpKey, wrappedKey);

                ret = wrappedKey.GetXmlWrappedKey();
            }
            return(ret);
        }
示例#3
0
        /// <summary>
        /// Wrap ключа Gost28147CryptoServiceProvider на agree
        /// по <see cref="GostKeyWrapMethod.CryptoProKeyWrap"/>.
        /// </summary>
        ///
        /// <param name="prov">Шифруемый ключ.</param>
        /// <param name="calgProExport">CALG алгоритма экспорта крипто про</param>
        /// <returns>Зашифрованный симметричный ключ.</returns>
        ///
        /// <exception cref="CryptographicException">При ошибках
        /// на managed уровне.</exception>
        private byte[] CryptoProWrap(Gost28147CryptoServiceProvider prov, int calgProExport = GostConstants.CALG_PRO_EXPORT)
        {
            if (calgProExport != GostConstants.CALG_PRO_EXPORT && calgProExport != GostConstants.CALG_PRO12_EXPORT)
            {
                throw new ArgumentOutOfRangeException("calgProExport");
            }

            SafeKeyHandle        hSimmKey   = prov.SafeKeyHandle;
            GostWrappedKeyObject wrappedKey = new GostWrappedKeyObject();
            SafeKeyHandle        hExpKey    = SafeKeyHandle.InvalidHandle;

            try
            {
                CapiHelper.ImportAndMakeSharedSecret(_safeProvHandle,
                                                     CspProviderFlags.NoFlags, _publicObject, _safeKeyHandle,
                                                     ref hExpKey, _algType);

                CapiHelper.SetKeyParamDw(hExpKey, GostConstants.KP_ALGID,
                                         calgProExport);

                CapiHelper.ExportSessionWrapedKey(hSimmKey,
                                                  hExpKey, wrappedKey);
            }
            finally
            {
                if (!hExpKey.IsClosed)
                {
                    hExpKey.Close();
                }
            }
            return(wrappedKey.GetXmlWrappedKey());
        }