/// <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); }
internal GostCryptoAPITransform( int cArgs, int[] rgArgIds, object[] rgArgValues, SafeKeyHandle hKey, SafeProvHandle hProv, PaddingMode padding, CipherMode cipherChainingMode, int blockSize, bool encrypting) { _blockSizeValue = blockSize; _modeValue = cipherChainingMode; _isStream = _modeValue == CipherMode.OFB || _modeValue == CipherMode.CFB; _paddingValue = padding; this._encrypting = encrypting; int[] numArray1 = new int[rgArgIds.Length]; Array.Copy(rgArgIds, numArray1, rgArgIds.Length); object[] objArray1 = new object[rgArgValues.Length]; for (int num2 = 0; num2 < rgArgValues.Length; num2++) { if (rgArgValues[num2] is byte[]) { byte[] buffer2 = (byte[])rgArgValues[num2]; byte[] buffer3 = new byte[buffer2.Length]; Array.Copy(buffer2, buffer3, buffer2.Length); objArray1[num2] = buffer3; } else if (rgArgValues[num2] is int) { objArray1[num2] = (int)rgArgValues[num2]; } else if (rgArgValues[num2] is CipherMode) { objArray1[num2] = (int)rgArgValues[num2]; } else if (rgArgValues[num2] is PaddingMode) { objArray1[num2] = (int)rgArgValues[num2]; } } safeKeyHandle_ = hKey; _safeProvHandle = hProv; for (int num3 = 0; num3 < cArgs; num3++) { switch (rgArgIds[num3]) { case GostConstants.KP_SV: { _ivValue = (byte[])objArray1[num3]; byte[] buffer1 = _ivValue; CapiHelper.SetKeyParameter(safeKeyHandle_, numArray1[num3], buffer1); break; } case GostConstants.KP_PADDING: { CapiHelper.SetKeyParameter(safeKeyHandle_, numArray1[num3], BitConverter.GetBytes((int)objArray1[num3])); break; } case GostConstants.KP_MODE: { CapiHelper.SetKeyParameter(safeKeyHandle_, numArray1[num3], BitConverter.GetBytes((int)objArray1[num3])); break; } default: { throw new CryptographicException(SR.Argument_InvalidValue); } } } }