/// <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> /// 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()); }
/// <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()); }
/// <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)); }