/// <summary> /// Расшифрование симметричного ключа по /// по <see cref="GostKeyWrapMethod.GostKeyWrap"/>. /// </summary> /// /// <param name="wrapped">Зашифрованный секретный ключ.</param> /// /// <returns>Объект класса <see cref="SymmetricAlgorithm"/>, /// содержащий расшифрованный закрытый ключ.</returns> /// /// <exception cref="CryptographicException">При ошибках /// на managed уровне.</exception> private SymmetricAlgorithm GostUnwrap(byte[] wrapped) { GostWrappedKeyObject gwk = new GostWrappedKeyObject(); gwk.SetByXmlWrappedKey(wrapped); SafeKeyHandle simmKey = SafeKeyHandle.InvalidHandle; 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.ImportSessionWrappedKey(_safeProvHandle, CspProviderFlags.NoFlags, gwk, hExpKey, ref simmKey); } finally { if (!hExpKey.IsClosed) { hExpKey.Close(); } } return(new Gost28147CryptoServiceProvider(simmKey, _safeProvHandle)); }
/// <summary> /// Импортирует (дешифрует) секретный ключ. /// </summary> /// <param name="wrapped">Зашифрованный секретный ключ.</param> /// <param name="method">Алгоритм экспорта ключа.</param> public override SymmetricAlgorithm Unwrap(byte[] wrapped, GostKeyWrapMethod method) { GostWrappedKeyObject gwk = new GostWrappedKeyObject(); gwk.SetByXmlWrappedKey(wrapped); 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"); } SymmetricAlgorithm ret = null; // Сохраняем состояние algid GOST12147 using (SafeKeyHandle hExpKey = CapiHelper.DuplicateKey(SafeKeyHandle.DangerousGetHandle())) { CapiHelper.SetKeyParamDw(hExpKey, GostConstants.KP_ALGID, calg); SafeKeyHandle simmKey = SafeKeyHandle.InvalidHandle; CapiHelper.AcquireCsp(_parameters, out SafeProvHandle hProv); CapiHelper.ImportSessionWrappedKey( hProv, CspProviderFlags.NoFlags, gwk, hExpKey, ref simmKey); ret = new Gost28147CryptoServiceProvider(simmKey, hProv); } return(ret); }
/// <summary> /// Расшифрование симметричного ключа по /// по <see cref="GostKeyWrapMethod.CryptoProKeyWrap"/>. /// </summary> /// /// <param name="wrapped">Зашифрованный секретный ключ.</param> /// <param name="calgProExport">OID алгоритма экспорта</param> /// <returns>Объект класса <see cref="SymmetricAlgorithm"/>, /// содержащий расшифрованный закрытый ключ.</returns> /// /// <exception cref="CryptographicException">При ошибках /// на managed уровне.</exception> private SymmetricAlgorithm CryptoProUnwrap(byte[] wrapped, int calgProExport) { if (calgProExport != GostConstants.CALG_PRO_EXPORT && calgProExport != GostConstants.CALG_PRO12_EXPORT) { throw new ArgumentOutOfRangeException("calgProExport"); } GostWrappedKeyObject gwk = new GostWrappedKeyObject(); gwk.SetByXmlWrappedKey(wrapped); SafeKeyHandle simmKey = SafeKeyHandle.InvalidHandle; SafeKeyHandle hExpKey = SafeKeyHandle.InvalidHandle; try { CapiHelper.ImportAndMakeSharedSecret(_safeProvHandle, CspProviderFlags.NoFlags, _publicObject, _safeKeyHandle, ref hExpKey, _algType); CapiHelper.SetKeyParamDw(hExpKey, GostConstants.KP_ALGID, calgProExport); CapiHelper.ImportSessionWrappedKey(_safeProvHandle, CspProviderFlags.NoFlags, gwk, hExpKey, ref simmKey); } finally { if (!hExpKey.IsClosed) { hExpKey.Close(); } } return(new Gost28147CryptoServiceProvider(simmKey, _safeProvHandle)); }